Skip to content

Commit

Permalink
fix admin search
Browse files Browse the repository at this point in the history
  • Loading branch information
dholms committed Sep 27, 2023
1 parent 701fba8 commit 660ad21
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 161 deletions.
8 changes: 4 additions & 4 deletions packages/pds/src/api/com/atproto/admin/searchRepos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ export default function (server: Server, ctx: AppContext) {
const { db, services } = ctx
const moderationService = services.moderation(db)
const { limit, cursor, invitedBy } = params
const term = params.term?.trim() ?? ''
const query = params.q?.trim() ?? params.term?.trim() ?? ''

const keyset = new ListKeyset(sql``, sql``)

if (!term) {
if (!query) {
const results = await services
.account(db)
.list({ limit, cursor, includeSoftDeleted: true, invitedBy })
Expand All @@ -47,13 +47,13 @@ export default function (server: Server, ctx: AppContext) {

const results = await services
.account(db)
.search({ term, limit, cursor, includeSoftDeleted: true })
.search({ query, limit, cursor, includeSoftDeleted: true })

return {
encoding: 'application/json',
body: {
// For did search, we can only find 1 or no match, cursors can be ignored entirely
cursor: term.startsWith('did:')
cursor: query.startsWith('did:')
? undefined
: keyset.packFromResult(results),
repos: await moderationService.views.repo(results, {
Expand Down
47 changes: 34 additions & 13 deletions packages/pds/src/services/account/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { countAll, notSoftDeletedClause } from '../../db/util'
import { paginate, TimeCidKeyset } from '../../db/pagination'
import * as sequencer from '../../sequencer'
import { AppPassword } from '../../lexicon/types/com/atproto/server/createAppPassword'
import { getUserSearchQueryPg, getUserSearchQuerySqlite } from '../util/search'

export class AccountService {
constructor(public db: Database) {}
Expand Down Expand Up @@ -275,22 +274,44 @@ export class AccountService {
}

async search(opts: {
term: string
query: string
limit: number
cursor?: string
includeSoftDeleted?: boolean
}): Promise<(RepoRoot & DidHandle)[]> {
const builder =
this.db.dialect === 'pg'
? getUserSearchQueryPg(this.db, opts)
.selectAll('did_handle')
.selectAll('repo_root')
: getUserSearchQuerySqlite(this.db, opts)
.selectAll('did_handle')
.selectAll('repo_root')
.select(sql<number>`0`.as('distance'))

return await builder.execute()
const { query, limit, cursor, includeSoftDeleted } = opts
const { ref } = this.db.db.dynamic

const builder = this.db.db
.selectFrom('did_handle')
.innerJoin('repo_root', 'repo_root.did', 'did_handle.did')
.innerJoin('user_account', 'user_account.did', 'did_handle.did')
.if(!includeSoftDeleted, (qb) =>
qb.where(notSoftDeletedClause(ref('repo_root'))),
)
.where((qb) => {
// sqlite doesn't support "ilike", but performs "like" case-insensitively
const likeOp = this.db.dialect === 'pg' ? 'ilike' : 'like'
if (query.includes('@')) {
return qb.where('user_account.email', likeOp, `%${query}%`)
}
if (query.startsWith('did:')) {
return qb.where('did_handle.did', '=', query)
}
return qb.where('did_handle.handle', likeOp, `${query}%`)
})
.selectAll(['did_handle', 'repo_root'])

const keyset = new ListKeyset(
ref('repo_root.indexedAt'),
ref('did_handle.handle'),
)

return await paginate(builder, {
limit,
cursor,
keyset,
}).execute()
}

async list(opts: {
Expand Down
144 changes: 0 additions & 144 deletions packages/pds/src/services/util/search.ts

This file was deleted.

0 comments on commit 660ad21

Please sign in to comment.