Skip to content

Commit

Permalink
✨ Optionally ack all open subjects from the author with acknowledge e…
Browse files Browse the repository at this point in the history
…vent (bluesky-social#3171)

* ✨ Acknowledge all open subjects of an account when acknowledging account level reports

* 📝 Add changeset
  • Loading branch information
foysalit authored Dec 5, 2024
1 parent c72145d commit ed22362
Show file tree
Hide file tree
Showing 13 changed files with 151 additions and 49 deletions.
6 changes: 6 additions & 0 deletions .changeset/new-poems-scream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@atproto/ozone": patch
"@atproto/api": patch
---

Allow moderators to optionally acknowledge all open subjects of an account when acknowledging account level reports
6 changes: 5 additions & 1 deletion lexicons/tools/ozone/moderation/defs.json
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,11 @@
"modEventAcknowledge": {
"type": "object",
"properties": {
"comment": { "type": "string" }
"comment": { "type": "string" },
"acknowledgeAccountSubjects": {
"type": "boolean",
"description": "If true, all other reports on content authored by this account will be resolved (acknowledged)."
}
}
},
"modEventEscalate": {
Expand Down
10 changes: 8 additions & 2 deletions packages/api/src/client/lexicons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11310,6 +11310,11 @@ export const schemaDict = {
comment: {
type: 'string',
},
acknowledgeAccountSubjects: {
type: 'boolean',
description:
'If true, all other reports on content authored by this account will be resolved (acknowledged).',
},
},
},
modEventEscalate: {
Expand Down Expand Up @@ -13489,8 +13494,9 @@ export const schemaDict = {
},
},
},
}
export const schemas: LexiconDoc[] = Object.values(schemaDict) as LexiconDoc[]
} as const satisfies Record<string, LexiconDoc>

export const schemas = Object.values(schemaDict)
export const lexicons: Lexicons = new Lexicons(schemas)
export const ids = {
ComAtprotoAdminDefs: 'com.atproto.admin.defs',
Expand Down
2 changes: 2 additions & 0 deletions packages/api/src/client/types/tools/ozone/moderation/defs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,8 @@ export function validateModEventLabel(v: unknown): ValidationResult {

export interface ModEventAcknowledge {
comment?: string
/** If true, all other reports on content authored by this account will be resolved (acknowledged). */
acknowledgeAccountSubjects?: boolean
[k: string]: unknown
}

Expand Down
5 changes: 3 additions & 2 deletions packages/bsky/src/lexicon/lexicons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10728,8 +10728,9 @@ export const schemaDict = {
},
},
},
}
export const schemas: LexiconDoc[] = Object.values(schemaDict) as LexiconDoc[]
} as const satisfies Record<string, LexiconDoc>

