From 45cc0cd36ac5e500e1d39d77e8986df42f5b1df2 Mon Sep 17 00:00:00 2001 From: Rushil Perera Date: Fri, 13 Sep 2024 12:06:42 -0400 Subject: [PATCH] feat: cancel "new episode" route early if no user is watching the title anymore --- src/controllers/internal/new-episode/index.ts | 10 +++++++++- src/models/watchStatus.ts | 8 ++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/controllers/internal/new-episode/index.ts b/src/controllers/internal/new-episode/index.ts index 58770d4..4a5f605 100644 --- a/src/controllers/internal/new-episode/index.ts +++ b/src/controllers/internal/new-episode/index.ts @@ -15,6 +15,7 @@ import { maybeScheduleNextAiringEpisode } from "~/libs/maybeScheduleNextAiringEp import { verifyQstashHeader } from "~/libs/qstash/verifyQstashHeader"; import { readEnvVariable } from "~/libs/readEnvVariable"; import { getTokensSubscribedToTitle } from "~/models/token"; +import { isWatchingTitle } from "~/models/watchStatus"; import type { Env } from "~/types/env"; import type { EpisodesResponseSchema } from "~/types/episode"; import type { FetchUrlResponse } from "~/types/episode/fetch-url-response"; @@ -49,7 +50,14 @@ app.post( return c.json(ErrorResponse, { status: 401 }); } - const domain = getCurrentDomain(c.req); + if (!(await isWatchingTitle(env(c, "workerd"), aniListId))) { + console.log(`Title ${aniListId} is no longer being watched`); + return c.json( + { success: true, result: { isNoLongerWatching: true } }, + 200, + ); + } + const isAnifyEnabled = readEnvVariable( env(c, "workerd"), "ENABLE_ANIFY", diff --git a/src/models/watchStatus.ts b/src/models/watchStatus.ts index d6b60ee..d5b55dc 100644 --- a/src/models/watchStatus.ts +++ b/src/models/watchStatus.ts @@ -57,3 +57,11 @@ function removeTitle(env: Env, deviceId: string, titleId: number) { ), ); } + +export function isWatchingTitle(env: Env, titleId: number) { + return getDb(env) + .select() + .from(watchStatusTable) + .where(eq(watchStatusTable.titleId, titleId)) + .then((results) => results.length > 0); +}