hydrateEvents: return early if the results would be empty

This commit is contained in:
Alex Gleason 2024-04-23 21:32:48 -05:00
parent b8d01ea3de
commit a2c3daade7
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
1 changed files with 24 additions and 0 deletions

View File

@ -99,6 +99,10 @@ function gatherReposts({ events, storage, signal }: HydrateOpts): Promise<DittoE
} }
} }
if (!ids.size) {
return Promise.resolve([]);
}
return storage.query( return storage.query(
[{ ids: [...ids], limit: ids.size }], [{ ids: [...ids], limit: ids.size }],
{ signal }, { signal },
@ -118,6 +122,10 @@ function gatherQuotes({ events, storage, signal }: HydrateOpts): Promise<DittoEv
} }
} }
if (!ids.size) {
return Promise.resolve([]);
}
return storage.query( return storage.query(
[{ ids: [...ids], limit: ids.size }], [{ ids: [...ids], limit: ids.size }],
{ signal }, { signal },
@ -128,6 +136,10 @@ function gatherQuotes({ events, storage, signal }: HydrateOpts): Promise<DittoEv
function gatherAuthors({ events, storage, signal }: HydrateOpts): Promise<DittoEvent[]> { function gatherAuthors({ events, storage, signal }: HydrateOpts): Promise<DittoEvent[]> {
const pubkeys = new Set(events.map((event) => event.pubkey)); const pubkeys = new Set(events.map((event) => event.pubkey));
if (!pubkeys.size) {
return Promise.resolve([]);
}
return storage.query( return storage.query(
[{ kinds: [0], authors: [...pubkeys], limit: pubkeys.size }], [{ kinds: [0], authors: [...pubkeys], limit: pubkeys.size }],
{ signal }, { signal },
@ -138,6 +150,10 @@ function gatherAuthors({ events, storage, signal }: HydrateOpts): Promise<DittoE
function gatherUsers({ events, storage, signal }: HydrateOpts): Promise<DittoEvent[]> { function gatherUsers({ events, storage, signal }: HydrateOpts): Promise<DittoEvent[]> {
const pubkeys = new Set(events.map((event) => event.pubkey)); const pubkeys = new Set(events.map((event) => event.pubkey));
if (!pubkeys.size) {
return Promise.resolve([]);
}
return storage.query( return storage.query(
[{ kinds: [30361], authors: [Conf.pubkey], '#d': [...pubkeys], limit: pubkeys.size }], [{ kinds: [30361], authors: [Conf.pubkey], '#d': [...pubkeys], limit: pubkeys.size }],
{ signal }, { signal },
@ -152,6 +168,10 @@ function gatherAuthorStats(events: DittoEvent[]): Promise<DittoTables['author_st
.map((event) => event.pubkey), .map((event) => event.pubkey),
); );
if (!pubkeys.size) {
return Promise.resolve([]);
}
return db return db
.selectFrom('author_stats') .selectFrom('author_stats')
.selectAll() .selectAll()
@ -167,6 +187,10 @@ function gatherEventStats(events: DittoEvent[]): Promise<DittoTables['event_stat
.map((event) => event.id), .map((event) => event.id),
); );
if (!ids.size) {
return Promise.resolve([]);
}
return db return db
.selectFrom('event_stats') .selectFrom('event_stats')
.selectAll() .selectAll()