|
| 1 | +import { getContext } from '@chatsift/backend-core'; |
| 2 | +import type { AmaQuestions, AmaSessions } from '@chatsift/db'; |
| 3 | +import type { APIMessageComponentInteraction } from '@discordjs/core'; |
| 4 | +import { ButtonStyle, ComponentType, MessageFlags } from '@discordjs/core'; |
| 5 | +import { client } from '../lib/client.js'; |
| 6 | +import type { ComponentHandler } from '../lib/components.js'; |
| 7 | +import { CurrentlyInQueue, getNextQueue, postToAnswersChannel, postToGuestQueue } from '../lib/queues.js'; |
| 8 | + |
| 9 | +export default class FlaggedApproveComponent implements ComponentHandler<string> { |
| 10 | + public readonly name = 'flagged-approve'; |
| 11 | + |
| 12 | + public readonly stateStore = null; |
| 13 | + |
| 14 | + public async handle(interaction: APIMessageComponentInteraction, questionIdStr: string) { |
| 15 | + const questionId = Number.parseInt(questionIdStr, 10); |
| 16 | + |
| 17 | + // Ack within Discord's 3s window before doing any DB/REST work below; everything past this point |
| 18 | + // finishes via editReply/followUp instead of reply/updateMessage. |
| 19 | + await client.api.interactions.deferMessageUpdate(interaction.id, interaction.token); |
| 20 | + |
| 21 | + try { |
| 22 | + const [question] = await getContext().db<AmaQuestions[]>` |
| 23 | + SELECT * FROM ama_questions WHERE id = ${questionId} |
| 24 | + `; |
| 25 | + |
| 26 | + if (!question) { |
| 27 | + await client.api.interactions.followUp(interaction.application_id, interaction.token, { |
| 28 | + content: 'Question not found. It may have been deleted.', |
| 29 | + flags: MessageFlags.Ephemeral, |
| 30 | + }); |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + const [session] = await getContext().db<AmaSessions[]>` |
| 35 | + SELECT * FROM ama_sessions WHERE id = ${question.amaId} |
| 36 | + `; |
| 37 | + |
| 38 | + if (!session) { |
| 39 | + throw new Error(`No AMA session found for id ${question.amaId}`); |
| 40 | + } |
| 41 | + |
| 42 | + if (session.ended) { |
| 43 | + await client.api.interactions.followUp(interaction.application_id, interaction.token, { |
| 44 | + content: 'This AMA session has ended.', |
| 45 | + flags: MessageFlags.Ephemeral, |
| 46 | + }); |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + // Get user details from the interaction |
| 51 | + const user = await client.api.users.get(question.authorId); |
| 52 | + const member = interaction.guild_id |
| 53 | + ? await client.api.guilds.getMember(interaction.guild_id, question.authorId).catch(() => undefined) |
| 54 | + : undefined; |
| 55 | + |
| 56 | + // Attachments aren't persisted on the row, so we carry them forward off the source message; the |
| 57 | + // question text itself comes straight from the DB (the source message's text has a footer baked in). |
| 58 | + const attachments = interaction.message.attachments ?? []; |
| 59 | + |
| 60 | + // A flag is a side-branch off the mod queue, so clearing it resumes the normal pipeline at the |
| 61 | + // stage that would've followed mod approval: guest queue if configured, otherwise straight to answers. |
| 62 | + const nextQueue = getNextQueue(CurrentlyInQueue.mod, session); |
| 63 | + |
| 64 | + // Post first, claim second: if the post throws, the row is never touched and stays FLAGGED, so |
| 65 | + // the button remains retryable. If we lose a claim race after posting (another moderator got |
| 66 | + // there first), we clean up the message we just created instead of leaving a stray duplicate. |
| 67 | + const reportLostRace = async (channelId: string, messageId: string) => { |
| 68 | + // eslint-disable-next-line promise/prefer-await-to-then |
| 69 | + void client.api.channels.deleteMessage(channelId, messageId).catch(() => null); |
| 70 | + await client.api.interactions.followUp(interaction.application_id, interaction.token, { |
| 71 | + content: 'This question was already handled by another moderator.', |
| 72 | + flags: MessageFlags.Ephemeral, |
| 73 | + }); |
| 74 | + }; |
| 75 | + |
| 76 | + if (nextQueue?.kind === CurrentlyInQueue.guest) { |
| 77 | + const msg = await postToGuestQueue({ |
| 78 | + attachments, |
| 79 | + content: question.content, |
| 80 | + member, |
| 81 | + question, |
| 82 | + session, |
| 83 | + user, |
| 84 | + }); |
| 85 | + |
| 86 | + const [claimed] = await getContext().db<AmaQuestions[]>` |
| 87 | + UPDATE ama_questions |
| 88 | + SET state = 'PENDING_GUEST_REVIEW', guest_queue_message_id = ${msg.id}, updated_at = now() |
| 89 | + WHERE id = ${question.id} AND state = 'FLAGGED' |
| 90 | + RETURNING * |
| 91 | + `; |
| 92 | + |
| 93 | + if (!claimed) { |
| 94 | + await reportLostRace(session.guestQueueId!, msg.id); |
| 95 | + return; |
| 96 | + } |
| 97 | + } else { |
| 98 | + const msg = await postToAnswersChannel({ |
| 99 | + attachments, |
| 100 | + content: question.content, |
| 101 | + member, |
| 102 | + question, |
| 103 | + session, |
| 104 | + user, |
| 105 | + }); |
| 106 | + |
| 107 | + const [claimed] = await getContext().db<AmaQuestions[]>` |
| 108 | + UPDATE ama_questions |
| 109 | + SET state = 'APPROVED', answers_message_id = ${msg.id}, updated_at = now() |
| 110 | + WHERE id = ${question.id} AND state = 'FLAGGED' |
| 111 | + RETURNING * |
| 112 | + `; |
| 113 | + |
| 114 | + if (!claimed) { |
| 115 | + await reportLostRace(session.answersChannelId, msg.id); |
| 116 | + return; |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + // Update the message to show it was approved |
| 121 | + await client.api.interactions.editReply(interaction.application_id, interaction.token, { |
| 122 | + components: [ |
| 123 | + { |
| 124 | + type: ComponentType.ActionRow, |
| 125 | + components: [ |
| 126 | + { |
| 127 | + type: ComponentType.Button, |
| 128 | + style: ButtonStyle.Success, |
| 129 | + label: '✅ Approved', |
| 130 | + custom_id: 'approved-disabled', |
| 131 | + disabled: true, |
| 132 | + }, |
| 133 | + ], |
| 134 | + }, |
| 135 | + ], |
| 136 | + }); |
| 137 | + |
| 138 | + getContext().logger.info({ questionId, amaId: question.amaId }, 'Flagged question approved by moderator'); |
| 139 | + } catch (error) { |
| 140 | + getContext().logger.error({ error, questionId }, 'Failed to approve flagged question'); |
| 141 | + await client.api.interactions.followUp(interaction.application_id, interaction.token, { |
| 142 | + content: 'Failed to approve question. Please try again.', |
| 143 | + flags: MessageFlags.Ephemeral, |
| 144 | + }); |
| 145 | + } |
| 146 | + } |
| 147 | +} |
0 commit comments