feat: delete message id when title no longer airing

This commit is contained in:
2024-09-19 15:31:14 -04:00
parent 027e8eaac5
commit 1ce79ed17a
4 changed files with 34 additions and 11 deletions

View File

@@ -2,6 +2,7 @@ import { OpenAPIHono, createRoute, z } from "@hono/zod-openapi";
import { Client } from "@upstash/qstash"; import { Client } from "@upstash/qstash";
import { env } from "hono/adapter"; import { env } from "hono/adapter";
import { deleteMessageIdForTitle } from "~/libs/deleteMessageIdForTitle";
import { maybeScheduleNextAiringEpisode } from "~/libs/maybeScheduleNextAiringEpisode"; import { maybeScheduleNextAiringEpisode } from "~/libs/maybeScheduleNextAiringEpisode";
import { verifyQstashHeader } from "~/libs/qstash/verifyQstashHeader"; import { verifyQstashHeader } from "~/libs/qstash/verifyQstashHeader";
import { readEnvVariable } from "~/libs/readEnvVariable"; import { readEnvVariable } from "~/libs/readEnvVariable";
@@ -95,20 +96,10 @@ app.openapi(route, async (c) => {
titleId, titleId,
); );
} else if (wasDeleted) { } else if (wasDeleted) {
const messageId = await getTitleMessage( await deleteMessageIdForTitle(
env<Env, typeof c>(c, "workerd"), env<Env, typeof c>(c, "workerd"),
titleId, titleId,
); );
if (messageId) {
try {
await client.messages.delete(messageId);
} catch (error) {
if (!error.message.contains("not found")) {
throw error;
}
}
await deleteTitleMessage(env<Env, typeof c>(c, "workerd"), titleId);
}
} }
} catch (error) { } catch (error) {
console.error(new Error("Error setting watch status", { cause: error })); console.error(new Error("Error setting watch status", { cause: error }));

View File

@@ -0,0 +1,24 @@
import { Client } from "@upstash/qstash";
import { deleteTitleMessage, getTitleMessage } from "~/models/titleMessages";
import type { Env } from "~/types/env";
import { readEnvVariable } from "./readEnvVariable";
export async function deleteMessageIdForTitle(env: Env, titleId: number) {
const messageId = await getTitleMessage(env, titleId);
if (!messageId) {
return;
}
try {
const client = new Client({ token: readEnvVariable(env, "QSTASH_TOKEN") });
await client.messages.delete(messageId);
} catch (error) {
if (!error.message.includes("not found")) {
throw error;
}
}
await deleteTitleMessage(env, titleId);
}

View File

@@ -5,6 +5,7 @@ import { setTitleMessage } from "~/models/titleMessages";
import type { Env } from "~/types/env"; import type { Env } from "~/types/env";
import { getNextEpisodeTimeUntilAiring } from "./anilist/getNextEpisodeAiringAt"; import { getNextEpisodeTimeUntilAiring } from "./anilist/getNextEpisodeAiringAt";
import { deleteMessageIdForTitle } from "./deleteMessageIdForTitle";
import { getCurrentDomain } from "./getCurrentDomain"; import { getCurrentDomain } from "./getCurrentDomain";
export async function maybeScheduleNextAiringEpisode( export async function maybeScheduleNextAiringEpisode(
@@ -14,6 +15,7 @@ export async function maybeScheduleNextAiringEpisode(
) { ) {
const nextAiring = await getNextEpisodeTimeUntilAiring(aniListId); const nextAiring = await getNextEpisodeTimeUntilAiring(aniListId);
if (!nextAiring) { if (!nextAiring) {
await deleteMessageIdForTitle(env, aniListId);
return; return;
} }

View File

@@ -8,14 +8,20 @@ export function getTestEnvVariables(): Omit<Env, "Bindings" | "Variables"> {
export function getTestEnv({ export function getTestEnv({
ADMIN_SDK_JSON = '{"clientEmail": "test@test.com"}', ADMIN_SDK_JSON = '{"clientEmail": "test@test.com"}',
ENABLE_ANIFY = "true", ENABLE_ANIFY = "true",
QSTASH_CURRENT_SIGNING_KEY = "123",
QSTASH_NEXT_SIGNING_KEY = "123",
QSTASH_URL = "https://qstash.com", QSTASH_URL = "https://qstash.com",
QSTASH_TOKEN = "123",
TURSO_AUTH_TOKEN = "123", TURSO_AUTH_TOKEN = "123",
TURSO_URL = "http://127.0.0.1:3001", TURSO_URL = "http://127.0.0.1:3001",
}: Partial<Env> = {}): Env { }: Partial<Env> = {}): Env {
return { return {
ADMIN_SDK_JSON, ADMIN_SDK_JSON,
ENABLE_ANIFY, ENABLE_ANIFY,
QSTASH_CURRENT_SIGNING_KEY,
QSTASH_NEXT_SIGNING_KEY,
QSTASH_URL, QSTASH_URL,
QSTASH_TOKEN,
TURSO_AUTH_TOKEN, TURSO_AUTH_TOKEN,
TURSO_URL, TURSO_URL,
}; };