Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🚧 Setting up groundwork for communicating to reporters on mod action #2448

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions packages/ozone/src/api/moderation/emitEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { subjectFromInput } from '../../mod-service/subject'
import { ModerationLangService } from '../../mod-service/lang'
import { retryHttp } from '../../util'
import { ModeratorOutput, AdminTokenOutput } from '../../auth-verifier'
import { ModerationNotifierService } from '../../mod-service/notifier'

const handleModerationEvent = async ({
ctx,
Expand Down Expand Up @@ -123,11 +124,17 @@ const handleModerationEvent = async ({
})

const moderationLangService = new ModerationLangService(moderationTxn)
await moderationLangService.tagSubjectWithLang({
subject,
createdBy: ctx.cfg.service.did,
subjectStatus: result.subjectStatus,
})
const moderationNotifierService = new ModerationNotifierService(db, result)

// These two operations are not dependent on each other in any way so paralellizing them here
await Promise.all([
moderationLangService.tagSubjectWithLang({
subject,
createdBy: ctx.cfg.service.did,
subjectStatus: result.subjectStatus,
}),
moderationNotifierService.notifyReporters(),
])

if (subject.isRepo()) {
if (isTakedownEvent) {
Expand Down
81 changes: 81 additions & 0 deletions packages/ozone/src/mod-service/notifier.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import Database from '../db'
import { ModerationEventRow, ModerationSubjectStatusRow } from './types'

export class ModerationNotifierService {
private db: Database
private event: ModerationEventRow
private subjectStatus: ModerationSubjectStatusRow | null

private eventsThatRequireReporterNotification = [
'tools.ozone.moderation.defs#modEventTakedown',
'tools.ozone.moderation.defs#modEventLabel',
]

constructor(
db: Database,
{
event,
subjectStatus,
}: {
event: ModerationEventRow
subjectStatus: ModerationSubjectStatusRow | null
},
) {
this.db = db
this.event = event
this.subjectStatus = subjectStatus
}

private shouldNotifyReporters() {
return (
this.eventsThatRequireReporterNotification.includes(this.event.action) &&
this.subjectStatus
)
}

private async getReportersToBeNotified(): Promise<string[]> {
const query = this.db.db
.selectFrom('moderation_event')
.where('action', '=', 'tools.ozone.moderation.defs#modEventReport')
.where('subjectType', '=', this.event.subjectType)
.where('subjectDid', '=', this.event.subjectDid)
.where((qb) => {
if (this.event.subjectUri) {
return qb.where('subjectUri', '=', this.event.subjectUri)
}
return qb.where('subjectUri', 'is', null)
})
.where('id', '<', this.event.id)
.where('id', '>', (qb) => {
// Find the last moderation event that would have triggered a notification
return qb
.selectFrom('moderation_event')
.where('subjectType', '=', this.event.subjectType)
.where('subjectDid', '=', this.event.subjectDid)
.where('action', 'in', this.eventsThatRequireReporterNotification)
.where('id', '<', this.event.id)
.orderBy('id', 'desc')
.select(({ fn }) => fn.max('id').as('id'))
})
.select('createdBy')
.distinct()

const reporterDids = await query.execute()
return reporterDids.map((row) => row.createdBy)
}

async notifyReporters(): Promise<boolean> {
if (!this.shouldNotifyReporters()) {
return false
}

const reporterDids = await this.getReportersToBeNotified()
if (!reporterDids.length) {
return false
}

// TODO: Actually send out notifications to the reporters here

return true
}
}
Loading