From df3944e31b3cf9a4971087c0568dddf1a985afae Mon Sep 17 00:00:00 2001 From: NickChokas <93598341+NickChokas@users.noreply.github.com> Date: Thu, 6 Jun 2024 12:57:23 -0400 Subject: [PATCH 1/5] Add option to Discard the Drafted Message --- functions/create_draft/blocks.ts | 7 ++++ .../create_draft/interactivity_handler.ts | 37 ++++++++++++++++++- 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/functions/create_draft/blocks.ts b/functions/create_draft/blocks.ts index 3947445..7263fbc 100644 --- a/functions/create_draft/blocks.ts +++ b/functions/create_draft/blocks.ts @@ -57,6 +57,13 @@ export const buildDraftBlocks = ( }, "value": "edit_message_overflow", }, + { + "text": { + "type": "plain_text", + "text": "Discard message", + }, + "value": "discard_message_overflow", + }, ], "action_id": "preview_overflow", }, diff --git a/functions/create_draft/interactivity_handler.ts b/functions/create_draft/interactivity_handler.ts index 1fa7443..8d25a31 100644 --- a/functions/create_draft/interactivity_handler.ts +++ b/functions/create_draft/interactivity_handler.ts @@ -23,7 +23,8 @@ import DraftDatastore from "../../datastores/drafts.ts"; export const openDraftEditView: BlockActionHandler< typeof CreateDraftFunction.definition -> = async ({ body, action, client }) => { +> = async ({ body, action, client, inputs }) => { + // If the user selects to edit the message if (action.selected_option.value == "edit_message_overflow") { const id = action.block_id; @@ -73,6 +74,40 @@ export const openDraftEditView: BlockActionHandler< }); } } + // If the user selects to discard the message + if (action.selected_option.value == "discard_message_overflow") { + const id = action.block_id; + const thread_ts = body.message?.ts || ""; + + // Delete the draft message from the Channel + const updateResp = await client.chat.delete({ + channel: inputs.channel, + ts: thread_ts, + }); + + if (!updateResp.ok) { + const updateDraftPreviewErrorMsg = + `Error Deleting the message: ${thread_ts} in channel ${inputs.channel}. Contact the app maintainers with the following - (Error detail: ${updateResp.error})`; + console.log(updateDraftPreviewErrorMsg); + } + + // Delete the draft from the drafts Datstore + const deleteResp = await client.apps.datastore.delete({ + datastore: DraftDatastore.name, + id: id, + }); + + if (!deleteResp.ok) { + const deleteDraftErrorMsg = + `Error deleting draft with id ${id}. Contact the app maintainers with the following - (Error detail: ${deleteResp.error})`; + console.log(deleteDraftErrorMsg); + + await client.functions.completeError({ + function_execution_id: body.function_data.execution_id, + error: deleteDraftErrorMsg, + }); + } + } }; export const saveDraftEditSubmission: ViewSubmissionHandler< From c0867aac7e64f1d59a2eb97306e10e31dde1afac Mon Sep 17 00:00:00 2001 From: NickChokas <93598341+NickChokas@users.noreply.github.com> Date: Thu, 6 Jun 2024 13:02:29 -0400 Subject: [PATCH 2/5] Update text verbiage and comments --- functions/create_draft/blocks.ts | 2 +- functions/create_draft/interactivity_handler.ts | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/functions/create_draft/blocks.ts b/functions/create_draft/blocks.ts index 7263fbc..c2aab21 100644 --- a/functions/create_draft/blocks.ts +++ b/functions/create_draft/blocks.ts @@ -60,7 +60,7 @@ export const buildDraftBlocks = ( { "text": { "type": "plain_text", - "text": "Discard message", + "text": "Discard the draft message", }, "value": "discard_message_overflow", }, diff --git a/functions/create_draft/interactivity_handler.ts b/functions/create_draft/interactivity_handler.ts index 8d25a31..5919377 100644 --- a/functions/create_draft/interactivity_handler.ts +++ b/functions/create_draft/interactivity_handler.ts @@ -24,7 +24,7 @@ import DraftDatastore from "../../datastores/drafts.ts"; export const openDraftEditView: BlockActionHandler< typeof CreateDraftFunction.definition > = async ({ body, action, client, inputs }) => { - // If the user selects to edit the message + // If the user selects to edit the draft message if (action.selected_option.value == "edit_message_overflow") { const id = action.block_id; @@ -74,12 +74,12 @@ export const openDraftEditView: BlockActionHandler< }); } } - // If the user selects to discard the message + // If the user selects to discard the draft message if (action.selected_option.value == "discard_message_overflow") { const id = action.block_id; const thread_ts = body.message?.ts || ""; - // Delete the draft message from the Channel + // Delete the draft message from the Draft Channel const updateResp = await client.chat.delete({ channel: inputs.channel, ts: thread_ts, @@ -87,11 +87,11 @@ export const openDraftEditView: BlockActionHandler< if (!updateResp.ok) { const updateDraftPreviewErrorMsg = - `Error Deleting the message: ${thread_ts} in channel ${inputs.channel}. Contact the app maintainers with the following - (Error detail: ${updateResp.error})`; + `Error deleting the draft message: ${thread_ts} in channel ${inputs.channel}. Contact the app maintainers with the following - (Error detail: ${updateResp.error})`; console.log(updateDraftPreviewErrorMsg); } - // Delete the draft from the drafts Datstore + // Delete the draft from the 'drafts' Datstore const deleteResp = await client.apps.datastore.delete({ datastore: DraftDatastore.name, id: id, From d4b7a6f8ea2daee3f6c1349a3e14fcfdd2ed786a Mon Sep 17 00:00:00 2001 From: NickChokas <93598341+NickChokas@users.noreply.github.com> Date: Sun, 16 Jun 2024 18:03:33 -0400 Subject: [PATCH 3/5] Cosmetic changes to clean up code --- .../create_draft/interactivity_handler.ts | 40 ++++++++++--------- 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/functions/create_draft/interactivity_handler.ts b/functions/create_draft/interactivity_handler.ts index 5919377..e94b132 100644 --- a/functions/create_draft/interactivity_handler.ts +++ b/functions/create_draft/interactivity_handler.ts @@ -25,7 +25,7 @@ export const openDraftEditView: BlockActionHandler< typeof CreateDraftFunction.definition > = async ({ body, action, client, inputs }) => { // If the user selects to edit the draft message - if (action.selected_option.value == "edit_message_overflow") { + if (action.selected_option.value === "edit_message_overflow") { const id = action.block_id; // Get the draft @@ -38,10 +38,11 @@ export const openDraftEditView: BlockActionHandler< }, ); + // If the GET datastore record operation fails, log the error and complete the function with an error if (!putResp.ok) { const draftGetErrorMsg = `Error getting draft with id ${id}. Contact the app maintainers with the following - (Error detail: ${putResp.error})`; - console.log(draftGetErrorMsg); + console.error(draftGetErrorMsg); await client.functions.completeError({ function_execution_id: body.function_data.execution_id, @@ -66,7 +67,7 @@ export const openDraftEditView: BlockActionHandler< if (!viewsOpenResp.ok) { const draftEditModalErrorMsg = `Error opening up the draft edit modal view. Contact the app maintainers with the following - (Error detail: ${viewsOpenResp.error}`; - console.log(draftEditModalErrorMsg); + console.error(draftEditModalErrorMsg); await client.functions.completeError({ function_execution_id: body.function_data.execution_id, @@ -74,22 +75,11 @@ export const openDraftEditView: BlockActionHandler< }); } } + // If the user selects to discard the draft message - if (action.selected_option.value == "discard_message_overflow") { + if (action.selected_option.value === "discard_message_overflow") { const id = action.block_id; - const thread_ts = body.message?.ts || ""; - - // Delete the draft message from the Draft Channel - const updateResp = await client.chat.delete({ - channel: inputs.channel, - ts: thread_ts, - }); - - if (!updateResp.ok) { - const updateDraftPreviewErrorMsg = - `Error deleting the draft message: ${thread_ts} in channel ${inputs.channel}. Contact the app maintainers with the following - (Error detail: ${updateResp.error})`; - console.log(updateDraftPreviewErrorMsg); - } + const message_ts = body.message?.ts || ""; // Delete the draft from the 'drafts' Datstore const deleteResp = await client.apps.datastore.delete({ @@ -97,16 +87,30 @@ export const openDraftEditView: BlockActionHandler< id: id, }); + // If the DELETE datastore record operation fails, log the error and complete the function with an error if (!deleteResp.ok) { const deleteDraftErrorMsg = `Error deleting draft with id ${id}. Contact the app maintainers with the following - (Error detail: ${deleteResp.error})`; - console.log(deleteDraftErrorMsg); + console.error(deleteDraftErrorMsg); await client.functions.completeError({ function_execution_id: body.function_data.execution_id, error: deleteDraftErrorMsg, }); } + + // Delete the draft message from the Draft Channel + const updateResp = await client.chat.delete({ + channel: inputs.channel, + ts: message_ts, + }); + + // If the DELETE message operation fails, log the error and complete the function with an error + if (!updateResp.ok) { + const updateDraftPreviewErrorMsg = + `Error deleting the draft message: ${message_ts} in channel ${inputs.channel}. Contact the app maintainers with the following - (Error detail: ${updateResp.error})`; + console.error(updateDraftPreviewErrorMsg); + } } }; From ba72dcb606f32c1cf5e6f9a45786425e47ff1110 Mon Sep 17 00:00:00 2001 From: NickChokas <93598341+NickChokas@users.noreply.github.com> Date: Tue, 18 Jun 2024 15:40:07 -0400 Subject: [PATCH 4/5] Add Try/Catch, check for empty message_ts, and update comment --- .../create_draft/interactivity_handler.ts | 59 +++++++++++-------- 1 file changed, 35 insertions(+), 24 deletions(-) diff --git a/functions/create_draft/interactivity_handler.ts b/functions/create_draft/interactivity_handler.ts index e94b132..816f47a 100644 --- a/functions/create_draft/interactivity_handler.ts +++ b/functions/create_draft/interactivity_handler.ts @@ -81,36 +81,47 @@ export const openDraftEditView: BlockActionHandler< const id = action.block_id; const message_ts = body.message?.ts || ""; - // Delete the draft from the 'drafts' Datstore - const deleteResp = await client.apps.datastore.delete({ - datastore: DraftDatastore.name, - id: id, - }); + try { + // Delete the draft from the 'drafts' Datstore + const deleteResp = await client.apps.datastore.delete({ + datastore: DraftDatastore.name, + id: id, + }); - // If the DELETE datastore record operation fails, log the error and complete the function with an error - if (!deleteResp.ok) { - const deleteDraftErrorMsg = - `Error deleting draft with id ${id}. Contact the app maintainers with the following - (Error detail: ${deleteResp.error})`; - console.error(deleteDraftErrorMsg); + // If the DELETE datastore record operation fails, log the error and complete the function with an error + if (!deleteResp.ok) { + const deleteDraftErrorMsg = + `Error deleting draft with id ${id}. Contact the app maintainers with the following - (Error detail: ${deleteResp.error})`; + console.error(deleteDraftErrorMsg); + + await client.functions.completeError({ + function_execution_id: body.function_data.execution_id, + error: deleteDraftErrorMsg, + }); + } + + // If message_ts isn't empty, Delete the draft message from the Draft Channel + if (message_ts) { + const updateResp = await client.chat.delete({ + channel: inputs.channel, + ts: message_ts, + }); + + // If the DELETE message operation fails, log the error + if (!updateResp.ok) { + const updateDraftPreviewErrorMsg = + `Error deleting the draft message: ${message_ts} in channel ${inputs.channel}. Contact the app maintainers with the following - (Error detail: ${updateResp.error})`; + console.error(updateDraftPreviewErrorMsg); + } + } + } catch (error) { + console.error(error); await client.functions.completeError({ function_execution_id: body.function_data.execution_id, - error: deleteDraftErrorMsg, + error: "Error discarding draft", }); } - - // Delete the draft message from the Draft Channel - const updateResp = await client.chat.delete({ - channel: inputs.channel, - ts: message_ts, - }); - - // If the DELETE message operation fails, log the error and complete the function with an error - if (!updateResp.ok) { - const updateDraftPreviewErrorMsg = - `Error deleting the draft message: ${message_ts} in channel ${inputs.channel}. Contact the app maintainers with the following - (Error detail: ${updateResp.error})`; - console.error(updateDraftPreviewErrorMsg); - } } }; From 80a909b4b27b744f6256f4f74560c2fa73126834 Mon Sep 17 00:00:00 2001 From: NickChokas <93598341+NickChokas@users.noreply.github.com> Date: Tue, 18 Jun 2024 16:09:13 -0400 Subject: [PATCH 5/5] Added 2 throw errors instead of erroring to the Console and completeError --- functions/create_draft/interactivity_handler.ts | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/functions/create_draft/interactivity_handler.ts b/functions/create_draft/interactivity_handler.ts index 816f47a..257e706 100644 --- a/functions/create_draft/interactivity_handler.ts +++ b/functions/create_draft/interactivity_handler.ts @@ -88,16 +88,11 @@ export const openDraftEditView: BlockActionHandler< id: id, }); - // If the DELETE datastore record operation fails, log the error and complete the function with an error + // If the DELETE datastore record operation fails, throw an error if (!deleteResp.ok) { const deleteDraftErrorMsg = `Error deleting draft with id ${id}. Contact the app maintainers with the following - (Error detail: ${deleteResp.error})`; - console.error(deleteDraftErrorMsg); - - await client.functions.completeError({ - function_execution_id: body.function_data.execution_id, - error: deleteDraftErrorMsg, - }); + throw new Error(deleteDraftErrorMsg); } // If message_ts isn't empty, Delete the draft message from the Draft Channel @@ -107,13 +102,14 @@ export const openDraftEditView: BlockActionHandler< ts: message_ts, }); - // If the DELETE message operation fails, log the error + // If the DELETE message operation fails, throw an error if (!updateResp.ok) { const updateDraftPreviewErrorMsg = `Error deleting the draft message: ${message_ts} in channel ${inputs.channel}. Contact the app maintainers with the following - (Error detail: ${updateResp.error})`; - console.error(updateDraftPreviewErrorMsg); + throw new Error(updateDraftPreviewErrorMsg); } } + // If the DELETE datastore record or Slack Message operations fail, log the error and complete the function with an error } catch (error) { console.error(error);