Better movie/serie watchlist types

This commit is contained in:
Zoe Roux 2025-04-07 09:47:42 +02:00
parent 83d8462003
commit bf361a79d1
No known key found for this signature in database
4 changed files with 44 additions and 33 deletions

View File

@ -18,7 +18,7 @@ import {
processLanguages, processLanguages,
} from "~/models/utils"; } from "~/models/utils";
import { desc } from "~/models/utils/descriptions"; import { desc } from "~/models/utils/descriptions";
import { WatchStatus } from "~/models/watchlist"; import { MovieWatchStatus, SerieWatchStatus } from "~/models/watchlist";
import { getShows, showFilters, showSort, watchStatusQ } from "./shows/logic"; import { getShows, showFilters, showSort, watchStatusQ } from "./shows/logic";
async function setWatchStatus({ async function setWatchStatus({
@ -26,8 +26,8 @@ async function setWatchStatus({
status, status,
userId, userId,
}: { }: {
showFilter?: SQL; showFilter: { id: SQL; kind: "movie" | "serie" };
status: Omit<WatchStatus, "percent">; status: SerieWatchStatus;
userId: string; userId: string;
}) { }) {
const profileQ = db const profileQ = db
@ -38,7 +38,7 @@ async function setWatchStatus({
const showQ = db const showQ = db
.select({ pk: shows.pk }) .select({ pk: shows.pk })
.from(shows) .from(shows)
.where(showFilter) .where(and(showFilter.id, eq(shows.kind, showFilter.kind)))
.as("showQ"); .as("showQ");
return await db return await db
@ -55,7 +55,12 @@ async function setWatchStatus({
"profilePk", "profilePk",
"showPk", "showPk",
"createdAt", "createdAt",
"seenCount",
]), ]),
// do not reset movie's progress during drop
...(showFilter.kind === "movie" && status.status !== "dropped"
? { seenCount: sql`excluded.seen_count` }
: {}),
}, },
}) })
.returning(); .returning();
@ -177,10 +182,10 @@ export const watchlistH = new Elysia({ tags: ["profiles"] })
"/series/:id/watchstatus", "/series/:id/watchstatus",
async ({ params: { id }, body, jwt: { sub } }) => { async ({ params: { id }, body, jwt: { sub } }) => {
return await setWatchStatus({ return await setWatchStatus({
showFilter: and( showFilter: {
eq(shows.kind, "serie"), kind: "serie",
isUuid(id) ? eq(shows.id, id) : eq(shows.slug, id), id: isUuid(id) ? eq(shows.id, id) : eq(shows.slug, id),
), },
userId: sub, userId: sub,
status: body, status: body,
}); });
@ -193,9 +198,9 @@ export const watchlistH = new Elysia({ tags: ["profiles"] })
example: madeInAbyss.slug, example: madeInAbyss.slug,
}), }),
}), }),
body: t.Omit(WatchStatus, ["percent"]), body: SerieWatchStatus,
response: { response: {
201: t.Union([t.Omit(WatchStatus, ["percent"]), DbMetadata]), 201: t.Union([SerieWatchStatus, DbMetadata]),
}, },
permissions: ["core.read"], permissions: ["core.read"],
}, },
@ -204,13 +209,17 @@ export const watchlistH = new Elysia({ tags: ["profiles"] })
"/movies/:id/watchstatus", "/movies/:id/watchstatus",
async ({ params: { id }, body, jwt: { sub } }) => { async ({ params: { id }, body, jwt: { sub } }) => {
return await setWatchStatus({ return await setWatchStatus({
showFilter: and( showFilter: {
eq(shows.kind, "movie"), kind: "movie",
isUuid(id) ? eq(shows.id, id) : eq(shows.slug, id), id: isUuid(id) ? eq(shows.id, id) : eq(shows.slug, id),
), },
userId: sub, userId: sub,
// for movies, watch-percent is stored in `seenCount`. status: {
status: { ...body, seenCount: body.status === "completed" ? 100 : 0 }, ...body,
startedAt: body.completedAt,
// for movies, watch-percent is stored in `seenCount`.
seenCount: body.status === "completed" ? 100 : 0,
},
}); });
}, },
{ {
@ -221,12 +230,9 @@ export const watchlistH = new Elysia({ tags: ["profiles"] })
example: bubble.slug, example: bubble.slug,
}), }),
}), }),
body: t.Omit(WatchStatus, ["seenCount", "percent"]), body: t.Omit(MovieWatchStatus, ["percent"]),
response: { response: {
201: t.Union([ 201: t.Union([MovieWatchStatus, DbMetadata]),
t.Omit(WatchStatus, ["seenCount", "percent"]),
DbMetadata,
]),
}, },
permissions: ["core.read"], permissions: ["core.read"],
}, },

