diff --git a/src/cron.ts b/src/cron.ts index b0d08f9d..7c934a7f 100644 --- a/src/cron.ts +++ b/src/cron.ts @@ -1,5 +1,6 @@ import { Stickynotes } from '@soapbox/stickynotes'; +import { Conf } from '@/config.ts'; import { DittoDB } from '@/db/DittoDB.ts'; import { handleEvent } from '@/pipeline.ts'; import { AdminSigner } from '@/signers/AdminSigner.ts'; @@ -21,7 +22,7 @@ async function updateTrendingNotesCache() { kinds: [1], since: yesterday, until: now, - limit: 20, + limit: 40, }); const signer = new AdminSigner(); @@ -32,7 +33,7 @@ async function updateTrendingNotesCache() { tags: [ ['L', 'pub.ditto.trends'], ['l', 'notes', 'pub.ditto.trends'], - ...events.map(({ id }) => ['e', id]), + ...events.map(({ id }) => ['e', id, Conf.relay]), ], created_at: Math.floor(Date.now() / 1000), }); @@ -63,7 +64,7 @@ async function updateTrendingHashtagsCache() { tags: [ ['L', 'pub.ditto.trends'], ['l', 'hashtags', 'pub.ditto.trends'], - ...hashtags.map(({ value }) => ['t', value]), + ...hashtags.map(({ value, authors, uses }) => ['t', value, authors.toString(), uses.toString()]), ], created_at: Math.floor(Date.now() / 1000), }); @@ -72,8 +73,40 @@ async function updateTrendingHashtagsCache() { console.info('Trending hashtags cache updated.'); } +async function updateTrendingLinksCache() { + console.info('Updating trending links cache...'); + 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, + }); + + const signer = new AdminSigner(); + + const label = await signer.signEvent({ + kind: 1985, + content: '', + tags: [ + ['L', 'pub.ditto.trends'], + ['l', 'links', '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 cache updated.'); +} + /** Start cron jobs for the application. */ export function cron() { - Deno.cron('update trending notes cache', { minute: { every: 15 } }, updateTrendingNotesCache); - Deno.cron('update trending hashtags cache', { dayOfMonth: { every: 1 } }, updateTrendingHashtagsCache); + Deno.cron('update trending notes cache', '15 * * * *', updateTrendingNotesCache); + Deno.cron('update trending hashtags cache', '30 * * * *', updateTrendingHashtagsCache); + Deno.cron('update trending links cache', '45 * * * *', updateTrendingLinksCache); }