Skip to content

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

TypeScript fluxerfluxerapp

fix(api): flag sensitive uploads so viewer filters can blur them

Hampusbb7df39a8612
diff --git a/fluxer_api/src/api/channel/services/message/MessageContentService.ts b/fluxer_api/src/api/channel/services/message/MessageContentService.ts
index 13d469c25..aa7e5494c 100644
--- a/fluxer_api/src/api/channel/services/message/MessageContentService.ts
+++ b/fluxer_api/src/api/channel/services/message/MessageContentService.ts
@@ -2,6 +2,7 @@
import type {GuildID, UserID, WebhookID} from '@app/api/BrandedTypes';
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';
import type {Channel} from '@app/api/models/Channel';
import type {UserRepository} from '@app/api/user/repositories/UserRepository';
@@ -18,6 +19,14 @@ export interface DmNsfwContext {
recipientFilterLevel: number;
}
+interface NsfwScope {
+ channel?: Channel;
+ guild?: GuildResponse | null;
+ member?: GuildMemberResponse | null;
+ isBot?: boolean;
+ dmNsfwContext?: DmNsfwContext;
+}
+
export class MessageContentService {
constructor(
private userRepository: UserRepository,
@@ -64,28 +73,9 @@ export class MessageContentService {
return {senderFilterLevel, recipientFilterLevel};
}
- isNSFWContentAllowed(params: {
- channel?: Channel;
- guild?: GuildResponse | null;
- member?: GuildMemberResponse | null;
- isBot?: boolean;
- dmNsfwContext?: DmNsfwContext;
- }): boolean {
- const {channel, guild, member, isBot, dmNsfwContext} = params;
- if (isBot) {
- return true;
- }
- if (
- channel &&
- (GUILD_TEXT_BASED_CHANNEL_TYPES.has(channel.type) || THREAD_ONLY_CHANNEL_TYPES.has(channel.type)) &&
- channel.isNsfw
- ) {
- return true;
- }
- if (channel?.type === ChannelTypes.DM_PERSONAL_NOTES) {
- return true;
- }
- if (channel?.type === ChannelTypes.GROUP_DM && channel.isNsfw) {
+ isNSFWContentAllowed(params: NsfwScope): boolean {
+ const {guild, member, isBot, dmNsfwContext} = params;
+ if (isBot || this.isMatureContext(params)) {
return true;
}
if (!guild) {
@@ -97,10 +87,6 @@ export class MessageContentService {
}
return false;
}
- const guildMarkedNsfw = guild.nsfw_level === GuildNSFWLevel.AGE_RESTRICTED;
- if (guildMarkedNsfw) {
- return true;
- }
const features = new Set(guild.features ?? []);
if (features.has(GuildFeatures.DISCOVERABLE)) {
return false;
@@ -115,4 +101,28 @@ export class MessageContentService {
}
return false;
}
+
+ attachmentNsfwMode(params: NsfwScope): MediaProxyNsfwMode {
+ if (!this.isNSFWContentAllowed(params)) {
+ return 'block';
+ }
+ return this.isMatureContext(params) ? 'allow' : 'flag';
+ }
+
+ 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
+ ) {
+ return true;
+ }
+ if (channel?.type === ChannelTypes.DM_PERSONAL_NOTES) {
+ return true;
+ }
+ if (channel?.type === ChannelTypes.GROUP_DM && channel.isNsfw) {
+ return true;
+ }
+ return guild?.nsfw_level === GuildNSFWLevel.AGE_RESTRICTED;
+ }
}
diff --git a/fluxer_api/src/api/channel/services/message/MessagePersistenceService.ts b/fluxer_api/src/api/channel/services/message/MessagePersistenceService.ts
index 592a93f82..d901d32d6 100644
--- a/fluxer_api/src/api/channel/services/message/MessagePersistenceService.ts
+++ b/fluxer_api/src/api/channel/services/message/MessagePersistenceService.ts
@@ -179,16 +179,17 @@ export class MessagePersistenceService {
} as const);
const isBot = params.user?.isBot ?? false;
const isBugHunterBot = isBot && ((params.user?.flags ?? 0n) & UserFlags.BUG_HUNTER) !== 0n;
- const isNSFWAllowed = this.contentService.isNSFWContentAllowed({
+ const attachmentNsfwMode = this.contentService.attachmentNsfwMode({
channel: params.channel ? await this.nsfwScopeChannel(params.channel) : undefined,
guild: params.guild,
member: params.member,
isBot,
dmNsfwContext: params.dmNsfwContext,
});
+ const isNSFWAllowed = attachmentNsfwMode !== 'block';
const [sanitizedContent, attachmentResult, processedStickers] = await Promise.all([
this.sanitizeContentIfNeeded(params, authorId),
- this.processAttachments(params, isNSFWAllowed ? 'allow' : 'block'),
+ this.processAttachments(params, attachmentNsfwMode),
this.processStickers(params, authorId),
]);
let messageContent = sanitizedContent;
@@ -412,13 +413,14 @@ export class MessagePersistenceService {
if (message.messageSnapshots && message.messageSnapshots.length > 0) {
throw InputValidationError.fromCode('message', ValidationErrorCodes.MESSAGES_WITH_SNAPSHOTS_CANNOT_BE_EDITED);
}
- const isNSFWAllowed = this.contentService.isNSFWContentAllowed({
+ const attachmentNsfwMode = this.contentService.attachmentNsfwMode({
channel: await this.nsfwScopeChannel(channel),
guild,
member,
isBot: params.isBot,
dmNsfwContext: params.dmNsfwContext,
});
+ const isNSFWAllowed = attachmentNsfwMode !== 'block';
const updatedRowData = {...message.toRow()};
let hasChanges = false;
const allowEmbeds = params.allowEmbeds ?? true;
@@ -521,7 +523,7 @@ export class MessagePersistenceService {
channel,
guild,
member,
- nsfwMode: isNSFWAllowed ? 'allow' : 'block',
+ nsfwMode: attachmentNsfwMode,
});
processedNewAttachments = attachmentResult.attachments;
if (attachmentResult.hasVirusDetected) {