View File

@ -16,7 +16,7 @@ import {
} from "./utils"; } from "./utils";
import { Original } from "./utils/original"; import { Original } from "./utils/original";
import { EmbeddedVideo } from "./video"; import { EmbeddedVideo } from "./video";
import { WatchStatus } from "./watchlist"; import { MovieWatchStatus } from "./watchlist";
export const MovieStatus = t.UnionEnum(["unknown", "finished", "planned"]); export const MovieStatus = t.UnionEnum(["unknown", "finished", "planned"]);
export type MovieStatus = typeof MovieStatus.static; export type MovieStatus = typeof MovieStatus.static;
@ -56,7 +56,7 @@ export const Movie = t.Intersect([
t.Object({ t.Object({
original: Original, original: Original,
isAvailable: t.Boolean(), isAvailable: t.Boolean(),
watchStatus: t.Nullable(t.Omit(WatchStatus, ["seenCount"])), watchStatus: t.Nullable(MovieWatchStatus),
}), }),
]); ]);
export type Movie = Prettify<typeof Movie.static>; export type Movie = Prettify<typeof Movie.static>;

View File

@ -17,7 +17,7 @@ import {
TranslationRecord, TranslationRecord,
} from "./utils"; } from "./utils";
import { Original } from "./utils/original"; import { Original } from "./utils/original";
import { WatchStatus } from "./watchlist"; import { SerieWatchStatus } from "./watchlist";
export const SerieStatus = t.UnionEnum([ export const SerieStatus = t.UnionEnum([
"unknown", "unknown",
@ -71,7 +71,7 @@ export const Serie = t.Intersect([
availableCount: t.Integer({ availableCount: t.Integer({
description: "The number of episodes that can be played right away", description: "The number of episodes that can be played right away",
}), }),
watchStatus: t.Nullable(t.Omit(WatchStatus, ["percent"])), watchStatus: t.Nullable(SerieWatchStatus),
}), }),
]); ]);
export type Serie = Prettify<typeof Serie.static>; export type Serie = Prettify<typeof Serie.static>;

View File

@ -36,20 +36,25 @@ export const WatchlistStatus = t.UnionEnum([
"planned", "planned",
]); ]);
export const WatchStatus = t.Object({ export const SerieWatchStatus = t.Object({
status: WatchlistStatus, status: WatchlistStatus,
score: t.Nullable(t.Integer({ minimum: 0, maximum: 100 })), score: t.Nullable(t.Integer({ minimum: 0, maximum: 100 })),
startedAt: t.Nullable(t.String({ format: "date-time" })), startedAt: t.Nullable(t.String({ format: "date-time" })),
completedAt: t.Nullable(t.String({ format: "date-time" })), completedAt: t.Nullable(t.String({ format: "date-time" })),
// only for series
seenCount: t.Integer({ seenCount: t.Integer({
description: "The number of episodes you watched in this serie.", description: "The number of episodes you watched in this serie.",
minimum: 0, minimum: 0,
}), }),
// only for movies
percent: t.Integer({
minimum: 0,
maximum: 100,
}),
}); });
export type WatchStatus = typeof WatchStatus.static; export type SerieWatchStatus = typeof SerieWatchStatus.static;
export const MovieWatchStatus = t.Intersect([
t.Omit(SerieWatchStatus, ["startedAt", "seenCount"]),
t.Object({
percent: t.Integer({
minimum: 0,
maximum: 100,
}),
}),
]);
export type MovieWatchStatus = typeof MovieWatchStatus.static;