Skip to content

Commit

Permalink
get record tests all working
Browse files Browse the repository at this point in the history
  • Loading branch information
dholms committed Dec 20, 2023
1 parent 731473c commit ecb163e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 21 deletions.
6 changes: 2 additions & 4 deletions packages/ozone/src/api/com/atproto/admin/getRecord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@ export default function (server: Server, ctx: AppContext) {
handler: async ({ params, auth }) => {
const db = ctx.db

const uri = new AtUri(params.uri)

const [record, accountInfo] = await Promise.all([
ctx.services.moderation(db).views.recordDetail(uri),
getPdsAccountInfo(ctx, uri.hostname),
ctx.services.moderation(db).views.recordDetail(params),
getPdsAccountInfo(ctx, new AtUri(params.uri).hostname),
])

if (!record) {
Expand Down
40 changes: 24 additions & 16 deletions packages/ozone/src/services/moderation/views.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ export class ModerationViews {
}
}

async fetchRecords(uris: AtUri[]): Promise<
async fetchRecords(subjects: RecordSubject[]): Promise<
Map<
string,
{
Expand All @@ -205,15 +205,19 @@ export class ModerationViews {
>
> {
const fetched = await Promise.all(
uris.map((uri) =>
this.appviewAgent.api.com.atproto.repo
.getRecord({
subjects.map(async (subject) => {
const uri = new AtUri(subject.uri)
try {
return await this.appviewAgent.api.com.atproto.repo.getRecord({
repo: uri.hostname,
collection: uri.collection,
rkey: uri.rkey,
cid: subject.cid,
})
.catch(() => null),
),
} catch {
return null
}
}),
)
return fetched.reduce((acc, cur) => {
if (!cur) return acc
Expand All @@ -225,13 +229,14 @@ export class ModerationViews {
}, new Map<string, { uri: string; cid: string; value: Record<string, unknown>; indexedAt: string }>())
}

async records(uris: AtUri[]): Promise<Map<string, RecordView>> {
async records(subjects: RecordSubject[]): Promise<Map<string, RecordView>> {
const uris = subjects.map((record) => new AtUri(record.uri))
const dids = uris.map((u) => u.hostname)

const [repos, subjectStatuses, records] = await Promise.all([
this.repos(dids),
this.getSubjectStatus(uris.map((uri) => uri.toString())),
this.fetchRecords(uris),
this.getSubjectStatus(subjects.map((s) => s.uri)),
this.fetchRecords(subjects),
])

return uris.reduce((acc, uri) => {
Expand All @@ -256,16 +261,17 @@ export class ModerationViews {
}, new Map<string, RecordView>())
}

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

const status = subjectStatusesResult.get(uriStr)
const status = subjectStatusesResult.get(subject.uri)

const [blobs, labels, subjectStatus] = await Promise.all([
this.blob(findBlobRefs(record.value)),
Expand Down Expand Up @@ -330,7 +336,7 @@ export class ModerationViews {
}
}
} else {
const records = await this.records([new AtUri(subject)])
const records = await this.records([{ uri: subject }])
const record = records.get(subject)
if (record) {
return {
Expand Down Expand Up @@ -491,6 +497,8 @@ export class ModerationViews {
}
}

type RecordSubject = { uri: string; cid?: string }

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

function parseSubjectId(subject: string) {
Expand Down
2 changes: 1 addition & 1 deletion packages/ozone/tests/admin/get-record.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ describe('admin get record view', () => {
})
})

it.only('gets a record by uri, even when taken down.', async () => {
it('gets a record by uri, even when taken down.', async () => {
const result = await agent.api.com.atproto.admin.getRecord(
{ uri: sc.posts[sc.dids.alice][0].ref.uriStr },
{ headers: network.pds.adminAuthHeaders() },
Expand Down

0 comments on commit ecb163e

Please sign in to comment.