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:
@@ -37,6 +37,12 @@ import {
|
||||
} from "./notification-service.ts";
|
||||
import { changeSlug, newSlug, resolveSlug } from "./slug-service.ts";
|
||||
import { linkAttachments } from "./attachment-service.ts";
|
||||
import {
|
||||
assertCategoriesExist,
|
||||
attachCategoryIds,
|
||||
categoryJoin,
|
||||
setCategories,
|
||||
} from "./category-service.ts";
|
||||
import {
|
||||
isMemberVisibleTo,
|
||||
type PlaylistMember,
|
||||
@@ -53,6 +59,16 @@ const PLAYLIST_SELECT = `p.*, u.username as owner_username,
|
||||
(SELECT COUNT(*) FROM playlist_dumps pd WHERE pd.playlist_id = p.id) as dump_count
|
||||
FROM playlists p JOIN users u ON u.id = p.user_id`;
|
||||
|
||||
/**
|
||||
* Converts the rows of a {@link PLAYLIST_SELECT} to playlists, their
|
||||
* categories filled in.
|
||||
*/
|
||||
function playlistsOf(rows: unknown[]): Playlist[] {
|
||||
const playlists = parseRows(rows, isPlaylistRow, playlistRowToApi);
|
||||
attachCategoryIds("playlist", playlists);
|
||||
return playlists;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns playlist `playlistId`, private or not. A caller that shows it to
|
||||
* someone must check `isPublic` first, as the OG middleware does.
|
||||
@@ -65,7 +81,9 @@ export function getPlaylistById(playlistId: string): Playlist {
|
||||
if (!row || !isPlaylistRow(row)) {
|
||||
throw new APIException(404, "Playlist not found");
|
||||
}
|
||||
return playlistRowToApi(row);
|
||||
const playlist = playlistRowToApi(row);
|
||||
attachCategoryIds("playlist", [playlist]);
|
||||
return playlist;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -80,9 +98,12 @@ export function getPlaylistByPath(userId: string, slug: string): Playlist {
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a playlist owned by `userId`, for `POST /api/playlists`. If it is
|
||||
* public, it notifies the users its description mentions. It links the
|
||||
* attachments the description embeds and broadcasts `playlist_created`.
|
||||
* Creates a playlist owned by `userId`, for `POST /api/playlists`, filed under
|
||||
* the playlist categories `req` names. If it is public, it notifies the users
|
||||
* its description mentions. It links the attachments the description embeds
|
||||
* and broadcasts `playlist_created`.
|
||||
*
|
||||
* @throws {APIException} 400 when a category id names no playlist category.
|
||||
*/
|
||||
export function createPlaylist(
|
||||
req: CreatePlaylistRequest,
|
||||
@@ -91,6 +112,7 @@ export function createPlaylist(
|
||||
const id = crypto.randomUUID();
|
||||
const createdAt = new Date();
|
||||
const title = req.title.trim();
|
||||
assertCategoriesExist("playlist", req.categoryIds ?? []);
|
||||
transaction(() => {
|
||||
db.prepare(
|
||||
`INSERT INTO playlists (id, user_id, title, slug, description, is_public, created_at)
|
||||
@@ -104,6 +126,9 @@ export function createPlaylist(
|
||||
req.isPublic ? 1 : 0,
|
||||
createdAt.toISOString(),
|
||||
);
|
||||
if (req.categoryIds?.length) {
|
||||
setCategories("playlist", id, req.categoryIds);
|
||||
}
|
||||
});
|
||||
const playlist = getPlaylistById(id);
|
||||
if (req.description) {
|
||||
@@ -157,27 +182,50 @@ export function getPlaylist(
|
||||
|
||||
/**
|
||||
* Returns up to `limit` playlists whose title or description contains
|
||||
* `query`, newest first, for `GET /api/search`. Private playlists are left
|
||||
* out, except `requestingUserId`'s own. A blank `query` finds nothing.
|
||||
* `query`, newest first, for `GET /api/search`, optionally only those in the
|
||||
* playlist category `categoryId`. Private playlists are left out, except
|
||||
* `requestingUserId`'s own. A blank `query` finds nothing.
|
||||
*/
|
||||
export function searchPlaylists(
|
||||
query: string,
|
||||
limit: number,
|
||||
requestingUserId?: string,
|
||||
categoryId?: string,
|
||||
): Playlist[] {
|
||||
if (!query.trim()) return [];
|
||||
const pattern = likePattern(query);
|
||||
const searchClause = `(p.title ${LIKE} OR p.description ${LIKE})`;
|
||||
const [catJoin, catParams] = categoryJoin("playlist", "p", categoryId);
|
||||
|
||||
const rows = requestingUserId
|
||||
? db.prepare(
|
||||
`SELECT ${PLAYLIST_SELECT} WHERE (p.is_public = 1 OR p.user_id = ?) AND ${searchClause} ORDER BY p.created_at DESC LIMIT ?;`,
|
||||
).all(requestingUserId, pattern, pattern, limit)
|
||||
`SELECT ${PLAYLIST_SELECT} ${catJoin} WHERE (p.is_public = 1 OR p.user_id = ?) AND ${searchClause} ORDER BY p.created_at DESC LIMIT ?;`,
|
||||
).all(...catParams, requestingUserId, pattern, pattern, limit)
|
||||
: db.prepare(
|
||||
`SELECT ${PLAYLIST_SELECT} WHERE p.is_public = 1 AND ${searchClause} ORDER BY p.created_at DESC LIMIT ?;`,
|
||||
).all(pattern, pattern, limit);
|
||||
`SELECT ${PLAYLIST_SELECT} ${catJoin} WHERE p.is_public = 1 AND ${searchClause} ORDER BY p.created_at DESC LIMIT ?;`,
|
||||
).all(...catParams, pattern, pattern, limit);
|
||||
|
||||
return parseRows(rows, isPlaylistRow, playlistRowToApi);
|
||||
return playlistsOf(rows);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a page of every public playlist, newest first, for
|
||||
* `GET /api/playlists`, optionally only those in the playlist category
|
||||
* `categoryId`. The client's `/playlists` page lists them.
|
||||
*/
|
||||
export function listPublicPlaylists(
|
||||
page: number,
|
||||
limit: number,
|
||||
categoryId?: string,
|
||||
): Page<Playlist> {
|
||||
const offset = (page - 1) * limit;
|
||||
const [catJoin, catParams] = categoryJoin("playlist", "p", categoryId);
|
||||
const rows = db.prepare(
|
||||
`SELECT ${PLAYLIST_SELECT} ${catJoin}
|
||||
WHERE p.is_public = 1
|
||||
ORDER BY p.created_at DESC LIMIT ? OFFSET ?;`,
|
||||
).all(...catParams, limit + 1, offset);
|
||||
return pageOf(playlistsOf(rows), limit);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -191,12 +239,13 @@ export function listNewPublicPlaylists(from: Date, to: Date): Playlist[] {
|
||||
WHERE p.is_public = 1 AND p.created_at >= ? AND p.created_at < ?
|
||||
ORDER BY dump_count DESC, p.created_at DESC;`,
|
||||
).all(from.toISOString(), to.toISOString());
|
||||
return parseRows(rows, isPlaylistRow, playlistRowToApi);
|
||||
return playlistsOf(rows);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a page of `userId`'s playlists, newest first, for
|
||||
* `GET /api/users/:key/playlists`. The private ones are listed only when
|
||||
* `GET /api/users/:key/playlists`, optionally only those in the playlist
|
||||
* category `categoryId`. The private ones are listed only when
|
||||
* `requestingUserId` is `userId`.
|
||||
*/
|
||||
export function listPlaylistsByUser(
|
||||
@@ -204,16 +253,19 @@ export function listPlaylistsByUser(
|
||||
requestingUserId: string | null,
|
||||
page: number,
|
||||
limit: number,
|
||||
categoryId?: string,
|
||||
): Page<Playlist> {
|
||||
const isOwner = requestingUserId === userId;
|
||||
const offset = (page - 1) * limit;
|
||||
const privacyFilter = isOwner ? "" : " AND p.is_public = 1";
|
||||
const [catJoin, catParams] = categoryJoin("playlist", "p", categoryId);
|
||||
|
||||
const sql = isOwner
|
||||
? `SELECT ${PLAYLIST_SELECT} WHERE p.user_id = ? ORDER BY p.created_at DESC LIMIT ? OFFSET ?;`
|
||||
: `SELECT ${PLAYLIST_SELECT} WHERE p.user_id = ? AND p.is_public = 1 ORDER BY p.created_at DESC LIMIT ? OFFSET ?;`;
|
||||
|
||||
const rows = db.prepare(sql).all(userId, limit + 1, offset);
|
||||
return pageOf(parseRows(rows, isPlaylistRow, playlistRowToApi), limit);
|
||||
const rows = db.prepare(
|
||||
`SELECT ${PLAYLIST_SELECT} ${catJoin}
|
||||
WHERE p.user_id = ?${privacyFilter}
|
||||
ORDER BY p.created_at DESC LIMIT ? OFFSET ?;`,
|
||||
).all(...catParams, userId, limit + 1, offset);
|
||||
return pageOf(playlistsOf(rows), limit);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -227,8 +279,9 @@ export function listPlaylistsByUser(
|
||||
* attachments and broadcasts `playlist_updated`.
|
||||
*
|
||||
* @throws {APIException} 404 when there is no such playlist, 403 when the user
|
||||
* is neither its owner nor a moderator, 400 when the slug isn't valid, 409
|
||||
* when it is taken, 429 when it changed too often lately.
|
||||
* is neither its owner nor a moderator, 400 when the slug isn't valid or a
|
||||
* category id names no playlist category, 409 when the slug is taken, 429
|
||||
* when it changed too often lately.
|
||||
*/
|
||||
export function updatePlaylist(
|
||||
playlistId: string,
|
||||
@@ -250,20 +303,28 @@ export function updatePlaylist(
|
||||
? req.isPublic
|
||||
: playlist.isPublic;
|
||||
|
||||
if (req.categoryIds !== undefined) {
|
||||
assertCategoriesExist("playlist", req.categoryIds);
|
||||
}
|
||||
if (req.slug !== undefined) {
|
||||
changeSlug("playlist", playlist, req.slug, canModerate);
|
||||
}
|
||||
|
||||
const now = new Date();
|
||||
db.prepare(
|
||||
`UPDATE playlists SET title = ?, description = ?, is_public = ?, updated_at = ? WHERE id = ?;`,
|
||||
).run(
|
||||
newTitle,
|
||||
newDescription,
|
||||
newIsPublic ? 1 : 0,
|
||||
now.toISOString(),
|
||||
playlist.id,
|
||||
);
|
||||
transaction(() => {
|
||||
db.prepare(
|
||||
`UPDATE playlists SET title = ?, description = ?, is_public = ?, updated_at = ? WHERE id = ?;`,
|
||||
).run(
|
||||
newTitle,
|
||||
newDescription,
|
||||
newIsPublic ? 1 : 0,
|
||||
now.toISOString(),
|
||||
playlist.id,
|
||||
);
|
||||
if (req.categoryIds !== undefined) {
|
||||
setCategories("playlist", playlist.id, req.categoryIds);
|
||||
}
|
||||
});
|
||||
|
||||
const updated: Playlist = {
|
||||
...playlist,
|
||||
@@ -272,6 +333,9 @@ export function updatePlaylist(
|
||||
description: newDescription ?? undefined,
|
||||
isPublic: newIsPublic,
|
||||
updatedAt: now,
|
||||
categoryIds: req.categoryIds !== undefined
|
||||
? [...new Set(req.categoryIds)]
|
||||
: playlist.categoryIds,
|
||||
};
|
||||
if (newDescription) {
|
||||
if (newIsPublic) {
|
||||
@@ -536,7 +600,7 @@ export function getPlaylistMembershipsForDump(
|
||||
ORDER BY p.created_at DESC;`,
|
||||
).all(dumpId, userId);
|
||||
|
||||
return rows.map((row) => {
|
||||
const memberships = rows.map((row) => {
|
||||
if (!isPlaylistRow(row)) {
|
||||
throw new APIException(500, "Malformed playlist data");
|
||||
}
|
||||
@@ -545,6 +609,8 @@ export function getPlaylistMembershipsForDump(
|
||||
hasDump: Boolean(row.has_dump),
|
||||
};
|
||||
});
|
||||
attachCategoryIds("playlist", memberships.map((m) => m.playlist));
|
||||
return memberships;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -564,7 +630,7 @@ export function getPlaylistsForDump(
|
||||
ORDER BY p.created_at DESC LIMIT 20;`,
|
||||
).all(dumpId, requestingUserId ?? "");
|
||||
|
||||
return parseRows(rows, isPlaylistRow, playlistRowToApi);
|
||||
return playlistsOf(rows);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user