Trends: track based on the event's created_at date, instead of whenever the row was inserted

This commit is contained in:
Alex Gleason 2023-07-25 18:35:07 -05:00
parent 79ec5dd4e0
commit 7c8aa88069
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
2 changed files with 6 additions and 5 deletions

View File

@ -1,7 +1,7 @@
import { Conf } from '@/config.ts'; import { Conf } from '@/config.ts';
import { relayInit } from '@/deps.ts'; import { relayInit } from '@/deps.ts';
import { trends } from '@/trends.ts'; import { trends } from '@/trends.ts';
import { nostrNow } from '@/utils.ts'; import { nostrDate, nostrNow } from '@/utils.ts';
import type { Event } from '@/event.ts'; import type { Event } from '@/event.ts';
@ -23,12 +23,14 @@ function handleEvent(event: Event): void {
/** Track whenever a hashtag is used, for processing trending tags. */ /** Track whenever a hashtag is used, for processing trending tags. */
function trackHashtags(event: Event): void { function trackHashtags(event: Event): void {
const date = nostrDate(event.created_at);
const tags = event.tags const tags = event.tags
.filter((tag) => tag[0] === 't') .filter((tag) => tag[0] === 't')
.map((tag) => tag[1]); .map((tag) => tag[1]);
try { try {
trends.addTagUsages(event.pubkey, tags); trends.addTagUsages(event.pubkey, tags, date);
} catch (_e) { } catch (_e) {
// do nothing // do nothing
} }

View File

@ -54,14 +54,13 @@ class TrendsDB {
})); }));
} }
addTagUsages(pubkey: string, hashtags: string[]): void { addTagUsages(pubkey: string, hashtags: string[], date = new Date()): void {
const pubkey8 = hexIdSchema.parse(pubkey).substring(0, 8); const pubkey8 = hexIdSchema.parse(pubkey).substring(0, 8);
const tags = hashtagSchema.array().min(1).parse(hashtags); const tags = hashtagSchema.array().min(1).parse(hashtags);
const now = new Date();
this.#db.query( this.#db.query(
'INSERT INTO tag_usages (tag, pubkey8, inserted_at) VALUES ' + tags.map(() => '(?, ?, ?)').join(', '), 'INSERT INTO tag_usages (tag, pubkey8, inserted_at) VALUES ' + tags.map(() => '(?, ?, ?)').join(', '),
tags.map((tag) => [tag, pubkey8, now]).flat(), tags.map((tag) => [tag, pubkey8, date]).flat(),
); );
} }