From ecb163ee5ebe5a0779d0cd6cb10e3700ec53a99d Mon Sep 17 00:00:00 2001 From: dholms Date: Tue, 19 Dec 2023 19:31:18 -0600 Subject: [PATCH] get record tests all working --- .../src/api/com/atproto/admin/getRecord.ts | 6 +-- .../ozone/src/services/moderation/views.ts | 40 +++++++++++-------- packages/ozone/tests/admin/get-record.test.ts | 2 +- 3 files changed, 27 insertions(+), 21 deletions(-) diff --git a/packages/ozone/src/api/com/atproto/admin/getRecord.ts b/packages/ozone/src/api/com/atproto/admin/getRecord.ts index f838c43f31b..3c3dd6838b5 100644 --- a/packages/ozone/src/api/com/atproto/admin/getRecord.ts +++ b/packages/ozone/src/api/com/atproto/admin/getRecord.ts @@ -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) { diff --git a/packages/ozone/src/services/moderation/views.ts b/packages/ozone/src/services/moderation/views.ts index eda9ecdb1c0..9496dc5f1ec 100644 --- a/packages/ozone/src/services/moderation/views.ts +++ b/packages/ozone/src/services/moderation/views.ts @@ -193,7 +193,7 @@ export class ModerationViews { } } - async fetchRecords(uris: AtUri[]): Promise< + async fetchRecords(subjects: RecordSubject[]): Promise< Map< string, { @@ -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 @@ -225,13 +229,14 @@ export class ModerationViews { }, new Map; indexedAt: string }>()) } - async records(uris: AtUri[]): Promise> { + async records(subjects: RecordSubject[]): Promise> { + 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) => { @@ -256,16 +261,17 @@ export class ModerationViews { }, new Map()) } - async recordDetail(uri: AtUri): Promise { - const uriStr = uri.toString() + async recordDetail( + subject: RecordSubject, + ): Promise { 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)), @@ -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 { @@ -491,6 +497,8 @@ export class ModerationViews { } } +type RecordSubject = { uri: string; cid?: string } + type SubjectView = ModEventViewDetail['subject'] & ReportViewDetail['subject'] function parseSubjectId(subject: string) { diff --git a/packages/ozone/tests/admin/get-record.test.ts b/packages/ozone/tests/admin/get-record.test.ts index e026770814d..8a303886966 100644 --- a/packages/ozone/tests/admin/get-record.test.ts +++ b/packages/ozone/tests/admin/get-record.test.ts @@ -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() },