From 8435782575d16630343623f547700ecb8250bd88 Mon Sep 17 00:00:00 2001 From: Rudy Fraser Date: Wed, 16 Sep 2026 20:24:12 -0400 Subject: [PATCH] feat(moderation): blur post text for adult content labels The built-in porn, sexual, nudity, graphic-media and gore definitions only blurred media, so a text-only post carrying one of those labels rendered uncovered. They now blur the post content as well, from any source: the built-in definitions are patched at startup and labeler declared definitions get the same content behavior and built-in flags, which also keeps the values self-labelable when the author is a subscribed labeler. Account and profile behaviors are unchanged. --- package.json | 2 +- .../moderation/LabelDialog/blackskyLabels.ts | 8 +- .../__tests__/adult-content-labels.test.ts | 287 ++++++++++++++++++ src/lib/moderation/adult-content-labels.ts | 36 +++ src/state/queries/preferences/moderation.ts | 3 +- src/state/session/moderation.ts | 5 +- 6 files changed, 334 insertions(+), 7 deletions(-) create mode 100644 src/lib/moderation/__tests__/adult-content-labels.test.ts create mode 100644 src/lib/moderation/adult-content-labels.ts diff --git a/package.json b/package.json index 818138f9d..980241c6a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "blacksky.community", - "version": "1.128.0", + "version": "1.129.0", "private": true, "packageManager": "pnpm@11.7.0", "engines": { diff --git a/src/components/moderation/LabelDialog/blackskyLabels.ts b/src/components/moderation/LabelDialog/blackskyLabels.ts index 51525b3cd..b3f179b84 100644 --- a/src/components/moderation/LabelDialog/blackskyLabels.ts +++ b/src/components/moderation/LabelDialog/blackskyLabels.ts @@ -179,7 +179,7 @@ export const GLOBAL_LABEL_DEFS: ComAtprotoLabelDefs.LabelValueDefinition[] = [ { identifier: 'porn', severity: 'none', - blurs: 'media', + blurs: 'content', defaultSetting: 'hide', adultOnly: true, locales: [ @@ -193,7 +193,7 @@ export const GLOBAL_LABEL_DEFS: ComAtprotoLabelDefs.LabelValueDefinition[] = [ { identifier: 'sexual', severity: 'none', - blurs: 'media', + blurs: 'content', defaultSetting: 'warn', adultOnly: true, locales: [ @@ -207,7 +207,7 @@ export const GLOBAL_LABEL_DEFS: ComAtprotoLabelDefs.LabelValueDefinition[] = [ { identifier: 'nudity', severity: 'none', - blurs: 'media', + blurs: 'content', defaultSetting: 'ignore', adultOnly: false, locales: [ @@ -221,7 +221,7 @@ export const GLOBAL_LABEL_DEFS: ComAtprotoLabelDefs.LabelValueDefinition[] = [ { identifier: 'graphic-media', severity: 'none', - blurs: 'media', + blurs: 'content', defaultSetting: 'warn', adultOnly: true, locales: [ diff --git a/src/lib/moderation/__tests__/adult-content-labels.test.ts b/src/lib/moderation/__tests__/adult-content-labels.test.ts new file mode 100644 index 000000000..0c9ee4b8e --- /dev/null +++ b/src/lib/moderation/__tests__/adult-content-labels.test.ts @@ -0,0 +1,287 @@ +import { + type AppBskyFeedDefs, + type AppBskyLabelerDefs, + type ComAtprotoLabelDefs, + interpretLabelValueDefinitions, + type LabelPreference, + LABELS, + moderatePost, + type ModerationOpts, +} from '@atproto/api' + +import { + configureAdultContentLabelDefs, + withAdultContentBehavior, +} from '../adult-content-labels' + +const AUTHOR = 'did:plc:author' +const LABELER = 'did:plc:labeler' +const URI = `at://${AUTHOR}/app.bsky.feed.post/3ktextonly` +const CID = 'bafyreipost' +const AT = '2026-09-16T00:00:00.000Z' +const ADULT_VALS = [ + 'porn', + 'sexual', + 'nudity', + 'graphic-media', + 'gore', +] as const +const CONTEXTS = [ + 'contentList', + 'contentView', + 'contentMedia', + 'avatar', + 'profileList', + 'profileView', +] as const + +const label = (val: string, src: string): ComAtprotoLabelDefs.Label => ({ + src, + uri: URI, + cid: CID, + val, + cts: AT, +}) + +const textPost = ({ + postLabels = [], + authorLabels = [], +}: { + postLabels?: ComAtprotoLabelDefs.Label[] + authorLabels?: ComAtprotoLabelDefs.Label[] +}): AppBskyFeedDefs.PostView => ({ + $type: 'app.bsky.feed.defs#postView', + uri: URI, + cid: CID, + author: { + did: AUTHOR, + handle: 'author.test', + viewer: {}, + labels: authorLabels, + }, + record: {$type: 'app.bsky.feed.post', text: 'words only', createdAt: AT}, + labels: postLabels, + indexedAt: AT, +}) + +const labelerView = ( + did: string, + defs: Array>, +): AppBskyLabelerDefs.LabelerViewDetailed => ({ + $type: 'app.bsky.labeler.defs#labelerViewDetailed', + uri: `at://${did}/app.bsky.labeler.service/self`, + cid: 'bafyreilabeler', + creator: {did, handle: 'labeler.test'}, + policies: { + labelValues: defs.map(d => d.identifier!), + labelValueDefinitions: defs.map(d => ({ + severity: 'none', + blurs: 'media', + locales: [], + ...d, + })) as ComAtprotoLabelDefs.LabelValueDefinition[], + }, + indexedAt: AT, +}) + +const opts = ({ + labels, + adultContentEnabled = true, + labelDefs = {}, +}: { + labels: Record + adultContentEnabled?: boolean + labelDefs?: ModerationOpts['labelDefs'] +}): ModerationOpts => ({ + userDid: 'did:plc:viewer', + prefs: { + adultContentEnabled, + labels, + labelers: [{did: LABELER, labels}], + mutedWords: [], + hiddenPosts: [], + }, + labelDefs, +}) + +const uiSummary = ( + post: AppBskyFeedDefs.PostView, + moderationOpts: ModerationOpts, +) => { + const mod = moderatePost(post, moderationOpts) + return Object.fromEntries( + CONTEXTS.map(ctx => { + const ui = mod.ui(ctx) + return [ + ctx, + {blur: ui.blur, filter: ui.filter, noOverride: ui.noOverride}, + ] + }), + ) +} + +beforeAll(configureAdultContentLabelDefs) + +describe('configureAdultContentLabelDefs', () => { + it('covers the text of a warned self-labeled post, not only its media', () => { + const mod = moderatePost( + textPost({postLabels: [label('porn', AUTHOR)]}), + opts({labels: {porn: 'warn'}}), + ) + + expect(mod.ui('contentList').blur).toBe(true) + expect(mod.ui('contentView').blur).toBe(true) + expect(mod.ui('contentMedia').blur).toBe(true) + expect(mod.ui('contentList').filter).toBe(false) + }) + + it('filters a hidden self-label out of lists and covers it in the thread', () => { + const mod = moderatePost( + textPost({postLabels: [label('porn', AUTHOR)]}), + opts({labels: {porn: 'hide'}}), + ) + + expect(mod.ui('contentList').filter).toBe(true) + expect(mod.ui('contentView').blur).toBe(true) + }) + + it('leaves an ignored self-label alone', () => { + const mod = moderatePost( + textPost({postLabels: [label('nudity', AUTHOR)]}), + opts({labels: {nudity: 'ignore'}}), + ) + + expect(mod.ui('contentList').blur).toBe(false) + expect(mod.ui('contentView').blur).toBe(false) + expect(mod.ui('contentMedia').blur).toBe(false) + }) + + it('is idempotent and leaves account and profile behaviors untouched', () => { + configureAdultContentLabelDefs() + + for (const val of ADULT_VALS) { + expect(LABELS[val].blurs).toBe('content') + expect(LABELS[val].behaviors.account).toEqual({ + avatar: 'blur', + banner: 'blur', + }) + expect(LABELS[val].behaviors.profile).toEqual({ + avatar: 'blur', + banner: 'blur', + }) + expect(LABELS[val].behaviors.content).toEqual({ + contentMedia: 'blur', + contentList: 'blur', + contentView: 'blur', + }) + } + expect(LABELS.porn.flags).toEqual(['adult']) + expect(LABELS.nudity.flags).toEqual([]) + }) +}) + +describe('withAdultContentBehavior', () => { + const labelerDefs = (val: string, wrap: boolean) => { + const defs = interpretLabelValueDefinitions( + labelerView(LABELER, [{identifier: val}]), + ) + return {[LABELER]: wrap ? withAdultContentBehavior(defs) : defs} + } + + it.each(ADULT_VALS)( + 'covers post text for a labeler that declares %s itself', + val => { + const post = textPost({postLabels: [label(val, LABELER)]}) + const prefs = {[val]: 'warn' as const} + + const patched = moderatePost( + post, + opts({labels: prefs, labelDefs: labelerDefs(val, true)}), + ) + const unpatched = moderatePost( + post, + opts({labels: prefs, labelDefs: labelerDefs(val, false)}), + ) + + expect(unpatched.ui('contentList').blur).toBe(false) + expect(patched.ui('contentList').blur).toBe(true) + expect(patched.ui('contentView').blur).toBe(true) + expect(patched.ui('contentMedia').blur).toBe(true) + }, + ) + + it.each(ADULT_VALS)( + 'keeps %s self-labelable when the author is also a subscribed labeler', + val => { + const post = textPost({postLabels: [label(val, AUTHOR)]}) + const prefs = {[val]: 'warn' as const} + const authorDefs = (wrap: boolean) => { + const defs = interpretLabelValueDefinitions( + labelerView(AUTHOR, [{identifier: val}]), + ) + return {[AUTHOR]: wrap ? withAdultContentBehavior(defs) : defs} + } + + const patched = moderatePost( + post, + opts({labels: prefs, labelDefs: authorDefs(true)}), + ) + const unpatched = moderatePost( + post, + opts({labels: prefs, labelDefs: authorDefs(false)}), + ) + + expect(unpatched.ui('contentList').blur).toBe(false) + expect(patched.ui('contentList').blur).toBe(true) + expect(patched.ui('contentView').blur).toBe(true) + }, + ) + + it('leaves a labeler custom value alone', () => { + const [def] = withAdultContentBehavior( + interpretLabelValueDefinitions( + labelerView(LABELER, [{identifier: 'misogynoir', blurs: 'content'}]), + ), + ) + + expect(def.flags).toContain('no-self') + expect(def.blurs).toBe('content') + expect(def.behaviors.content?.contentMedia).toBeUndefined() + }) + + it.each([ + ['adult content disabled, pref ignore', false, 'ignore' as const], + ['adult content enabled, pref warn', true, 'warn' as const], + ])( + 'matches the built-in account-level decision (%s)', + (_name, adultContentEnabled, pref) => { + const post = textPost({authorLabels: [label('porn', LABELER)]}) + const prefs = {porn: pref} + + const viaLabeler = uiSummary( + post, + opts({ + labels: prefs, + adultContentEnabled, + labelDefs: { + [LABELER]: withAdultContentBehavior( + interpretLabelValueDefinitions( + labelerView(LABELER, [{identifier: 'porn', adultOnly: false}]), + ), + ), + }, + }), + ) + const builtIn = uiSummary( + post, + opts({labels: prefs, adultContentEnabled, labelDefs: {}}), + ) + + expect(viaLabeler).toEqual(builtIn) + expect(viaLabeler.contentList.filter).toBe(!adultContentEnabled) + expect(viaLabeler.contentList.blur).toBe(false) + expect(viaLabeler.avatar.blur).toBe(true) + expect(viaLabeler.profileView.blur).toBe(false) + }, + ) +}) diff --git a/src/lib/moderation/adult-content-labels.ts b/src/lib/moderation/adult-content-labels.ts new file mode 100644 index 000000000..e102bdcf5 --- /dev/null +++ b/src/lib/moderation/adult-content-labels.ts @@ -0,0 +1,36 @@ +import {type InterpretedLabelValueDefinition, LABELS} from '@atproto/api' + +import {SELF_LABELS} from '../moderation' + +const ADULT_LABEL_VALS = [...SELF_LABELS, 'gore'] as const + +function adultLabelVal(identifier: string) { + return ADULT_LABEL_VALS.find(val => val === identifier) +} + +export function applyAdultContentBehavior( + def: InterpretedLabelValueDefinition, +): InterpretedLabelValueDefinition { + const val = adultLabelVal(def.identifier) + if (!val) return def + def.blurs = 'content' + def.flags = [...LABELS[val].flags] + def.behaviors.content = { + ...def.behaviors.content, + contentList: 'blur', + contentView: 'blur', + } + return def +} + +export function configureAdultContentLabelDefs() { + for (const val of ADULT_LABEL_VALS) { + applyAdultContentBehavior(LABELS[val]) + } +} + +export function withAdultContentBehavior( + defs: InterpretedLabelValueDefinition[], +): InterpretedLabelValueDefinition[] { + return defs.map(applyAdultContentBehavior) +} diff --git a/src/state/queries/preferences/moderation.ts b/src/state/queries/preferences/moderation.ts index 55c155dc5..f797d0fb2 100644 --- a/src/state/queries/preferences/moderation.ts +++ b/src/state/queries/preferences/moderation.ts @@ -1,6 +1,7 @@ import {useMemo} from 'react' import {AtpAgent, interpretLabelValueDefinitions} from '@atproto/api' +import {withAdultContentBehavior} from '#/lib/moderation/adult-content-labels' import {isNonConfigurableModerationAuthority} from '#/state/session/additional-moderation-authorities' import {useLabelersDetailedInfoQuery} from '../labeler' import {usePreferencesQuery} from './index' @@ -41,7 +42,7 @@ export function useLabelDefinitionsQuery() { labelDefs: Object.fromEntries( (labelers.data || []).map(labeler => [ labeler.creator.did, - interpretLabelValueDefinitions(labeler), + withAdultContentBehavior(interpretLabelValueDefinitions(labeler)), ]), ), labelers: labelers.data || [], diff --git a/src/state/session/moderation.ts b/src/state/session/moderation.ts index a3f61a924..48a8d013a 100644 --- a/src/state/session/moderation.ts +++ b/src/state/session/moderation.ts @@ -1,6 +1,7 @@ -import {Agent, AtpAgent, BSKY_LABELER_DID} from '@atproto/api' +import {type Agent, AtpAgent, BSKY_LABELER_DID} from '@atproto/api' import {IS_TEST_USER} from '#/lib/constants' +import {configureAdultContentLabelDefs} from '#/lib/moderation/adult-content-labels' import {getNoAppLabelers} from '../preferences/no-app-labelers' import { BLACKSKY_LABELER, @@ -14,6 +15,7 @@ export function configureModerationForGuest() { // Don't add any other global behavior here! switchToBskyAppLabeler() configureAdditionalModerationAuthorities() + configureAdultContentLabelDefs() } export async function configureModerationForAccount( @@ -41,6 +43,7 @@ export async function configureModerationForAccount( } configureAdditionalModerationAuthorities() + configureAdultContentLabelDefs() } function switchToBskyAppLabeler() { -- knotmirror 0.0.1