Merge branch 'kind-zero-lookups' into 'main'

Kind zero lookups

See merge request soapbox-pub/ditto!333
This commit is contained in:
Siddharth Singh 2024-06-10 02:46:07 +00:00
commit e0bec07f22
4 changed files with 56 additions and 11 deletions

3
.gitignore vendored
View file

@ -1,4 +1,5 @@
.env
*.cpuprofile
*.swp
deno-test.xml
deno-test.xml
*.db

View file

@ -38,14 +38,31 @@ const getEvent = async (
.then(([event]) => event);
};
const queryAndUnwrapEvt = (store: NStore, filter: NostrFilter, signal = AbortSignal.timeout(5000), limit = 1) =>
store.query([filter], { limit, signal } as any)
.then((events) => hydrateEvents({ events, store, signal }))
.then(([event]) => event);
/** Get a Nostr `set_medatadata` event for a user's pubkey. */
const getAuthor = async (pubkey: string, opts: GetEventOpts = {}): Promise<NostrEvent | undefined> => {
const store = await Storages.db();
const { signal = AbortSignal.timeout(1000) } = opts;
return await queryAndUnwrapEvt(store, { authors: [pubkey], kinds: [0], limit: 1 }, signal);
};
return await store.query([{ authors: [pubkey], kinds: [0], limit: 1 }], { limit: 1, signal })
.then((events) => hydrateEvents({ events, store, signal }))
.then(([event]) => event);
const getAuthorFallback = async (pubkey: string, opts: GetEventOpts = {}): Promise<NostrEvent | undefined> => {
const author = await getAuthor(pubkey, opts);
if (author) return author;
const pool = await Storages.kind0Finder();
const res = await queryAndUnwrapEvt(pool, { authors: [pubkey], kinds: [0], limit: 1 }, opts.signal);
if (res) {
const store = await Storages.db();
store.event(res);
}
return res;
};
/** Get users the given pubkey follows. */
@ -113,6 +130,7 @@ async function isLocallyFollowed(pubkey: string): Promise<boolean> {
export {
getAncestors,
getAuthor,
getAuthorFallback,
getDescendants,
getEvent,
getFeedPubkeys,

View file

@ -15,6 +15,7 @@ export class Storages {
private static _client: Promise<PoolStore> | undefined;
private static _pubsub: Promise<InternalRelay> | undefined;
private static _search: Promise<SearchStore> | undefined;
private static _kind0Finder: PoolStore | undefined;
/** SQLite database to store events this Ditto server cares about. */
public static async db(): Promise<EventsDB> {
@ -43,6 +44,31 @@ export class Storages {
return this._pubsub;
}
public static async kind0Finder(): Promise<PoolStore> {
if (this._kind0Finder) return this._kind0Finder;
const worker = new Worker('https://unpkg.com/nostr-relaypool2@0.6.34/lib/nostr-relaypool.worker.js', {
type: 'module',
});
const DEFAULT_RELAYS = [
'wss://nos.lol',
'wss://relay.damus.io',
'wss://purplepag.es',
'wss://nostr.mom',
'wss://relay.snort.social',
'wss://relay.primal.net',
];
// @ts-ignore Wrong types.
const pool = new RelayPoolWorker(worker, DEFAULT_RELAYS, {
autoReconnect: true,
skipVerification: false,
logErrorsAndNotices: false,
});
return this._kind0Finder = new PoolStore({ pool, relays: DEFAULT_RELAYS });
}
/** Relay pool storage. */
public static async client(): Promise<PoolStore> {
if (!this._client) {

View file

@ -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<string | undefined> {
export async function lookupPubkey(identifier: string, signal?: AbortSignal): Promise<string | undefined> {
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);