Compare commits

...

7 Commits

11 changed files with 87 additions and 61 deletions

View File

@@ -66,6 +66,7 @@ pnpm run deploy
- `src/controllers`: API route handlers (titles, episodes, search, etc.) - `src/controllers`: API route handlers (titles, episodes, search, etc.)
- `src/libs`: Shared utilities and logic (AniList integration, background tasks) - `src/libs`: Shared utilities and logic (AniList integration, background tasks)
- `src/middleware`: Middleware handlers (authentication, authorization, etc.)
- `src/models`: Database schema and models - `src/models`: Database schema and models
- `src/scripts`: Utility scripts for maintenance and setup - `src/scripts`: Utility scripts for maintenance and setup
- `src/types`: TypeScript type definitions - `src/types`: TypeScript type definitions

View File

@@ -15,7 +15,7 @@ type AiringSchedule = {
id: number; id: number;
}; };
export async function getUpcomingTitlesFromAnilist(req: HonoRequest) { export async function getUpcomingTitlesFromAnilist() {
const durableObjectId = env.ANILIST_DO.idFromName("GLOBAL"); const durableObjectId = env.ANILIST_DO.idFromName("GLOBAL");
const stub = env.ANILIST_DO.get(durableObjectId); const stub = env.ANILIST_DO.get(durableObjectId);

View File

@@ -9,8 +9,8 @@ import { getUpcomingTitlesFromAnilist } from "./anilist";
const app = new Hono(); const app = new Hono();
app.post("/", async (c) => { export async function checkUpcomingTitles() {
const titles = await getUpcomingTitlesFromAnilist(c.req); const titles = await getUpcomingTitlesFromAnilist();
await Promise.allSettled( await Promise.allSettled(
titles.map(async (title) => { titles.map(async (title) => {
@@ -44,6 +44,10 @@ app.post("/", async (c) => {
}); });
}), }),
); );
}
app.post("/", async (c) => {
await checkUpcomingTitles();
return c.json(SuccessResponse, 200); return c.json(SuccessResponse, 200);
}); });

View File

@@ -30,7 +30,7 @@ export async function fetchPopularTitlesFromAnilist(
); );
break; break;
case "upcoming": case "upcoming":
data = await stub.nextSeasonPopular(next.season, next.year, limit); data = await stub.nextSeasonPopular(next.season, next.year, page, limit);
break; break;
default: default:
throw new Error(`Unknown category: ${category}`); throw new Error(`Unknown category: ${category}`);

View File

@@ -51,7 +51,7 @@ describe('requests the "/title" route', () => {
headers: new Headers({ "x-anilist-token": "asd" }), headers: new Headers({ "x-anilist-token": "asd" }),
}); });
expect(await response.json()).toMatchSnapshot(); await expect(response.json()).resolves.toMatchSnapshot();
expect(response.status).toBe(200); expect(response.status).toBe(200);
}); });
@@ -63,7 +63,7 @@ describe('requests the "/title" route', () => {
const response = await app.request("/title?id=10"); const response = await app.request("/title?id=10");
expect(await response.json()).toMatchSnapshot(); await expect(response.json()).resolves.toMatchSnapshot();
expect(response.status).toBe(200); expect(response.status).toBe(200);
}); });
@@ -75,7 +75,7 @@ describe('requests the "/title" route', () => {
const response = await app.request("/title?id=-1"); const response = await app.request("/title?id=-1");
expect(await response.json()).toEqual({ success: false }); await expect(response.json()).resolves.toEqual({ success: false });
expect(response.status).toBe(404); expect(response.status).toBe(404);
}); });
}); });

View File

