Rework trending posts (it's much faster)

This commit is contained in:
Alex Gleason 2024-05-21 20:38:57 -05:00
parent 5da5848ca6
commit 80e886bfff
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
1 changed files with 10 additions and 11 deletions

View File

@ -91,27 +91,26 @@ const trendingStatusesController: AppController = async (c) => {
}; };
async function getTrendingNotes(): Promise<NostrEvent[]> { async function getTrendingNotes(): Promise<NostrEvent[]> {
const store = await Storages.db();
const kysely = await DittoDB.getInstance(); const kysely = await DittoDB.getInstance();
const since = Math.floor((Date.now() - Time.days(1)) / 1000); const since = Math.floor((Date.now() - Time.days(1)) / 1000);
const tags = await kysely const rows = await kysely
.selectFrom('nostr_tags') .selectFrom('nostr_events')
.select('nostr_tags.value') .selectAll('nostr_events')
.leftJoin('nostr_events', 'nostr_events.id', 'nostr_tags.event_id') .innerJoin('event_stats', 'event_stats.event_id', 'nostr_events.id')
.where('nostr_events.kind', 'in', [1, 6, 7]) .where('nostr_events.kind', '=', 1)
.where('nostr_events.created_at', '>', since) .where('nostr_events.created_at', '>', since)
.where('nostr_tags.name', '=', 'e')
.groupBy('nostr_tags.value')
.orderBy( .orderBy(
sql`SUM(case when nostr_events.kind = 6 then 2 else 0 end) + SUM(case when nostr_events.kind = 1 or nostr_events.kind = 7 then 1 else 0 end)`, sql`(event_stats.reposts_count * 2) + (event_stats.replies_count) + (event_stats.reactions_count)`,
'desc', 'desc',
) )
.limit(20) .limit(20)
.execute(); .execute();
const ids = tags.map(({ value }) => value); return rows.map((row) => ({
return store.query([{ kinds: [1], ids, limit: ids.length }]); ...row,
tags: JSON.parse(row.tags),
}));
} }
export { trendingStatusesController, trendingTagsController }; export { trendingStatusesController, trendingTagsController };