From 2bd68febaeb6580cefec3f66ee36781e747284c1 Mon Sep 17 00:00:00 2001 From: Hampus Date: Wed, 25 Nov 2026 07:10:47 +0100 Subject: [PATCH] fix(api): fill report context fields and drop a dead dm field --- fluxer_admin/openapi-admin.json | 4 - fluxer_admin/src/api/types/user_detail.rs | 1 - .../api/admin/services/AdminReportService.ts | 115 +++++++++--------- .../api/admin/services/AdminUserService.ts | 1 - .../src/domains/admin/AdminUserSchemas.ts | 2 - 5 files changed, 55 insertions(+), 68 deletions(-) diff --git a/fluxer_admin/openapi-admin.json b/fluxer_admin/openapi-admin.json index 261d944fe..a97c28113 100644 --- a/fluxer_admin/openapi-admin.json +++ b/fluxer_admin/openapi-admin.json @@ -15713,7 +15713,6 @@ "channel_id": {"$ref": "#/components/schemas/SnowflakeStringType"}, "channel_type": {"nullable": true, "allOf": [{"$ref": "#/components/schemas/Int32Type"}]}, "channel_nsfw": {"nullable": true, "type": "boolean"}, - "guild_nsfw_level": {"nullable": true, "allOf": [{"$ref": "#/components/schemas/NSFWLevel"}]}, "recipient_ids": { "maxItems": 100, "type": "array", @@ -15748,7 +15747,6 @@ "channel_id", "channel_type", "channel_nsfw", - "guild_nsfw_level", "recipient_ids", "recipients", "last_message_id", @@ -15776,7 +15774,6 @@ "channel_id": {"$ref": "#/components/schemas/SnowflakeStringType"}, "channel_type": {"nullable": true, "allOf": [{"$ref": "#/components/schemas/Int32Type"}]}, "channel_nsfw": {"nullable": true, "type": "boolean"}, - "guild_nsfw_level": {"nullable": true, "allOf": [{"$ref": "#/components/schemas/NSFWLevel"}]}, "recipient_ids": { "maxItems": 100, "type": "array", @@ -15811,7 +15808,6 @@ "channel_id", "channel_type", "channel_nsfw", - "guild_nsfw_level", "recipient_ids", "recipients", "last_message_id", diff --git a/fluxer_admin/src/api/types/user_detail.rs b/fluxer_admin/src/api/types/user_detail.rs index c605426a5..8b70aff0b 100644 --- a/fluxer_admin/src/api/types/user_detail.rs +++ b/fluxer_admin/src/api/types/user_detail.rs @@ -57,7 +57,6 @@ pub struct DmChannel { pub channel_id: String, pub channel_type: Option, pub channel_nsfw: Option, - pub guild_nsfw_level: Option, #[serde(default)] pub recipient_ids: Vec, #[serde(default)] diff --git a/fluxer_api/src/api/admin/services/AdminReportService.ts b/fluxer_api/src/api/admin/services/AdminReportService.ts index 1fc96062b..a24c0b98b 100644 --- a/fluxer_api/src/api/admin/services/AdminReportService.ts +++ b/fluxer_api/src/api/admin/services/AdminReportService.ts @@ -30,6 +30,7 @@ import type {UserCacheService} from '@app/api/infrastructure/UserCacheService'; import {Logger} from '@app/api/Logger'; import type {RequestCache} from '@app/api/middleware/RequestCacheMiddleware'; import {createRequestCache} from '@app/api/middleware/RequestCacheMiddleware'; +import type {Guild} from '@app/api/models/Guild'; import {describeReportFlowAnswers} from '@app/api/report/flows/ReportFlowRegistry'; import {findReportReason, listReportReasons} from '@app/api/report/flows/ReportReasonCatalog'; import type {IARMessageContext, IARSubmission} from '@app/api/report/ReportModels'; @@ -106,16 +107,23 @@ function staffOutcome( } } +interface ChannelContext { + nsfw: boolean | null; + contentWarningLevel: number; + contentWarningText: string | null; + guildId: GuildID | null; +} + interface ReportLookupCache { - channelNsfwByChannelId: Map; - guildNsfwLevelByGuildId: Map; + channelContextByChannelId: Map; + guildByGuildId: Map; reporterEmailByReporterId: Map>; } function createReportLookupCache(): ReportLookupCache { return { - channelNsfwByChannelId: new Map(), - guildNsfwLevelByGuildId: new Map(), + channelContextByChannelId: new Map(), + guildByGuildId: new Map(), reporterEmailByReporterId: new Map(), }; } @@ -381,16 +389,17 @@ export class AdminReportService { if (report.reportedGuildNsfw !== null) { reportedGuildNsfwLevel = report.reportedGuildNsfw ? 3 : 0; } else { - reportedGuildNsfwLevel = await this.getGuildNsfwLevelForContext( - report.reportedChannelId, - report.reportedGuildId ?? report.guildContextId ?? null, + const channel = await this.getChannelContext(report.reportedChannelId, reportLookupCache); + const guild = await this.getGuild( + report.reportedGuildId ?? report.guildContextId ?? channel?.guildId ?? null, reportLookupCache, ); + reportedGuildNsfwLevel = guild?.nsfwLevel ?? null; } const reportedChannelNsfw = report.reportedChannelEffectiveNsfw !== null ? report.reportedChannelEffectiveNsfw - : await this.getChannelNsfwState(report.reportedChannelId, reportLookupCache); + : ((await this.getChannelContext(report.reportedChannelId, reportLookupCache))?.nsfw ?? null); const baseResponse = { report_id: report.reportId.toString(), reporter_id: report.reporterId?.toString() ?? null, @@ -450,7 +459,7 @@ export class AdminReportService { if (!includeContext) { return baseResponse; } - const authorBotFlags = await this.getContextAuthorBotFlags(report, requestCache); + const authors = await this.getContextAuthors(report, requestCache); const messageContext = ( await Promise.all( (report.messageContext ?? []).map((message) => @@ -459,7 +468,7 @@ export class AdminReportService { report.reportedChannelId ?? null, report.reportedGuildId ?? report.guildContextId ?? null, reportLookupCache, - authorBotFlags, + authors, ), ), ) @@ -492,10 +501,10 @@ export class AdminReportService { return email; } - private async getContextAuthorBotFlags( + private async getContextAuthors( report: IARSubmission, requestCache: RequestCache, - ): Promise> { + ): Promise> { const authorIds = [ ...new Set((report.messageContext ?? []).flatMap((message) => (message.authorId ? [message.authorId] : []))), ]; @@ -504,9 +513,9 @@ export class AdminReportService { } try { const partials = await this.deps.userCacheService.getUserPartialResponses(authorIds, requestCache); - return new Map([...partials].map(([userId, partial]) => [userId.toString(), mapUserPartialBotFlag(partial)])); + return new Map([...partials].map(([userId, partial]) => [userId.toString(), partial])); } catch (error) { - Logger.warn({reportId: report.reportId.toString(), error}, 'Failed to resolve author bot flags for report'); + Logger.warn({reportId: report.reportId.toString(), error}, 'Failed to resolve context authors for report'); return new Map(); } } @@ -634,15 +643,16 @@ export class AdminReportService { fallbackChannelId: ChannelID | null, fallbackGuildId: GuildID | null, reportLookupCache: ReportLookupCache, - authorBotFlags: Map, + authors: Map, ) { const channelId = message.channelId ?? fallbackChannelId; if (!channelId) { return null; } const authorId = (message.authorId ?? message.webhookId ?? 0n).toString(); - const channelNsfw = await this.getChannelNsfwState(channelId, reportLookupCache); - const guildNsfwLevel = await this.getGuildNsfwLevelForContext(channelId, fallbackGuildId, reportLookupCache); + const channel = await this.getChannelContext(channelId, reportLookupCache); + const guild = await this.getGuild(fallbackGuildId ?? channel?.guildId ?? null, reportLookupCache); + const author = authors.get(authorId); const attachments = message.attachments && message.attachments.length > 0 ? ( @@ -667,83 +677,68 @@ export class AdminReportService { return { id: message.messageId.toString(), channel_id: channelId.toString(), - channel_nsfw: channelNsfw, - channel_content_warning_level: null, - channel_content_warning_text: null, + channel_nsfw: channel?.nsfw ?? null, + channel_content_warning_level: channel?.contentWarningLevel ?? null, + channel_content_warning_text: channel?.contentWarningText ?? null, guild_id: fallbackGuildId ? fallbackGuildId.toString() : null, - guild_nsfw_level: guildNsfwLevel, - guild_nsfw: null, - guild_content_warning_level: null, - guild_content_warning_text: null, + guild_nsfw_level: guild?.nsfwLevel ?? null, + guild_nsfw: guild?.nsfw ?? null, + guild_content_warning_level: guild?.contentWarningLevel ?? null, + guild_content_warning_text: guild?.contentWarningText ?? null, content: message.content ?? '', timestamp: message.timestamp.toISOString(), attachments, author_id: authorId, author_username: message.authorUsername, - author_global_name: null, + author_global_name: author?.global_name ?? null, author_discriminator: message.authorDiscriminator.toString().padStart(4, '0'), author_avatar: message.authorAvatarHash, webhook_id: message.webhookId?.toString() ?? null, - author_bot: message.authorId ? (authorBotFlags.get(authorId) ?? null) : null, + author_bot: author ? mapUserPartialBotFlag(author) : null, missing_attachments: message.missingAttachments .filter((attachment) => attachment.attachment_id != null && attachment.filename) .map(mapMissingAttachmentToResponse), }; } - private async getChannelNsfwState( + private async getChannelContext( channelId: ChannelID | null, reportLookupCache: ReportLookupCache, - ): Promise { + ): Promise { if (!channelId) { return null; } const channelIdString = channelId.toString(); - if (reportLookupCache.channelNsfwByChannelId.has(channelIdString)) { - return reportLookupCache.channelNsfwByChannelId.get(channelIdString) ?? null; + if (reportLookupCache.channelContextByChannelId.has(channelIdString)) { + return reportLookupCache.channelContextByChannelId.get(channelIdString) ?? null; } const channel = await this.deps.channelRepository.channelData.findUnique(channelId); const scope = channel ? await resolveNsfwScopeChannel(channel, (id) => this.deps.channelRepository.channelData.findUnique(id)) : null; - const channelNsfw = scope?.isNsfw ?? null; - reportLookupCache.channelNsfwByChannelId.set(channelIdString, channelNsfw); - return channelNsfw; + const context = channel + ? { + nsfw: scope?.isNsfw ?? null, + contentWarningLevel: channel.contentWarningLevel, + contentWarningText: channel.contentWarningText, + guildId: channel.guildId ?? null, + } + : null; + reportLookupCache.channelContextByChannelId.set(channelIdString, context); + return context; } - private async getGuildNsfwLevel( - guildId: GuildID | null, - reportLookupCache: ReportLookupCache, - ): Promise { + private async getGuild(guildId: GuildID | null, reportLookupCache: ReportLookupCache): Promise { if (!guildId) { return null; } const guildIdString = guildId.toString(); - if (reportLookupCache.guildNsfwLevelByGuildId.has(guildIdString)) { - return reportLookupCache.guildNsfwLevelByGuildId.get(guildIdString) ?? null; + if (reportLookupCache.guildByGuildId.has(guildIdString)) { + return reportLookupCache.guildByGuildId.get(guildIdString) ?? null; } const guild = await this.deps.guildRepository.findUnique(guildId); - const guildNsfwLevel = guild?.nsfwLevel ?? null; - reportLookupCache.guildNsfwLevelByGuildId.set(guildIdString, guildNsfwLevel); - return guildNsfwLevel; - } - - private async getGuildNsfwLevelForContext( - channelId: ChannelID | null, - fallbackGuildId: GuildID | null, - reportLookupCache: ReportLookupCache, - ): Promise { - if (fallbackGuildId) { - return this.getGuildNsfwLevel(fallbackGuildId, reportLookupCache); - } - if (!channelId) { - return null; - } - const channel = await this.deps.channelRepository.channelData.findUnique(channelId); - if (!channel?.guildId) { - return null; - } - return this.getGuildNsfwLevel(channel.guildId, reportLookupCache); + reportLookupCache.guildByGuildId.set(guildIdString, guild); + return guild; } private async mapReportAttachmentToResponse( diff --git a/fluxer_api/src/api/admin/services/AdminUserService.ts b/fluxer_api/src/api/admin/services/AdminUserService.ts index 6a9799a5b..0823492e2 100644 --- a/fluxer_api/src/api/admin/services/AdminUserService.ts +++ b/fluxer_api/src/api/admin/services/AdminUserService.ts @@ -223,7 +223,6 @@ export class AdminUserService { channel_id: summary.channelId.toString(), channel_type: summary.channelType, channel_nsfw: model?.isNsfw ?? null, - guild_nsfw_level: null, recipient_ids: recipientIds.map((id) => id.toString()), recipients: recipientIds .map((id) => resolvedUsers.get(id)) diff --git a/packages/schema/src/domains/admin/AdminUserSchemas.ts b/packages/schema/src/domains/admin/AdminUserSchemas.ts index 2c446402e..d64191623 100644 --- a/packages/schema/src/domains/admin/AdminUserSchemas.ts +++ b/packages/schema/src/domains/admin/AdminUserSchemas.ts @@ -18,7 +18,6 @@ import { } from '@fluxer/schema/src/domains/admin/EnforcementSchemas'; import {AdminProfileBadgeResponse} from '@fluxer/schema/src/domains/user/ProfileBadgeSchemas'; import {CalendarDateType} from '@fluxer/schema/src/primitives/DateValidators'; -import {NSFWLevelSchema} from '@fluxer/schema/src/primitives/GuildValidators'; import {createQueryIntegerType, QueryBooleanType} from '@fluxer/schema/src/primitives/QueryValidators'; import { createBitflagInt32Type, @@ -243,7 +242,6 @@ const AdminUserDmChannelSchema = z.object({ channel_id: SnowflakeStringType, channel_type: Int32Type.nullable(), channel_nsfw: z.boolean().nullable(), - guild_nsfw_level: NSFWLevelSchema.nullable(), recipient_ids: z.array(SnowflakeStringType).max(100), recipients: z.array(AdminResolvedUserSchema).max(100), last_message_id: SnowflakeStringType.nullable(),