From 61d14093868b4eac7cc0bf891e75b42cc9aba97e Mon Sep 17 00:00:00 2001 From: Merlin04 Date: Fri, 6 Oct 2023 14:11:10 -0700 Subject: [PATCH] Improvements to undo + support for old handler callback format --- lib/interaction_handlers/block_action.ts | 57 +++++++++++++-------- lib/interaction_handlers/view_submission.ts | 31 +++++++++-- lib/main.ts | 6 +-- pages/api/interaction_work.ts | 4 ++ 4 files changed, 70 insertions(+), 28 deletions(-) diff --git a/lib/interaction_handlers/block_action.ts b/lib/interaction_handlers/block_action.ts index a10c7c8..1b44fa2 100644 --- a/lib/interaction_handlers/block_action.ts +++ b/lib/interaction_handlers/block_action.ts @@ -92,28 +92,43 @@ const block_action: InteractionHandler = async data => { // await unviewConfession(repo, data.message.ts, reviewer_uid, undoer_uid); - const resp = await web.views.open({ - trigger_id: data.trigger_id, - view: { - callback_id: undo_confirm_id({ - ts: data.message.ts, - reviewer_uid, - undoer_uid - }), - type: "modal", - title: new PlainText(`Undo confession review`).render(), - submit: new PlainText("Undo").render(), - close: new PlainText("Cancel").render(), - blocks: new Blocks([ - // text - new TextSection( - new MarkdownText( - "Undoing approval is undoable, however replies will not be preserved." + const resp = Number(new Date()) - Number(data.message.edited?.ts ?? data.message.ts) * 1000 > /* 1 week */ 7 * 24 * 60 * 60 * 1000 + ? await web.views.open({ + trigger_id: data.trigger_id, + view: { + type: "modal", + title: new PlainText(`Undo confession review`).render(), + close: new PlainText("Close").render(), + blocks: new Blocks([ + new TextSection( + new MarkdownText( + "It has been a week since the review of this confession, so you can not undo it." + ) ) - ) - ]).render() - } - }); + ]).render() + } + }) : await web.views.open({ + trigger_id: data.trigger_id, + view: { + callback_id: undo_confirm_id({ + ts: data.message.ts, + reviewer_uid, + undoer_uid + }), + type: "modal", + title: new PlainText(`Undo confession review`).render(), + submit: new PlainText("Undo").render(), + close: new PlainText("Cancel").render(), + blocks: new Blocks([ + // text + new TextSection( + new MarkdownText( + "Undoing approval is undoable, however replies will not be preserved." + ) + ) + ]).render() + } + }); if (!resp.ok) { throw "Failed to open modal"; } diff --git a/lib/interaction_handlers/view_submission.ts b/lib/interaction_handlers/view_submission.ts index 144f3fb..298f7ef 100644 --- a/lib/interaction_handlers/view_submission.ts +++ b/lib/interaction_handlers/view_submission.ts @@ -7,13 +7,19 @@ import { confessions_channel } from "../secrets_wrapper"; import { sanitize } from "../sanitizer"; import getRepository from "../db"; -const make_dialog = (name: string): [(d: T) => string, (f: ((d: T) => Promise)) => (id: string) => Promise] => [ +const make_dialog = (name: string): [(d: T) => string, (f: ((d: T | string[]) => Promise)) => (id: string) => Promise] => [ // generate callback id d => `${name}_${btoa(JSON.stringify(d))}`, // callback handler f => async id => { if(!id.startsWith(name)) return null; - return await f(JSON.parse(atob(id.slice(name.length + 1))) as T); + const data = id.slice(name.length + 1); + try { + return await f(JSON.parse(atob(data)) as T); + } catch(_) { + // old format + return await f(data.split("_")); + } } ]; @@ -36,6 +42,9 @@ const view_submission: InteractionHandler = async (da // todo passthrough return const dialogs = [ reply_modal_handler(async (published_ts) => { + if(Array.isArray(published_ts)) { + published_ts = published_ts[0]; + } const repo = await getRepository(); const record = await repo.findOne({ published_ts }); if (record === undefined) { @@ -86,7 +95,14 @@ const view_submission: InteractionHandler = async (da return true; }), - react_modal_handler(async ({ published_ts, thread_ts }) => { + react_modal_handler(async (args) => { + if(Array.isArray(args)) { + args = { + published_ts: args[0], + thread_ts: args[1] + }; + } + const { published_ts, thread_ts } = args; // try to fetch record const repo = await getRepository(); const record = await repo.findOne({ published_ts }); @@ -139,6 +155,9 @@ const view_submission: InteractionHandler = async (da }), approve_tw_handler(async (staging_ts) => { + if(Array.isArray(staging_ts)) { + staging_ts = staging_ts[0]; + } const repo = await getRepository(); const record = await repo.findOne({ staging_ts }); if (record === undefined) { @@ -174,7 +193,11 @@ const view_submission: InteractionHandler = async (da return true; }), - undo_confirm_handler(async ({ ts, reviewer_uid, undoer_uid }) => { + undo_confirm_handler(async (args) => { + if(Array.isArray(args)) { + throw `undo_confirm_handler is old format!`; + } + const { ts, reviewer_uid, undoer_uid } = args; const repo = await getRepository(); await unviewConfession(repo, ts, reviewer_uid, undoer_uid); return true; diff --git a/lib/main.ts b/lib/main.ts index c1db186..63c2abe 100644 --- a/lib/main.ts +++ b/lib/main.ts @@ -204,7 +204,7 @@ export async function stageDMConfession( console.log(`Posting confirmation message...`); const confirmation_message = await web.chat.postMessage({ channel: uid, - text: "", + text: "Would you like to stage this confession?", thread_ts: message_ts, reply_broadcast: true, blocks: new Blocks([ @@ -445,7 +445,7 @@ export async function viewConfession( new TextSection(new MarkdownText(statusText)), new ActionsSection([ new ButtonAction( - new PlainText(":oops: Undo"), + new PlainText(":rewind: Undo"), "undo", "undo" ) @@ -549,7 +549,7 @@ export async function unviewConfession( // Log undo console.log(`Logging undo...`); - const log_message_text = `:oops: ${old_approved ? "Approval" : "Rejection"} (by <@${reviewer_uid}>) of confession #${record.id} undone by <@${undoer_uid}>`; + const log_message_text = `:rewind: ${old_approved ? "Approval" : "Rejection"} (by <@${reviewer_uid}>) of confession #${record.id} undone by <@${undoer_uid}>`; const log_message = await web.chat.postMessage({ channel: staging_channel, text: "", diff --git a/pages/api/interaction_work.ts b/pages/api/interaction_work.ts index a63058f..72301b5 100644 --- a/pages/api/interaction_work.ts +++ b/pages/api/interaction_work.ts @@ -47,6 +47,10 @@ export interface BlockActionInteraction { text: string; ts: string; thread_ts?: string; + edited?: { + user: string; + ts: string; + } }; actions: { block_id: string;