From c7d6f7a28882b1ca6bfe30cfbc47bc4e440fe3ce Mon Sep 17 00:00:00 2001 From: dholms Date: Wed, 20 Dec 2023 11:32:37 -0600 Subject: [PATCH] search repos --- .../src/api/com/atproto/admin/searchRepos.ts | 36 ++++++++++--------- packages/ozone/src/context.ts | 9 +++++ .../ozone/src/services/moderation/views.ts | 1 + packages/ozone/tests/repo-search.test.ts | 1 + 4 files changed, 30 insertions(+), 17 deletions(-) diff --git a/packages/ozone/src/api/com/atproto/admin/searchRepos.ts b/packages/ozone/src/api/com/atproto/admin/searchRepos.ts index d6255f3c9cb..cab795eb364 100644 --- a/packages/ozone/src/api/com/atproto/admin/searchRepos.ts +++ b/packages/ozone/src/api/com/atproto/admin/searchRepos.ts @@ -1,27 +1,29 @@ import { Server } from '../../../../lexicon' import AppContext from '../../../../context' -import { InvalidRequestError } from '@atproto/xrpc-server' export default function (server: Server, ctx: AppContext) { server.com.atproto.admin.searchRepos({ auth: ctx.roleVerifier, handler: async ({ params }) => { - throw new InvalidRequestError('@TODO') - // const db = ctx.db - // const moderationService = ctx.services.moderation(db) - // const { limit, cursor } = params - // // prefer new 'q' query param over deprecated 'term' - // const query = params.q ?? params.term - // const { results, cursor: resCursor } = await ctx.services - // .actor(db) - // .getSearchResults({ query, limit, cursor, includeSoftDeleted: true }) - // return { - // encoding: 'application/json', - // body: { - // cursor: resCursor, - // repos: await moderationService.views.repo(results), - // }, - // } + const res = await ctx.appviewAgent.api.com.atproto.admin.searchRepos( + params, + ) + const db = ctx.db + const moderationService = ctx.services.moderation(db) + const views = await moderationService.views.repos( + res.data.repos.map((r) => r.did), + ) + const repos = res.data.repos.map((r) => ({ + ...r, + moderation: views.get(r.did)?.moderation ?? {}, + })) + return { + encoding: 'application/json', + body: { + cursor: res.data.cursor, + repos, + }, + } }, }) } diff --git a/packages/ozone/src/context.ts b/packages/ozone/src/context.ts index 1f285b14c6c..3a21758bba7 100644 --- a/packages/ozone/src/context.ts +++ b/packages/ozone/src/context.ts @@ -15,6 +15,7 @@ export class AppContext { private opts: { db: Database appviewAgent: AtpAgent + searchAgent: AtpAgent cfg: ServerConfig services: Services signingKey: Keypair @@ -44,6 +45,14 @@ export class AppContext { return this.opts.services } + get appviewAgent(): AtpAgent { + return this.opts.appviewAgent + } + + get searchAgent(): AtpAgent { + return this.opts.searchAgent + } + get signingKey(): Keypair { return this.opts.signingKey } diff --git a/packages/ozone/src/services/moderation/views.ts b/packages/ozone/src/services/moderation/views.ts index 9496dc5f1ec..3df906d6989 100644 --- a/packages/ozone/src/services/moderation/views.ts +++ b/packages/ozone/src/services/moderation/views.ts @@ -28,6 +28,7 @@ export class ModerationViews { constructor(private db: Database, private appviewAgent: AtpAgent) {} async repos(dids: string[]): Promise> { + if (dids.length === 0) return new Map() const [appviewRes, subjectStatuses] = await Promise.all([ this.appviewAgent.api.com.atproto.admin.getAccountInfos({ dids }), this.getSubjectStatus(dids), diff --git a/packages/ozone/tests/repo-search.test.ts b/packages/ozone/tests/repo-search.test.ts index 57da070fa49..c46103d0c0f 100644 --- a/packages/ozone/tests/repo-search.test.ts +++ b/packages/ozone/tests/repo-search.test.ts @@ -12,6 +12,7 @@ describe('admin repo search view', () => { beforeAll(async () => { network = await TestNetwork.create({ dbPostgresSchema: 'views_admin_repo_search', + ozone: { enabled: true }, }) agent = network.pds.getClient() sc = network.getSeedClient()