From 19a82b767f5a1874eaf3bc8901ed42136704d3f3 Mon Sep 17 00:00:00 2001 From: Dragory <2606411+Dragory@users.noreply.github.com> Date: Mon, 10 Aug 2020 02:35:47 +0300 Subject: [PATCH] When updating cases, update existing case embed in case log channel if possible --- backend/src/data/entities/Case.ts | 6 ++++++ .../1597015567215-AddLogMessageIdToCases.ts | 21 +++++++++++++++++++ .../src/plugins/Cases/functions/createCase.ts | 2 +- .../Cases/functions/postToCaseLogChannel.ts | 21 ++++++++++++++++--- 4 files changed, 46 insertions(+), 4 deletions(-) create mode 100644 backend/src/migrations/1597015567215-AddLogMessageIdToCases.ts diff --git a/backend/src/data/entities/Case.ts b/backend/src/data/entities/Case.ts index 29fc9061..a492b695 100644 --- a/backend/src/data/entities/Case.ts +++ b/backend/src/data/entities/Case.ts @@ -29,6 +29,12 @@ export class Case { @Column() pp_name: string; + /** + * ID of the channel and message where this case was logged. + * Format: "channelid-messageid" + */ + @Column() log_message_id: string; + @OneToMany( type => CaseNote, note => note.case, diff --git a/backend/src/migrations/1597015567215-AddLogMessageIdToCases.ts b/backend/src/migrations/1597015567215-AddLogMessageIdToCases.ts new file mode 100644 index 00000000..95fa7027 --- /dev/null +++ b/backend/src/migrations/1597015567215-AddLogMessageIdToCases.ts @@ -0,0 +1,21 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; +import { TableColumn } from "typeorm/index"; + +export class AddLogMessageIdToCases1597015567215 implements MigrationInterface { + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.addColumn( + "cases", + new TableColumn({ + name: "log_message_id", + type: "varchar", + length: "64", + isNullable: true, + default: null, + }), + ); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.dropColumn("cases", "log_message_id"); + } +} diff --git a/backend/src/plugins/Cases/functions/createCase.ts b/backend/src/plugins/Cases/functions/createCase.ts index 8cc31d3a..447dca85 100644 --- a/backend/src/plugins/Cases/functions/createCase.ts +++ b/backend/src/plugins/Cases/functions/createCase.ts @@ -1,6 +1,6 @@ import { CaseArgs, CasesPluginType } from "../types"; import { resolveUser } from "../../../utils"; -import { PluginData } from "knub"; +import { plugin, PluginData } from "knub"; import { createCaseNote } from "./createCaseNote"; import { postCaseToCaseLogChannel } from "./postToCaseLogChannel"; import { logger } from "../../../logger"; diff --git a/backend/src/plugins/Cases/functions/postToCaseLogChannel.ts b/backend/src/plugins/Cases/functions/postToCaseLogChannel.ts index 40b76283..3d464adc 100644 --- a/backend/src/plugins/Cases/functions/postToCaseLogChannel.ts +++ b/backend/src/plugins/Cases/functions/postToCaseLogChannel.ts @@ -12,7 +12,7 @@ export async function postToCaseLogChannel( pluginData: PluginData, content: MessageContent, file: MessageFile = null, -): Promise { +): Promise { const caseLogChannelId = pluginData.config.get().case_log_channel; if (!caseLogChannelId) return; @@ -42,15 +42,30 @@ export async function postToCaseLogChannel( export async function postCaseToCaseLogChannel( pluginData: PluginData, caseOrCaseId: Case | number, -): Promise { +): Promise { const theCase = await pluginData.state.cases.find(resolveCaseId(caseOrCaseId)); if (!theCase) return; const caseEmbed = await getCaseEmbed(pluginData, caseOrCaseId); if (!caseEmbed) return; + if (theCase.log_message_id) { + const [channelId, messageId] = theCase.log_message_id.split("-"); + + try { + await pluginData.client.editMessage(channelId, messageId, caseEmbed); + return; + } catch (e) {} // tslint:disable-line:no-empty + } + try { - return postToCaseLogChannel(pluginData, caseEmbed); + const postedMessage = await postToCaseLogChannel(pluginData, caseEmbed); + if (postedMessage) { + await pluginData.state.cases.update(theCase.id, { + log_message_id: `${postedMessage.channel.id}-${postedMessage.id}`, + }); + } + return postedMessage; } catch (e) { pluginData.state.logs.log(LogType.BOT_ALERT, { body: `Failed to post case #${theCase.case_number} to the case log channel`,