From 7c8aa8806993e903eede11abb0066a1a357bb069 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Tue, 25 Jul 2023 18:35:07 -0500 Subject: [PATCH] Trends: track based on the event's created_at date, instead of whenever the row was inserted --- src/loopback.ts | 6 ++++-- src/trends.ts | 5 ++--- 2 files changed, 6 insertions(+), 5 deletions(-) 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(), ); }