From 6cb9525f77347ab62e54242898571de4fa66cb55 Mon Sep 17 00:00:00 2001 From: didinele Date: Fri, 12 Jul 2024 17:03:41 +0300 Subject: [PATCH] feat: store log message info for cases --- packages/core/package.json | 1 + packages/core/src/database/IDatabase.ts | 5 +++++ .../src/database/KyselyPostgresDatabase.ts | 8 +++++++- packages/core/src/db.ts | 6 ++++++ packages/core/src/notifications/Notifier.ts | 19 ++++++++++++++++--- prisma/schema.prisma | 12 ++++++++++-- yarn.lock | 1 + 7 files changed, 46 insertions(+), 6 deletions(-) diff --git a/packages/core/package.json b/packages/core/package.json index 5f6de04d..5a1e87ef 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -23,6 +23,7 @@ "@discordjs/brokers": "^0.3.0", "@discordjs/builders": "^1.8.2", "@discordjs/core": "^1.2.0", + "@discordjs/formatters": "^0.4.0", "@discordjs/rest": "^2.3.0", "@msgpack/msgpack": "^3.0.0-beta2", "@naval-base/ms": "^3.1.0", diff --git a/packages/core/src/database/IDatabase.ts b/packages/core/src/database/IDatabase.ts index 6a5b7ebd..0556ad8e 100644 --- a/packages/core/src/database/IDatabase.ts +++ b/packages/core/src/database/IDatabase.ts @@ -8,6 +8,7 @@ import type { LogWebhookKind, ModCase, ModCaseKind, + ModCaseLogMessage, } from '../db.js'; export type ExperimentWithOverrides = Selectable & { overrides: Selectable[] }; @@ -46,5 +47,9 @@ export abstract class IDatabase { public abstract createModCase(options: CreateModCaseOptions): Promise>; + public abstract createModCaseLogMessage( + options: Selectable, + ): Promise>; + public abstract getLogWebhook(guildId: string, kind: LogWebhookKind): Promise | undefined>; } diff --git a/packages/core/src/database/KyselyPostgresDatabase.ts b/packages/core/src/database/KyselyPostgresDatabase.ts index fd860ee3..22645be7 100644 --- a/packages/core/src/database/KyselyPostgresDatabase.ts +++ b/packages/core/src/database/KyselyPostgresDatabase.ts @@ -1,7 +1,7 @@ import { injectable } from 'inversify'; import { sql, Kysely, type Selectable, PostgresDialect } from 'kysely'; import { jsonArrayFrom } from 'kysely/helpers/postgres'; -import type { CaseReference, DB, Incident, LogWebhook, LogWebhookKind, ModCase } from '../db.js'; +import type { CaseReference, DB, Incident, LogWebhook, LogWebhookKind, ModCase, ModCaseLogMessage } from '../db.js'; import { Env } from '../util/Env.js'; import { IDatabase, @@ -104,6 +104,12 @@ export class KyselyPostgresDatabase extends IDatabase { }); } + public override async createModCaseLogMessage( + options: Selectable, + ): Promise> { + return this.#database.insertInto('ModCaseLogMessage').values(options).returningAll().executeTakeFirstOrThrow(); + } + public override async getLogWebhook( guildId: string, kind: LogWebhookKind, diff --git a/packages/core/src/db.ts b/packages/core/src/db.ts index 731e888d..741e1ef9 100644 --- a/packages/core/src/db.ts +++ b/packages/core/src/db.ts @@ -58,6 +58,11 @@ export type ModCase = { modId: string; targetId: string; }; +export type ModCaseLogMessage = { + caseId: number; + messageId: string; + channelId: string; +}; export type DB = { CaseReference: CaseReference; Experiment: Experiment; @@ -65,4 +70,5 @@ export type DB = { Incident: Incident; LogWebhook: LogWebhook; ModCase: ModCase; + ModCaseLogMessage: ModCaseLogMessage; }; diff --git a/packages/core/src/notifications/Notifier.ts b/packages/core/src/notifications/Notifier.ts index a3bf9c28..491e3041 100644 --- a/packages/core/src/notifications/Notifier.ts +++ b/packages/core/src/notifications/Notifier.ts @@ -1,5 +1,5 @@ import { truncateEmbed } from '@chatsift/discord-utils'; -import { API, type APIEmbed } from '@discordjs/core'; +import { API, type APIEmbed, type APIMessage } from '@discordjs/core'; import { inject, injectable } from 'inversify'; import type { Selectable } from 'kysely'; import type { Logger } from 'pino'; @@ -93,11 +93,24 @@ export class Notifier extends INotifier { thread_id: webhook.threadId ?? undefined, }; + let message: APIMessage; + if (options.existingMessage) { - await this.api.webhooks.editMessage(webhook.webhookId, webhook.webhookToken, options.existingMessage.id, data); + message = await this.api.webhooks.editMessage( + webhook.webhookId, + webhook.webhookToken, + options.existingMessage.id, + data, + ); } else { - await this.api.webhooks.execute(webhook.webhookId, webhook.webhookToken, data); + message = await this.api.webhooks.execute(webhook.webhookId, webhook.webhookToken, { ...data, wait: true }); } + + await this.database.createModCaseLogMessage({ + messageId: message.id, + caseId: options.modCase.id, + channelId: message.channel_id, + }); } // TODO: Take in APIGuild? diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 81ce8382..662ba927 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -54,8 +54,9 @@ model ModCase { modId String targetId String - referencedBy CaseReference[] @relation("caseReferencedBy") - references CaseReference[] @relation("caseReferences") + referencedBy CaseReference[] @relation("caseReferencedBy") + references CaseReference[] @relation("caseReferences") + ModCaseLogMessage ModCaseLogMessage? } model CaseReference { @@ -67,6 +68,13 @@ model CaseReference { @@id([referencedById, referencesId]) } +model ModCaseLogMessage { + caseId Int @id + modCase ModCase @relation(fields: [caseId], references: [id], onDelete: Cascade) + messageId String + channelId String +} + enum LogWebhookKind { Mod } diff --git a/yarn.lock b/yarn.lock index 62ab63d6..7d7a5842 100644 --- a/yarn.lock +++ b/yarn.lock @@ -107,6 +107,7 @@ __metadata: "@discordjs/brokers": ^0.3.0 "@discordjs/builders": ^1.8.2 "@discordjs/core": ^1.2.0 + "@discordjs/formatters": ^0.4.0 "@discordjs/rest": ^2.3.0 "@msgpack/msgpack": ^3.0.0-beta2 "@naval-base/ms": ^3.1.0