From 80e886bfff7cc384479b28ecc985a404e2af6cff Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Tue, 21 May 2024 20:38:57 -0500 Subject: [PATCH] Rework trending posts (it's much faster) --- src/controllers/api/trends.ts | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/controllers/api/trends.ts b/src/controllers/api/trends.ts index d531ffc..b5a56a4 100644 --- a/src/controllers/api/trends.ts +++ b/src/controllers/api/trends.ts @@ -91,27 +91,26 @@ const trendingStatusesController: AppController = async (c) => { }; async function getTrendingNotes(): Promise { - const store = await Storages.db(); const kysely = await DittoDB.getInstance(); const since = Math.floor((Date.now() - Time.days(1)) / 1000); - const tags = await kysely - .selectFrom('nostr_tags') - .select('nostr_tags.value') - .leftJoin('nostr_events', 'nostr_events.id', 'nostr_tags.event_id') - .where('nostr_events.kind', 'in', [1, 6, 7]) + const rows = await kysely + .selectFrom('nostr_events') + .selectAll('nostr_events') + .innerJoin('event_stats', 'event_stats.event_id', 'nostr_events.id') + .where('nostr_events.kind', '=', 1) .where('nostr_events.created_at', '>', since) - .where('nostr_tags.name', '=', 'e') - .groupBy('nostr_tags.value') .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', ) .limit(20) .execute(); - const ids = tags.map(({ value }) => value); - return store.query([{ kinds: [1], ids, limit: ids.length }]); + return rows.map((row) => ({ + ...row, + tags: JSON.parse(row.tags), + })); } export { trendingStatusesController, trendingTagsController };