create special relay pool with popular relays for getting events out of.

This commit is contained in:
Siddharth Singh 2024-05-30 19:45:28 +05:30
parent 78cae13885
commit cf5655f14f
No known key found for this signature in database
2 changed files with 41 additions and 11 deletions

View file

@ -1,4 +1,4 @@
import { NostrEvent, NostrFilter } from '@nostrify/nostrify';
import { NostrEvent, NostrFilter, NStore } from '@nostrify/nostrify';
import Debug from '@soapbox/stickynotes/debug';
import { Conf } from '@/config.ts';
@ -38,27 +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 store.query([{ authors: [pubkey], kinds: [0], limit: 1 }], { limit: 1, signal })
.then((events) => hydrateEvents({ events, store, signal }))
.then(([event]) => event);
return await queryAndUnwrapEvt(store, { authors: [pubkey], kinds: [0], limit: 1 }, signal);
};
const getAuthorFallback = async (pubkey: string, opts: GetEventOpts = {}): Promise<NostrEvent | undefined> => {
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);
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. */

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) {