feat: create route to handle updating watch status in AniList

This commit is contained in:
2024-06-12 09:33:55 -04:00
parent 09ffff7c56
commit 0a859e0f16
8 changed files with 284 additions and 10 deletions

View File

@@ -0,0 +1,30 @@
import { graphql } from "gql.tada";
import { GraphQLClient } from "graphql-request";
import type { WatchStatus } from "~/types/title";
const UpdateWatchStatusQuery = graphql(`
mutation UpdateWatchStatus($titleId: Int!, $watchStatus: MediaListStatus!) {
SaveMediaListEntry(mediaId: $titleId, status: $watchStatus) {
id
}
}
`);
/** Updates the watch status for a title on Anilist. If the token is null, the watch status will not be updated. */
export async function maybeUpdateWatchStatusOnAnilist(
titleId: number,
watchStatus: WatchStatus,
aniListToken: string | undefined,
) {
if (!aniListToken) {
return;
}
const client = new GraphQLClient("https://graphql.anilist.co/");
const headers = new Headers({ Authorization: `Bearer ${aniListToken}` });
return client
.request(UpdateWatchStatusQuery, { titleId, watchStatus }, headers)
.then((data) => !!data?.SaveMediaListEntry?.id);
}

View File

@@ -0,0 +1,99 @@
import { beforeEach, describe, expect, it } from "bun:test";
import app from "~/index";
import { server } from "~/mocks";
import { getDb, resetDb } from "~/models/db";
import { tokenTable } from "~/models/schema";
server.listen();
console.error = () => {};
describe("requests the /watch-status route", () => {
const db = getDb({
TURSO_URL: "http://127.0.0.1:3000",
TURSO_AUTH_TOKEN: "asd",
});
beforeEach(async () => {
await resetDb();
});
it("saving title, deviceId in db, should succeed", async () => {
await db.insert(tokenTable).values({ deviceId: "123", token: "asd" });
const res = await app.request(
"/watch-status",
{
method: "POST",
headers: new Headers({
"x-anilist-token": "asd",
"Content-Type": "application/json",
}),
body: JSON.stringify({
deviceId: "123",
watchStatus: "CURRENT",
titleId: 10,
}),
},
{
TURSO_URL: process.env.TURSO_URL,
TURSO_AUTH_TOKEN: process.env.TURSO_AUTH_TOKEN,
},
);
expect(res.json()).resolves.toEqual({ success: true });
expect(res.status).toBe(200);
});
it("saving title, deviceId not in db, should fail", async () => {
const res = await app.request(
"/watch-status",
{
method: "POST",
headers: new Headers({
"x-anilist-token": "asd",
"Content-Type": "application/json",
}),
body: JSON.stringify({
deviceId: "123",
watchStatus: "CURRENT",
titleId: 10,
}),
},
{
TURSO_URL: process.env.TURSO_URL,
TURSO_AUTH_TOKEN: process.env.TURSO_AUTH_TOKEN,
},
);
expect(res.json()).resolves.toEqual({ success: false });
expect(res.status).toBe(500);
});
it("saving title, Anilist request fails, should succeed", async () => {
await db.insert(tokenTable).values({ deviceId: "123", token: "asd" });
const res = await app.request(
"/watch-status",
{
method: "POST",
headers: new Headers({
"x-anilist-token": "asd",
"Content-Type": "application/json",
}),
body: JSON.stringify({
deviceId: "123",
watchStatus: "CURRENT",
titleId: -1,
}),
},
{
TURSO_URL: process.env.TURSO_URL,
TURSO_AUTH_TOKEN: process.env.TURSO_AUTH_TOKEN,
},
);
expect(res.json()).resolves.toEqual({ success: true });
expect(res.status).toBe(200);
});
});

View File

@@ -0,0 +1,96 @@
import { OpenAPIHono, createRoute, z } from "@hono/zod-openapi";
import { env } from "hono/adapter";
import { setWatchStatus } from "~/models/watchStatus";
import type { Env } from "~/types/env";
import {
AniListIdSchema,
ErrorResponse,
SuccessResponse,
} from "~/types/schema";
import { WatchStatus } from "~/types/title/watchStatus";
import { maybeUpdateWatchStatusOnAnilist } from "./anilist";
const app = new OpenAPIHono<Env>();
const UpdateWatchStatusRequest = z.object({
deviceId: z.string(),
watchStatus: WatchStatus,
titleId: AniListIdSchema,
isRetrying: z.boolean().optional().default(false),
});
const route = createRoute({
tags: ["aniplay", "title"],
operationId: "updateWatchStatus",
summary: "Update watch status for a title",
description:
"Updates the watch status for a title. If the user sets the watch status to 'watching', they'll start getting notified about new episodes.",
method: "post",
path: "/",
request: {
body: {
content: {
"application/json": {
schema: UpdateWatchStatusRequest,
},
},
},
headers: z.object({ "x-anilist-token": z.string().nullish() }),
},
responses: {
200: {
content: {
"application/json": {
schema: z.boolean(),
},
},
description: "Watch status was successfully updated",
},
500: {
content: {
"application/json": {
schema: z.boolean(),
},
},
description: "Failed to update watch status",
},
},
});
app.openapi(route, async (c) => {
const { deviceId, watchStatus, titleId, isRetrying } =
await c.req.json<typeof UpdateWatchStatusRequest._type>();
const aniListToken = c.req.header("X-AniList-Token");
if (!isRetrying) {
try {
const { wasAdded, wasDeleted } = await setWatchStatus(
env<Env, typeof c>(c, "workerd"),
deviceId,
Number(titleId),
watchStatus,
);
} catch (error) {
console.error(new Error("Error setting watch status", { cause: error }));
return c.json(ErrorResponse, { status: 500 });
}
}
try {
await maybeUpdateWatchStatusOnAnilist(
Number(titleId),
watchStatus,
aniListToken,
);
} catch (error) {
console.error(
new Error("Failed to update watch status on Anilist", { cause: error }),
);
}
return c.json(SuccessResponse, { status: 200 });
});
export default app;