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:
@@ -29,7 +29,7 @@ test("categories: listed by position, then slug", async () => {
|
||||
test("categories: only category:manage may create, update or delete", async () => {
|
||||
const admin = await createUser({ role: "admin" });
|
||||
const mod = await createUser({ role: "moderator" });
|
||||
const json = { slug: "music", name: "Music" };
|
||||
const json = { kind: "dump", slug: "music", name: "Music" };
|
||||
assertEquals((await post("/api/categories", { json })).status, 401);
|
||||
assertEquals(
|
||||
(await post("/api/categories", { auth: mod.auth, json })).status,
|
||||
@@ -64,13 +64,17 @@ test("categories: only category:manage may create, update or delete", async () =
|
||||
|
||||
test("categories: slug and name rules, and friendly conflicts", async () => {
|
||||
const admin = await createUser({ role: "admin" });
|
||||
const create = (json: unknown) =>
|
||||
post<Category>("/api/categories", { auth: admin.auth, json });
|
||||
const create = (json: Record<string, unknown>) =>
|
||||
post<Category>("/api/categories", {
|
||||
auth: admin.auth,
|
||||
json: { kind: "dump", ...json },
|
||||
});
|
||||
for (
|
||||
const [json, message] of [
|
||||
[{ slug: "Bad Slug", name: "x" }, "lowercase"],
|
||||
[{ slug: "hot", name: "x" }, "reserved"],
|
||||
[{ slug: "settings", name: "x" }, "reserved"],
|
||||
[{ slug: "playlists", name: "x" }, "reserved"],
|
||||
[{ slug: "a".repeat(51), name: "x" }, "at most 50"],
|
||||
[{ slug: "ok", name: " " }, "Name is required"],
|
||||
[{ slug: "ok", name: "x".repeat(51) }, "at most 50"],
|
||||
@@ -144,3 +148,37 @@ test("GET /api/dumps?category=: members only, private for the owner", async () =
|
||||
[],
|
||||
);
|
||||
});
|
||||
|
||||
test("categories: each kind is a set apart", async () => {
|
||||
const admin = await createUser({ role: "admin" });
|
||||
const create = (json: Record<string, unknown>) =>
|
||||
post<Category>("/api/categories", { auth: admin.auth, json });
|
||||
|
||||
assertEquals((await create({ slug: "music", name: "Music" })).status, 400);
|
||||
const dump = await create({ kind: "dump", slug: "music", name: "Music" });
|
||||
assertEquals(dump.data.kind, "dump");
|
||||
// The same slug and name are free in the other kind, taken in the same.
|
||||
const playlist = await create({
|
||||
kind: "playlist",
|
||||
slug: "music",
|
||||
name: "Music",
|
||||
});
|
||||
assertEquals(playlist.status, 201);
|
||||
assertEquals(playlist.data.kind, "playlist");
|
||||
assertEquals(
|
||||
(await create({ kind: "playlist", slug: "music", name: "Other" })).status,
|
||||
409,
|
||||
);
|
||||
// A playlist category's page lives under /playlists/: nothing is reserved.
|
||||
assertEquals(
|
||||
(await create({ kind: "playlist", slug: "hot", name: "Hot" })).status,
|
||||
201,
|
||||
);
|
||||
|
||||
const list = await get<Category[]>("/api/categories");
|
||||
assertEquals(list.data.map((c) => [c.kind, c.slug]), [
|
||||
["dump", "music"],
|
||||
["playlist", "hot"],
|
||||
["playlist", "music"],
|
||||
]);
|
||||
});
|
||||
|
||||
@@ -130,6 +130,18 @@ test("POST /api/dumps: an unknown category is refused and nothing is left behind
|
||||
assertEquals(count(`SELECT COUNT(*) as n FROM dumps;`), 0);
|
||||
});
|
||||
|
||||
test("POST /api/dumps: a playlist category is refused", async () => {
|
||||
const user = await createUser();
|
||||
const lists = insertCategory({ slug: "mixes", kind: "playlist" });
|
||||
using _ = stubNetwork(() => PAGE.clone());
|
||||
const res = await post("/api/dumps", {
|
||||
auth: user.auth,
|
||||
json: { url: "https://example.com/e", categoryIds: [lists] },
|
||||
});
|
||||
assertEquals(res.status, 400);
|
||||
assertEquals(count(`SELECT COUNT(*) as n FROM dumps;`), 0);
|
||||
});
|
||||
|
||||
test("POST /api/dumps: a file dump is stored and served back", async () => {
|
||||
const user = await createUser();
|
||||
const res = await post<Dump>("/api/dumps", {
|
||||
|
||||
@@ -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,
|
||||
[],
|
||||
);
|
||||
});
|
||||
|
||||
@@ -2,6 +2,7 @@ import {
|
||||
createUser,
|
||||
db,
|
||||
get,
|
||||
insertCategory,
|
||||
insertDump,
|
||||
insertPlaylist,
|
||||
test,
|
||||
@@ -62,3 +63,44 @@ test("GET /api/search: dumps, users and playlists, privacy-filtered", async () =
|
||||
playlists: [],
|
||||
});
|
||||
});
|
||||
|
||||
test("GET /api/search: category and playlistCategory narrow each list", async () => {
|
||||
const owner = await createUser();
|
||||
const music = insertCategory({ slug: "music" });
|
||||
const mixes = insertCategory({ slug: "mixes", kind: "playlist" });
|
||||
const filed = insertDump({ userId: owner.id, title: "Banana song" });
|
||||
insertDump({ userId: owner.id, title: "Banana bread" });
|
||||
const list = insertPlaylist({ userId: owner.id, title: "Banana mix" });
|
||||
insertPlaylist({ userId: owner.id, title: "Banana bin" });
|
||||
db.prepare(
|
||||
`INSERT INTO dump_categories (dump_id, category_id) VALUES (?, ?);`,
|
||||
).run(filed.id, music);
|
||||
db.prepare(
|
||||
`INSERT INTO playlist_categories (playlist_id, category_id) VALUES (?, ?);`,
|
||||
).run(list.id, mixes);
|
||||
|
||||
const all = (await get<SearchResults>("/api/search?q=banana")).data;
|
||||
assertEquals(all.dumps.total, 2);
|
||||
assertEquals(all.playlists.length, 2);
|
||||
|
||||
const dumps = (await get<SearchResults>(
|
||||
"/api/search?q=banana&category=music",
|
||||
)).data;
|
||||
assertEquals(dumps.dumps.items.map((d) => d.id), [filed.id]);
|
||||
assertEquals(dumps.dumps.total, 1);
|
||||
assertEquals(dumps.playlists.length, 2);
|
||||
|
||||
const playlists = (await get<SearchResults>(
|
||||
"/api/search?q=banana&playlistCategory=mixes",
|
||||
)).data;
|
||||
assertEquals(playlists.dumps.total, 2);
|
||||
assertEquals(playlists.playlists.map((p) => p.id), [list.id]);
|
||||
assertEquals(playlists.playlists[0].categoryIds, [mixes]);
|
||||
|
||||
// Each names a category of its own kind only.
|
||||
const crossed = (await get<SearchResults>(
|
||||
"/api/search?q=banana&category=mixes&playlistCategory=music",
|
||||
)).data;
|
||||
assertEquals(crossed.dumps.total, 0);
|
||||
assertEquals(crossed.playlists, []);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user