From 3bdde98f8fa5638b11b15eb72e3428e5f384940c Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Tue, 25 Jul 2023 12:10:37 -0500 Subject: [PATCH] TrendsDB: fix parameterized queries --- src/trends.ts | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/trends.ts b/src/trends.ts index 15b80d7..f35ddea 100644 --- a/src/trends.ts +++ b/src/trends.ts @@ -17,15 +17,18 @@ class TrendsDB { `); } - getTrendingTags(): string[] { - return this.#db.query(` + getTrendingTags(since: Date, until: Date): string[] { + return this.#db.query( + ` SELECT tag, COUNT(DISTINCT pubkey8) FROM tag_usages - WHERE inserted_at >= $1 AND inserted_at < $2 + WHERE inserted_at >= ? AND inserted_at < ? GROUP BY tag ORDER BY COUNT(DISTINCT pubkey8) DESC LIMIT 10; - `).map((row) => row[0]); + `, + [since, until], + ).map((row) => row[0]); } addTagUsage(tag: string, pubkey8: string): void { @@ -35,10 +38,10 @@ class TrendsDB { ); } - cleanupTagUsages(): void { + cleanupTagUsages(until: Date): void { this.#db.query( 'DELETE FROM tag_usages WHERE inserted_at < ?', - [new Date(Date.now() - 1000 * 60 * 60 * 24 * 7)], + [until], ); } }