export const schemas = Object.values(schemaDict)
export const lexicons: Lexicons = new Lexicons(schemas)
export const ids = {
ComAtprotoAdminDefs: 'com.atproto.admin.defs',
Expand Down
13 changes: 11 additions & 2 deletions packages/ozone/src/api/moderation/emitEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { AuthRequiredError, InvalidRequestError } from '@atproto/xrpc-server'
import { Server } from '../../lexicon'
import AppContext from '../../context'
import {
isModEventAcknowledge,
isModEventDivert,
isModEventEmail,
isModEventLabel,
Expand Down Expand Up @@ -39,6 +40,7 @@ const handleModerationEvent = async ({
const moderationService = ctx.modService(db)
const settingService = ctx.settingService(db)
const { event } = input.body
const isAcknowledgeEvent = isModEventAcknowledge(event)
const isTakedownEvent = isModEventTakedown(event)
const isReverseTakedownEvent = isModEventReverseTakedown(event)
const isLabelEvent = isModEventLabel(event)
Expand Down Expand Up @@ -219,8 +221,15 @@ const handleModerationEvent = async ({
}
}

if (isTakedownEvent && result.event.meta?.acknowledgeAccountSubjects) {
await moderationTxn.resolveSubjectsForAccount(subject.did, createdBy)
if (
(isTakedownEvent || isAcknowledgeEvent) &&
result.event.meta?.acknowledgeAccountSubjects
) {
await moderationTxn.resolveSubjectsForAccount(
subject.did,
createdBy,
result.event,
)
}

if (isLabelEvent) {
Expand Down
10 changes: 8 additions & 2 deletions packages/ozone/src/lexicon/lexicons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11310,6 +11310,11 @@ export const schemaDict = {
comment: {
type: 'string',
},
acknowledgeAccountSubjects: {
type: 'boolean',
description:
'If true, all other reports on content authored by this account will be resolved (acknowledged).',
},
},
},
modEventEscalate: {
Expand Down Expand Up @@ -13489,8 +13494,9 @@ export const schemaDict = {
},
},
},
}
export const schemas: LexiconDoc[] = Object.values(schemaDict) as LexiconDoc[]
} as const satisfies Record<string, LexiconDoc>

export const schemas = Object.values(schemaDict)
export const lexicons: Lexicons = new Lexicons(schemas)
export const ids = {
ComAtprotoAdminDefs: 'com.atproto.admin.defs',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,8 @@ export function validateModEventLabel(v: unknown): ValidationResult {

export interface ModEventAcknowledge {
comment?: string
/** If true, all other reports on content authored by this account will be resolved (acknowledged). */
acknowledgeAccountSubjects?: boolean
[k: string]: unknown
}

Expand Down
23 changes: 17 additions & 6 deletions packages/ozone/src/mod-service/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
isRecordEvent,
REVIEWESCALATED,
REVIEWOPEN,
isModEventAcknowledge,
} from '../lexicon/types/tools/ozone/moderation/defs'
import { RepoRef, RepoBlobRef } from '../lexicon/types/com/atproto/admin/defs'
import {
Expand Down Expand Up @@ -314,7 +315,11 @@ export class ModerationService {
return await builder.execute()
}

async resolveSubjectsForAccount(did: string, createdBy: string) {
async resolveSubjectsForAccount(
did: string,
createdBy: string,
accountEvent: ModerationEventRow,
) {
const subjectsToBeResolved = await this.db.db
.selectFrom('moderation_subject_status')
.where('did', '=', did)
Expand All @@ -327,6 +332,10 @@ export class ModerationService {
return
}

let accountEventInfo = `Account Event ID: ${accountEvent.id}`
if (accountEvent.comment) {
accountEventInfo += ` | Account Event Comment: ${accountEvent.comment}`
}
// Process subjects in chunks of 100 since each of these will trigger multiple db queries
for (const subjects of chunkArray(subjectsToBeResolved, 100)) {
await Promise.all(
Expand All @@ -335,13 +344,13 @@ export class ModerationService {
createdBy,
subject: subjectFromStatusRow(subject),
}

// For consistency's sake, when acknowledging appealed subjects, we should first resolve the appeal
if (subject.appealed) {
await this.logEvent({
event: {
$type: 'tools.ozone.moderation.defs#modEventResolveAppeal',
comment:
'[AUTO_RESOLVE_FOR_TAKENDOWN_ACCOUNT]: Automatically resolving all appealed content for a takendown account',
comment: `[AUTO_RESOLVE_ON_ACCOUNT_ACTION]: Automatically resolving all appealed content due to account level action | ${accountEventInfo}`,
},
...eventData,
})
Expand All @@ -350,8 +359,7 @@ export class ModerationService {
await this.logEvent({
event: {
$type: 'tools.ozone.moderation.defs#modEventAcknowledge',
comment:
'[AUTO_RESOLVE_FOR_TAKENDOWN_ACCOUNT]: Automatically resolving all reported content for a takendown account',
comment: `[AUTO_RESOLVE_ON_ACCOUNT_ACTION]: Automatically resolving all reported content due to account level action | ${accountEventInfo}`,
},
...eventData,
})
Expand Down Expand Up @@ -420,7 +428,10 @@ export class ModerationService {
if (event.cid) meta.cid = event.cid
}

if (isModEventTakedown(event) && event.acknowledgeAccountSubjects) {
if (
(isModEventTakedown(event) || isModEventAcknowledge(event)) &&
event.acknowledgeAccountSubjects
) {
meta.acknowledgeAccountSubjects = true
}

Expand Down
5 changes: 3 additions & 2 deletions packages/ozone/src/mod-service/views.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,13 @@ export class ModerationViews {
}

if (
event.action === 'tools.ozone.moderation.defs#modEventTakedown' &&
(event.action === 'tools.ozone.moderation.defs#modEventTakedown' ||
event.action === 'tools.ozone.moderation.defs#modEventAcknowledge') &&
event.meta?.acknowledgeAccountSubjects
) {
eventView.event = {
...eventView.event,
acknowledgeAccountSubjects: event.meta?.acknowledgeAccountSubjects,
acknowledgeAccountSubjects: event.meta.acknowledgeAccountSubjects,
}
}

Expand Down
106 changes: 76 additions & 30 deletions packages/ozone/tests/ack-all-subjects-of-account.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,28 +35,28 @@ describe('moderation', () => {
cid: ref.cidStr,
})

beforeAll(async () => {
network = await TestNetwork.create({
dbPostgresSchema: 'ozone_ack_all_subjects_of_account',
const getReviewStateBySubject = (subjects: SubjectStatusView[]) => {
const states = new Map<string, SubjectStatusView>()

subjects.forEach((item) => {
if (ComAtprotoRepoStrongRef.isMain(item.subject)) {
states.set(item.subject.uri, item)
} else if (isRepoRef(item.subject)) {
states.set(item.subject.did, item)
}
})
sc = network.getSeedClient()
modClient = network.ozone.getModClient()
await basicSeed(sc)
await network.processAll()
})

afterAll(async () => {
await network.close()
})
return states
}

it('acknowledges all open/escalated review subjects.', async () => {
const postOne = sc.posts[sc.dids.bob][0].ref
const postTwo = sc.posts[sc.dids.bob][1].ref
const reportUserAndPost = async (did: string) => {
const postOne = sc.posts[did][0].ref
const postTwo = sc.posts[did][1].ref
await Promise.all([
sc.createReport({
reasonType: REASONSPAM,
subject: repoSubject(sc.dids.bob),
reportedBy: sc.dids.alice,
subject: repoSubject(did),
reportedBy: sc.dids.carol,
}),
sc.createReport({
reasonType: REASONOTHER,
Expand All @@ -71,7 +71,6 @@ describe('moderation', () => {
reportedBy: sc.dids.carol,
}),
])

await modClient.emitEvent({
event: {
$type: 'tools.ozone.moderation.defs#modEventReport',
Expand All @@ -80,6 +79,26 @@ describe('moderation', () => {
subject: recordSubject(postTwo),
})

return { postOne, postTwo }
}

beforeAll(async () => {
network = await TestNetwork.create({
dbPostgresSchema: 'ozone_ack_all_subjects_of_account',
})
sc = network.getSeedClient()
modClient = network.ozone.getModClient()
await basicSeed(sc)
await network.processAll()
})

afterAll(async () => {
await network.close()
})

it('acknowledges all open/escalated review subjects with takedown.', async () => {
const { postOne, postTwo } = await reportUserAndPost(sc.dids.bob)

const { subjectStatuses: statusesBefore } = await modClient.queryStatuses({
subject: sc.dids.bob,
includeAllUserRecords: true,
Expand All @@ -95,19 +114,46 @@ describe('moderation', () => {
includeAllUserRecords: true,
})

const getReviewStateBySubject = (subjects: SubjectStatusView[]) => {
const states = new Map<string, SubjectStatusView>()
const reviewStatesBefore = getReviewStateBySubject(statusesBefore)
const reviewStatesAfter = getReviewStateBySubject(statusesAfter)

// Check that review states before were different for different subjects
expect(reviewStatesBefore.get(postOne.uriStr)?.reviewState).toBe(REVIEWOPEN)
expect(reviewStatesBefore.get(postTwo.uriStr)?.reviewState).toBe(
REVIEWESCALATED,
)
expect(reviewStatesBefore.get(sc.dids.bob)?.reviewState).toBe(REVIEWOPEN)

subjects.forEach((item) => {
if (ComAtprotoRepoStrongRef.isMain(item.subject)) {
states.set(item.subject.uri, item)
} else if (isRepoRef(item.subject)) {
states.set(item.subject.did, item)
}
})
// Check that review states after are all closed
expect(reviewStatesAfter.get(postOne.uriStr)?.reviewState).toBe(
REVIEWCLOSED,
)
expect(reviewStatesAfter.get(postTwo.uriStr)?.reviewState).toBe(
REVIEWCLOSED,
)
expect(reviewStatesAfter.get(sc.dids.bob)?.reviewState).toBe(REVIEWCLOSED)
})

return states
}
it('acknowledges all open/escalated review subjects with acknowledge.', async () => {
const { postOne, postTwo } = await reportUserAndPost(sc.dids.alice)

const { subjectStatuses: statusesBefore } = await modClient.queryStatuses({
subject: sc.dids.alice,
includeAllUserRecords: true,
})

await modClient.emitEvent({
subject: repoSubject(sc.dids.alice),
event: {
$type: 'tools.ozone.moderation.defs#modEventAcknowledge',
acknowledgeAccountSubjects: true,
},
})

const { subjectStatuses: statusesAfter } = await modClient.queryStatuses({
subject: sc.dids.alice,
includeAllUserRecords: true,
})

const reviewStatesBefore = getReviewStateBySubject(statusesBefore)
const reviewStatesAfter = getReviewStateBySubject(statusesAfter)
Expand All @@ -117,7 +163,7 @@ describe('moderation', () => {
expect(reviewStatesBefore.get(postTwo.uriStr)?.reviewState).toBe(
REVIEWESCALATED,
)
expect(reviewStatesBefore.get(sc.dids.bob)?.reviewState).toBe(REVIEWOPEN)
expect(reviewStatesBefore.get(sc.dids.alice)?.reviewState).toBe(REVIEWOPEN)

// Check that review states after are all closed
expect(reviewStatesAfter.get(postOne.uriStr)?.reviewState).toBe(
Expand All @@ -126,6 +172,6 @@ describe('moderation', () => {
expect(reviewStatesAfter.get(postTwo.uriStr)?.reviewState).toBe(
REVIEWCLOSED,
)
expect(reviewStatesAfter.get(sc.dids.bob)?.reviewState).toBe(REVIEWCLOSED)
expect(reviewStatesAfter.get(sc.dids.alice)?.reviewState).toBe(REVIEWCLOSED)
})
})
10 changes: 8 additions & 2 deletions packages/pds/src/lexicon/lexicons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11310,6 +11310,11 @@ export const schemaDict = {
comment: {
type: 'string',
},
acknowledgeAccountSubjects: {
type: 'boolean',
description:
'If true, all other reports on content authored by this account will be resolved (acknowledged).',
},
},
},
modEventEscalate: {
Expand Down Expand Up @@ -13489,8 +13494,9 @@ export const schemaDict = {
},
},
},
}
export const schemas: LexiconDoc[] = Object.values(schemaDict) as LexiconDoc[]
} as const satisfies Record<string, LexiconDoc>

export const schemas = Object.values(schemaDict)
export const lexicons: Lexicons = new Lexicons(schemas)
export const ids = {
ComAtprotoAdminDefs: 'com.atproto.admin.defs',
Expand Down
Loading

0 comments on commit ed22362

Please sign in to comment.