mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 11:29:46 +00:00
37 lines
932 B
TypeScript
37 lines
932 B
TypeScript
import { DittoTables } from '@ditto/db';
|
|
import { Kysely, sql } from 'kysely';
|
|
|
|
/** Get pubkeys whose name and NIP-05 is similar to 'q' */
|
|
export async function getPubkeysBySearch(
|
|
kysely: Kysely<DittoTables>,
|
|
opts: { q: string; limit: number; offset: number; following: Set<string> },
|
|
): Promise<Set<string>> {
|
|
const { q, limit, following, offset } = opts;
|
|
|
|
const pubkeys = new Set<string>();
|
|
|
|
const query = kysely
|
|
.selectFrom('top_authors')
|
|
.select('pubkey')
|
|
.where('search', sql`%>`, q)
|
|
.limit(limit)
|
|
.offset(offset);
|
|
|
|
if (following.size) {
|
|
const authorsQuery = query.where('pubkey', 'in', [...following]);
|
|
|
|
for (const { pubkey } of await authorsQuery.execute()) {
|
|
pubkeys.add(pubkey);
|
|
}
|
|
}
|
|
|
|
if (pubkeys.size >= limit) {
|
|
return pubkeys;
|
|
}
|
|
|
|
for (const { pubkey } of await query.limit(limit - pubkeys.size).execute()) {
|
|
pubkeys.add(pubkey);
|
|
}
|
|
|
|
return pubkeys;
|
|
}
|