Skip to content

A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.

TypeScript fluxerfluxerapp

fix(api): apply media filters to forwarded embeds and categories (#3905)

Hampusa67baf9f8078
diff --git a/fluxer_api/src/api/channel/services/message/AttachmentProcessingService.ts b/fluxer_api/src/api/channel/services/message/AttachmentProcessingService.ts
index 351382b0c..0d5845fda 100644
--- a/fluxer_api/src/api/channel/services/message/AttachmentProcessingService.ts
+++ b/fluxer_api/src/api/channel/services/message/AttachmentProcessingService.ts
@@ -462,9 +462,36 @@ export class AttachmentProcessingService {
} else {
await this.scanContentModerationWithLocalFile({localPath: null, bucket, key, message});
}
- if (!isSupportedMediaContentType(attachment.content_type)) {
+ return this.classifyStoredMedia({attachment, bucket, key, nsfwMode});
+ }
+
+ async classifyStoredMedia(params: {
+ attachment: MessageAttachment;
+ bucket: string;
+ key: string;
+ nsfwMode: MediaProxyNsfwMode;
+ }): Promise<MessageAttachment> {
+ const {attachment, bucket, key, nsfwMode} = params;
+ if (!(await this.isExplicitStoredMedia({contentType: attachment.content_type, bucket, key, nsfwMode}))) {
return attachment;
}
+ return {
+ ...attachment,
+ nsfw: true,
+ flags: attachment.flags | MessageAttachmentFlags.CONTAINS_EXPLICIT_MEDIA,
+ };
+ }
+
+ async isExplicitStoredMedia(params: {
+ contentType: string | null;
+ bucket: string;
+ key: string;
+ nsfwMode: MediaProxyNsfwMode;
+ }): Promise<boolean> {
+ const {contentType, bucket, key, nsfwMode} = params;
+ if (!contentType || !isSupportedMediaContentType(contentType)) {
+ return false;
+ }
let metadata: MediaProxyMetadataResponse | null;
try {
metadata = await this.mediaService.getMetadata({type: 's3', bucket, key, nsfw: nsfwMode});
@@ -473,17 +500,10 @@ export class AttachmentProcessingService {
await this.storageService.deleteObject(bucket, key);
throw error;
}
- Logger.warn({error, context: METADATA_PROBE_DEGRADED_CONTEXT, key}, 'Favorite meme media probe failed');
- return attachment;
+ Logger.warn({error, context: METADATA_PROBE_DEGRADED_CONTEXT, key}, 'Stored media probe failed');
+ return false;
}
- if (!metadata?.nsfw) {
- return attachment;
- }
- return {
- ...attachment,
- nsfw: true,
- flags: attachment.flags | MessageAttachmentFlags.CONTAINS_EXPLICIT_MEDIA,
- };
+ return metadata?.nsfw === true;
}
private async getAttachmentMediaMetadata(params: {
diff --git a/fluxer_api/src/api/channel/services/message/CrosspostDeliveryService.ts b/fluxer_api/src/api/channel/services/message/CrosspostDeliveryService.ts
index 724e0a66c..0d32ea7b4 100644
--- a/fluxer_api/src/api/channel/services/message/CrosspostDeliveryService.ts
+++ b/fluxer_api/src/api/channel/services/message/CrosspostDeliveryService.ts
@@ -18,7 +18,11 @@ import {
dispatchMessageCreateBroadcast,
dispatchMessageUpdateBroadcast,
} from '@app/api/channel/services/message/MessageGatewayDispatch';
-import {isOperationDisabled, purgeMessageAttachments} from '@app/api/channel/services/message/MessageHelpers';
+import {
+ attachmentStorageChannelId,
+ isOperationDisabled,
+ purgeMessageAttachments,
+} from '@app/api/channel/services/message/MessageHelpers';
import type {MessagePersistenceService} from '@app/api/channel/services/message/MessagePersistenceService';
import type {MessageSearchService} from '@app/api/channel/services/message/MessageSearchService';
import {MessageWriteLock} from '@app/api/channel/services/message/MessageWriteLock';
@@ -85,6 +89,7 @@ export interface CrosspostSourceContext {
fingerprint: string;
accessCache: Map<string, Promise<boolean>>;
authorAvatarCache: Map<string, Promise<string | null>>;
+ classifiedAttachments: Promise<Array<MessageAttachment>> | null;
}
export type CrosspostSourceLoadResult =
@@ -236,6 +241,7 @@ export class CrosspostDeliveryService {
fingerprint: crosspostSourceFingerprint(message),
accessCache: new Map(),
authorAvatarCache: new Map(),
+ classifiedAttachments: null,
};
}
@@ -329,7 +335,7 @@ export class CrosspostDeliveryService {
}
}
const authorAvatar = await this.resolveCopyAuthorAvatar(context, webhook);
- const built = this.buildCopyPayload(context, target);
+ const built = await this.buildCopyPayload(context, target);
if (built.kind === 'blocked') {
Logger.warn(
{sourceMessageId: context.message.id.toString(), webhookId: webhook.id.toString()},
@@ -512,7 +518,7 @@ export class CrosspostDeliveryService {
await this.markCopySourceDeleted(row);
return;
}
- const built = this.buildCopyPayload(source, target);
+ const built = await this.buildCopyPayload(source, target);
if (built.kind === 'blocked') {
Logger.warn(
{sourceMessageId: row.source_message_id.toString(), webhookId: row.webhook_id.toString()},
@@ -740,20 +746,27 @@ export class CrosspostDeliveryService {
await deleteMessageSearchDocuments([messageId], {context: {source: 'crosspost_family_purge'}});
}
- private buildCopyPayload(source: CrosspostSourceContext, target: CrosspostTarget): CrosspostCopyPayloadResult {
+ private async buildCopyPayload(
+ source: CrosspostSourceContext,
+ target: CrosspostTarget,
+ ): Promise<CrosspostCopyPayloadResult> {
const message = source.message;
if (this.isBlocked(source)) {
return {kind: 'blocked'};
}
- const nsfwAllowed = this.targetAllowsNsfw(target);
+ const targetNsfw = this.targetIsNsfw(target);
+ const nsfwAllowed = targetNsfw || this.targetAcceptsFlaggedMedia(target);
+ const sourceAttachments = targetNsfw
+ ? message.attachments.map((attachment) => attachment.toMessageAttachment())
+ : await this.classifiedAttachments(source);
const excludedAttachmentIds = new Set<string>();
const attachments: Array<MessageAttachment> = [];
- for (const attachment of message.attachments) {
+ for (const attachment of sourceAttachments) {
if (!nsfwAllowed && attachment.nsfw) {
- excludedAttachmentIds.add(attachment.id.toString());
+ excludedAttachmentIds.add(attachment.attachment_id.toString());
continue;
}
- attachments.push(attachment.toMessageAttachment());
+ attachments.push(attachment);
}
let embeds = message.embeds.map((embed) => cloneEmbed(embed.toMessageEmbed()));
if (!nsfwAllowed) {
@@ -798,16 +811,29 @@ export class CrosspostDeliveryService {
return false;
}
- private targetAllowsNsfw(target: CrosspostTarget): boolean {
- if (
- computeEffectiveChannelNsfw(
- channelToContentWarningView(target.channel),
- target.parent ? channelToContentWarningView(target.parent) : null,
- guildResponseToContentWarningView(target.guild),
- )
- ) {
- return true;
+ private classifiedAttachments(source: CrosspostSourceContext): Promise<Array<MessageAttachment>> {
+ if (!source.classifiedAttachments) {
+ const pending = this.deps.persistenceService.classifyStoredAttachments(
+ source.message.attachments.map((attachment) => attachment.toMessageAttachment()),
+ attachmentStorageChannelId(source.message),
+ );
+ source.classifiedAttachments = pending;
+ pending.catch(() => {
+ if (source.classifiedAttachments === pending) source.classifiedAttachments = null;
+ });
}
+ return source.classifiedAttachments;
+ }
+
+ private targetIsNsfw(target: CrosspostTarget): boolean {
+ return computeEffectiveChannelNsfw(
+ channelToContentWarningView(target.channel),
+ target.parent ? channelToContentWarningView(target.parent) : null,
+ guildResponseToContentWarningView(target.guild),
+ );
+ }
+
+ private targetAcceptsFlaggedMedia(target: CrosspostTarget): boolean {
return this.contentService.isNSFWContentAllowed({
channel: target.channel,
guild: target.guild,
diff --git a/fluxer_api/src/api/channel/services/message/MessageContentService.test.ts b/fluxer_api/src/api/channel/services/message/MessageContentService.test.ts
new file mode 100644
index 000000000..5ba8f28ff
--- /dev/null
+++ b/fluxer_api/src/api/channel/services/message/MessageContentService.test.ts
@@ -0,0 +1,53 @@
+// SPDX-License-Identifier: AGPL-3.0-or-later
+
+import {MessageContentService} from '@app/api/channel/services/message/MessageContentService';
+import type {GuildRepository} from '@app/api/guild/repositories/GuildRepository';
+import type {LimitConfigService} from '@app/api/limits/LimitConfigService';
+import type {Channel} from '@app/api/models/Channel';
+import type {UserRepository} from '@app/api/user/repositories/UserRepository';
+import {ChannelTypes} from '@fluxer/constants/src/ChannelConstants';
+import {GuildExplicitContentFilterTypes, GuildNSFWLevel} from '@fluxer/constants/src/GuildConstants';
+import type {GuildResponse} from '@fluxer/schema/src/domains/guild/GuildResponseSchemas';
+import {describe, expect, it} from 'vitest';
+
+const service = new MessageContentService({} as UserRepository, {} as GuildRepository, {} as LimitConfigService);
+
+const ageRestrictedGuild = {
+ features: [],
+ nsfw_level: GuildNSFWLevel.AGE_RESTRICTED,
+ explicit_content_filter: GuildExplicitContentFilterTypes.DISABLED,
+} as unknown as GuildResponse;
+
+function channel(type: number, nsfwOverride: boolean | null): Channel {
+ return {
+ type,
+ isNsfw: nsfwOverride ?? false,
+ nsfwOverride,
+ contentWarningLevel: 0,
+ contentWarningText: null,
+ } as unknown as Channel;
+}
+
+describe('attachmentNsfwMode in age-restricted guilds', () => {
+ it('skips classification in channels that inherit the guild setting', () => {
+ expect(
+ service.attachmentNsfwMode({channel: channel(ChannelTypes.GUILD_TEXT, null), guild: ageRestrictedGuild}),
+ ).toBe('allow');
+ });
+
+ it('classifies uploads in a channel marked safe for work', () => {
+ expect(
+ service.attachmentNsfwMode({channel: channel(ChannelTypes.GUILD_TEXT, false), guild: ageRestrictedGuild}),
+ ).toBe('flag');
+ });
+
+ it('classifies uploads in a channel under a category marked safe for work', () => {
+ expect(
+ service.attachmentNsfwMode({
+ channel: channel(ChannelTypes.GUILD_TEXT, null),
+ parentCategory: channel(ChannelTypes.GUILD_CATEGORY, false),
+ guild: ageRestrictedGuild,
+ }),
+ ).toBe('flag');
+ });
+});
diff --git a/fluxer_api/src/api/channel/services/message/MessageContentService.ts b/fluxer_api/src/api/channel/services/message/MessageContentService.ts
index aa7e5494c..0f4b64f17 100644
--- a/fluxer_api/src/api/channel/services/message/MessageContentService.ts
+++ b/fluxer_api/src/api/channel/services/message/MessageContentService.ts
@@ -1,6 +1,11 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
import type {GuildID, UserID, WebhookID} from '@app/api/BrandedTypes';
+import {
+ channelToContentWarningView,
+ computeEffectiveChannelNsfw,
+ guildResponseToContentWarningView,
+} from '@app/api/channel/utils/EffectiveContentWarning';
import type {GuildRepository} from '@app/api/guild/repositories/GuildRepository';
import type {MediaProxyNsfwMode} from '@app/api/infrastructure/IMediaService';
import type {LimitConfigService} from '@app/api/limits/LimitConfigService';
@@ -21,12 +26,17 @@ export interface DmNsfwContext {
interface NsfwScope {
channel?: Channel;
+ parentCategory?: Channel | null;
guild?: GuildResponse | null;
member?: GuildMemberResponse | null;
isBot?: boolean;
dmNsfwContext?: DmNsfwContext;
}
+function isGuildMessageChannel(channel: Channel): boolean {
+ return GUILD_TEXT_BASED_CHANNEL_TYPES.has(channel.type) || THREAD_ONLY_CHANNEL_TYPES.has(channel.type);
+}
+
export class MessageContentService {
constructor(
private userRepository: UserRepository,
@@ -106,15 +116,26 @@ export class MessageContentService {
if (!this.isNSFWContentAllowed(params)) {
return 'block';
}
- return this.isMatureContext(params) ? 'allow' : 'flag';
+ return this.skipsMediaScan(params) ? 'allow' : 'flag';
+ }
+
+ skipsMediaScan(params: NsfwScope): boolean {
+ if (!this.isMatureContext(params)) {
+ return false;
+ }
+ const {channel, parentCategory, guild} = params;
+ if (!channel || !guild || !isGuildMessageChannel(channel)) {
+ return true;
+ }
+ return computeEffectiveChannelNsfw(
+ channelToContentWarningView(channel),
+ parentCategory ? channelToContentWarningView(parentCategory) : null,
+ guildResponseToContentWarningView(guild),
+ );
}
- private isMatureContext({channel, guild}: NsfwScope): boolean {
- if (
- channel &&
- (GUILD_TEXT_BASED_CHANNEL_TYPES.has(channel.type) || THREAD_ONLY_CHANNEL_TYPES.has(channel.type)) &&
- channel.isNsfw
- ) {
+ isMatureContext({channel, guild}: NsfwScope): boolean {
+ if (channel && isGuildMessageChannel(channel) && channel.isNsfw) {
return true;
}
if (channel?.type === ChannelTypes.DM_PERSONAL_NOTES) {
diff --git a/fluxer_api/src/api/channel/services/message/MessageHelpers.ts b/fluxer_api/src/api/channel/services/message/MessageHelpers.ts
index 6fd022a00..b7759347e 100644
--- a/fluxer_api/src/api/channel/services/message/MessageHelpers.ts
+++ b/fluxer_api/src/api/channel/services/message/MessageHelpers.ts
@@ -332,7 +332,7 @@ export async function createMessageSnapshotsForForward(
export const EMBED_MEDIA_OWNED_ATTACHMENT_FLAG = 1 << 30;
-function isOwnedEmbedAttachment(media: Pick<MessageEmbedMedia, 'flags'>): boolean {
+export function isOwnedEmbedAttachment(media: Pick<MessageEmbedMedia, 'flags'>): boolean {
return (media.flags & EMBED_MEDIA_OWNED_ATTACHMENT_FLAG) !== 0;
}
@@ -389,6 +389,18 @@ async function cloneOwnedEmbedAttachments(
}
}
+export function ownedEmbedAttachmentKeys(embeds: ReadonlyArray<MessageEmbed>, channelId: ChannelID): Set<string> {
+ const mediaPrefix = `${Config.endpoints.media}/`;
+ const ownPrefix = `${mediaPrefix}attachments/${channelId}/`;
+ const keys = new Set<string>();
+ forEachEmbedMedia(embeds, (media) => {
+ if (media.url?.startsWith(ownPrefix) && isOwnedEmbedAttachment(media)) {
+ keys.add(media.url.slice(mediaPrefix.length));
+ }
+ });
+ return keys;
+}
+
export function collectOwnedEmbedAttachments(message: Message): Array<{key: string; media: MessageEmbedMedia}> {
const mediaPrefix = `${Config.endpoints.media}/`;
const ownPrefix = `${mediaPrefix}attachments/${attachmentStorageChannelId(message)}/`;
diff --git a/fluxer_api/src/api/channel/services/message/MessagePersistenceService.ts b/fluxer_api/src/api/channel/services/message/MessagePersistenceService.ts
index 381c196ec..1899bced8 100644
--- a/fluxer_api/src/api/channel/services/message/MessagePersistenceService.ts
+++ b/fluxer_api/src/api/channel/services/message/MessagePersistenceService.ts
@@ -10,13 +10,17 @@ import type {ChannelRepository} from '@app/api/channel/ChannelRepository';
import type {MessageUpdateRequest} from '@app/api/channel/MessageTypes';
import type {AttachmentUploadTraceRepository} from '@app/api/channel/repositories/message/AttachmentUploadTraceRepository';
import {AttachmentProcessingService} from '@app/api/channel/services/message/AttachmentProcessingService';
+import {forEachEmbedMedia} from '@app/api/channel/services/message/CrosspostEmbedObjects';
import {type DmNsfwContext, MessageContentService} from '@app/api/channel/services/message/MessageContentService';
import {MessageEmbedAttachmentResolver} from '@app/api/channel/services/message/MessageEmbedAttachmentResolver';
import {
assertAttachmentFileSizesWithinLimit,
collectMessageAttachments,
isCrosspostCopy,
+ isOwnedEmbedAttachment,
keepOwnedEmbedAttachments,
+ makeAttachmentCdnKey,
+ ownedEmbedAttachmentKeys,
} from '@app/api/channel/services/message/MessageHelpers';
import {MessageStickerService} from '@app/api/channel/services/message/MessageStickerService';
import {resolveNsfwScopeChannel} from '@app/api/channel/utils/ThreadNsfwScope';
@@ -24,6 +28,8 @@ import {getContentMessage} from '@app/api/content_i18n/ContentI18n';
import type {
MessageAttachment,
MessageEmbed,
+ MessageEmbedChild,
+ MessageEmbedMedia,
MessageReference,
MessageStickerItem,
} from '@app/api/database/types/MessageTypes';
@@ -36,11 +42,19 @@ import {resolveAttachmentMaxFileSize} from '@app/api/limits/AttachmentFileSizeLi
import type {LimitConfigService} from '@app/api/limits/LimitConfigService';
import type {Channel} from '@app/api/models/Channel';
import type {Message} from '@app/api/models/Message';
-import type {MessageSnapshot} from '@app/api/models/MessageSnapshot';
+import {MessageSnapshot} from '@app/api/models/MessageSnapshot';
import type {User} from '@app/api/models/User';
import type {ReadStateService} from '@app/api/read_state/ReadStateService';
import type {UserRepository} from '@app/api/user/repositories/UserRepository';
-import {MessageFlags, Permissions, SENDABLE_MESSAGE_FLAGS} from '@fluxer/constants/src/ChannelConstants';
+import {
+ ChannelTypes,
+ EmbedMediaFlags,
+ MessageEmbedTypes,
+ MessageFlags,
+ Permissions,
+ SENDABLE_MESSAGE_FLAGS,
+} from '@fluxer/constants/src/ChannelConstants';
+import {GuildNSFWLevel} from '@fluxer/constants/src/GuildConstants';
import {UserFlags} from '@fluxer/constants/src/UserConstants';
import {ValidationErrorCodes} from '@fluxer/constants/src/ValidationErrorCodes';
import {InputValidationError} from '@fluxer/errors/src/domains/core/InputValidationError';
@@ -53,6 +67,23 @@ import {snowflakeToDate} from '@fluxer/snowflake/src/Snowflake';
import * as BucketUtils from '@fluxer/snowflake/src/SnowflakeBuckets';
import type {IVirusScanService} from '@pkgs/virus_scan/src/IVirusScanService';
+const FORWARD_MEDIA_EMBED_TYPES: ReadonlySet<string> = new Set([
+ MessageEmbedTypes.IMAGE,
+ MessageEmbedTypes.VIDEO,
+ MessageEmbedTypes.GIFV,
+]);
+
+function childWithoutVisualMedia(child: MessageEmbedChild): MessageEmbedChild {
+ return {...child, thumbnail: null, image: null, video: null, html: null, html_width: null, html_height: null};
+}
+
+function withoutVisualMedia(embed: MessageEmbed): MessageEmbed {
+ return {
+ ...childWithoutVisualMedia(embed),
+ children: embed.children ? embed.children.map(childWithoutVisualMedia) : embed.children,
+ };
+}
+
function mapAttachmentForEmbedResolution(att: MessageAttachment) {
return {
attachment_id: att.attachment_id,
@@ -153,8 +184,141 @@ export class MessagePersistenceService {
this.attachmentDecayService = new AttachmentDecayService();
}
- private nsfwScopeChannel(channel: Channel): Promise<Channel> {
- return resolveNsfwScopeChannel(channel, (channelId) => this.channelRepository.channelData.findUnique(channelId));
+ private findChannel(channelId: ChannelID): Promise<Channel | null> {
+ return this.channelRepository.channelData.findUnique(channelId);
+ }
+
+ private parentCategory(channel: Channel): Promise<Channel | null> {
+ if (!channel.guildId || !channel.parentId || channel.type === ChannelTypes.GUILD_CATEGORY) {
+ return Promise.resolve(null);
+ }
+ return this.findChannel(channel.parentId);
+ }
+
+ private async nsfwScope(
+ channel: Channel,
+ guild: GuildResponse | null | undefined,
+ hasMedia: boolean,
+ ): Promise<{channel: Channel; parentCategory: Channel | null}> {
+ const scopeChannel = await resolveNsfwScopeChannel(channel, (channelId) => this.findChannel(channelId));
+ const needsCategory =
+ hasMedia && guild?.nsfw_level === GuildNSFWLevel.AGE_RESTRICTED && scopeChannel.nsfwOverride === null;
+ return {channel: scopeChannel, parentCategory: needsCategory ? await this.parentCategory(scopeChannel) : null};
+ }
+
+ async isNsfwThroughCategory(channel: Channel): Promise<boolean> {
+ if (channel.nsfwOverride !== null) {
+ return false;
+ }
+ return (await this.parentCategory(channel))?.nsfwOverride === true;
+ }
+
+ async screenForwardSnapshots(params: {
+ snapshots: Array<MessageSnapshot>;
+ source: {channel: Channel; guild: GuildResponse | null};
+ destination: {channel: Channel; guild: GuildResponse | null};
+ channelId: ChannelID;
+ }): Promise<Array<MessageSnapshot>> {
+ const {snapshots, source, destination, channelId} = params;
+ const hasUnflaggedAttachments = snapshots.some((snapshot) =>
+ snapshot.attachments.some((attachment) => !attachment.nsfw),
+ );
+ const hasEmbeds = snapshots.some((snapshot) => snapshot.embeds.length > 0);
+ if (!hasUnflaggedAttachments && !hasEmbeds) {
+ return snapshots;
+ }
+ const destinationScope = await this.nsfwScope(destination.channel, destination.guild, true);
+ if (this.contentService.skipsMediaScan({...destinationScope, guild: destination.guild})) {
+ return snapshots;
+ }
+ const stripEmbeds =
+ hasEmbeds &&
+ this.contentService.isMatureContext({
+ channel: await resolveNsfwScopeChannel(source.channel, (id) => this.findChannel(id)),
+ guild: source.guild,
+ });
+ const screened = await Promise.all(
+ snapshots.map(async (snapshot) => {
+ const data = snapshot.toMessageSnapshot();
+ const [attachments, embeds] = await Promise.all([
+ this.classifyStoredAttachments(data.attachments ?? [], channelId),
+ stripEmbeds
+ ? (data.embeds ?? [])
+ .filter((embed) => !FORWARD_MEDIA_EMBED_TYPES.has(embed.type ?? ''))
+ .map(withoutVisualMedia)
+ : this.flagExplicitOwnedEmbedMedia(data.embeds ?? [], channelId),
+ ]);
+ return new MessageSnapshot({
+ ...data,
+ attachments: attachments.length > 0 ? attachments : null,
+ embeds: embeds.length > 0 ? embeds : null,
+ });
+ }),
+ );
+ if (stripEmbeds) {
+ await this.deleteStrippedEmbedObjects(snapshots, screened, channelId);
+ }
+ return screened;
+ }
+
+ private async flagExplicitOwnedEmbedMedia(
+ embeds: Array<MessageEmbed>,
+ channelId: ChannelID,
+ ): Promise<Array<MessageEmbed>> {
+ const mediaPrefix = `${Config.endpoints.media}/`;
+ const ownPrefix = `${mediaPrefix}attachments/${channelId}/`;
+ const owned: Array<{media: MessageEmbedMedia; owner: MessageEmbedChild; key: string}> = [];
+ forEachEmbedMedia(embeds, (media, owner, field) => {
+ if (field === 'audio' || owner.nsfw || !isOwnedEmbedAttachment(media) || !media.url?.startsWith(ownPrefix)) {
+ return;
+ }
+ owned.push({media, owner, key: media.url.slice(mediaPrefix.length)});
+ });
+ await Promise.all(
+ owned.map(async ({media, owner, key}) => {
+ const explicit = await this.attachmentService.isExplicitStoredMedia({
+ contentType: media.content_type,
+ bucket: Config.s3.buckets.cdn,
+ key,
+ nsfwMode: 'flag',
+ });
+ if (explicit) {
+ media.flags |= EmbedMediaFlags.CONTAINS_EXPLICIT_MEDIA;
+ owner.nsfw = true;
+ }
+ }),
+ );
+ return embeds;
+ }
+
+ classifyStoredAttachments(
+ attachments: Array<MessageAttachment>,
+ storageChannelId: ChannelID,
+ ): Promise<Array<MessageAttachment>> {
+ return Promise.all(
+ attachments.map((attachment) =>
+ attachment.nsfw
+ ? attachment
+ : this.attachmentService.classifyStoredMedia({
+ attachment,
+ bucket: Config.s3.buckets.cdn,
+ key: makeAttachmentCdnKey(storageChannelId, attachment.attachment_id, attachment.filename),
+ nsfwMode: 'flag',
+ }),
+ ),
+ );
+ }
+
+ private async deleteStrippedEmbedObjects(
+ before: Array<MessageSnapshot>,
+ after: Array<MessageSnapshot>,
+ channelId: ChannelID,
+ ): Promise<void> {
+ const embedsOf = (snapshots: Array<MessageSnapshot>) =>
+ snapshots.flatMap((snapshot) => snapshot.embeds.map((embed) => embed.toMessageEmbed()));
+ const kept = ownedEmbedAttachmentKeys(embedsOf(after), channelId);
+ const stripped = [...ownedEmbedAttachmentKeys(embedsOf(before), channelId)].filter((key) => !kept.has(key));
+ await Promise.all(stripped.map((key) => this.storageService.deleteObject(Config.s3.buckets.cdn, key)));
}
resolveDmNsfwContext(channel: Channel, senderId: UserID): Promise<DmNsfwContext | undefined> {
@@ -179,7 +343,13 @@ export class MessagePersistenceService {
const isBot = params.user?.isBot ?? false;
const isBugHunterBot = isBot && ((params.user?.flags ?? 0n) & UserFlags.BUG_HUNTER) !== 0n;
const attachmentNsfwMode = this.contentService.attachmentNsfwMode({
- channel: params.channel ? await this.nsfwScopeChannel(params.channel) : undefined,
+ ...(params.channel
+ ? await this.nsfwScope(
+ params.channel,
+ params.guild,
+ (params.attachments?.length ?? 0) > 0 || params.favoriteMemeAttachment != null,
+ )
+ : {}),
guild: params.guild,
member: params.member,
isBot,
@@ -422,7 +592,11 @@ export class MessagePersistenceService {
throw InputValidationError.fromCode('message', ValidationErrorCodes.MESSAGES_WITH_SNAPSHOTS_CANNOT_BE_EDITED);
}
const attachmentNsfwMode = this.contentService.attachmentNsfwMode({
- channel: await this.nsfwScopeChannel(channel),
+ ...(await this.nsfwScope(
+ channel,
+ guild,
+ (data.attachments ?? []).some((attachment) => 'upload_filename' in attachment && !!attachment.upload_filename),
+ )),
guild,
member,
isBot: params.isBot,
diff --git a/fluxer_api/src/api/channel/services/message/MessageSendService.ts b/fluxer_api/src/api/channel/services/message/MessageSendService.ts
index d23b268e5..ad582239d 100644
--- a/fluxer_api/src/api/channel/services/message/MessageSendService.ts
+++ b/fluxer_api/src/api/channel/services/message/MessageSendService.ts
@@ -439,6 +439,7 @@ export class MessageSendService {
referencedMessage: Message | null;
referencedChannelGuildId?: GuildID | null;
messageSnapshots?: Array<MessageSnapshot>;
+ forwardSource?: AuthenticatedChannel;
}> {
let referenceChannelId = channelId;
let forwardReferenceAuthChannel: AuthenticatedChannel | null = null;
@@ -479,7 +480,12 @@ export class MessageSendService {
this.getForwardMediaSelection(data),
);
}
- return {referencedMessage, referencedChannelGuildId, messageSnapshots};
+ return {
+ referencedMessage,
+ referencedChannelGuildId,
+ messageSnapshots,
+ forwardSource: forwardReferenceAuthChannel ?? undefined,
+ };
}
private getForwardMediaSelection(data: MessageRequest): ForwardMediaSelection | undefined {
@@ -706,11 +712,11 @@ export class MessageSendService {
forumStarter ? Promise.resolve(channelIdToMessageId(channel.id)) : this.generateMessageId(channel),
);
const referenceContext = await referenceContextTask;
- const {referencedMessage, referencedChannelGuildId, messageSnapshots} = referenceContext;
+ const {referencedMessage, referencedChannelGuildId, forwardSource} = referenceContext;
this.assertForwardableReference(isForwardMessage, referencedMessage);
- if (isForwardMessage && messageSnapshots && guild) {
- const snapshotHasEmbeds = messageSnapshots.some((s) => s.embeds.length > 0);
- const snapshotHasAttachments = messageSnapshots.some((s) => s.attachments.length > 0);
+ if (isForwardMessage && referenceContext.messageSnapshots && guild) {
+ const snapshotHasEmbeds = referenceContext.messageSnapshots.some((s) => s.embeds.length > 0);
+ const snapshotHasAttachments = referenceContext.messageSnapshots.some((s) => s.attachments.length > 0);
if (snapshotHasEmbeds && !canEmbedLinks) {
throw new MissingPermissionsError();
}
@@ -718,9 +724,23 @@ export class MessageSendService {
throw new MissingPermissionsError();
}
}
+ const messageId = await messageIdTask;
+ const messageSnapshots =
+ isForwardMessage && referenceContext.messageSnapshots && forwardSource
+ ? await this.deps.persistenceService.screenForwardSnapshots({
+ snapshots: referenceContext.messageSnapshots,
+ source: forwardSource,
+ destination: {channel, guild},
+ channelId,
+ })
+ : referenceContext.messageSnapshots;
if (isForwardMessage && messageSnapshots && this.snapshotsContainNsfwContent(messageSnapshots)) {
const guildNsfw = guild != null && guild.nsfw_level === GuildNSFWLevel.AGE_RESTRICTED;
- const destAllowsNsfw = (authChannel.thread?.parent ?? channel).isNsfw || guildNsfw;
+ const scopeChannel = authChannel.thread?.parent ?? channel;
+ const destAllowsNsfw =
+ guildNsfw ||
+ scopeChannel.isNsfw ||
+ (guild != null && (await this.deps.persistenceService.isNsfwThroughCategory(scopeChannel)));
if (!destAllowsNsfw) {
throw new NsfwContentRequiresAgeVerificationError();
}
@@ -752,7 +772,6 @@ export class MessageSendService {
data,
});
const dmNsfwContext = await dmNsfwContextTask;
- const messageId = await messageIdTask;
let mentionData: SendMentionData | undefined;
const shouldExtractMentions =
channel && !isForwardMessage && (data.content !== undefined || data.message_reference != null);