Skip to content

Commit

Permalink
feat: store log message info for cases
Browse files Browse the repository at this point in the history
  • Loading branch information
didinele committed Jul 12, 2024
1 parent 0fdb2c8 commit 6cb9525
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 6 deletions.
1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/database/IDatabase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type {
LogWebhookKind,
ModCase,
ModCaseKind,
ModCaseLogMessage,
} from '../db.js';

export type ExperimentWithOverrides = Selectable<Experiment> & { overrides: Selectable<ExperimentOverride>[] };
Expand Down Expand Up @@ -46,5 +47,9 @@ export abstract class IDatabase {

public abstract createModCase(options: CreateModCaseOptions): Promise<Selectable<ModCase>>;

public abstract createModCaseLogMessage(
options: Selectable<ModCaseLogMessage>,
): Promise<Selectable<ModCaseLogMessage>>;

public abstract getLogWebhook(guildId: string, kind: LogWebhookKind): Promise<Selectable<LogWebhook> | undefined>;
}
8 changes: 7 additions & 1 deletion packages/core/src/database/KyselyPostgresDatabase.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -104,6 +104,12 @@ export class KyselyPostgresDatabase extends IDatabase {
});
}

public override async createModCaseLogMessage(
options: Selectable<ModCaseLogMessage>,
): Promise<Selectable<ModCaseLogMessage>> {
return this.#database.insertInto('ModCaseLogMessage').values(options).returningAll().executeTakeFirstOrThrow();
}

public override async getLogWebhook(
guildId: string,
kind: LogWebhookKind,
Expand Down
6 changes: 6 additions & 0 deletions packages/core/src/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,17 @@ export type ModCase = {
modId: string;
targetId: string;
};
export type ModCaseLogMessage = {
caseId: number;
messageId: string;
channelId: string;
};
export type DB = {
CaseReference: CaseReference;
Experiment: Experiment;
ExperimentOverride: ExperimentOverride;
Incident: Incident;
LogWebhook: LogWebhook;
ModCase: ModCase;
ModCaseLogMessage: ModCaseLogMessage;
};
19 changes: 16 additions & 3 deletions packages/core/src/notifications/Notifier.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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?

Check warning on line 116 in packages/core/src/notifications/Notifier.ts

View workflow job for this annotation

GitHub Actions / Quality Check

Unexpected 'todo' comment: 'TODO: Take in APIGuild?'
Expand Down
12 changes: 10 additions & 2 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6cb9525

Please sign in to comment.