diff --git a/src/db/events.ts b/src/db/events.ts index adb3abc8..b88d38d1 100644 --- a/src/db/events.ts +++ b/src/db/events.ts @@ -171,7 +171,7 @@ function getFilterQuery(filter: DittoFilter): EventQuery { return exp .orderBy('created_at', 'desc') - .groupBy('pubkey') + .groupBy('events.pubkey') .as('authors'); }, (join) => join.onRef('authors.pubkey', '=', 'events.pubkey'), @@ -184,9 +184,13 @@ function getFilterQuery(filter: DittoFilter): EventQuery { 'authors.tags as author_tags', 'authors.created_at as author_created_at', 'authors.sig as author_sig', - 'authors.author_stats_followers_count', - 'authors.author_stats_following_count', - 'authors.author_stats_notes_count', + ...(filter.relations?.includes('stats') + ? [ + 'authors.author_stats_followers_count', + 'authors.author_stats_following_count', + 'authors.author_stats_notes_count', + ] as const + : []), ]); } diff --git a/src/stats.ts b/src/stats.ts index f54b7dea..05e6be35 100644 --- a/src/stats.ts +++ b/src/stats.ts @@ -9,19 +9,17 @@ type EventStatDiff = ['event_stats', eventId: string, stat: EventStat, diff: num type StatDiff = PubkeyStatDiff | EventStatDiff; /** Store stats for the event in LMDB. */ -function updateStats(event: Event) { +async function updateStats(event: Event) { const statDiffs = getStatsDiff(event); if (!statDiffs.length) return; const pubkeyDiffs = statDiffs.filter(([table]) => table === 'pubkey_stats') as PubkeyStatDiff[]; const eventDiffs = statDiffs.filter(([table]) => table === 'event_stats') as EventStatDiff[]; - return db.transaction().execute(() => { - return Promise.all([ - pubkeyStatsQuery(pubkeyDiffs).execute(), - eventStatsQuery(eventDiffs).execute(), - ]); - }); + await Promise.all([ + pubkeyDiffs.length ? pubkeyStatsQuery(pubkeyDiffs).execute() : undefined, + eventDiffs.length ? eventStatsQuery(eventDiffs).execute() : undefined, + ]); } /** Calculate stats changes ahead of time so we can build an efficient query. */