mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 11:29:46 +00:00
refactor: move getPubkeysBySearch () function to a new location
This commit is contained in:
parent
4d0ae401b3
commit
2fe6a8fde5
4 changed files with 20 additions and 17 deletions
|
|
@ -6,7 +6,6 @@ import { type AppController } from '@/app.ts';
|
||||||
import { Conf } from '@/config.ts';
|
import { Conf } from '@/config.ts';
|
||||||
import { getAuthor, getFollowedPubkeys } from '@/queries.ts';
|
import { getAuthor, getFollowedPubkeys } from '@/queries.ts';
|
||||||
import { booleanParamSchema, fileSchema } from '@/schema.ts';
|
import { booleanParamSchema, fileSchema } from '@/schema.ts';
|
||||||
import { getPubkeysBySearch } from '@/controllers/api/search.ts';
|
|
||||||
import { Storages } from '@/storages.ts';
|
import { Storages } from '@/storages.ts';
|
||||||
import { uploadFile } from '@/utils/upload.ts';
|
import { uploadFile } from '@/utils/upload.ts';
|
||||||
import { nostrNow } from '@/utils.ts';
|
import { nostrNow } from '@/utils.ts';
|
||||||
|
|
@ -19,6 +18,7 @@ import { renderReblog, renderStatus } from '@/views/mastodon/statuses.ts';
|
||||||
import { hydrateEvents } from '@/storages/hydrate.ts';
|
import { hydrateEvents } from '@/storages/hydrate.ts';
|
||||||
import { bech32ToPubkey } from '@/utils.ts';
|
import { bech32ToPubkey } from '@/utils.ts';
|
||||||
import { addTag, deleteTag, findReplyTag, getTagSet } from '@/utils/tags.ts';
|
import { addTag, deleteTag, findReplyTag, getTagSet } from '@/utils/tags.ts';
|
||||||
|
import { getPubkeysBySearch } from '@/utils/search.ts';
|
||||||
|
|
||||||
const usernameSchema = z
|
const usernameSchema = z
|
||||||
.string().min(1).max(30)
|
.string().min(1).max(30)
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,8 @@
|
||||||
import { NostrEvent, NostrFilter, NSchema as n } from '@nostrify/nostrify';
|
import { NostrEvent, NostrFilter, NSchema as n } from '@nostrify/nostrify';
|
||||||
import { nip19 } from 'nostr-tools';
|
import { nip19 } from 'nostr-tools';
|
||||||
import { Kysely, sql } from 'kysely';
|
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
|
|
||||||
import { AppController } from '@/app.ts';
|
import { AppController } from '@/app.ts';
|
||||||
import { DittoTables } from '@/db/DittoTables.ts';
|
|
||||||
import { booleanParamSchema } from '@/schema.ts';
|
import { booleanParamSchema } from '@/schema.ts';
|
||||||
import { Storages } from '@/storages.ts';
|
import { Storages } from '@/storages.ts';
|
||||||
import { hydrateEvents } from '@/storages/hydrate.ts';
|
import { hydrateEvents } from '@/storages/hydrate.ts';
|
||||||
|
|
@ -12,6 +10,7 @@ import { extractIdentifier, lookupPubkey } from '@/utils/lookup.ts';
|
||||||
import { nip05Cache } from '@/utils/nip05.ts';
|
import { nip05Cache } from '@/utils/nip05.ts';
|
||||||
import { accountFromPubkey, renderAccount } from '@/views/mastodon/accounts.ts';
|
import { accountFromPubkey, renderAccount } from '@/views/mastodon/accounts.ts';
|
||||||
import { renderStatus } from '@/views/mastodon/statuses.ts';
|
import { renderStatus } from '@/views/mastodon/statuses.ts';
|
||||||
|
import { getPubkeysBySearch } from '@/utils/search.ts';
|
||||||
|
|
||||||
const searchQuerySchema = z.object({
|
const searchQuerySchema = z.object({
|
||||||
q: z.string().transform(decodeURIComponent),
|
q: z.string().transform(decodeURIComponent),
|
||||||
|
|
@ -194,16 +193,4 @@ async function getLookupFilters({ q, type, resolve }: SearchQuery, signal: Abort
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Get pubkeys whose name and NIP-05 is similar to 'q' */
|
export { searchController };
|
||||||
async function getPubkeysBySearch(kysely: Kysely<DittoTables>, { q, limit }: Pick<SearchQuery, 'q' | 'limit'>) {
|
|
||||||
const pubkeys = (await sql<{ pubkey: string }>`
|
|
||||||
SELECT *, word_similarity(${q}, search) AS sml
|
|
||||||
FROM author_search
|
|
||||||
WHERE ${q} % search
|
|
||||||
ORDER BY sml DESC, search LIMIT ${limit}
|
|
||||||
`.execute(kysely)).rows.map(({ pubkey }) => pubkey);
|
|
||||||
|
|
||||||
return pubkeys;
|
|
||||||
}
|
|
||||||
|
|
||||||
export { getPubkeysBySearch, searchController };
|
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
import { assertEquals } from '@std/assert';
|
import { assertEquals } from '@std/assert';
|
||||||
|
|
||||||
import { createTestDB } from '@/test.ts';
|
import { createTestDB } from '@/test.ts';
|
||||||
import { getPubkeysBySearch } from '@/controllers/api/search.ts';
|
import { getPubkeysBySearch } from '@/utils/search.ts';
|
||||||
|
|
||||||
Deno.test('fuzzy search works', async () => {
|
Deno.test('fuzzy search works', async () => {
|
||||||
await using db = await createTestDB();
|
await using db = await createTestDB();
|
||||||
16
src/utils/search.ts
Normal file
16
src/utils/search.ts
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
import { Kysely, sql } from 'kysely';
|
||||||
|
|
||||||
|
import { DittoTables } from '@/db/DittoTables.ts';
|
||||||
|
|
||||||
|
/** Get pubkeys whose name and NIP-05 is similar to 'q' */
|
||||||
|
export async function getPubkeysBySearch(kysely: Kysely<DittoTables>, opts: { q: string; limit: number }) {
|
||||||
|
const { q, limit } = opts;
|
||||||
|
const pubkeys = (await sql<{ pubkey: string }>`
|
||||||
|
SELECT *, word_similarity(${q}, search) AS sml
|
||||||
|
FROM author_search
|
||||||
|
WHERE ${q} % search
|
||||||
|
ORDER BY sml DESC, search LIMIT ${limit}
|
||||||
|
`.execute(kysely)).rows.map(({ pubkey }) => pubkey);
|
||||||
|
|
||||||
|
return pubkeys;
|
||||||
|
}
|
||||||
Loading…
Add table
Reference in a new issue