Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 46 additions & 14 deletions app/discord/deletionLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { logEffect } from "#~/effects/observability";
import { quoteMessageContent } from "#~/helpers/discord";
import { getOrCreateDeletionLogThread } from "#~/models/deletionLogThreads";
import { fetchSettingsEffect, SETTINGS } from "#~/models/guilds.server";
import { getOrCreateUserThread } from "#~/models/userThreads";

import {
MessageCacheService,
Expand Down Expand Up @@ -110,8 +111,8 @@ export async function startDeletionLogging(client: Client) {

const sent = `<t:${Math.floor(msg.createdTimestamp / 1000)}:R>`;
const header = uncachedAuditEntry?.executor
? `<@${uncachedAuditEntry.executor.id}> deleted from ${channelMention}, sent ${sent}`
: `Message deleted from ${channelMention}, sent ${sent}`;
? `-# <@${uncachedAuditEntry.executor.id}> deleted from ${channelMention}, sent ${sent}`
: `-# Message deleted from ${channelMention}, sent ${sent}`;

yield* Effect.tryPromise({
try: () =>
Expand Down Expand Up @@ -164,23 +165,23 @@ export async function startDeletionLogging(client: Client) {

const sent = `<t:${Math.floor(msg.createdTimestamp / 1000)}:R>`;
const header = auditEntry?.executor
? `<@${auditEntry.executor.id}> deleted from ${channelMention}, sent ${sent}`
: `Message deleted from ${channelMention}, sent ${sent}`;
? `-# <@${auditEntry.executor.id}> deleted from ${channelMention}, sent ${sent}`
: `-# Message deleted from ${channelMention}, sent ${sent}`;

const embed = {
description: [
header,
`<@${user.id}>`,
quoteMessageContent(content ?? "*(content not cached)*"),
].join("\n"),
color: Colors.Red,
};

yield* Effect.tryPromise({
try: () =>
thread.send({
allowedMentions: { parse: [] },
embeds: [
{
description: [
header,
`<@${user.id}>`,
quoteMessageContent(content ?? "*(content not cached)*"),
].join("\n"),
color: Colors.Red,
},
],
embeds: [embed],
}),
catch: (error) =>
logEffect(
Expand All @@ -190,6 +191,37 @@ export async function startDeletionLogging(client: Client) {
{ guildId: guild.id, error: String(error) },
),
}).pipe(Effect.catchAll((e) => e));

// If a mod deleted this message, also log to the moderation thread
if (auditEntry?.executor) {
const modThread = yield* getOrCreateUserThread(guild, user).pipe(
Effect.catchAll((error) =>
logEffect(
"warn",
"DeletionLogger",
"Failed to get/create moderation thread for mod deletion",
{ guildId: guild.id, userId: user.id, error: String(error) },
),
),
);

if (modThread) {
yield* Effect.tryPromise({
try: () =>
modThread.send({
allowedMentions: { parse: [] },
embeds: [embed],
}),
catch: (error) =>
logEffect(
"warn",
"DeletionLogger",
"Failed to post mod deletion to moderation thread",
{ guildId: guild.id, error: String(error) },
),
}).pipe(Effect.catchAll((e) => e));
}
}
}).pipe(
Effect.catchAll((e) =>
logEffect(
Expand Down
25 changes: 5 additions & 20 deletions app/models/reportedMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,24 +158,21 @@ export const getUserReportStats = (userId: string, guildId: string) =>
.selectFrom("reported_messages")
.select((eb) => eb.fn.count("id").as("count"))
.where("reported_user_id", "=", userId)
.where("guild_id", "=", guildId)
.where("deleted_at", "is", null),
.where("guild_id", "=", guildId),
kysely
.selectFrom("reported_messages")
.select(({ fn }) =>
fn.count("reported_message_id").distinct().as("count"),
)
.where("reported_user_id", "=", userId)
.where("guild_id", "=", guildId)
.where("deleted_at", "is", null),
.where("guild_id", "=", guildId),
kysely
.selectFrom("reported_messages")
.select(({ fn }) =>
fn.count("reported_channel_id").distinct().as("count"),
)
.where("reported_user_id", "=", userId)
.where("guild_id", "=", guildId)
.where("deleted_at", "is", null),
.where("guild_id", "=", guildId),
]);

return {
Expand All @@ -200,8 +197,7 @@ export const getSpamReportCount = (userId: string, guildId: string) =>
.select((eb) => eb.fn.count("id").as("count"))
.where("reported_user_id", "=", userId)
.where("guild_id", "=", guildId)
.where("reason", "=", ReportReasons.spam)
.where("deleted_at", "is", null);
.where("reason", "=", ReportReasons.spam);

return Number(result?.count ?? 0);
}).pipe(
Expand Down Expand Up @@ -233,7 +229,6 @@ export const getUserReportSummary = (userId: string, guildId: string) =>
.select((eb) => eb.fn.count("id").as("count"))
.where("reported_user_id", "=", userId)
.where("guild_id", "=", guildId)
.where("deleted_at", "is", null)
.groupBy("reason")
.orderBy("count", "desc"),
// Get first report (earliest created_at)
Expand All @@ -242,7 +237,6 @@ export const getUserReportSummary = (userId: string, guildId: string) =>
.select("created_at")
.where("reported_user_id", "=", userId)
.where("guild_id", "=", guildId)
.where("deleted_at", "is", null)
.orderBy("created_at", "asc")
.limit(1),
// Get last report (latest created_at)
Expand All @@ -251,15 +245,13 @@ export const getUserReportSummary = (userId: string, guildId: string) =>
.select("created_at")
.where("reported_user_id", "=", userId)
.where("guild_id", "=", guildId)
.where("deleted_at", "is", null)
.orderBy("created_at", "desc")
.limit(1),
kysely
.selectFrom("reported_messages")
.select((eb) => eb.fn.count("id").as("count"))
.where("reported_user_id", "=", userId)
.where("guild_id", "=", guildId)
.where("deleted_at", "is", null)
.where("reason", "=", ReportReasons.anonReport),
kysely
.selectFrom("reported_messages")
Expand All @@ -269,7 +261,6 @@ export const getUserReportSummary = (userId: string, guildId: string) =>
])
.where("reported_user_id", "=", userId)
.where("guild_id", "=", guildId)
.where("deleted_at", "is", null)
.groupBy(({ fn }) => fn("date", ["created_at"]))
.orderBy("count", "desc")
.limit(1),
Expand All @@ -278,7 +269,6 @@ export const getUserReportSummary = (userId: string, guildId: string) =>
.select(({ fn }) => fn.count("staff_id").distinct().as("count"))
.where("reported_user_id", "=", userId)
.where("guild_id", "=", guildId)
.where("deleted_at", "is", null)
.where("staff_id", "is not", null),
]);

Expand Down Expand Up @@ -324,7 +314,6 @@ export const getMonthlyReportCounts = (
])
.where("reported_user_id", "=", userId)
.where("guild_id", "=", guildId)
.where("deleted_at", "is", null)
.where("created_at", ">=", cutoff)
.groupBy(({ fn, val, ref }) =>
fn("strftime", [val("%Y-%m"), ref("created_at")]),
Expand Down Expand Up @@ -355,14 +344,12 @@ export const getRecentReportCount = (
.selectFrom("reported_messages")
.select((eb) => eb.fn.count("id").as("count"))
.where("reported_user_id", "=", userId)
.where("guild_id", "=", guildId)
.where("deleted_at", "is", null),
.where("guild_id", "=", guildId),
kysely
.selectFrom("reported_messages")
.select((eb) => eb.fn.count("id").as("count"))
.where("reported_user_id", "=", userId)
.where("guild_id", "=", guildId)
.where("deleted_at", "is", null)
.where("created_at", ">=", cutoff),
]);

Expand Down Expand Up @@ -394,7 +381,6 @@ export const getChannelBreakdown = (
.select((eb) => eb.fn.count("id").as("count"))
.where("reported_user_id", "=", userId)
.where("guild_id", "=", guildId)
.where("deleted_at", "is", null)
.groupBy("reported_channel_id")
.orderBy("count", "desc")
.limit(limit);
Expand All @@ -417,7 +403,6 @@ export const getStaffBreakdown = (userId: string, guildId: string, limit = 5) =>
.select((eb) => eb.fn.count("id").as("count"))
.where("reported_user_id", "=", userId)
.where("guild_id", "=", guildId)
.where("deleted_at", "is", null)
.where("staff_id", "is not", null)
.groupBy(["staff_id", "staff_username"])
.orderBy("count", "desc")
Expand Down