From 1f4b555a5e7e158e1c678958f2eba7c584d21ecb Mon Sep 17 00:00:00 2001 From: Mark Gibson Date: Wed, 13 Mar 2024 15:54:07 +0000 Subject: [PATCH] Skip quote generation if no-one has viewed the current quote --- deno.lock | 5 ++++- routes/quote/_cron/generate_quote.ts | 7 +++++++ routes/quote/_lib/quote_store.ts | 11 +++++++++-- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/deno.lock b/deno.lock index e2dd7fe..9a2dc68 100644 --- a/deno.lock +++ b/deno.lock @@ -128,7 +128,10 @@ ] }, "@std/path@0.219.1": { - "integrity": "e5c0ffef3a8ef2b48e9e3d88a1489320e8fb2cc7be767b17c91a1424ffb4c8ed" + "integrity": "e5c0ffef3a8ef2b48e9e3d88a1489320e8fb2cc7be767b17c91a1424ffb4c8ed", + "dependencies": [ + "jsr:@std/assert@^0.219.1" + ] }, "@std/streams@0.219.1": { "integrity": "6f5dac5773a4fafdbe7ee612d0a0d5a2cbe465b9c9e2c85d371877dc8a52d1d3" diff --git a/routes/quote/_cron/generate_quote.ts b/routes/quote/_cron/generate_quote.ts index bf6760b..e56384e 100644 --- a/routes/quote/_cron/generate_quote.ts +++ b/routes/quote/_cron/generate_quote.ts @@ -1,3 +1,4 @@ +import { isQuoteUnseen } from "../_lib/quote_store.ts"; import { setQuote } from "../_lib/quote_store.ts"; import OpenAI from "npm:openai"; @@ -6,6 +7,11 @@ export const name = "Generate a new quote of the moment"; export const schedule = Deno.env.get("QUOTE_SCHEDULE") ?? "*/30 * * * *"; export default async function generateQuote() { + if (await isQuoteUnseen()) { + console.log("Skipping quote generation"); + return; + } + console.log("Generating a new quote..."); const openai = new OpenAI(); @@ -24,6 +30,7 @@ export default async function generateQuote() { const content = completion?.choices[0]?.message.content; if (content) { + console.log(content); await setQuote(content); } } diff --git a/routes/quote/_lib/quote_store.ts b/routes/quote/_lib/quote_store.ts index fed08f0..b8075be 100644 --- a/routes/quote/_lib/quote_store.ts +++ b/routes/quote/_lib/quote_store.ts @@ -1,9 +1,16 @@ import * as store from "$store"; -export async function getQuote() { - return await store.getItem(["quote"]) ?? "Nothing to see here"; +export async function getQuote(): Promise { + const quote = await store.getItem(["quote"]) ?? "Nothing to see here"; + await store.removeItem(["quote", "unseen"]); + return quote; } export async function setQuote(quote: string) { await store.setItem(["quote"], quote); + await store.setItem(["quote", "unseen"], true); +} + +export async function isQuoteUnseen(): Promise { + return await store.getItem(["quote", "unseen"]) ?? false; }