Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add discard option #42

Merged
merged 7 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions functions/create_draft/blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ export const buildDraftBlocks = (
},
"value": "edit_message_overflow",
},
{
"text": {
"type": "plain_text",
"text": "Discard the draft message",
},
"value": "discard_message_overflow",
},
],
"action_id": "preview_overflow",
},
Expand Down
47 changes: 43 additions & 4 deletions functions/create_draft/interactivity_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ import DraftDatastore from "../../datastores/drafts.ts";

export const openDraftEditView: BlockActionHandler<
typeof CreateDraftFunction.definition
> = async ({ body, action, client }) => {
if (action.selected_option.value == "edit_message_overflow") {
> = async ({ body, action, client, inputs }) => {
// If the user selects to edit the draft message
if (action.selected_option.value === "edit_message_overflow") {
const id = action.block_id;

// Get the draft
Expand All @@ -37,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,
Expand All @@ -65,14 +67,51 @@ 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,
error: draftEditModalErrorMsg,
});
}
}

// If the user selects to discard the draft message
if (action.selected_option.value === "discard_message_overflow") {
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,
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without getting too deep in thought, I might opt to delete from the datastore before deleting the message in case something goes wrong with the datastore. In either case an error from either of these gets us into a weird state, so I don't think this has to be changed, but would rather the datastore act as a source of drafting truths if possible!

Copy link
Contributor Author

@NickChokas NickChokas Jun 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh yeah that makes sense to me. I'll move the Delete Datastore operation before the Slack Message Deletion 👌

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the update! Realizing now that moving the datastore check first might also let us return from the function early and avoid deleting the chat message after an error 😳

I'm thinking a try / catch wrapping this function could be interesting, then throwing errors instead of returning early 🤔 But an early return works well too!


// 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,
});
}

// Delete the draft message from the Draft Channel
const updateResp = await client.chat.delete({
channel: inputs.channel,
ts: message_ts,
zimeg marked this conversation as resolved.
Show resolved Hide resolved
});

// If the DELETE message operation fails, log the error and complete the function with an error
zimeg marked this conversation as resolved.
Show resolved Hide resolved
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);
}
}
};

export const saveDraftEditSubmission: ViewSubmissionHandler<
Expand Down