From b35090ef1056989e4730c3fc1c11163267414803 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sun, 2 Jun 2024 19:18:42 -0500 Subject: [PATCH] cron: DRY updateTrendingTags --- src/cron.ts | 53 +++++++++-------------------------------------------- 1 file changed, 9 insertions(+), 44 deletions(-) diff --git a/src/cron.ts b/src/cron.ts index 4823e16..aa19caf 100644 --- a/src/cron.ts +++ b/src/cron.ts @@ -46,21 +46,21 @@ async function updateTrendingNotes() { console.info('Trending notes updated.'); } -async function updateTrendingHashtags() { - console.info('Updating trending hashtags...'); +async function updateTrendingTags(tagName: string, extra = '') { + console.info(`Updating trending #${tagName}...`); const kysely = await DittoDB.getInstance(); const signal = AbortSignal.timeout(1000); const yesterday = Math.floor((Date.now() - Time.days(1)) / 1000); const now = Math.floor(Date.now() / 1000); - const hashtags = await getTrendingTagValues(kysely, 't', { + const trends = await getTrendingTagValues(kysely, tagName, { since: yesterday, until: now, limit: 20, }); - if (!hashtags.length) { + if (!trends.length) { return; } @@ -71,54 +71,19 @@ async function updateTrendingHashtags() { content: '', tags: [ ['L', 'pub.ditto.trends'], - ['l', '#t', 'pub.ditto.trends'], - ...hashtags.map(({ value, authors, uses }) => ['t', value, '', authors.toString(), uses.toString()]), + ['l', `#${tagName}`, 'pub.ditto.trends'], + ...trends.map(({ value, authors, uses }) => [tagName, value, extra, authors.toString(), uses.toString()]), ], created_at: Math.floor(Date.now() / 1000), }); await handleEvent(label, signal); - console.info('Trending hashtags updated.'); -} - -async function updateTrendingLinks() { - console.info('Updating trending links...'); - const kysely = await DittoDB.getInstance(); - const signal = AbortSignal.timeout(1000); - - const yesterday = Math.floor((Date.now() - Time.days(1)) / 1000); - const now = Math.floor(Date.now() / 1000); - - const links = await getTrendingTagValues(kysely, 'r', { - since: yesterday, - until: now, - limit: 20, - }); - - if (!links.length) { - return; - } - - const signer = new AdminSigner(); - - const label = await signer.signEvent({ - kind: 1985, - content: '', - tags: [ - ['L', 'pub.ditto.trends'], - ['l', '#r', 'pub.ditto.trends'], - ...links.map(({ value, authors, uses }) => ['r', value, '', authors.toString(), uses.toString()]), - ], - created_at: Math.floor(Date.now() / 1000), - }); - - await handleEvent(label, signal); - console.info('Trending links updated.'); + console.info(`Trending #${tagName} updated.`); } /** Start cron jobs for the application. */ export function cron() { Deno.cron('update trending notes', '15 * * * *', updateTrendingNotes); - Deno.cron('update trending hashtags', '30 * * * *', updateTrendingHashtags); - Deno.cron('update trending links', '45 * * * *', updateTrendingLinks); + Deno.cron('update trending hashtags', '30 * * * *', () => updateTrendingTags('t')); + Deno.cron('update trending links', '45 * * * *', () => updateTrendingTags('r')); }