@@ -1,5 +1,4 @@
import { OpenAPIHono, createRoute, z } from "@hono/zod-openapi"; import { OpenAPIHono, createRoute, z } from "@hono/zod-openapi";
import type { HonoRequest } from "hono";
import { AnilistUpdateType } from "~/libs/anilist/updateType.ts"; import { AnilistUpdateType } from "~/libs/anilist/updateType.ts";
import { maybeScheduleNextAiringEpisode } from "~/libs/maybeScheduleNextAiringEpisode"; import { maybeScheduleNextAiringEpisode } from "~/libs/maybeScheduleNextAiringEpisode";
@@ -22,7 +21,6 @@ const UpdateWatchStatusRequest = z.object({
deviceId: z.string(), deviceId: z.string(),
watchStatus: WatchStatus.nullable(), watchStatus: WatchStatus.nullable(),
titleId: AniListIdSchema, titleId: AniListIdSchema,
isRetrying: z.boolean().optional().default(false),
}); });
const route = createRoute({ const route = createRoute({
@@ -81,12 +79,8 @@ export async function updateWatchStatus(
} }
app.openapi(route, async (c) => { app.openapi(route, async (c) => {
const { const { deviceId, watchStatus, titleId } =
deviceId, await c.req.json<typeof UpdateWatchStatusRequest._type>();
watchStatus,
titleId,
isRetrying = false,
} = await c.req.json<typeof UpdateWatchStatusRequest._type>();
// Check if we should use mock data // Check if we should use mock data
const { useMockData } = await import("~/libs/useMockData"); const { useMockData } = await import("~/libs/useMockData");
if (useMockData()) { if (useMockData()) {
@@ -94,7 +88,6 @@ app.openapi(route, async (c) => {
return c.json(SuccessResponse, { status: 200 }); return c.json(SuccessResponse, { status: 200 });
} }
if (!isRetrying) {
try { try {
await updateWatchStatus(deviceId, titleId, watchStatus); await updateWatchStatus(deviceId, titleId, watchStatus);
} catch (error) { } catch (error) {
@@ -102,7 +95,6 @@ app.openapi(route, async (c) => {
console.error(error); console.error(error);
return c.json(ErrorResponse, { status: 500 }); return c.json(ErrorResponse, { status: 500 });
} }
}
const aniListToken = c.req.header("X-AniList-Token"); const aniListToken = c.req.header("X-AniList-Token");
if (aniListToken) { if (aniListToken) {

View File

@@ -12,6 +12,8 @@ import {
} from "~/libs/tasks/queueTask"; } from "~/libs/tasks/queueTask";
import { maybeUpdateLastConnectedAt } from "~/middleware/maybeUpdateLastConnectedAt"; import { maybeUpdateLastConnectedAt } from "~/middleware/maybeUpdateLastConnectedAt";
import { checkUpcomingTitles } from "./controllers/internal/upcoming-titles";
export const app = new OpenAPIHono<{ Bindings: Env }>(); export const app = new OpenAPIHono<{ Bindings: Env }>();
app.use(maybeUpdateLastConnectedAt); app.use(maybeUpdateLastConnectedAt);
@@ -82,6 +84,7 @@ export default {
case "ANILIST_UPDATES": case "ANILIST_UPDATES":
const anilistUpdateBody = const anilistUpdateBody =
message.body as QueueBody["ANILIST_UPDATES"]; message.body as QueueBody["ANILIST_UPDATES"];
console.log("queue run", message.body);
switch (anilistUpdateBody.updateType) { switch (anilistUpdateBody.updateType) {
case AnilistUpdateType.UpdateWatchStatus: case AnilistUpdateType.UpdateWatchStatus:
if (!anilistUpdateBody[AnilistUpdateType.UpdateWatchStatus]) { if (!anilistUpdateBody[AnilistUpdateType.UpdateWatchStatus]) {
@@ -120,9 +123,20 @@ export default {
}); });
}, },
async scheduled(event, env, ctx) { async scheduled(event, env, ctx) {
switch (event.cron) {
case "0 */12 * * *":
const { processDelayedTasks } = const { processDelayedTasks } =
await import("~/libs/tasks/processDelayedTasks"); await import("~/libs/tasks/processDelayedTasks");
await processDelayedTasks(env); await processDelayedTasks(env);
break;
case "0 18 * * *":
const { checkUpcomingTitles } =
await import("~/controllers/internal/upcoming-titles");
await checkUpcomingTitles();
break;
default:
throw new Error(`Unhandled cron: ${event.cron}`);
}
}, },
} satisfies ExportedHandler<Env>; } satisfies ExportedHandler<Env>;

View File

@@ -1,6 +1,7 @@
import type { TypedDocumentNode } from "@graphql-typed-document-node/core"; import type { TypedDocumentNode } from "@graphql-typed-document-node/core";
import { DurableObject } from "cloudflare:workers"; import { DurableObject } from "cloudflare:workers";
import { print } from "graphql"; import { print } from "graphql";
import { DateTime } from "luxon";
import { z } from "zod"; import { z } from "zod";
import { import {
@@ -42,7 +43,7 @@ export class AnilistDurableObject extends DurableObject {
async getTitle( async getTitle(
id: number, id: number,
userId?: string, userId?: number,
token?: string, token?: string,
): Promise<Title | null> { ): Promise<Title | null> {
const promises: Promise<any>[] = [ const promises: Promise<any>[] = [
@@ -99,7 +100,7 @@ export class AnilistDurableObject extends DurableObject {
}); });
return data?.Media; return data?.Media;
}, },
60 * 60 * 1000, DateTime.now().plus({ hours: 1 }),
); );
} }
@@ -114,7 +115,7 @@ export class AnilistDurableObject extends DurableObject {
}); });
return data?.Page; return data?.Page;
}, },
60 * 60 * 1000, DateTime.now().plus({ hours: 1 }),
); );
} }
@@ -127,8 +128,7 @@ export class AnilistDurableObject extends DurableObject {
) { ) {
return this.handleCachedRequest( return this.handleCachedRequest(
`popular:${JSON.stringify({ season, seasonYear, nextSeason, nextYear, limit })}`, `popular:${JSON.stringify({ season, seasonYear, nextSeason, nextYear, limit })}`,
async () => { () => {
console.log(nextSeason, nextYear, print(BrowsePopularQuery));
return this.fetchFromAnilist(BrowsePopularQuery, { return this.fetchFromAnilist(BrowsePopularQuery, {
season, season,
seasonYear, seasonYear,
@@ -137,21 +137,27 @@ export class AnilistDurableObject extends DurableObject {
limit, limit,
}); });
}, },
24 * 60 * 60 * 1000, DateTime.now().plus({ days: 1 }),
); );
} }
async nextSeasonPopular(nextSeason: any, nextYear: number, limit: number) { async nextSeasonPopular(
nextSeason: any,
nextYear: number,
page: number,
limit: number,
) {
return this.handleCachedRequest( return this.handleCachedRequest(
`next_season:${JSON.stringify({ nextSeason, nextYear, limit })}`, `next_season:${JSON.stringify({ nextSeason, nextYear, page, limit })}`,
async () => { async () => {
return this.fetchFromAnilist(NextSeasonPopularQuery, { return this.fetchFromAnilist(NextSeasonPopularQuery, {
nextSeason, nextSeason,
nextYear, nextYear,
limit, limit,
}); page,
}).then((data) => data?.Page);
}, },
24 * 60 * 60 * 1000, DateTime.now().plus({ days: 1 }),
); );
} }
@@ -164,15 +170,14 @@ export class AnilistDurableObject extends DurableObject {
return this.handleCachedRequest( return this.handleCachedRequest(
`popular:${JSON.stringify({ page, limit, season, seasonYear })}`, `popular:${JSON.stringify({ page, limit, season, seasonYear })}`,
async () => { async () => {
const data = await this.fetchFromAnilist(GetPopularTitlesQuery, { return this.fetchFromAnilist(GetPopularTitlesQuery, {
page, page,
limit, limit,
season, season,
seasonYear, seasonYear,
}); }).then((data) => data?.Page);
return data?.Page;
}, },
24 * 60 * 60 * 1000, DateTime.now().plus({ days: 1 }),
); );
} }
@@ -186,7 +191,7 @@ export class AnilistDurableObject extends DurableObject {
}); });
return data?.Page; return data?.Page;
}, },
24 * 60 * 60 * 1000, DateTime.now().plus({ days: 1 }),
); );
} }
@@ -205,7 +210,7 @@ export class AnilistDurableObject extends DurableObject {
}); });
return data?.Page; return data?.Page;
}, },
24 * 60 * 60 * 1000, DateTime.now().plus({ days: 1 }),
); );
} }
@@ -213,10 +218,10 @@ export class AnilistDurableObject extends DurableObject {
return this.handleCachedRequest( return this.handleCachedRequest(
`user:${token}`, `user:${token}`,
async () => { async () => {
const data = await this.fetchFromAnilist(GetUserQuery, {}, token); const data = await this.fetchFromAnilist(GetUserQuery, {}, { token });
return data?.Viewer; return data?.Viewer;
}, },
60 * 60 * 24 * 30 * 1000, DateTime.now().plus({ days: 30 }),
); );
} }
@@ -227,11 +232,11 @@ export class AnilistDurableObject extends DurableObject {
const data = await this.fetchFromAnilist( const data = await this.fetchFromAnilist(
GetUserProfileQuery, GetUserProfileQuery,
{ token }, { token },
token, { token },
); );
return data?.Viewer; return data?.Viewer;
}, },
60 * 60 * 24 * 30 * 1000, DateTime.now().plus({ days: 30 }),
); );
} }
@@ -243,7 +248,7 @@ export class AnilistDurableObject extends DurableObject {
const data = await this.fetchFromAnilist( const data = await this.fetchFromAnilist(
MarkEpisodeAsWatchedMutation, MarkEpisodeAsWatchedMutation,
{ titleId, episodeNumber }, { titleId, episodeNumber },
token, { token },
); );
return data?.SaveMediaListEntry; return data?.SaveMediaListEntry;
} }
@@ -252,7 +257,7 @@ export class AnilistDurableObject extends DurableObject {
const data = await this.fetchFromAnilist( const data = await this.fetchFromAnilist(
MarkTitleAsWatchedMutation, MarkTitleAsWatchedMutation,
{ titleId }, { titleId },
token, { token },
); );
return data?.SaveMediaListEntry; return data?.SaveMediaListEntry;
} }
@@ -261,7 +266,7 @@ export class AnilistDurableObject extends DurableObject {
async handleCachedRequest<T>( async handleCachedRequest<T>(
key: string, key: string,
fetcher: () => Promise<T>, fetcher: () => Promise<T>,
ttl?: number | ((data: T) => number | undefined), ttl?: DateTime | ((data: T) => DateTime | undefined),
) { ) {
const cache = await this.state.storage.get(key); const cache = await this.state.storage.get(key);
console.debug(`Retrieving request ${key} from cache:`, cache != null); console.debug(`Retrieving request ${key} from cache:`, cache != null);
@@ -273,9 +278,8 @@ export class AnilistDurableObject extends DurableObject {
await this.state.storage.put(key, result); await this.state.storage.put(key, result);
const calculatedTtl = typeof ttl === "function" ? ttl(result) : ttl; const calculatedTtl = typeof ttl === "function" ? ttl(result) : ttl;
if (calculatedTtl) {
if (calculatedTtl && calculatedTtl > 0) { const alarmTime = calculatedTtl.toMillis();
const alarmTime = Date.now() + calculatedTtl;
await this.state.storage.setAlarm(alarmTime); await this.state.storage.setAlarm(alarmTime);
await this.state.storage.put(`alarm:${key}`, alarmTime); await this.state.storage.put(`alarm:${key}`, alarmTime);
} }
@@ -286,11 +290,13 @@ export class AnilistDurableObject extends DurableObject {
async alarm() { async alarm() {
const now = Date.now(); const now = Date.now();
const alarms = await this.state.storage.list({ prefix: "alarm:" }); const alarms = await this.state.storage.list({ prefix: "alarm:" });
console.debug(`Retrieved alarms from cache:`, Object.entries(alarms));
for (const [key, ttl] of Object.entries(alarms)) { for (const [key, ttl] of Object.entries(alarms)) {
if (now >= ttl) { if (now >= ttl) {
// The key in alarms is `alarm:${storageKey}` // The key in alarms is `alarm:${storageKey}`
// We want to delete the storageKey // We want to delete the storageKey
const storageKey = key.replace("alarm:", ""); const storageKey = key.replace("alarm:", "");
console.debug(`Deleting storage key ${storageKey} & alarm ${key}`);
await this.state.storage.delete(storageKey); await this.state.storage.delete(storageKey);
await this.state.storage.delete(key); await this.state.storage.delete(key);
} }
@@ -300,8 +306,11 @@ export class AnilistDurableObject extends DurableObject {
async fetchFromAnilist<Result = any, Variables = any>( async fetchFromAnilist<Result = any, Variables = any>(
query: TypedDocumentNode<Result, Variables>, query: TypedDocumentNode<Result, Variables>,
variables: Variables, variables: Variables,
token?: string | undefined, {
): Promise<Result> { token,
shouldRetryOnRateLimit = true,
}: { token?: string | undefined; shouldRetryOnRateLimit?: boolean } = {},
): Promise<Result | undefined> {
const headers: any = { const headers: any = {
"Content-Type": "application/json", "Content-Type": "application/json",
}; };
@@ -331,14 +340,17 @@ export class AnilistDurableObject extends DurableObject {
}); });
// 1. Handle Rate Limiting (429) // 1. Handle Rate Limiting (429)
if (response.status === 429) { if (shouldRetryOnRateLimit && response.status === 429) {
const retryAfter = await response const retryAfter = await response
.json() .json<{ headers: Record<string, string> }>()
.then(({ headers }) => new Headers(headers).get("Retry-After")); .then(({ headers }) => new Headers(headers).get("Retry-After"));
console.log("429, retrying in", retryAfter); console.log("429, retrying in", retryAfter);
await sleep(Number(retryAfter || 1) * 1000); // specific fallback or ensure logic await sleep(Number(retryAfter || 1) * 1000); // specific fallback or ensure logic
return this.fetchFromAnilist(query, variables, token); return this.fetchFromAnilist(query, variables, {
token,
shouldRetryOnRateLimit: false,
});
} }
// 2. Handle HTTP Errors (like 404 or 500) // 2. Handle HTTP Errors (like 404 or 500)

View File

@@ -259,8 +259,9 @@ export const NextSeasonPopularQuery = graphql(
$nextSeason: MediaSeason $nextSeason: MediaSeason
$nextYear: Int $nextYear: Int
$limit: Int! $limit: Int!
$page: Int!
) { ) {
Page(page: 1, perPage: $limit) { Page(page: $page, perPage: $limit) {
media( media(
season: $nextSeason season: $nextSeason
seasonYear: $nextYear seasonYear: $nextYear

View File

@@ -3,11 +3,13 @@ import mapKeys from "lodash.mapkeys";
import { Case, changeStringCase } from "../changeStringCase"; import { Case, changeStringCase } from "../changeStringCase";
export function getAdminSdkCredentials(env: Cloudflare.Env = cloudflareEnv) { export function getAdminSdkCredentials(
env: Cloudflare.Env = cloudflareEnv,
): AdminSdkCredentials {
return mapKeys( return mapKeys(
JSON.parse(env.ADMIN_SDK_JSON) as AdminSdkCredentials, JSON.parse(env.ADMIN_SDK_JSON) as AdminSdkCredentials,
(_, key) => changeStringCase(key, Case.snake_case, Case.camelCase), (_, key) => changeStringCase(key, Case.snake_case, Case.camelCase),
); ) satisfies AdminSdkCredentials;
} }
export interface AdminSdkCredentials { export interface AdminSdkCredentials {

View File

@@ -67,7 +67,7 @@ id = "c8db249d8ee7462b91f9c374321776e4"
preview_id = "ff38240eb2aa4b1388c705f4974f5aec" preview_id = "ff38240eb2aa4b1388c705f4974f5aec"
[triggers] [triggers]
crons = ["0 */12 * * *"] crons = ["0 */12 * * *", "0 18 * * *"]
[[d1_databases]] [[d1_databases]]
binding = "DB" binding = "DB"