queries: get author from memory when possible

This commit is contained in:
Alex Gleason 2023-12-28 00:39:05 -06:00
parent c2b25bc798
commit 2b1e97ee1b
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
1 changed files with 14 additions and 1 deletions

View File

@ -22,7 +22,7 @@ const getEvent = async <K extends number = number>(
const { kind, relations, signal = AbortSignal.timeout(1000) } = opts; const { kind, relations, signal = AbortSignal.timeout(1000) } = opts;
const microfilter: IdMicrofilter = { ids: [id] }; const microfilter: IdMicrofilter = { ids: [id] };
const [memoryEvent] = await memorelay.getFilters([microfilter], opts) as Event<K>[]; const [memoryEvent] = await memorelay.getFilters([microfilter], opts) as eventsDB.DittoEvent<K>[];
if (memoryEvent && !relations) { if (memoryEvent && !relations) {
return memoryEvent; return memoryEvent;
@ -36,7 +36,20 @@ const getEvent = async <K extends number = number>(
const dbEvent = await eventsDB.getFilters([filter], { limit: 1, signal }) const dbEvent = await eventsDB.getFilters([filter], { limit: 1, signal })
.then(([event]) => event); .then(([event]) => event);
// TODO: make this DRY-er.
if (dbEvent && !dbEvent.author) {
const [author] = await memorelay.getFilters([{ kinds: [0], authors: [dbEvent.pubkey] }], opts);
dbEvent.author = author;
}
if (dbEvent) return dbEvent; if (dbEvent) return dbEvent;
if (memoryEvent && !memoryEvent.author) {
const [author] = await memorelay.getFilters([{ kinds: [0], authors: [memoryEvent.pubkey] }], opts);
memoryEvent.author = author;
}
if (memoryEvent) return memoryEvent; if (memoryEvent) return memoryEvent;
return await reqmeister.req(microfilter).catch(() => undefined) as Event<K> | undefined; return await reqmeister.req(microfilter).catch(() => undefined) as Event<K> | undefined;