Skip to content

Commit

Permalink
Improvements to undo + support for old handler callback format (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
anirudhb authored Nov 13, 2023
2 parents 74e4041 + 61d1409 commit e9a566c
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 28 deletions.
57 changes: 36 additions & 21 deletions lib/interaction_handlers/block_action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,28 +92,43 @@ const block_action: InteractionHandler<BlockActionInteraction> = 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";
}
Expand Down
31 changes: 27 additions & 4 deletions lib/interaction_handlers/view_submission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,19 @@ import { confessions_channel } from "../secrets_wrapper";
import { sanitize } from "../sanitizer";
import getRepository from "../db";

const make_dialog = <T>(name: string): [(d: T) => string, (f: ((d: T) => Promise<boolean>)) => (id: string) => Promise<boolean | null>] => [
const make_dialog = <T>(name: string): [(d: T) => string, (f: ((d: T | string[]) => Promise<boolean>)) => (id: string) => Promise<boolean | null>] => [
// 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("_"));
}
}
];

Expand All @@ -36,6 +42,9 @@ const view_submission: InteractionHandler<ViewSubmissionInteraction> = 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) {
Expand Down Expand Up @@ -86,7 +95,14 @@ const view_submission: InteractionHandler<ViewSubmissionInteraction> = 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 });
Expand Down Expand Up @@ -139,6 +155,9 @@ const view_submission: InteractionHandler<ViewSubmissionInteraction> = 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) {
Expand Down Expand Up @@ -174,7 +193,11 @@ const view_submission: InteractionHandler<ViewSubmissionInteraction> = 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;
Expand Down
6 changes: 3 additions & 3 deletions lib/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down Expand Up @@ -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"
)
Expand Down Expand Up @@ -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: "",
Expand Down
4 changes: 4 additions & 0 deletions pages/api/interaction_work.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ export interface BlockActionInteraction {
text: string;
ts: string;
thread_ts?: string;
edited?: {
user: string;
ts: string;
}
};
actions: {
block_id: string;
Expand Down

1 comment on commit e9a566c

@vercel
Copy link

@vercel vercel bot commented on e9a566c Nov 13, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

prox2 – ./

prox2-git-master-anirudhb1.vercel.app
prox2.vercel.app
prox2-anirudhb1.vercel.app

Please sign in to comment.