From 8f02f5ef567e188c4786a2da9c2f7c85d775214d Mon Sep 17 00:00:00 2001 From: Pinapelz Date: Thu, 25 Jun 2026 16:13:47 -0700 Subject: add guard to guarantee no repeats for last 30 days --- server/shared.ts | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/server/shared.ts b/server/shared.ts index b3fad21..003e4df 100644 --- a/server/shared.ts +++ b/server/shared.ts @@ -58,6 +58,32 @@ export function getDailySong(date: string): Song { return songs[index]; } +function getLastNDates(n: number): string[] { + const dates: string[] = []; + for (let i = 1; i <= n; i++) { + const d = new Date(); + d.setUTCDate(d.getUTCDate() - i); + dates.push(d.toISOString().slice(0, 10)); + } + return dates; +} + +export function getDailySongNoRepeat(): Song { + const today = getUtcDate(); + const recentSongs = new Set( + getLastNDates(30).map(d => getDailySong(d).youtubeId) + ); + let candidate = getDailySong(today); + let guard = 0; + while (recentSongs.has(candidate.youtubeId) && guard < songs.length) { + const rerollSeed = hashString(today + ":" + guard); + const index = rerollSeed % songs.length; + candidate = songs[index]; + guard++; + } + return candidate; +} + function signValue(prefix: string, value: string): string { return createHmac("sha256", getSigningSecret()) .update(`${prefix}:${value}`) -- cgit v1.2.3