Support trending links

This commit is contained in:
Alex Gleason 2024-06-02 16:42:25 -05:00
parent cd46d63999
commit 7f16a8d556
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
1 changed files with 38 additions and 5 deletions

View File

@ -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);
}