Skip to content

Commit

Permalink
Skip quote generation if no-one has viewed the current quote
Browse files Browse the repository at this point in the history
  • Loading branch information
jollytoad committed Mar 13, 2024
1 parent 7ac622e commit 1f4b555
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
5 changes: 4 additions & 1 deletion deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions routes/quote/_cron/generate_quote.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { isQuoteUnseen } from "../_lib/quote_store.ts";
import { setQuote } from "../_lib/quote_store.ts";
import OpenAI from "npm:openai";

Expand All @@ -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();
Expand All @@ -24,6 +30,7 @@ export default async function generateQuote() {
const content = completion?.choices[0]?.message.content;

if (content) {
console.log(content);
await setQuote(content);
}
}
Expand Down
11 changes: 9 additions & 2 deletions routes/quote/_lib/quote_store.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import * as store from "$store";

export async function getQuote() {
return await store.getItem<string>(["quote"]) ?? "Nothing to see here";
export async function getQuote(): Promise<string> {
const quote = await store.getItem<string>(["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<boolean> {
return await store.getItem(["quote", "unseen"]) ?? false;
}

0 comments on commit 1f4b555

Please sign in to comment.