diff --git a/src/discord.ts b/src/discord.ts index 2fefe91c..8bbd3241 100644 --- a/src/discord.ts +++ b/src/discord.ts @@ -17,8 +17,9 @@ import { import commands from './commands' import config from './config/discord' import elastic from './elastic' +import { EventEmitter } from 'events' -export class Discord { +export class Discord extends EventEmitter { client: Client rest: REST commands: Array @@ -26,6 +27,7 @@ export class Discord { channelId: string constructor({ token, guildId, clientId, channelId }) { + super() this.token = token this.channelId = channelId this.client = new Client({ intents: [GatewayIntentBits.Guilds] }) @@ -53,10 +55,11 @@ export class Discord { } else if (interaction.isButton()) { let reportState = '' - const [action, documentId] = interaction.customId.split('-'); + const [action, documentId] = interaction.customId.split('-') switch (action) { case 'approve': - reportState = 'approved'; + this.emit('approve', documentId) + reportState = 'approved' interaction.update({ embeds: [ new EmbedBuilder() @@ -69,7 +72,7 @@ export class Discord { }) break case 'edit': - reportState = 'edited'; + reportState = 'edited' const input = new TextInputBuilder() .setCustomId('editInput') .setLabel(`Granska utsläppsdata`) @@ -100,13 +103,16 @@ export class Discord { if (submitted) { const userInput = submitted.fields.getTextInputValue('editInput') + this.emit('edit', documentId, userInput) + await submitted.reply({ content: `Tack för din granskning: \n ${userInput}`, }) } break case 'reject': - reportState = 'rejected'; + reportState = 'rejected' + this.emit('reject', documentId) interaction.update({ content: 'Rejected!', embeds: [], diff --git a/src/workers/discordReview.ts b/src/workers/discordReview.ts index 9369307c..563a9595 100644 --- a/src/workers/discordReview.ts +++ b/src/workers/discordReview.ts @@ -29,7 +29,8 @@ class JobData extends Job { async function saveToDb(id: string, report: any) { return await elastic.indexReport(id, report) } -const createButtonRow = (documentId) => { // TODO: move to discord.ts +const createButtonRow = (documentId) => { + // TODO: move to discord.ts return new ActionRowBuilder().addComponents( new ButtonBuilder() .setCustomId(`approve-${documentId}`) @@ -86,6 +87,18 @@ ${job.data.url} throw error } + discord.once('edit', async (documentId, feedback) => { + if (documentId === documentId) { + job.log(`Got feedback: ${feedback}`) + await userFeedback.add('userFeedback', { + ...job.data, + documentId, + json: parsedJson, + feedback, + }) + } + }) + job.updateProgress(40) job.updateProgress(100) }, diff --git a/src/workers/userFeedback.ts b/src/workers/userFeedback.ts index 8e7009b7..da81a6d1 100644 --- a/src/workers/userFeedback.ts +++ b/src/workers/userFeedback.ts @@ -21,11 +21,8 @@ const embedder = new OpenAIEmbeddingFunction({ class JobData extends Job { data: { url: string - json: string - channelId: string threadId: string - messageId: string - pdfHash: string + json: string feedback: string } } @@ -33,7 +30,7 @@ class JobData extends Job { const worker = new Worker( 'userFeedback', async (job: JobData) => { - const { feedback, url, json: previousJson } = job.data + const { feedback, url, json: previousJson, threadId } = job.data const client = new ChromaClient(chromadb) const collection = await client.getCollection({ name: cleanCollectionName(url), @@ -80,7 +77,7 @@ const worker = new Worker( const parsedJson = JSON.parse(json) // we want to make sure it's valid JSON- otherwise we'll get an error which will trigger a new retry - const thread = await discord.client.channels.fetch(job.data.threadId) + const thread = await discord.client.channels.fetch(threadId) if (thread.isThread()) { const summary = await summaryTable(parsedJson)