v4: playlists have categories of their own — a set apart from the dumps', which admins manage on /manage under "Playlist categories", next to "Dump categories". Each category now has a kind, dump or playlist, its slug and name unique within its kind only; migration 0025 rebuilds categories with the column, every existing one becoming a dump category with its dumps, and adds playlist_categories. A playlist's owner files it under any of them in the create and edit forms, and it shows them as chips on its card and its page, each leading to /playlists/<slug>.
/playlists is the new page of every public playlist, newest first (GET /api/playlists), and /playlists/<slug> a playlist category's, with a menu of the categories, linked from the user menu as "All playlists"; `playlists` is reserved as a dump category's slug, and one that had it is renamed. A profile's Playlists tab narrows to a playlist category with the same menu as its Dumps tab (?category=), and so do the Dumps and Playlists tabs of /search, each with its own kind (category and playlistCategory for GET /api/search), another tab dropping it. The category service takes the kind throughout, so a dump can't be filed under a playlist category nor the other way round, and categoryJoin is the one filter join the dump, follow and playlist services share. useCategories(kind) gives one kind's set from the one fetch, CategoryChips replaces the chips the dump card and page each drew, useCategoryFilter keeps a list to its category as the socket brings more, and useCategorySelection holds a form's picked categories. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -6,6 +6,7 @@ import {
|
||||
del,
|
||||
formWithFile,
|
||||
get,
|
||||
insertCategory,
|
||||
insertDump,
|
||||
insertPlaylist,
|
||||
PNG,
|
||||
@@ -539,3 +540,126 @@ test("reorder: a moderator orders around the owner's private dumps", async () =>
|
||||
b.id,
|
||||
]);
|
||||
});
|
||||
|
||||
test("playlist categories: filed on creation, replaced on update, dump categories refused", async () => {
|
||||
const user = await createUser();
|
||||
const mixes = insertCategory({ slug: "mixes", kind: "playlist" });
|
||||
const live = insertCategory({ slug: "live", kind: "playlist" });
|
||||
const music = insertCategory({ slug: "music" });
|
||||
|
||||
const refused = await post("/api/playlists", {
|
||||
auth: user.auth,
|
||||
json: { title: "Wrong", isPublic: true, categoryIds: [music] },
|
||||
});
|
||||
assertEquals(refused.status, 400);
|
||||
assertEquals(count(`SELECT COUNT(*) as n FROM playlists;`), 0);
|
||||
assertEquals(
|
||||
(await post("/api/playlists", {
|
||||
auth: user.auth,
|
||||
json: { title: "Bad", isPublic: true, categoryIds: "mixes" },
|
||||
})).status,
|
||||
400,
|
||||
);
|
||||
|
||||
const created = await post<Playlist>("/api/playlists", {
|
||||
auth: user.auth,
|
||||
json: { title: "Mix", isPublic: true, categoryIds: [mixes, mixes] },
|
||||
});
|
||||
assertEquals(created.status, 201);
|
||||
assertEquals(created.data.categoryIds, [mixes]);
|
||||
assertEquals(
|
||||
(await get<Playlist>(`/api/playlists/${created.data.id}`)).data
|
||||
.categoryIds,
|
||||
[mixes],
|
||||
);
|
||||
|
||||
const id = created.data.id;
|
||||
assertEquals(
|
||||
(await put(`/api/playlists/${id}`, {
|
||||
auth: user.auth,
|
||||
json: { categoryIds: [music] },
|
||||
})).status,
|
||||
400,
|
||||
);
|
||||
const updated = await put<Playlist>(`/api/playlists/${id}`, {
|
||||
auth: user.auth,
|
||||
json: { categoryIds: [live] },
|
||||
});
|
||||
assertEquals(updated.data.categoryIds, [live]);
|
||||
// A change of anything else keeps them.
|
||||
const renamed = await put<Playlist>(`/api/playlists/${id}`, {
|
||||
auth: user.auth,
|
||||
json: { title: "Live mix" },
|
||||
});
|
||||
assertEquals(renamed.data.categoryIds, [live]);
|
||||
const cleared = await put<Playlist>(`/api/playlists/${id}`, {
|
||||
auth: user.auth,
|
||||
json: { categoryIds: [] },
|
||||
});
|
||||
assertEquals(cleared.data.categoryIds, []);
|
||||
});
|
||||
|
||||
test("GET /api/playlists: every public playlist, newest first, by category", async () => {
|
||||
const owner = await createUser();
|
||||
const other = await createUser();
|
||||
const mixes = insertCategory({ slug: "mixes", kind: "playlist" });
|
||||
insertCategory({ slug: "music" });
|
||||
const a = insertPlaylist({ userId: owner.id, title: "A" });
|
||||
insertPlaylist({ userId: other.id, title: "B" });
|
||||
const hidden = insertPlaylist({
|
||||
userId: owner.id,
|
||||
title: "Hidden",
|
||||
isPublic: false,
|
||||
});
|
||||
db.prepare(`UPDATE playlists SET created_at = ? WHERE id = ?;`)
|
||||
.run("2026-01-01T00:00:00.000Z", a.id);
|
||||
for (const p of [a, hidden]) {
|
||||
db.prepare(
|
||||
`INSERT INTO playlist_categories (playlist_id, category_id) VALUES (?, ?);`,
|
||||
).run(p.id, mixes);
|
||||
}
|
||||
|
||||
const all = await get<PaginatedData<Playlist>>("/api/playlists", {
|
||||
auth: owner.auth,
|
||||
});
|
||||
assertEquals(all.data.items.map((p) => p.title), ["B", "A"]);
|
||||
assertEquals(all.data.items[1].categoryIds, [mixes]);
|
||||
assertEquals(
|
||||
(await get<PaginatedData<Playlist>>("/api/playlists?limit=1")).data
|
||||
.hasMore,
|
||||
true,
|
||||
);
|
||||
assertEquals(
|
||||
(await get<PaginatedData<Playlist>>("/api/playlists?category=mixes")).data
|
||||
.items.map((p) => p.id),
|
||||
[a.id],
|
||||
);
|
||||
// A dump category's slug names no playlist category.
|
||||
assertEquals((await get("/api/playlists?category=music")).status, 404);
|
||||
assertEquals((await get("/api/playlists?category=nope")).status, 404);
|
||||
});
|
||||
|
||||
test("GET /api/users/:key/playlists?category=: their playlists in one category", async () => {
|
||||
const owner = await createUser();
|
||||
const mixes = insertCategory({ slug: "mixes", kind: "playlist" });
|
||||
const a = insertPlaylist({ userId: owner.id, title: "A" });
|
||||
insertPlaylist({ userId: owner.id, title: "B" });
|
||||
db.prepare(
|
||||
`INSERT INTO playlist_categories (playlist_id, category_id) VALUES (?, ?);`,
|
||||
).run(a.id, mixes);
|
||||
|
||||
const url = `/api/users/${owner.id}/playlists`;
|
||||
assertEquals(
|
||||
(await get<PaginatedData<Playlist>>(url)).data.items.length,
|
||||
2,
|
||||
);
|
||||
assertEquals(
|
||||
(await get<PaginatedData<Playlist>>(`${url}?category=mixes`)).data.items
|
||||
.map((p) => p.id),
|
||||
[a.id],
|
||||
);
|
||||
assertEquals(
|
||||
(await get<PaginatedData<Playlist>>(`${url}?category=nope`)).data.items,
|
||||
[],
|
||||
);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user