From aa9476680c19c50c477724d4747aff49255e5616 Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Sun, 6 Apr 2025 23:25:53 +0200 Subject: [PATCH 01/11] Allow filter & sort by `watchStatus` --- api/src/controllers/shows/logic.ts | 55 ++++++++++++------------------ api/src/db/schema/watchlist.ts | 2 +- 2 files changed, 23 insertions(+), 34 deletions(-) diff --git a/api/src/controllers/shows/logic.ts b/api/src/controllers/shows/logic.ts index 90eb9555..96e883e4 100644 --- a/api/src/controllers/shows/logic.ts +++ b/api/src/controllers/shows/logic.ts @@ -1,20 +1,9 @@ -import { - type SQL, - type Subquery, - and, - desc, - eq, - exists, - ne, - sql, -} from "drizzle-orm"; -import type { PgSelect } from "drizzle-orm/pg-core"; +import { type SQL, and, eq, exists, ne, sql } from "drizzle-orm"; import { db } from "~/db"; import { entries, entryTranslations, entryVideoJoin, - history, profiles, showStudioJoin, showTranslations, @@ -46,8 +35,19 @@ import { sortToSql, } from "~/models/utils"; import type { EmbeddedVideo } from "~/models/video"; +import { WatchlistStatus } from "~/models/watchlist"; import { entryVideosQ, getEntryProgressQ, mapProgress } from "../entries"; +export const watchStatusQ = db + .select({ + ...getColumns(watchlist), + percent: sql`${watchlist.seenCount}`.as("percent"), + }) + .from(watchlist) + .leftJoin(profiles, eq(watchlist.profilePk, profiles.pk)) + .where(eq(profiles.id, sql.placeholder("userId"))) + .as("watchstatus"); + export const showFilters: FilterDef = { genres: { column: shows.genres, @@ -70,6 +70,11 @@ export const showFilters: FilterDef = { type: "string", isArray: true, }, + watchStatus: { + column: watchStatusQ.status, + type: "enum", + values: WatchlistStatus.enum, + }, }; export const showSort = Sort( { @@ -80,6 +85,7 @@ export const showSort = Sort( endAir: shows.endAir, createdAt: shows.createdAt, nextRefresh: shows.nextRefresh, + watchStatus: watchStatusQ.status, }, { default: ["slug"], @@ -196,11 +202,9 @@ const showRelations = { nextEntry: ({ languages, userId, - watchStatusQ, }: { languages: string[]; userId: string; - watchStatusQ: Subquery; }) => { const transQ = db .selectDistinctOn([entryTranslations.pk]) @@ -228,9 +232,7 @@ const showRelations = { .innerJoin(transQ, eq(entries.pk, transQ.pk)) .leftJoin(progressQ, eq(entries.pk, progressQ.entryPk)) .leftJoinLateral(entryVideosQ, sql`true`) - .where( - eq((watchStatusQ as unknown as typeof watchlist).nextEntry, entries.pk), - ) + .where(eq(watchStatusQ.nextEntry, entries.pk)) .as("nextEntry"); }, }; @@ -272,16 +274,6 @@ export async function getShows({ ) .as("t"); - const watchStatusQ = db - .select({ - ...getColumns(watchlist), - percent: sql`${watchlist.seenCount}`.as("percent"), - }) - .from(watchlist) - .leftJoin(profiles, eq(watchlist.profilePk, profiles.pk)) - .where(eq(profiles.id, userId)) - .as("watchstatus"); - return await db .select({ ...getColumns(shows), @@ -302,11 +294,7 @@ export async function getShows({ watchStatus: getColumns(watchStatusQ), - ...buildRelations(relations, showRelations, { - languages, - userId, - watchStatusQ, - }), + ...buildRelations(relations, showRelations, { languages, userId }), }) .from(shows) .leftJoin(watchStatusQ, eq(shows.pk, watchStatusQ.showPk)) @@ -327,5 +315,6 @@ export async function getShows({ : sortToSql(sort)), shows.pk, ) - .limit(limit); + .limit(limit) + .execute({ userId }); } diff --git a/api/src/db/schema/watchlist.ts b/api/src/db/schema/watchlist.ts index a00d7967..e3a32949 100644 --- a/api/src/db/schema/watchlist.ts +++ b/api/src/db/schema/watchlist.ts @@ -6,9 +6,9 @@ import { shows } from "./shows"; import { schema } from "./utils"; export const watchlistStatus = schema.enum("watchlist_status", [ - "completed", "watching", "rewatching", + "completed", "dropped", "planned", ]); From a7f355531d707f2c38854284f4c6292d2bd2d042 Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Sun, 6 Apr 2025 23:26:29 +0200 Subject: [PATCH 02/11] Add function to fetch user info from api --- api/.env.example | 2 +- api/src/auth.ts | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/api/.env.example b/api/.env.example index 3f5257dc..7ef45abf 100644 --- a/api/.env.example +++ b/api/.env.example @@ -10,7 +10,7 @@ JWT_SECRET= # used to verify who's making the jwt JWT_ISSUER=$PUBLIC_URL # keibi's server to retrieve the public jwt secret -AUHT_SERVER=http://auth:4568 +AUTH_SERVER=http://auth:4568 IMAGES_PATH=./images diff --git a/api/src/auth.ts b/api/src/auth.ts index 02698625..f8b3638b 100644 --- a/api/src/auth.ts +++ b/api/src/auth.ts @@ -69,3 +69,33 @@ export const auth = new Elysia({ name: "auth" }) }, }) .as("plugin"); + +const User = t.Object({ + id: t.String({ format: "uuid" }), + username: t.String(), + email: t.String({ format: "email" }), + createdDate: t.String({ format: "date-time" }), + lastSeen: t.String({ format: "date-time" }), + claims: t.Record(t.String(), t.Any()), + oidc: t.Record( + t.String(), + t.Object({ + id: t.String({ format: "uuid" }), + username: t.String(), + profileUrl: t.Nullable(t.String({ format: "url" })), + }), + ), +}); +const UserC = TypeCompiler.Compile(User); + +export async function getUserInfo( + id: string, + headers: { authorization: string }, +) { + const resp = await fetch( + new URL(`/auth/users/${id}`, process.env.AUTH_SERVER ?? "http://auth:4568"), + { headers }, + ); + + return UserC.Decode(await resp.json()); +} From 4b0100c52d3429155e09e0290a813e74d6db03ee Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Sun, 6 Apr 2025 23:26:44 +0200 Subject: [PATCH 03/11] Create watchlist read api --- api/src/base.ts | 4 +- api/src/controllers/watchlist.ts | 123 +++++++++++++++++++++++++++++++ api/src/index.ts | 4 + 3 files changed, 130 insertions(+), 1 deletion(-) create mode 100644 api/src/controllers/watchlist.ts diff --git a/api/src/base.ts b/api/src/base.ts index d6de2a49..abfd1d86 100644 --- a/api/src/base.ts +++ b/api/src/base.ts @@ -11,6 +11,7 @@ import { showsH } from "./controllers/shows/shows"; import { staffH } from "./controllers/staff"; import { studiosH } from "./controllers/studios"; import { videosH } from "./controllers/videos"; +import { watchlistH } from "./controllers/watchlist"; import type { KError } from "./models/error"; export const base = new Elysia({ name: "base" }) @@ -93,4 +94,5 @@ export const app = new Elysia({ prefix }) permissions: ["core.write"], }, (app) => app.use(videosH).use(seed), - ); + ) + .use(watchlistH); diff --git a/api/src/controllers/watchlist.ts b/api/src/controllers/watchlist.ts new file mode 100644 index 00000000..8a937134 --- /dev/null +++ b/api/src/controllers/watchlist.ts @@ -0,0 +1,123 @@ +import { and, isNotNull, isNull } from "drizzle-orm"; +import Elysia, { t } from "elysia"; +import { auth, getUserInfo } from "~/auth"; +import { shows } from "~/db/schema"; +import { KError } from "~/models/error"; +import { Show } from "~/models/show"; +import { + AcceptLanguage, + Filter, + Page, + createPage, + isUuid, + processLanguages, +} from "~/models/utils"; +import { desc } from "~/models/utils/descriptions"; +import { getShows, showFilters, showSort, watchStatusQ } from "./shows/logic"; + +export const watchlistH = new Elysia({ tags: ["profiles"] }) + .use(auth) + .guard({ + query: t.Object({ + sort: showSort, + filter: t.Optional(Filter({ def: showFilters })), + query: t.Optional(t.String({ description: desc.query })), + limit: t.Integer({ + minimum: 1, + maximum: 250, + default: 50, + description: "Max page size.", + }), + after: t.Optional(t.String({ description: desc.after })), + preferOriginal: t.Optional( + t.Boolean({ + description: desc.preferOriginal, + }), + ), + }), + response: { + 200: Page(Show), + 422: KError, + }, + }) + .get( + "/profiles/me/watchlist", + async ({ + query: { limit, after, query, sort, filter, preferOriginal }, + headers: { "accept-language": languages }, + request: { url }, + jwt: { sub }, + }) => { + const langs = processLanguages(languages); + const items = await getShows({ + limit, + after, + query, + sort, + filter: and( + isNotNull(watchStatusQ.status), + isNull(shows.collectionPk), + filter, + ), + languages: langs, + preferOriginal, + userId: sub, + }); + return createPage(items, { url, sort, limit }); + }, + { + detail: { description: "Get all movies/series in your watchlist" }, + headers: t.Object( + { + "accept-language": AcceptLanguage({ autoFallback: true }), + }, + { additionalProperties: true }, + ), + }, + ) + .get( + "/profiles/:id/watchlist", + async ({ + params: { id }, + query: { limit, after, query, sort, filter, preferOriginal }, + headers: { "accept-language": languages, authorization }, + request: { url }, + }) => { + if (!isUuid(id)) { + const uInfo = await getUserInfo(id, { authorization }); + id = uInfo.id; + } + + const langs = processLanguages(languages); + const items = await getShows({ + limit, + after, + query, + sort, + filter: and( + isNotNull(watchStatusQ.status), + isNull(shows.collectionPk), + filter, + ), + languages: langs, + preferOriginal, + userId: id, + }); + return createPage(items, { url, sort, limit }); + }, + { + detail: { description: "Get all movies/series in someone's watchlist" }, + params: t.Object({ + id: t.String({ + description: + "The id or username of the user to read the watchlist of", + example: "zoriya", + }), + }), + headers: t.Object({ + authorization: t.TemplateLiteral("Bearer ${string}"), + "accept-language": AcceptLanguage({ autoFallback: true }), + }), + permissions: ["users.read"], + }, + ); diff --git a/api/src/index.ts b/api/src/index.ts index 0926dc30..1769ddaf 100644 --- a/api/src/index.ts +++ b/api/src/index.ts @@ -61,6 +61,10 @@ new Elysia() name: "images", description: "Routes about images: posters, thumbnails...", }, + { + name: "profiles", + description: "Routes about user profiles, watchlist & history.", + }, ], components: { securitySchemes: { From 18eb1b02a3852de421a0469dc0712c6a36c60263 Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Sun, 6 Apr 2025 23:42:34 +0200 Subject: [PATCH 04/11] Add /users/:username in keibi --- .github/workflows/auth-hurl.yml | 1 + auth/dbc/users.sql.go | 16 ++++++++++++---- auth/sql/queries/users.sql | 6 ++++-- auth/tests/users.hurl | 29 +++++++++++++++++++++++++++++ auth/users.go | 28 ++++++++++++++++++---------- auth/utils.go | 12 +++++++++++- 6 files changed, 75 insertions(+), 17 deletions(-) diff --git a/.github/workflows/auth-hurl.yml b/.github/workflows/auth-hurl.yml index 6ecbed1c..061e3743 100644 --- a/.github/workflows/auth-hurl.yml +++ b/.github/workflows/auth-hurl.yml @@ -52,6 +52,7 @@ jobs: hurl --error-format long --variable host=http://localhost:4568 tests/* env: POSTGRES_SERVER: localhost + FIRST_USER_CLAIMS: '{"permissions": ["users.read"]}' - name: Show logs working-directory: ./auth diff --git a/auth/dbc/users.sql.go b/auth/dbc/users.sql.go index 17e2e903..ecc89fbb 100644 --- a/auth/dbc/users.sql.go +++ b/auth/dbc/users.sql.go @@ -175,10 +175,18 @@ select from users as u left join oidc_handle as h on u.pk = h.user_pk -where - u.id = $1 +where ($1::boolean + and u.id = $2) + or (not $1 + and u.username = $3) ` +type GetUserParams struct { + UseId bool `json:"useId"` + Id uuid.UUID `json:"id"` + Username string `json:"username"` +} + type GetUserRow struct { User User `json:"user"` Provider *string `json:"provider"` @@ -187,8 +195,8 @@ type GetUserRow struct { ProfileUrl *string `json:"profileUrl"` } -func (q *Queries) GetUser(ctx context.Context, id uuid.UUID) ([]GetUserRow, error) { - rows, err := q.db.Query(ctx, getUser, id) +func (q *Queries) GetUser(ctx context.Context, arg GetUserParams) ([]GetUserRow, error) { + rows, err := q.db.Query(ctx, getUser, arg.UseId, arg.Id, arg.Username) if err != nil { return nil, err } diff --git a/auth/sql/queries/users.sql b/auth/sql/queries/users.sql index 1f1711dd..8bc00feb 100644 --- a/auth/sql/queries/users.sql +++ b/auth/sql/queries/users.sql @@ -28,8 +28,10 @@ select from users as u left join oidc_handle as h on u.pk = h.user_pk -where - u.id = $1; +where (@use_id::boolean + and u.id = @id) + or (not @use_id + and u.username = @username); -- name: GetUserByLogin :one select diff --git a/auth/tests/users.hurl b/auth/tests/users.hurl index 8e840a12..0a8da0fa 100644 --- a/auth/tests/users.hurl +++ b/auth/tests/users.hurl @@ -15,6 +15,15 @@ HTTP 200 [Captures] jwt: jsonpath "$.token" +GET {{host}}/users/me +Authorization: Bearer {{jwt}} +HTTP 200 +[Captures] +userId: jsonpath "$.id" +[Asserts] +# this should be defined in the `FIRST_USER_CLAIMS='{"permissions": ["users.read"]}'` env var +jsonpath "$.claims.permissions" contains "users.read" + # Duplicates usernames POST {{host}}/users @@ -35,6 +44,26 @@ POST {{host}}/users } HTTP 409 +# Cannot get non-existing user +GET {{host}}/users/dont-exist +Authorization: Bearer {{jwt}} +HTTP 404 + +# Can get user by id +GET {{host}}/users/{{userId}} +Authorization: Bearer {{jwt}} +HTTP 200 +[Asserts] +jsonpath "$.username" == "user-1" + +# Can get user by username +GET {{host}}/users/user-1 +Authorization: Bearer {{jwt}} +HTTP 200 +[Asserts] +jsonpath "$.id" == {{userId}} +jsonpath "$.username" == "user-1" + DELETE {{host}}/users/me Authorization: Bearer {{jwt}} diff --git a/auth/users.go b/auth/users.go index e0bfdb79..7aef41e4 100644 --- a/auth/users.go +++ b/auth/users.go @@ -94,7 +94,7 @@ func MapOidc(oidc *dbc.GetUserRow) OidcHandle { // @Failure 422 {object} KError "Invalid after id" // @Router /users [get] func (h *Handler) ListUsers(c echo.Context) error { - err := CheckPermissions(c, []string{"user.read"}) + err := CheckPermissions(c, []string{"users.read"}) if err != nil { return err } @@ -139,19 +139,24 @@ func (h *Handler) ListUsers(c echo.Context) error { // @Failure 422 {object} KError "Invalid id (not a uuid)" // @Router /users/{id} [get] func (h *Handler) GetUser(c echo.Context) error { - err := CheckPermissions(c, []string{"user.read"}) + err := CheckPermissions(c, []string{"users.read"}) if err != nil { return err } - id, err := uuid.Parse(c.Param("id")) - if err != nil { - return echo.NewHTTPError(http.StatusUnprocessableEntity, "Invalid id") - } - dbuser, err := h.db.GetUser(context.Background(), id) + id := c.Param("id") + uid, err := uuid.Parse(c.Param("id")) + dbuser, err := h.db.GetUser(context.Background(), dbc.GetUserParams{ + UseId: err == nil, + Id: uid, + Username: id, + }) if err != nil { return err } + if len(dbuser) == 0 { + return echo.NewHTTPError(404, fmt.Sprintf("No user found with id or username: '%s'.", id)) + } user := MapDbUser(&dbuser[0].User) for _, oidc := range dbuser { @@ -177,7 +182,10 @@ func (h *Handler) GetMe(c echo.Context) error { if err != nil { return err } - dbuser, err := h.db.GetUser(context.Background(), id) + dbuser, err := h.db.GetUser(context.Background(), dbc.GetUserParams{ + UseId: true, + Id: id, + }) if err != nil { return err } @@ -246,7 +254,7 @@ func (h *Handler) Register(c echo.Context) error { // @Failure 404 {object} KError "Invalid user id" // @Router /users/{id} [delete] func (h *Handler) DeleteUser(c echo.Context) error { - err := CheckPermissions(c, []string{"user.delete"}) + err := CheckPermissions(c, []string{"users.delete"}) if err != nil { return err } @@ -348,7 +356,7 @@ func (h *Handler) EditSelf(c echo.Context) error { // @Success 422 {object} KError "Invalid body" // @Router /users/{id} [patch] func (h *Handler) EditUser(c echo.Context) error { - err := CheckPermissions(c, []string{"user.write"}) + err := CheckPermissions(c, []string{"users.write"}) if err != nil { return err } diff --git a/auth/utils.go b/auth/utils.go index 70bca2fb..e607cbee 100644 --- a/auth/utils.go +++ b/auth/utils.go @@ -68,11 +68,21 @@ func CheckPermissions(c echo.Context, perms []string) error { if !ok { return echo.NewHTTPError(403, fmt.Sprintf("Missing permissions: %s.", ", ")) } - permissions, ok := permissions_claims.([]string) + fmt.Printf("%v\n", permissions_claims) + fmt.Printf("%t\n", permissions_claims) + permissions_int, ok := permissions_claims.([]any) if !ok { return echo.NewHTTPError(403, "Invalid permission claim.") } + permissions := make([]string, len(permissions_int)) + for i, perm := range permissions_int { + permissions[i], ok = perm.(string) + if !ok { + return echo.NewHTTPError(403, "Invalid permission claim.") + } + } + missing := make([]string, 0) for _, perm := range perms { if !slices.Contains(permissions, perm) { From 3f5af4b7fa77f97e1a45b54493648c7dd6074b7a Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Sun, 6 Apr 2025 23:58:01 +0200 Subject: [PATCH 05/11] Fix `FIRST_USER_CLAIMS` --- .env.example | 2 +- auth/.env.example | 2 +- auth/config.go | 3 +-- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/.env.example b/.env.example index a4c83d29..889216c1 100644 --- a/.env.example +++ b/.env.example @@ -97,6 +97,6 @@ RABBITMQ_DEFAULT_PASS=aohohunuhouhuhhoahothonseuhaoensuthoaentsuhha # v5 stuff, does absolutely nothing on master (aka: you can delete this) EXTRA_CLAIMS='{"permissions": ["core.read"], "verified": false}' -FIRST_USER_CLAIMS='{"permissions": ["user.read", "users.write", "users.delete", "core.read"], "verified": true}' +FIRST_USER_CLAIMS='{"permissions": ["users.read", "users.write", "users.delete", "core.read"], "verified": true}' GUEST_CLAIMS='{"permissions": ["core.read"]}' PROTECTED_CLAIMS="permissions,verified" diff --git a/auth/.env.example b/auth/.env.example index 1e69fdee..c1ea11eb 100644 --- a/auth/.env.example +++ b/auth/.env.example @@ -11,7 +11,7 @@ RSA_PRIVATE_KEY_PATH="" EXTRA_CLAIMS='{}' # json object with the claims to add to every jwt of the FIRST user (this can be used to mark the first user as admin). # Those claims are merged with the `EXTRA_CLAIMS`. -FIRST_USER_CLAIMS='{}' +FIRST_USER_CLAIMS='{"permissions": ["users.read", "users.write", "users.delete"]}' # If this is not empty, calls to `/jwt` without an `Authorization` header will still create a jwt (with `null` in `sub`) GUEST_CLAIMS="" # Comma separated list of claims that users without the `user.write` permissions should NOT be able to edit diff --git a/auth/config.go b/auth/config.go index cc7ac656..b503ca30 100644 --- a/auth/config.go +++ b/auth/config.go @@ -47,14 +47,13 @@ func LoadConfiguration(db *dbc.Queries) (*Configuration, error) { return nil, err } } + maps.Insert(ret.FirstUserClaims, maps.All(ret.DefaultClaims)) claims = os.Getenv("FIRST_USER_CLAIMS") if claims != "" { err := json.Unmarshal([]byte(claims), &ret.FirstUserClaims) if err != nil { return nil, err } - - maps.Insert(ret.FirstUserClaims, maps.All(ret.DefaultClaims)) } else { ret.FirstUserClaims = ret.DefaultClaims } From 83d8462003bd438a383a209932f41ab4eee06754 Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Mon, 7 Apr 2025 00:40:03 +0200 Subject: [PATCH 06/11] Add watchstatus create/edit apis --- api/src/controllers/watchlist.ts | 292 +++++++++++++++++++++---------- 1 file changed, 201 insertions(+), 91 deletions(-) diff --git a/api/src/controllers/watchlist.ts b/api/src/controllers/watchlist.ts index 8a937134..00bd695a 100644 --- a/api/src/controllers/watchlist.ts +++ b/api/src/controllers/watchlist.ts @@ -1,11 +1,16 @@ -import { and, isNotNull, isNull } from "drizzle-orm"; +import { type SQL, and, eq, isNotNull, isNull, sql } from "drizzle-orm"; import Elysia, { t } from "elysia"; import { auth, getUserInfo } from "~/auth"; -import { shows } from "~/db/schema"; +import { db } from "~/db"; +import { profiles, shows } from "~/db/schema"; +import { watchlist } from "~/db/schema/watchlist"; +import { conflictUpdateAllExcept } from "~/db/utils"; import { KError } from "~/models/error"; +import { bubble, madeInAbyss } from "~/models/examples"; import { Show } from "~/models/show"; import { AcceptLanguage, + DbMetadata, Filter, Page, createPage, @@ -13,111 +18,216 @@ import { processLanguages, } from "~/models/utils"; import { desc } from "~/models/utils/descriptions"; +import { WatchStatus } from "~/models/watchlist"; import { getShows, showFilters, showSort, watchStatusQ } from "./shows/logic"; +async function setWatchStatus({ + showFilter, + status, + userId, +}: { + showFilter?: SQL; + status: Omit; + userId: string; +}) { + const profileQ = db + .select({ pk: profiles.pk }) + .from(profiles) + .where(eq(profiles.id, userId)) + .as("profileQ"); + const showQ = db + .select({ pk: shows.pk }) + .from(shows) + .where(showFilter) + .as("showQ"); + + return await db + .insert(watchlist) + .values({ + ...status, + profilePk: sql`${profileQ}`, + showPk: sql`${showQ}`, + }) + .onConflictDoUpdate({ + target: [watchlist.profilePk, watchlist.showPk], + set: { + ...conflictUpdateAllExcept(watchlist, [ + "profilePk", + "showPk", + "createdAt", + ]), + }, + }) + .returning(); +} + export const watchlistH = new Elysia({ tags: ["profiles"] }) .use(auth) - .guard({ - query: t.Object({ - sort: showSort, - filter: t.Optional(Filter({ def: showFilters })), - query: t.Optional(t.String({ description: desc.query })), - limit: t.Integer({ - minimum: 1, - maximum: 250, - default: 50, - description: "Max page size.", - }), - after: t.Optional(t.String({ description: desc.after })), - preferOriginal: t.Optional( - t.Boolean({ - description: desc.preferOriginal, + .guard( + { + query: t.Object({ + sort: showSort, + filter: t.Optional(Filter({ def: showFilters })), + query: t.Optional(t.String({ description: desc.query })), + limit: t.Integer({ + minimum: 1, + maximum: 250, + default: 50, + description: "Max page size.", }), - ), - }), - response: { - 200: Page(Show), - 422: KError, - }, - }) - .get( - "/profiles/me/watchlist", - async ({ - query: { limit, after, query, sort, filter, preferOriginal }, - headers: { "accept-language": languages }, - request: { url }, - jwt: { sub }, - }) => { - const langs = processLanguages(languages); - const items = await getShows({ - limit, - after, - query, - sort, - filter: and( - isNotNull(watchStatusQ.status), - isNull(shows.collectionPk), - filter, + after: t.Optional(t.String({ description: desc.after })), + preferOriginal: t.Optional( + t.Boolean({ + description: desc.preferOriginal, + }), ), - languages: langs, - preferOriginal, - userId: sub, - }); - return createPage(items, { url, sort, limit }); + }), + response: { + 200: Page(Show), + 422: KError, + }, }, - { - detail: { description: "Get all movies/series in your watchlist" }, - headers: t.Object( - { - "accept-language": AcceptLanguage({ autoFallback: true }), - }, - { additionalProperties: true }, - ), - }, - ) - .get( - "/profiles/:id/watchlist", - async ({ - params: { id }, - query: { limit, after, query, sort, filter, preferOriginal }, - headers: { "accept-language": languages, authorization }, - request: { url }, - }) => { - if (!isUuid(id)) { - const uInfo = await getUserInfo(id, { authorization }); - id = uInfo.id; - } + (app) => + app + .get( + "/profiles/me/watchlist", + async ({ + query: { limit, after, query, sort, filter, preferOriginal }, + headers: { "accept-language": languages }, + request: { url }, + jwt: { sub }, + }) => { + const langs = processLanguages(languages); + const items = await getShows({ + limit, + after, + query, + sort, + filter: and( + isNotNull(watchStatusQ.status), + isNull(shows.collectionPk), + filter, + ), + languages: langs, + preferOriginal, + userId: sub, + }); + return createPage(items, { url, sort, limit }); + }, + { + detail: { description: "Get all movies/series in your watchlist" }, + headers: t.Object( + { + "accept-language": AcceptLanguage({ autoFallback: true }), + }, + { additionalProperties: true }, + ), + }, + ) + .get( + "/profiles/:id/watchlist", + async ({ + params: { id }, + query: { limit, after, query, sort, filter, preferOriginal }, + headers: { "accept-language": languages, authorization }, + request: { url }, + }) => { + if (!isUuid(id)) { + const uInfo = await getUserInfo(id, { authorization }); + id = uInfo.id; + } - const langs = processLanguages(languages); - const items = await getShows({ - limit, - after, - query, - sort, - filter: and( - isNotNull(watchStatusQ.status), - isNull(shows.collectionPk), - filter, + const langs = processLanguages(languages); + const items = await getShows({ + limit, + after, + query, + sort, + filter: and( + isNotNull(watchStatusQ.status), + isNull(shows.collectionPk), + filter, + ), + languages: langs, + preferOriginal, + userId: id, + }); + return createPage(items, { url, sort, limit }); + }, + { + detail: { + description: "Get all movies/series in someone's watchlist", + }, + params: t.Object({ + id: t.String({ + description: + "The id or username of the user to read the watchlist of", + example: "zoriya", + }), + }), + headers: t.Object({ + authorization: t.TemplateLiteral("Bearer ${string}"), + "accept-language": AcceptLanguage({ autoFallback: true }), + }), + permissions: ["users.read"], + }, ), - languages: langs, - preferOriginal, - userId: id, + ) + .post( + "/series/:id/watchstatus", + async ({ params: { id }, body, jwt: { sub } }) => { + return await setWatchStatus({ + showFilter: and( + eq(shows.kind, "serie"), + isUuid(id) ? eq(shows.id, id) : eq(shows.slug, id), + ), + userId: sub, + status: body, }); - return createPage(items, { url, sort, limit }); }, { - detail: { description: "Get all movies/series in someone's watchlist" }, + detail: { description: "Set watchstatus of a series." }, params: t.Object({ id: t.String({ - description: - "The id or username of the user to read the watchlist of", - example: "zoriya", + description: "The id or slug of the serie.", + example: madeInAbyss.slug, }), }), - headers: t.Object({ - authorization: t.TemplateLiteral("Bearer ${string}"), - "accept-language": AcceptLanguage({ autoFallback: true }), + body: t.Omit(WatchStatus, ["percent"]), + response: { + 201: t.Union([t.Omit(WatchStatus, ["percent"]), DbMetadata]), + }, + permissions: ["core.read"], + }, + ) + .post( + "/movies/:id/watchstatus", + async ({ params: { id }, body, jwt: { sub } }) => { + return await setWatchStatus({ + showFilter: and( + eq(shows.kind, "movie"), + isUuid(id) ? eq(shows.id, id) : eq(shows.slug, id), + ), + userId: sub, + // for movies, watch-percent is stored in `seenCount`. + status: { ...body, seenCount: body.status === "completed" ? 100 : 0 }, + }); + }, + { + detail: { description: "Set watchstatus of a movie." }, + params: t.Object({ + id: t.String({ + description: "The id or slug of the movie.", + example: bubble.slug, + }), }), - permissions: ["users.read"], + body: t.Omit(WatchStatus, ["seenCount", "percent"]), + response: { + 201: t.Union([ + t.Omit(WatchStatus, ["seenCount", "percent"]), + DbMetadata, + ]), + }, + permissions: ["core.read"], }, ); From bf361a79d16490aa1de848fadf40baa8722445d7 Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Mon, 7 Apr 2025 09:47:42 +0200 Subject: [PATCH 07/11] Better movie/serie watchlist types --- api/src/controllers/watchlist.ts | 48 ++++++++++++++++++-------------- api/src/models/movie.ts | 4 +-- api/src/models/serie.ts | 4 +-- api/src/models/watchlist.ts | 21 ++++++++------ 4 files changed, 44 insertions(+), 33 deletions(-) diff --git a/api/src/controllers/watchlist.ts b/api/src/controllers/watchlist.ts index 00bd695a..324e8a20 100644 --- a/api/src/controllers/watchlist.ts +++ b/api/src/controllers/watchlist.ts @@ -18,7 +18,7 @@ import { processLanguages, } from "~/models/utils"; 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"; async function setWatchStatus({ @@ -26,8 +26,8 @@ async function setWatchStatus({ status, userId, }: { - showFilter?: SQL; - status: Omit; + showFilter: { id: SQL; kind: "movie" | "serie" }; + status: SerieWatchStatus; userId: string; }) { const profileQ = db @@ -38,7 +38,7 @@ async function setWatchStatus({ const showQ = db .select({ pk: shows.pk }) .from(shows) - .where(showFilter) + .where(and(showFilter.id, eq(shows.kind, showFilter.kind))) .as("showQ"); return await db @@ -55,7 +55,12 @@ async function setWatchStatus({ "profilePk", "showPk", "createdAt", + "seenCount", ]), + // do not reset movie's progress during drop + ...(showFilter.kind === "movie" && status.status !== "dropped" + ? { seenCount: sql`excluded.seen_count` } + : {}), }, }) .returning(); @@ -177,10 +182,10 @@ export const watchlistH = new Elysia({ tags: ["profiles"] }) "/series/:id/watchstatus", async ({ params: { id }, body, jwt: { sub } }) => { return await setWatchStatus({ - showFilter: and( - eq(shows.kind, "serie"), - isUuid(id) ? eq(shows.id, id) : eq(shows.slug, id), - ), + showFilter: { + kind: "serie", + id: isUuid(id) ? eq(shows.id, id) : eq(shows.slug, id), + }, userId: sub, status: body, }); @@ -193,9 +198,9 @@ export const watchlistH = new Elysia({ tags: ["profiles"] }) example: madeInAbyss.slug, }), }), - body: t.Omit(WatchStatus, ["percent"]), + body: SerieWatchStatus, response: { - 201: t.Union([t.Omit(WatchStatus, ["percent"]), DbMetadata]), + 201: t.Union([SerieWatchStatus, DbMetadata]), }, permissions: ["core.read"], }, @@ -204,13 +209,17 @@ export const watchlistH = new Elysia({ tags: ["profiles"] }) "/movies/:id/watchstatus", async ({ params: { id }, body, jwt: { sub } }) => { return await setWatchStatus({ - showFilter: and( - eq(shows.kind, "movie"), - isUuid(id) ? eq(shows.id, id) : eq(shows.slug, id), - ), + showFilter: { + kind: "movie", + id: isUuid(id) ? eq(shows.id, id) : eq(shows.slug, id), + }, userId: sub, - // for movies, watch-percent is stored in `seenCount`. - status: { ...body, seenCount: body.status === "completed" ? 100 : 0 }, + status: { + ...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, }), }), - body: t.Omit(WatchStatus, ["seenCount", "percent"]), + body: t.Omit(MovieWatchStatus, ["percent"]), response: { - 201: t.Union([ - t.Omit(WatchStatus, ["seenCount", "percent"]), - DbMetadata, - ]), + 201: t.Union([MovieWatchStatus, DbMetadata]), }, permissions: ["core.read"], }, diff --git a/api/src/models/movie.ts b/api/src/models/movie.ts index 9de88479..31f28487 100644 --- a/api/src/models/movie.ts +++ b/api/src/models/movie.ts @@ -16,7 +16,7 @@ import { } from "./utils"; import { Original } from "./utils/original"; import { EmbeddedVideo } from "./video"; -import { WatchStatus } from "./watchlist"; +import { MovieWatchStatus } from "./watchlist"; export const MovieStatus = t.UnionEnum(["unknown", "finished", "planned"]); export type MovieStatus = typeof MovieStatus.static; @@ -56,7 +56,7 @@ export const Movie = t.Intersect([ t.Object({ original: Original, isAvailable: t.Boolean(), - watchStatus: t.Nullable(t.Omit(WatchStatus, ["seenCount"])), + watchStatus: t.Nullable(MovieWatchStatus), }), ]); export type Movie = Prettify; diff --git a/api/src/models/serie.ts b/api/src/models/serie.ts index 78ed77e8..2757f493 100644 --- a/api/src/models/serie.ts +++ b/api/src/models/serie.ts @@ -17,7 +17,7 @@ import { TranslationRecord, } from "./utils"; import { Original } from "./utils/original"; -import { WatchStatus } from "./watchlist"; +import { SerieWatchStatus } from "./watchlist"; export const SerieStatus = t.UnionEnum([ "unknown", @@ -71,7 +71,7 @@ export const Serie = t.Intersect([ availableCount: t.Integer({ 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; diff --git a/api/src/models/watchlist.ts b/api/src/models/watchlist.ts index 2b1f270d..b341ea3e 100644 --- a/api/src/models/watchlist.ts +++ b/api/src/models/watchlist.ts @@ -36,20 +36,25 @@ export const WatchlistStatus = t.UnionEnum([ "planned", ]); -export const WatchStatus = t.Object({ +export const SerieWatchStatus = t.Object({ status: WatchlistStatus, score: t.Nullable(t.Integer({ minimum: 0, maximum: 100 })), startedAt: t.Nullable(t.String({ format: "date-time" })), completedAt: t.Nullable(t.String({ format: "date-time" })), - // only for series seenCount: t.Integer({ description: "The number of episodes you watched in this serie.", 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; From b3edf31afca678af2e38710fcc2984d473315b5a Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Mon, 7 Apr 2025 09:55:12 +0200 Subject: [PATCH 08/11] Create simple watchlist tests --- api/tests/helpers/index.ts | 1 + api/tests/helpers/movies-helper.ts | 19 ++++++ api/tests/helpers/series-helper.ts | 19 ++++++ api/tests/helpers/shows-helper.ts | 60 ++++++++++++++++++ api/tests/movies/watchstatus.test.ts | 94 ++++++++++++++++++++++++++++ 5 files changed, 193 insertions(+) create mode 100644 api/tests/helpers/shows-helper.ts create mode 100644 api/tests/movies/watchstatus.test.ts diff --git a/api/tests/helpers/index.ts b/api/tests/helpers/index.ts index e2898d52..f9111196 100644 --- a/api/tests/helpers/index.ts +++ b/api/tests/helpers/index.ts @@ -1,5 +1,6 @@ export * from "./movies-helper"; export * from "./series-helper"; +export * from "./shows-helper"; export * from "./studio-helper"; export * from "./staff-helper"; export * from "./videos-helper"; diff --git a/api/tests/helpers/movies-helper.ts b/api/tests/helpers/movies-helper.ts index 85b09916..f0b1711f 100644 --- a/api/tests/helpers/movies-helper.ts +++ b/api/tests/helpers/movies-helper.ts @@ -1,6 +1,7 @@ import { buildUrl } from "tests/utils"; import { app } from "~/base"; import type { SeedMovie } from "~/models/movie"; +import type { MovieWatchStatus } from "~/models/watchlist"; import { getJwtHeaders } from "./jwt"; export const getMovie = async ( @@ -66,3 +67,21 @@ export const createMovie = async (movie: SeedMovie) => { const body = await resp.json(); return [resp, body] as const; }; + +export const setMovieStatus = async ( + id: string, + status: Omit, +) => { + const resp = await app.handle( + new Request(buildUrl(`movies/${id}/watchstatus`), { + method: "POST", + body: JSON.stringify(status), + headers: { + "Content-Type": "application/json", + ...(await getJwtHeaders()), + }, + }), + ); + const body = await resp.json(); + return [resp, body] as const; +}; diff --git a/api/tests/helpers/series-helper.ts b/api/tests/helpers/series-helper.ts index 7004278e..843ff375 100644 --- a/api/tests/helpers/series-helper.ts +++ b/api/tests/helpers/series-helper.ts @@ -2,6 +2,7 @@ import { buildUrl } from "tests/utils"; import { app } from "~/base"; import type { SeedSerie } from "~/models/serie"; import { getJwtHeaders } from "./jwt"; +import { SerieWatchStatus } from "~/models/watchlist"; export const createSerie = async (serie: SeedSerie) => { const resp = await app.handle( @@ -162,3 +163,21 @@ export const getNews = async ({ const body = await resp.json(); return [resp, body] as const; }; + +export const setSerieStatus = async ( + id: string, + status: SerieWatchStatus +) => { + const resp = await app.handle( + new Request(buildUrl(`movies/${id}/watchstatus`), { + method: "POST", + body: JSON.stringify(status), + headers: { + "Content-Type": "application/json", + ...(await getJwtHeaders()), + }, + }), + ); + const body = await resp.json(); + return [resp, body] as const; +}; diff --git a/api/tests/helpers/shows-helper.ts b/api/tests/helpers/shows-helper.ts new file mode 100644 index 00000000..f1a138dd --- /dev/null +++ b/api/tests/helpers/shows-helper.ts @@ -0,0 +1,60 @@ +import { buildUrl } from "tests/utils"; +import { app } from "~/base"; +import { getJwtHeaders } from "./jwt"; + +export const getShows = async ({ + langs, + ...query +}: { + filter?: string; + limit?: number; + after?: string; + sort?: string | string[]; + query?: string; + langs?: string; + preferOriginal?: boolean; +}) => { + const resp = await app.handle( + new Request(buildUrl("shows", query), { + method: "GET", + headers: langs + ? { + "Accept-Language": langs, + ...(await getJwtHeaders()), + } + : await getJwtHeaders(), + }), + ); + const body = await resp.json(); + return [resp, body] as const; +}; + +export const getWatchlist = async ( + id: string, + { + langs, + ...query + }: { + filter?: string; + limit?: number; + after?: string; + sort?: string | string[]; + query?: string; + langs?: string; + preferOriginal?: boolean; + }, +) => { + const resp = await app.handle( + new Request(buildUrl(`profiles/${id}/watchlist`, query), { + method: "GET", + headers: langs + ? { + "Accept-Language": langs, + ...(await getJwtHeaders()), + } + : await getJwtHeaders(), + }), + ); + const body = await resp.json(); + return [resp, body] as const; +}; diff --git a/api/tests/movies/watchstatus.test.ts b/api/tests/movies/watchstatus.test.ts new file mode 100644 index 00000000..98caa014 --- /dev/null +++ b/api/tests/movies/watchstatus.test.ts @@ -0,0 +1,94 @@ +import { beforeAll, describe, expect, it } from "bun:test"; +import { + createMovie, + getMovie, + getShows, + getWatchlist, + setMovieStatus, +} from "tests/helpers"; +import { expectStatus } from "tests/utils"; +import { db } from "~/db"; +import { shows } from "~/db/schema"; +import { bubble } from "~/models/examples"; + +beforeAll(async () => { + await db.delete(shows); + const [ret, body] = await createMovie(bubble); + expectStatus(ret, body).toBe(201); +}); + +describe("Set & get watch status", () => { + it("Creates watchlist entry", async () => { + let [resp, body] = await getWatchlist("me", {}); + expectStatus(resp, body).toBe(200); + expect(body.items).toBeArrayOfSize(0); + + const [r, b] = await setMovieStatus(bubble.slug, { + status: "completed", + completedAt: "2024-12-21", + score: 85, + }); + expectStatus(r, b).toBe(201); + + [resp, body] = await getWatchlist("me", {}); + expectStatus(resp, body).toBe(200); + expect(body.items).toBeArrayOfSize(1); + expect(body.items[0].slug).toBe(bubble.slug); + expect(body.items[0].watchStatus).toMatchObject({ + status: "completed", + completedAt: "2024-12-21", + score: 85, + percent: 100, + }); + }); + + it("Edit watchlist entry", async () => { + let [resp, body] = await getWatchlist("me", {}); + expectStatus(resp, body).toBe(200); + expect(body.items).toBeArrayOfSize(1); + + const [r, b] = await setMovieStatus(bubble.slug, { + status: "rewatching", + // we still need to specify all values + completedAt: "2024-12-21", + score: 85, + }); + expectStatus(r, b).toBe(201); + + [resp, body] = await getWatchlist("me", {}); + expectStatus(resp, body).toBe(200); + expect(body.items).toBeArrayOfSize(1); + expect(body.items[0].slug).toBe(bubble.slug); + expect(body.items[0].watchStatus).toMatchObject({ + status: "completed", + completedAt: "2024-12-21", + score: 85, + percent: 0, + }); + }); + + it("Return watchstatus in /shows", async () => { + const [resp, body] = await getShows({}); + expectStatus(resp, body).toBe(200); + expect(body.items).toBeArrayOfSize(1); + expect(body.items[0].slug).toBe(bubble.slug); + expect(body.items[0].watchStatus).toMatchObject({ + status: "completed", + completedAt: "2024-12-21", + score: 85, + percent: 0, + }); + }); + + it("Return watchstatus in /movies/:id", async () => { + const [resp, body] = await getMovie(bubble.slug, {}); + expectStatus(resp, body).toBe(200); + expect(body.slug).toBe(bubble.slug); + expect(body.watchStatus).toMatchObject({ + status: "completed", + completedAt: "2024-12-21", + score: 85, + percent: 0, + }); + }); +}); From 59533e5f0c64646f159af726122ceef0788ccf10 Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Mon, 7 Apr 2025 12:38:48 +0200 Subject: [PATCH 09/11] Handle 404 for user accounts --- api/src/auth.ts | 3 ++- api/src/controllers/watchlist.ts | 9 +++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/api/src/auth.ts b/api/src/auth.ts index f8b3638b..c9d64614 100644 --- a/api/src/auth.ts +++ b/api/src/auth.ts @@ -1,6 +1,7 @@ import { TypeCompiler } from "@sinclair/typebox/compiler"; import Elysia, { t } from "elysia"; import { createRemoteJWKSet, jwtVerify } from "jose"; +import { KError } from "./models/error"; const jwtSecret = process.env.JWT_SECRET ? new TextEncoder().encode(process.env.JWT_SECRET) @@ -86,7 +87,7 @@ const User = t.Object({ }), ), }); -const UserC = TypeCompiler.Compile(User); +const UserC = TypeCompiler.Compile(t.Union([User, KError])); export async function getUserInfo( id: string, diff --git a/api/src/controllers/watchlist.ts b/api/src/controllers/watchlist.ts index 324e8a20..622f5460 100644 --- a/api/src/controllers/watchlist.ts +++ b/api/src/controllers/watchlist.ts @@ -174,6 +174,15 @@ export const watchlistH = new Elysia({ tags: ["profiles"] }) authorization: t.TemplateLiteral("Bearer ${string}"), "accept-language": AcceptLanguage({ autoFallback: true }), }), + response: { + 200: Page(Show), + 403: KError, + 404: { + ...KError, + description: "No user found with the specified id/username.", + }, + 422: KError, + }, permissions: ["users.read"], }, ), From db0b244286e7fbe18e2626880af8730a7cde3ed1 Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Mon, 7 Apr 2025 12:39:43 +0200 Subject: [PATCH 10/11] Handle profiles creation in `kyoo` db schema --- api/src/controllers/watchlist.ts | 54 ++++++++++++++++++---------- api/tests/helpers/jwt.ts | 2 +- api/tests/helpers/series-helper.ts | 7 ++-- api/tests/movies/watchstatus.test.ts | 18 +++++----- 4 files changed, 47 insertions(+), 34 deletions(-) diff --git a/api/src/controllers/watchlist.ts b/api/src/controllers/watchlist.ts index 622f5460..cc6acfac 100644 --- a/api/src/controllers/watchlist.ts +++ b/api/src/controllers/watchlist.ts @@ -4,7 +4,7 @@ import { auth, getUserInfo } from "~/auth"; import { db } from "~/db"; import { profiles, shows } from "~/db/schema"; import { watchlist } from "~/db/schema/watchlist"; -import { conflictUpdateAllExcept } from "~/db/utils"; +import { conflictUpdateAllExcept, getColumns } from "~/db/utils"; import { KError } from "~/models/error"; import { bubble, madeInAbyss } from "~/models/examples"; import { Show } from "~/models/show"; @@ -30,22 +30,34 @@ async function setWatchStatus({ status: SerieWatchStatus; userId: string; }) { - const profileQ = db + let [profile] = await db .select({ pk: profiles.pk }) .from(profiles) .where(eq(profiles.id, userId)) - .as("profileQ"); + .limit(1); + if (!profile) { + [profile] = await db + .insert(profiles) + .values({ id: userId }) + .onConflictDoUpdate({ + // we can't do `onConflictDoNothing` because on race conditions + // we still want the profile to be returned. + target: [profiles.id], + set: { id: sql`excluded.id` }, + }) + .returning({ pk: profiles.pk }); + } + const showQ = db .select({ pk: shows.pk }) .from(shows) - .where(and(showFilter.id, eq(shows.kind, showFilter.kind))) - .as("showQ"); + .where(and(showFilter.id, eq(shows.kind, showFilter.kind))); - return await db + const [ret] = await db .insert(watchlist) .values({ ...status, - profilePk: sql`${profileQ}`, + profilePk: profile.pk, showPk: sql`${showQ}`, }) .onConflictDoUpdate({ @@ -63,7 +75,11 @@ async function setWatchStatus({ : {}), }, }) - .returning(); + .returning({ + ...getColumns(watchlist), + percent: sql`${watchlist.seenCount}`.as("percent"), + }); + return ret; } export const watchlistH = new Elysia({ tags: ["profiles"] }) @@ -87,10 +103,6 @@ export const watchlistH = new Elysia({ tags: ["profiles"] }) }), ), }), - response: { - 200: Page(Show), - 422: KError, - }, }, (app) => app @@ -127,6 +139,10 @@ export const watchlistH = new Elysia({ tags: ["profiles"] }) }, { additionalProperties: true }, ), + response: { + 200: Page(Show), + 422: KError, + }, }, ) .get( @@ -136,11 +152,11 @@ export const watchlistH = new Elysia({ tags: ["profiles"] }) query: { limit, after, query, sort, filter, preferOriginal }, headers: { "accept-language": languages, authorization }, request: { url }, + error, }) => { - if (!isUuid(id)) { - const uInfo = await getUserInfo(id, { authorization }); - id = uInfo.id; - } + const uInfo = await getUserInfo(id, { authorization }); + + if ("status" in uInfo) return error(uInfo.status as 404, uInfo); const langs = processLanguages(languages); const items = await getShows({ @@ -155,7 +171,7 @@ export const watchlistH = new Elysia({ tags: ["profiles"] }) ), languages: langs, preferOriginal, - userId: id, + userId: uInfo.id, }); return createPage(items, { url, sort, limit }); }, @@ -209,7 +225,7 @@ export const watchlistH = new Elysia({ tags: ["profiles"] }) }), body: SerieWatchStatus, response: { - 201: t.Union([SerieWatchStatus, DbMetadata]), + 200: t.Union([SerieWatchStatus, DbMetadata]), }, permissions: ["core.read"], }, @@ -241,7 +257,7 @@ export const watchlistH = new Elysia({ tags: ["profiles"] }) }), body: t.Omit(MovieWatchStatus, ["percent"]), response: { - 201: t.Union([MovieWatchStatus, DbMetadata]), + 200: t.Union([MovieWatchStatus, DbMetadata]), }, permissions: ["core.read"], }, diff --git a/api/tests/helpers/jwt.ts b/api/tests/helpers/jwt.ts index 80efc31b..668dfb7f 100644 --- a/api/tests/helpers/jwt.ts +++ b/api/tests/helpers/jwt.ts @@ -5,7 +5,7 @@ export async function getJwtHeaders() { sub: "39158be0-3f59-4c45-b00d-d25b3bc2b884", sid: "04ac7ecc-255b-481d-b0c8-537c1578e3d5", username: "test-username", - permissions: ["core.read", "core.write"], + permissions: ["core.read", "core.write", "users.read"], }) .setProtectedHeader({ alg: "HS256" }) .setIssuedAt() diff --git a/api/tests/helpers/series-helper.ts b/api/tests/helpers/series-helper.ts index 843ff375..d585ef7a 100644 --- a/api/tests/helpers/series-helper.ts +++ b/api/tests/helpers/series-helper.ts @@ -1,8 +1,8 @@ import { buildUrl } from "tests/utils"; import { app } from "~/base"; import type { SeedSerie } from "~/models/serie"; +import type { SerieWatchStatus } from "~/models/watchlist"; import { getJwtHeaders } from "./jwt"; -import { SerieWatchStatus } from "~/models/watchlist"; export const createSerie = async (serie: SeedSerie) => { const resp = await app.handle( @@ -164,10 +164,7 @@ export const getNews = async ({ return [resp, body] as const; }; -export const setSerieStatus = async ( - id: string, - status: SerieWatchStatus -) => { +export const setSerieStatus = async (id: string, status: SerieWatchStatus) => { const resp = await app.handle( new Request(buildUrl(`movies/${id}/watchstatus`), { method: "POST", diff --git a/api/tests/movies/watchstatus.test.ts b/api/tests/movies/watchstatus.test.ts index 98caa014..96025ab3 100644 --- a/api/tests/movies/watchstatus.test.ts +++ b/api/tests/movies/watchstatus.test.ts @@ -28,7 +28,7 @@ describe("Set & get watch status", () => { completedAt: "2024-12-21", score: 85, }); - expectStatus(r, b).toBe(201); + expectStatus(r, b).toBe(200); [resp, body] = await getWatchlist("me", {}); expectStatus(resp, body).toBe(200); @@ -36,7 +36,7 @@ describe("Set & get watch status", () => { expect(body.items[0].slug).toBe(bubble.slug); expect(body.items[0].watchStatus).toMatchObject({ status: "completed", - completedAt: "2024-12-21", + completedAt: "2024-12-21 00:00:00+00", score: 85, percent: 100, }); @@ -53,15 +53,15 @@ describe("Set & get watch status", () => { completedAt: "2024-12-21", score: 85, }); - expectStatus(r, b).toBe(201); + expectStatus(r, b).toBe(200); [resp, body] = await getWatchlist("me", {}); expectStatus(resp, body).toBe(200); expect(body.items).toBeArrayOfSize(1); expect(body.items[0].slug).toBe(bubble.slug); expect(body.items[0].watchStatus).toMatchObject({ - status: "completed", - completedAt: "2024-12-21", + status: "rewatching", + completedAt: "2024-12-21 00:00:00+00", score: 85, percent: 0, }); @@ -73,8 +73,8 @@ describe("Set & get watch status", () => { expect(body.items).toBeArrayOfSize(1); expect(body.items[0].slug).toBe(bubble.slug); expect(body.items[0].watchStatus).toMatchObject({ - status: "completed", - completedAt: "2024-12-21", + status: "rewatching", + completedAt: "2024-12-21 00:00:00+00", score: 85, percent: 0, }); @@ -85,8 +85,8 @@ describe("Set & get watch status", () => { expectStatus(resp, body).toBe(200); expect(body.slug).toBe(bubble.slug); expect(body.watchStatus).toMatchObject({ - status: "completed", - completedAt: "2024-12-21", + status: "rewatching", + completedAt: "2024-12-21 00:00:00+00", score: 85, percent: 0, }); From 080da9bc27aa4e98acdc9db2783deee30bba8273 Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Mon, 7 Apr 2025 15:38:08 +0200 Subject: [PATCH 11/11] Fix tests & misc errors --- api/src/auth.ts | 9 ++-- api/src/controllers/shows/collections.ts | 12 +++++ api/src/controllers/shows/movies.ts | 6 +++ api/src/controllers/staff.ts | 4 +- api/src/controllers/studios.ts | 8 +++ api/src/controllers/watchlist.ts | 62 ++++++++++++++++-------- api/src/models/serie.ts | 2 +- api/tests/helpers/series-helper.ts | 21 +++++++- api/tests/manual.ts | 31 +++++++----- api/tests/misc/images.test.ts | 26 +++++----- api/tests/movies/watchstatus.test.ts | 8 +++ 11 files changed, 136 insertions(+), 53 deletions(-) diff --git a/api/src/auth.ts b/api/src/auth.ts index c9d64614..812f754f 100644 --- a/api/src/auth.ts +++ b/api/src/auth.ts @@ -23,9 +23,12 @@ const validator = TypeCompiler.Compile(Jwt); export const auth = new Elysia({ name: "auth" }) .guard({ - headers: t.Object({ - authorization: t.TemplateLiteral("Bearer ${string}"), - }), + headers: t.Object( + { + authorization: t.TemplateLiteral("Bearer ${string}"), + }, + { additionalProperties: true }, + ), }) .resolve(async ({ headers: { authorization }, error }) => { const bearer = authorization?.slice(7); diff --git a/api/src/controllers/shows/collections.ts b/api/src/controllers/shows/collections.ts index f2fd177a..3d5bb02b 100644 --- a/api/src/controllers/shows/collections.ts +++ b/api/src/controllers/shows/collections.ts @@ -1,5 +1,6 @@ import { and, eq, sql } from "drizzle-orm"; import { Elysia, t } from "elysia"; +import { auth } from "~/auth"; import { prefix } from "~/base"; import { db } from "~/db"; import { shows } from "~/db/schema"; @@ -32,12 +33,14 @@ export const collections = new Elysia({ collection: Collection, "collection-translation": CollectionTranslation, }) + .use(auth) .get( "/:id", async ({ params: { id }, headers: { "accept-language": languages }, query: { preferOriginal, with: relations }, + jwt: { sub }, error, set, }) => { @@ -52,6 +55,7 @@ export const collections = new Elysia({ fallbackLanguage: langs.includes("*"), preferOriginal, relations, + userId: sub, }); if (!ret) { return error(404, { @@ -140,6 +144,7 @@ export const collections = new Elysia({ async ({ query: { limit, after, query, sort, filter, preferOriginal }, headers: { "accept-language": languages }, + jwt: { sub }, request: { url }, }) => { const langs = processLanguages(languages); @@ -151,6 +156,7 @@ export const collections = new Elysia({ filter: and(eq(shows.kind, "collection"), filter), languages: langs, preferOriginal, + userId: sub, }); return createPage(items, { url, sort, limit }); }, @@ -222,6 +228,7 @@ export const collections = new Elysia({ params: { id }, query: { limit, after, query, sort, filter, preferOriginal }, headers: { "accept-language": languages }, + jwt: { sub }, request: { url }, error, }) => { @@ -256,6 +263,7 @@ export const collections = new Elysia({ ), languages: langs, preferOriginal, + userId: sub, }); return createPage(items, { url, sort, limit }); }, @@ -277,6 +285,7 @@ export const collections = new Elysia({ params: { id }, query: { limit, after, query, sort, filter, preferOriginal }, headers: { "accept-language": languages }, + jwt: { sub }, request: { url }, error, }) => { @@ -311,6 +320,7 @@ export const collections = new Elysia({ ), languages: langs, preferOriginal, + userId: sub, }); return createPage(items, { url, sort, limit }); }, @@ -332,6 +342,7 @@ export const collections = new Elysia({ params: { id }, query: { limit, after, query, sort, filter, preferOriginal }, headers: { "accept-language": languages }, + jwt: { sub }, request: { url }, error, }) => { @@ -362,6 +373,7 @@ export const collections = new Elysia({ filter: and(eq(shows.collectionPk, collection.pk), filter), languages: langs, preferOriginal, + userId: sub, }); return createPage(items, { url, sort, limit }); }, diff --git a/api/src/controllers/shows/movies.ts b/api/src/controllers/shows/movies.ts index 2781d171..18b969dd 100644 --- a/api/src/controllers/shows/movies.ts +++ b/api/src/controllers/shows/movies.ts @@ -1,5 +1,6 @@ import { and, eq, sql } from "drizzle-orm"; import { Elysia, t } from "elysia"; +import { auth } from "~/auth"; import { prefix } from "~/base"; import { db } from "~/db"; import { shows } from "~/db/schema"; @@ -22,12 +23,14 @@ export const movies = new Elysia({ prefix: "/movies", tags: ["movies"] }) movie: Movie, "movie-translation": MovieTranslation, }) + .use(auth) .get( "/:id", async ({ params: { id }, headers: { "accept-language": languages }, query: { preferOriginal, with: relations }, + jwt: { sub }, error, set, }) => { @@ -42,6 +45,7 @@ export const movies = new Elysia({ prefix: "/movies", tags: ["movies"] }) fallbackLanguage: langs.includes("*"), preferOriginal, relations, + userId: sub, }); if (!ret) { return error(404, { @@ -131,6 +135,7 @@ export const movies = new Elysia({ prefix: "/movies", tags: ["movies"] }) query: { limit, after, query, sort, filter, preferOriginal }, headers: { "accept-language": languages }, request: { url }, + jwt: { sub }, }) => { const langs = processLanguages(languages); const items = await getShows({ @@ -141,6 +146,7 @@ export const movies = new Elysia({ prefix: "/movies", tags: ["movies"] }) filter: and(eq(shows.kind, "movie"), filter), languages: langs, preferOriginal, + userId: sub, }); return createPage(items, { url, sort, limit }); }, diff --git a/api/src/controllers/staff.ts b/api/src/controllers/staff.ts index b9184b59..7ce783f3 100644 --- a/api/src/controllers/staff.ts +++ b/api/src/controllers/staff.ts @@ -25,7 +25,7 @@ import { sortToSql, } from "~/models/utils"; import { desc } from "~/models/utils/descriptions"; -import type { WatchStatus } from "~/models/watchlist"; +import type { MovieWatchStatus, SerieWatchStatus } from "~/models/watchlist"; import { showFilters, showSort } from "./shows/logic"; const staffSort = Sort( @@ -219,7 +219,7 @@ export const staffH = new Elysia({ tags: ["staff"] }) const watchStatusQ = db .select({ - watchStatus: jsonbBuildObject({ + watchStatus: jsonbBuildObject({ ...getColumns(watchlist), percent: watchlist.seenCount, }).as("watchStatus"), diff --git a/api/src/controllers/studios.ts b/api/src/controllers/studios.ts index ba8a5290..d9682b78 100644 --- a/api/src/controllers/studios.ts +++ b/api/src/controllers/studios.ts @@ -1,5 +1,6 @@ import { type SQL, and, eq, exists, sql } from "drizzle-orm"; import Elysia, { t } from "elysia"; +import { auth } from "~/auth"; import { prefix } from "~/base"; import { db } from "~/db"; import { @@ -127,6 +128,7 @@ export const studiosH = new Elysia({ prefix: "/studios", tags: ["studios"] }) studio: Studio, "studio-translation": StudioTranslation, }) + .use(auth) .get( "/:id", async ({ @@ -301,6 +303,7 @@ export const studiosH = new Elysia({ prefix: "/studios", tags: ["studios"] }) params: { id }, query: { limit, after, query, sort, filter, preferOriginal }, headers: { "accept-language": languages }, + jwt: { sub }, request: { url }, error, }) => { @@ -339,6 +342,7 @@ export const studiosH = new Elysia({ prefix: "/studios", tags: ["studios"] }) ), languages: langs, preferOriginal, + userId: sub, }); return createPage(items, { url, sort, limit }); }, @@ -360,6 +364,7 @@ export const studiosH = new Elysia({ prefix: "/studios", tags: ["studios"] }) params: { id }, query: { limit, after, query, sort, filter, preferOriginal }, headers: { "accept-language": languages }, + jwt: { sub }, request: { url }, error, }) => { @@ -399,6 +404,7 @@ export const studiosH = new Elysia({ prefix: "/studios", tags: ["studios"] }) ), languages: langs, preferOriginal, + userId: sub, }); return createPage(items, { url, sort, limit }); }, @@ -420,6 +426,7 @@ export const studiosH = new Elysia({ prefix: "/studios", tags: ["studios"] }) params: { id }, query: { limit, after, query, sort, filter, preferOriginal }, headers: { "accept-language": languages }, + jwt: { sub }, request: { url }, error, }) => { @@ -459,6 +466,7 @@ export const studiosH = new Elysia({ prefix: "/studios", tags: ["studios"] }) ), languages: langs, preferOriginal, + userId: sub, }); return createPage(items, { url, sort, limit }); }, diff --git a/api/src/controllers/watchlist.ts b/api/src/controllers/watchlist.ts index cc6acfac..ae3256e4 100644 --- a/api/src/controllers/watchlist.ts +++ b/api/src/controllers/watchlist.ts @@ -22,11 +22,11 @@ import { MovieWatchStatus, SerieWatchStatus } from "~/models/watchlist"; import { getShows, showFilters, showSort, watchStatusQ } from "./shows/logic"; async function setWatchStatus({ - showFilter, + show, status, userId, }: { - showFilter: { id: SQL; kind: "movie" | "serie" }; + show: { pk: number; kind: "movie" | "serie" }; status: SerieWatchStatus; userId: string; }) { @@ -48,17 +48,12 @@ async function setWatchStatus({ .returning({ pk: profiles.pk }); } - const showQ = db - .select({ pk: shows.pk }) - .from(shows) - .where(and(showFilter.id, eq(shows.kind, showFilter.kind))); - const [ret] = await db .insert(watchlist) .values({ ...status, profilePk: profile.pk, - showPk: sql`${showQ}`, + showPk: show.pk, }) .onConflictDoUpdate({ target: [watchlist.profilePk, watchlist.showPk], @@ -70,7 +65,7 @@ async function setWatchStatus({ "seenCount", ]), // do not reset movie's progress during drop - ...(showFilter.kind === "movie" && status.status !== "dropped" + ...(show.kind === "movie" && status.status !== "dropped" ? { seenCount: sql`excluded.seen_count` } : {}), }, @@ -205,12 +200,25 @@ export const watchlistH = new Elysia({ tags: ["profiles"] }) ) .post( "/series/:id/watchstatus", - async ({ params: { id }, body, jwt: { sub } }) => { + async ({ params: { id }, body, jwt: { sub }, error }) => { + const [show] = await db + .select({ pk: shows.pk }) + .from(shows) + .where( + and( + eq(shows.kind, "serie"), + isUuid(id) ? eq(shows.id, id) : eq(shows.slug, id), + ), + ); + + if (!show) { + return error(404, { + status: 404, + message: `No serie found for the id/slug: '${id}'.`, + }); + } return await setWatchStatus({ - showFilter: { - kind: "serie", - id: isUuid(id) ? eq(shows.id, id) : eq(shows.slug, id), - }, + show: { pk: show.pk, kind: "serie" }, userId: sub, status: body, }); @@ -226,18 +234,33 @@ export const watchlistH = new Elysia({ tags: ["profiles"] }) body: SerieWatchStatus, response: { 200: t.Union([SerieWatchStatus, DbMetadata]), + 404: KError, }, permissions: ["core.read"], }, ) .post( "/movies/:id/watchstatus", - async ({ params: { id }, body, jwt: { sub } }) => { + async ({ params: { id }, body, jwt: { sub }, error }) => { + const [show] = await db + .select({ pk: shows.pk }) + .from(shows) + .where( + and( + eq(shows.kind, "movie"), + isUuid(id) ? eq(shows.id, id) : eq(shows.slug, id), + ), + ); + + if (!show) { + return error(404, { + status: 404, + message: `No movie found for the id/slug: '${id}'.`, + }); + } + return await setWatchStatus({ - showFilter: { - kind: "movie", - id: isUuid(id) ? eq(shows.id, id) : eq(shows.slug, id), - }, + show: { pk: show.pk, kind: "movie" }, userId: sub, status: { ...body, @@ -258,6 +281,7 @@ export const watchlistH = new Elysia({ tags: ["profiles"] }) body: t.Omit(MovieWatchStatus, ["percent"]), response: { 200: t.Union([MovieWatchStatus, DbMetadata]), + 404: KError, }, permissions: ["core.read"], }, diff --git a/api/src/models/serie.ts b/api/src/models/serie.ts index 2757f493..3e4c3f2a 100644 --- a/api/src/models/serie.ts +++ b/api/src/models/serie.ts @@ -84,7 +84,7 @@ export const FullSerie = t.Intersect([ firstEntry: t.Optional(Entry), }), ]); -export type FullMovie = Prettify; +export type FullSerie = Prettify; export const SeedSerie = t.Intersect([ t.Omit(BaseSerie, ["kind", "nextRefresh"]), diff --git a/api/tests/helpers/series-helper.ts b/api/tests/helpers/series-helper.ts index d585ef7a..ac856226 100644 --- a/api/tests/helpers/series-helper.ts +++ b/api/tests/helpers/series-helper.ts @@ -41,6 +41,25 @@ export const getSerie = async ( return [resp, body] as const; }; +export const getSeries = async ({ + langs, + ...query +}: { langs?: string; preferOriginal?: boolean; with?: string[] }) => { + const resp = await app.handle( + new Request(buildUrl("series", query), { + method: "GET", + headers: langs + ? { + "Accept-Language": langs, + ...(await getJwtHeaders()), + } + : await getJwtHeaders(), + }), + ); + const body = await resp.json(); + return [resp, body] as const; +}; + export const getSeasons = async ( serie: string, { @@ -166,7 +185,7 @@ export const getNews = async ({ export const setSerieStatus = async (id: string, status: SerieWatchStatus) => { const resp = await app.handle( - new Request(buildUrl(`movies/${id}/watchstatus`), { + new Request(buildUrl(`series/${id}/watchstatus`), { method: "POST", body: JSON.stringify(status), headers: { diff --git a/api/tests/manual.ts b/api/tests/manual.ts index 4d4cb906..6ad8c521 100644 --- a/api/tests/manual.ts +++ b/api/tests/manual.ts @@ -1,24 +1,29 @@ -import { processImages } from "~/controllers/seed/images"; import { db, migrate } from "~/db"; -import { mqueue, shows, videos } from "~/db/schema"; -import { madeInAbyss, madeInAbyssVideo } from "~/models/examples"; -import { createSerie, createVideo, getSerie } from "./helpers"; +import { profiles, shows } from "~/db/schema"; +import { madeInAbyss } from "~/models/examples"; +import { createSerie, getSerie, setSerieStatus } from "./helpers"; +import { getJwtHeaders } from "./helpers/jwt"; // test file used to run manually using `bun tests/manual.ts` await migrate(); await db.delete(shows); -await db.delete(videos); -await db.delete(mqueue); +await db.delete(profiles); -const [_, vid] = await createVideo(madeInAbyssVideo); -console.log(vid); -const [__, ser] = await createSerie(madeInAbyss); +console.log(await getJwtHeaders()); + +const [_, ser] = await createSerie(madeInAbyss); console.log(ser); +const [__, ret] = await setSerieStatus(madeInAbyss.slug, { + status: "watching", + startedAt: "2024-12-21", + completedAt: "2024-12-21", + seenCount: 2, + score: 85, +}); +console.log(ret); -await processImages(); - -const [___, got] = await getSerie(madeInAbyss.slug, { with: ["translations"] }); -console.log(got); +const [___, got] = await getSerie(madeInAbyss.slug, {}); +console.log(JSON.stringify(got, undefined, 4)); process.exit(0); diff --git a/api/tests/misc/images.test.ts b/api/tests/misc/images.test.ts index c59a3a92..86d6252a 100644 --- a/api/tests/misc/images.test.ts +++ b/api/tests/misc/images.test.ts @@ -1,4 +1,4 @@ -import { beforeAll, describe, expect, it } from "bun:test"; +import { describe, expect, it } from "bun:test"; import { eq } from "drizzle-orm"; import { defaultBlurhash, processImages } from "~/controllers/seed/images"; import { db } from "~/db"; @@ -6,21 +6,19 @@ import { mqueue, shows, staff, studios, videos } from "~/db/schema"; import { madeInAbyss } from "~/models/examples"; import { createSerie } from "../helpers"; -beforeAll(async () => { - await db.delete(shows); - await db.delete(studios); - await db.delete(staff); - await db.delete(videos); - await db.delete(mqueue); - - await createSerie(madeInAbyss); - const release = await processImages(); - // remove notifications to prevent other images to be downloaded (do not curl 20000 images for nothing) - release(); -}); - describe("images", () => { it("Create a serie download images", async () => { + await db.delete(shows); + await db.delete(studios); + await db.delete(staff); + await db.delete(videos); + await db.delete(mqueue); + + await createSerie(madeInAbyss); + const release = await processImages(); + // remove notifications to prevent other images to be downloaded (do not curl 20000 images for nothing) + release(); + const ret = await db.query.shows.findFirst({ where: eq(shows.slug, madeInAbyss.slug), }); diff --git a/api/tests/movies/watchstatus.test.ts b/api/tests/movies/watchstatus.test.ts index 96025ab3..609133d8 100644 --- a/api/tests/movies/watchstatus.test.ts +++ b/api/tests/movies/watchstatus.test.ts @@ -81,6 +81,14 @@ describe("Set & get watch status", () => { }); it("Return watchstatus in /movies/:id", async () => { + const [r, b] = await setMovieStatus(bubble.slug, { + status: "rewatching", + // we still need to specify all values + completedAt: "2024-12-21", + score: 85, + }); + expectStatus(r, b).toBe(200); + const [resp, body] = await getMovie(bubble.slug, {}); expectStatus(resp, body).toBe(200); expect(body.slug).toBe(bubble.slug);