Skip to content

Commit

Permalink
✨ Add subject tags with report type (#3260)
Browse files Browse the repository at this point in the history
* ✨ Add subject tags with report type

* ✅ Update snapshots

* ✅ Update snpshot for pds admin

* ✅ Update ozone snapshot

* ✅ Update ozone snapshot

* 🧹 Cleanup
  • Loading branch information
foysalit authored Dec 18, 2024
1 parent 6a3e781 commit c559e7e
Show file tree
Hide file tree
Showing 14 changed files with 118 additions and 22 deletions.
7 changes: 6 additions & 1 deletion packages/ozone/src/api/moderation/emitEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
isModEventEmail,
isModEventLabel,
isModEventMuteReporter,
isModEventReport,
isModEventReverseTakedown,
isModEventTag,
isModEventTakedown,
Expand All @@ -21,6 +22,7 @@ import { ModeratorOutput, AdminTokenOutput } from '../../auth-verifier'
import { SettingService } from '../../setting/service'
import { ProtectedTagSettingKey } from '../../setting/constants'
import { ProtectedTagSetting } from '../../setting/types'
import { getTagForReport } from '../../tag-service/util'

const handleModerationEvent = async ({
ctx,
Expand Down Expand Up @@ -160,7 +162,10 @@ const handleModerationEvent = async ({
ctx.cfg.service.did,
moderationTxn,
)
await tagService.evaluateForSubject()
const initialTags = isModEventReport(event)
? [getTagForReport(event.reportType)]
: undefined
await tagService.evaluateForSubject(initialTags)

if (subject.isRepo()) {
if (isTakedownEvent) {
Expand Down
3 changes: 2 additions & 1 deletion packages/ozone/src/api/report/createReport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { subjectFromInput } from '../../mod-service/subject'
import { REASONAPPEAL } from '../../lexicon/types/com/atproto/moderation/defs'
import { ForbiddenError } from '@atproto/xrpc-server'
import { TagService } from '../../tag-service'
import { getTagForReport } from '../../tag-service/util'

export default function (server: Server, ctx: AppContext) {
server.com.atproto.moderation.createReport({
Expand Down Expand Up @@ -37,7 +38,7 @@ export default function (server: Server, ctx: AppContext) {
ctx.cfg.service.did,
moderationTxn,
)
await tagService.evaluateForSubject()
await tagService.evaluateForSubject([getTagForReport(reasonType)])

return reportEvent
})
Expand Down
21 changes: 16 additions & 5 deletions packages/ozone/src/tag-service/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,18 @@ export class TagService {
]
}

async evaluateForSubject() {
// Allow the caller to seed the initial tags
async evaluateForSubject(initialTags?: Iterable<string>) {
try {
const tags: string[] = []
const tags = new Set(initialTags)

await Promise.all(
this.taggers.map(async (tagger) => {
try {
const newTags = await tagger.getTags()
if (newTags.length) tags.push(...newTags)
for (const newTag of newTags) {
tags.add(newTag)
}
} catch (e) {
// Don't let one tagger error stop the rest from running
log.error(
Expand All @@ -41,11 +44,19 @@ export class TagService {
}),
)

if (tags.length > 0) {
// Ensure that before inserting new tags, we discard any tag that may
// have been evaluated to be added but is already present in the subject
if (this.subjectStatus?.tags?.length) {
for (const tag of this.subjectStatus.tags) {
tags.delete(tag)
}
}

if (tags.size) {
await this.moderationService.logEvent({
event: {
$type: 'tools.ozone.moderation.defs#modEventTag',
add: tags,
add: [...tags],
remove: [],
},
subject: this.subject,
Expand Down
5 changes: 5 additions & 0 deletions packages/ozone/src/tag-service/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { ReasonType } from '../lexicon/types/com/atproto/moderation/defs'

export const getTagForReport = (reasonType: ReasonType) => {
return `report:${reasonType.replace('com.atproto.moderation.defs#reason', '').toLowerCase()}`
}
4 changes: 4 additions & 0 deletions packages/ozone/tests/__snapshots__/get-record.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ Object {
"subjectBlobCids": Array [],
"subjectRepoHandle": "alice.test",
"tags": Array [
"report:spam",
"lang:en",
"report:other",
],
"takendown": true,
"updatedAt": "1970-01-01T00:00:00.000Z",
Expand Down Expand Up @@ -150,7 +152,9 @@ Object {
"subjectBlobCids": Array [],
"subjectRepoHandle": "alice.test",
"tags": Array [
"report:spam",
"lang:en",
"report:other",
],
"takendown": true,
"updatedAt": "1970-01-01T00:00:00.000Z",
Expand Down
2 changes: 2 additions & 0 deletions packages/ozone/tests/__snapshots__/get-records.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ Object {
"subjectBlobCids": Array [],
"subjectRepoHandle": "alice.test",
"tags": Array [
"report:spam",
"lang:en",
"report:other",
],
"takendown": true,
"updatedAt": "1970-01-01T00:00:00.000Z",
Expand Down
2 changes: 2 additions & 0 deletions packages/ozone/tests/__snapshots__/get-repo.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ Object {
"subjectRepoHandle": "alice.test",
"tags": Array [
"lang:und",
"report:spam",
"report:other",
],
"takendown": true,
"updatedAt": "1970-01-01T00:00:00.000Z",
Expand Down
2 changes: 2 additions & 0 deletions packages/ozone/tests/__snapshots__/get-repos.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ Object {
"subjectRepoHandle": "alice.test",
"tags": Array [
"lang:und",
"report:spam",
"report:other",
],
"takendown": true,
"updatedAt": "1970-01-01T00:00:00.000Z",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ Object {
"subjectBlobCids": Array [],
"subjectRepoHandle": "alice.test",
"tags": Array [
"report:misleading",
"lang:und",
"report:spam",
],
"takendown": false,
"updatedAt": "1970-01-01T00:00:00.000Z",
Expand Down Expand Up @@ -101,6 +103,7 @@ Array [
"event": Object {
"$type": "tools.ozone.moderation.defs#modEventTag",
"add": Array [
"report:spam",
"lang:en",
"lang:i",
],
Expand Down Expand Up @@ -163,6 +166,7 @@ Array [
"event": Object {
"$type": "tools.ozone.moderation.defs#modEventTag",
"add": Array [
"report:spam",
"lang:en",
],
"remove": Array [],
Expand Down Expand Up @@ -210,7 +214,7 @@ Array [
"isReporterMuted": false,
"reportType": "com.atproto.moderation.defs#reasonSpam",
},
"id": 16,
"id": 17,
"subject": Object {
"$type": "com.atproto.admin.defs#repoRef",
"did": "user(0)",
Expand Down Expand Up @@ -251,7 +255,7 @@ Array [
"isReporterMuted": false,
"reportType": "com.atproto.moderation.defs#reasonSpam",
},
"id": 16,
"id": 17,
"subject": Object {
"$type": "com.atproto.admin.defs#repoRef",
"did": "user(0)",
Expand Down Expand Up @@ -292,7 +296,7 @@ Array [
"isReporterMuted": false,
"reportType": "com.atproto.moderation.defs#reasonSpam",
},
"id": 16,
"id": 17,
"subject": Object {
"$type": "com.atproto.admin.defs#repoRef",
"did": "user(0)",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Array [
"subjectBlobCids": Array [],
"subjectRepoHandle": "bob.test",
"tags": Array [
"report:spam",
"lang:en",
"lang:i",
],
Expand All @@ -41,6 +42,7 @@ Array [
"subjectBlobCids": Array [],
"subjectRepoHandle": "bob.test",
"tags": Array [
"report:spam",
"lang:en",
"lang:i",
],
Expand Down Expand Up @@ -69,6 +71,7 @@ Array [
"subjectBlobCids": Array [],
"subjectRepoHandle": "bob.test",
"tags": Array [
"report:spam",
"lang:en",
"lang:i",
],
Expand All @@ -91,6 +94,7 @@ Array [
"subjectBlobCids": Array [],
"subjectRepoHandle": "bob.test",
"tags": Array [
"report:spam",
"lang:en",
"lang:i",
],
Expand All @@ -114,6 +118,7 @@ Array [
"subjectBlobCids": Array [],
"subjectRepoHandle": "alice.test",
"tags": Array [
"report:spam",
"lang:ha",
],
"takendown": false,
Expand All @@ -135,6 +140,7 @@ Array [
"subjectBlobCids": Array [],
"subjectRepoHandle": "alice.test",
"tags": Array [
"report:misleading",
"lang:und",
],
"takendown": false,
Expand Down
8 changes: 4 additions & 4 deletions packages/ozone/tests/__snapshots__/moderation.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ exports[`moderation reporting creates reports of a DM chat. 1`] = `
Array [
Object {
"createdAt": "1970-01-01T00:00:00.000Z",
"id": 12,
"id": 14,
"reasonType": "com.atproto.moderation.defs#reasonSpam",
"reportedBy": "user(0)",
"subject": Object {
Expand All @@ -16,7 +16,7 @@ Array [
},
Object {
"createdAt": "1970-01-01T00:00:00.000Z",
"id": 14,
"id": 16,
"reason": "defamation",
"reasonType": "com.atproto.moderation.defs#reasonOther",
"reportedBy": "user(1)",
Expand All @@ -34,7 +34,7 @@ exports[`moderation reporting creates reports of a record. 1`] = `
Array [
Object {
"createdAt": "1970-01-01T00:00:00.000Z",
"id": 5,
"id": 7,
"reasonType": "com.atproto.moderation.defs#reasonSpam",
"reportedBy": "user(0)",
"subject": Object {
Expand All @@ -45,7 +45,7 @@ Array [
},
Object {
"createdAt": "1970-01-01T00:00:00.000Z",
"id": 7,
"id": 9,
"reason": "defamation",
"reasonType": "com.atproto.moderation.defs#reasonOther",
"reportedBy": "user(1)",
Expand Down
29 changes: 29 additions & 0 deletions packages/ozone/tests/content-tagger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
basicSeed,
} from '@atproto/dev-env'
import { REASONSPAM } from '../src/lexicon/types/com/atproto/moderation/defs'
import { REASONMISLEADING } from '../dist/lexicon/types/com/atproto/moderation/defs'

describe('moderation subject content tagging', () => {
let network: TestNetwork
Expand Down Expand Up @@ -138,4 +139,32 @@ describe('moderation subject content tagging', () => {
expect(imagePostStatus.tags).toContain('embed:image')
})
})

describe('report tagger', () => {
it('Adds report reason tag', async () => {
await Promise.all([
sc.createReport({
reasonType: REASONSPAM,
subject: {
$type: 'com.atproto.admin.defs#repoRef',
did: sc.dids.carol,
},
reportedBy: sc.dids.alice,
}),
sc.createReport({
reasonType: REASONMISLEADING,
subject: {
$type: 'com.atproto.admin.defs#repoRef',
did: sc.dids.carol,
},
reportedBy: sc.dids.alice,
}),
])

const accountStatus = await getStatus(sc.dids.carol)

expect(accountStatus.tags).toContain('report:spam')
expect(accountStatus.tags).toContain('report:misleading')
})
})
})
2 changes: 1 addition & 1 deletion packages/ozone/tests/report-muting.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ describe('report-muting', () => {
})

// Verify that a subject status was not created for bob's post since the reporter was muted
await assertSubjectStatus(bobsPostSubject.uri, undefined)
await assertSubjectStatus(bobsPostSubject.uri, REVIEWNONE)
// Verify, however, that the event was logged
await modClient.queryEvents({
subject: bobsPostSubject.uri,
Expand Down
Loading

0 comments on commit c559e7e

Please sign in to comment.