diff --git a/src/loopback.ts b/src/loopback.ts index d7b8069..013c951 100644 --- a/src/loopback.ts +++ b/src/loopback.ts @@ -1,7 +1,7 @@ import { Conf } from '@/config.ts'; import { relayInit } from '@/deps.ts'; import { trends } from '@/trends.ts'; -import { nostrNow } from '@/utils.ts'; +import { nostrDate, nostrNow } from '@/utils.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. */ function trackHashtags(event: Event): void { + const date = nostrDate(event.created_at); + const tags = event.tags .filter((tag) => tag[0] === 't') .map((tag) => tag[1]); try { - trends.addTagUsages(event.pubkey, tags); + trends.addTagUsages(event.pubkey, tags, date); } catch (_e) { // do nothing } diff --git a/src/trends.ts b/src/trends.ts index 4a471ce..a74fd46 100644 --- a/src/trends.ts +++ b/src/trends.ts @@ -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 tags = hashtagSchema.array().min(1).parse(hashtags); - const now = new Date(); this.#db.query( '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(), ); }