Skip to content

Commit

Permalink
first pass at mod servce
Browse files Browse the repository at this point in the history
  • Loading branch information
dholms committed Dec 15, 2023
1 parent a72ba16 commit fb3161e
Showing 1 changed file with 49 additions and 49 deletions.
98 changes: 49 additions & 49 deletions packages/mod-service/src/services/moderation/views.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { sql } from 'kysely'
import { ArrayEl } from '@atproto/common'
import { AtUri, INVALID_HANDLE } from '@atproto/syntax'
import { BlobRef, jsonStringToLex } from '@atproto/lexicon'
import { BlobRef } from '@atproto/lexicon'
import { Database } from '../../db'
import {
ModEventView,
Expand All @@ -18,9 +18,7 @@ import {
import { OutputSchema as ReportOutput } from '../../lexicon/types/com/atproto/moderation/createReport'
import { Label } from '../../lexicon/types/com/atproto/label/defs'
import {
ModerationEventRow,
ModerationEventRowWithHandle,
ModerationSubjectStatusRow,
ModerationSubjectStatusRowWithHandle,
} from './types'
import { getSelfLabels } from '../label'
Expand Down Expand Up @@ -157,7 +155,14 @@ export class ModerationViews {
async eventDetail(
result: ModerationEventRowWithHandle,
): Promise<ModEventViewDetail> {
const subject = await this.subject(result)
const subjectId =
result.subjectType === 'com.atproto.admin.defs#repoRef'
? result.subjectDid
: result.subjectUri
if (!subjectId) {
throw new Error(`Bad subject: ${result.id}`)
}
const subject = await this.subject(subjectId)
const eventView = this.formatEvent(result)
const allBlobs = findBlobRefs(subject.value)
const subjectBlobs = await this.blob(
Expand Down Expand Up @@ -242,23 +247,26 @@ export class ModerationViews {
}, new Map<string, RecordView>())
}

async recordDetail(result: RecordResult): Promise<RecordViewDetail> {
const [record, subjectStatusResult] = await Promise.all([
this.record(result),
this.getSubjectStatus(didAndRecordPathFromUri(result.uri)),
async recordDetail(uri: AtUri): Promise<RecordViewDetail | undefined> {
const uriStr = uri.toString()
const [records, subjectStatusesResult] = await Promise.all([
this.records([uri]),
this.getSubjectStatus([uriStr]),
])
const record = records.get(uriStr)
if (!record) return undefined

const status = subjectStatusesResult.get(uriStr)

const [blobs, labels, subjectStatus] = await Promise.all([
this.blob(findBlobRefs(record.value)),
this.labels(record.uri),
subjectStatusResult?.length
? this.subjectStatus(subjectStatusResult[0])
: Promise.resolve(undefined),
status ? this.formatSubjectStatus(status) : Promise.resolve(undefined),
])
const selfLabels = getSelfLabels({
uri: result.uri,
cid: result.cid,
record: jsonStringToLex(result.json) as Record<string, unknown>,
uri: record.uri,
cid: record.cid,
record: record.value,
})
return {
...record,
Expand All @@ -270,7 +278,8 @@ export class ModerationViews {
labels: [...labels, ...selfLabels],
}
}
reportPublic(report: ReportResult): ReportOutput {

formatReport(report: ModerationEventRowWithHandle): ReportOutput {
return {
id: report.id,
createdAt: report.createdAt,
Expand All @@ -296,41 +305,36 @@ export class ModerationViews {
}
// Partial view for subjects

async subject(result: SubjectResult): Promise<SubjectView> {
let subject: SubjectView
if (result.subjectType === 'com.atproto.admin.defs#repoRef') {
const repoResult = await this.db.db
.selectFrom('actor')
.selectAll()
.where('did', '=', result.subjectDid)
.executeTakeFirst()
if (repoResult) {
subject = await this.repo(repoResult)
subject.$type = 'com.atproto.admin.defs#repoView'
async subject(subject: string): Promise<SubjectView> {
if (subject.startsWith('did:')) {
const repos = await this.repos([subject])
const repo = repos.get(subject)
if (repo) {
return {
$type: 'com.atproto.admin.defs#repoView',
...repo,
}
} else {
subject = { did: result.subjectDid }
subject.$type = 'com.atproto.admin.defs#repoViewNotFound'
return {
$type: 'com.atproto.admin.defs#repoViewNotFound',
did: subject,
}
}
} else if (
result.subjectType === 'com.atproto.repo.strongRef' &&
result.subjectUri !== null
) {
const recordResult = await this.db.db
.selectFrom('record')
.selectAll()
.where('uri', '=', result.subjectUri)
.executeTakeFirst()
if (recordResult) {
subject = await this.record(recordResult)
subject.$type = 'com.atproto.admin.defs#recordView'
} else {
const records = await this.records([new AtUri(subject)])
const record = records.get(subject)
if (record) {
return {
$type: 'com.atproto.admin.defs#recordView',
...record,
}
} else {
subject = { uri: result.subjectUri }
subject.$type = 'com.atproto.admin.defs#recordViewNotFound'
return {
$type: 'com.atproto.admin.defs#recordViewNotFound',
uri: subject,
}
}
} else {
throw new Error(`Bad subject data: (${result.id}) ${result.subjectType}`)
}
return subject
}

// Partial view for blobs
Expand Down Expand Up @@ -480,10 +484,6 @@ export class ModerationViews {

type SubjectView = ModEventViewDetail['subject'] & ReportViewDetail['subject']

function didFromUri(uri: string) {
return new AtUri(uri).host
}

function parseSubjectId(subject: string) {
if (subject.startsWith('did:')) {
return { did: subject }
Expand Down

0 comments on commit fb3161e

Please sign in to comment.