From 78cae13885112d93c5336ec092ceef4afb614cef Mon Sep 17 00:00:00 2001 From: Siddharth Singh Date: Thu, 30 May 2024 00:05:07 +0530 Subject: [PATCH] fix kind 0 lookups for account search --- .gitignore | 3 ++- src/queries.ts | 14 ++++++++++++++ src/utils/lookup.ts | 14 +++++++------- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index 39dbfbbb..ec391bf6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .env *.cpuprofile *.swp -deno-test.xml \ No newline at end of file +deno-test.xml +*.db \ No newline at end of file diff --git a/src/queries.ts b/src/queries.ts index 1fccb68d..74d32611 100644 --- a/src/queries.ts +++ b/src/queries.ts @@ -48,6 +48,19 @@ const getAuthor = async (pubkey: string, opts: GetEventOpts = {}): Promise event); }; +const getAuthorFallback = async (pubkey: string, opts: GetEventOpts = {}): Promise => { + const author = await getAuthor(pubkey, opts); + const { signal = AbortSignal.timeout(1000) } = opts; + + if (author) return author; + + const pool = await Storages.client(); + return await pool.query([{ authors: [pubkey], kinds: [0], limit: 1 }], { limit: 1, signal }) + .then((events) => hydrateEvents({ events, store: pool, signal })) + .then(([event]) => event); + +}; + /** Get users the given pubkey follows. */ const getFollows = async (pubkey: string, signal?: AbortSignal): Promise => { const store = await Storages.db(); @@ -113,6 +126,7 @@ async function isLocallyFollowed(pubkey: string): Promise { export { getAncestors, getAuthor, + getAuthorFallback, getDescendants, getEvent, getFeedPubkeys, diff --git a/src/utils/lookup.ts b/src/utils/lookup.ts index 90b30c2b..0f163172 100644 --- a/src/utils/lookup.ts +++ b/src/utils/lookup.ts @@ -1,6 +1,6 @@ import { NIP05, NostrEvent, NSchema as n } from '@nostrify/nostrify'; -import { getAuthor } from '@/queries.ts'; +import { getAuthorFallback } from '@/queries.ts'; import { bech32ToPubkey } from '@/utils.ts'; import { nip05Cache } from '@/utils/nip05.ts'; import { Stickynotes } from '@soapbox/stickynotes'; @@ -13,21 +13,21 @@ export async function lookupAccount( const pubkey = await lookupPubkey(value, signal); if (pubkey) { - return getAuthor(pubkey); + return getAuthorFallback(pubkey); } } /** Resolve a bech32 or NIP-05 identifier to a pubkey. */ -export async function lookupPubkey(value: string, signal?: AbortSignal): Promise { +export async function lookupPubkey(identifier: string, signal?: AbortSignal): Promise { const console = new Stickynotes('ditto:lookup'); - if (n.bech32().safeParse(value).success) { - return bech32ToPubkey(value); + if (n.bech32().safeParse(identifier).success) { + return bech32ToPubkey(identifier); } - if (NIP05.regex().test(value)) { + if (NIP05.regex().test(identifier)) { try { - const { pubkey } = await nip05Cache.fetch(value, { signal }); + const { pubkey } = await nip05Cache.fetch(identifier, { signal }); return pubkey; } catch (e) { console.debug(e);