v4: playlists sort by their last dump, their creation or their size, and /playlists is a grid of covers — every list of playlists came newest first, so one fed every week sank under those made since and left empty. GET /api/playlists and /api/users/:key/playlists take ?sort=updated|new|dumps (PLAYLIST_SORTS, shared/playlist.ts), updated being the default: the last to take a dump first, or its creation while it holds none; a new title, cover or description doesn't bring it back up. A playlist carries that date as lastDumpAt, read with its dumpCount by isMemberVisibleTo's rule (VISIBLE_DUMP_FIGURES, the viewer's id as ?1), so the owner's private dumps date and count their playlists for them alone, in the lists and over the socket: playlist_created, playlist_updated and playlist_dumps_updated carry each socket's own. The /playlists page and a profile's playlists tab show the orders as tabs beside the category menu and keep the one picked in ?sort=, the default being the bare path; a tab takes the current history entry's place, and picking a category keeps the order. Live, usePlaylistListSync takes the list's order: a new playlist goes where it puts it, one that rises moves up to its place, as in the updated order when it takes a dump, and one that falls stays until the list is fetched again, its place being perhaps on a page not loaded. The lists in an order of their own, the followed playlists and the search's, pass none, and take no new playlist as before (noNewEntries goes). comparePlaylists is the order on the client, which a test holds to the API's.
/playlists lays its cards out as tiles, the cover across the top, four to a row and two on a phone, on the wide column: the same PlaylistCard, laid out by its list's class (.playlist-grid), so each theme's card skin carries over. It drops the "public" badge there, every playlist listed being public. On every card, a playlist without a cover shows a tile of its own colour with its initial (ThumbnailPlaceholder) in place of the 📋, and the date is "updated …" of its last dump, or "created …". A middle, ctrl, cmd or shift click anywhere on a playlist card opens it in a new tab — only its title was a link, the rest navigating by a click handler. useCardLink (hooks/shared) is useDumpCardLink's handlers for any card, which the dump cards now get through it. A profile tab's query stays with the tab — going from the playlists to the dumps kept the whole query, so a playlist category, and now ?sort=, landed on the dumps' URL. Only the dump lists pass their category to each other; any other change of tab drops the query. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -12,6 +12,7 @@ import type {
|
||||
CreatePlaylistRequest,
|
||||
Playlist,
|
||||
PlaylistMembership,
|
||||
PlaylistSort,
|
||||
PlaylistWithDumps,
|
||||
UpdatePlaylistRequest,
|
||||
} from "../../shared/playlist.ts";
|
||||
@@ -42,25 +43,36 @@ import {
|
||||
setCategories,
|
||||
} from "./category-service.ts";
|
||||
import {
|
||||
type DatedPlaylistMember,
|
||||
isMemberVisibleTo,
|
||||
type PlaylistMember,
|
||||
VISIBLE_DUMP_COUNT,
|
||||
VISIBLE_DUMP_FIGURES,
|
||||
} from "../lib/playlist-members.ts";
|
||||
import { LIKE, likePattern } from "../lib/like.ts";
|
||||
import { parseRows } from "../model/rows.ts";
|
||||
|
||||
/**
|
||||
* The select list and `FROM` clause of a playlist query, adding the owner's
|
||||
* name and the count of the dumps the viewer may see. Callers append the
|
||||
* `WHERE`, and pass the viewer's id first, as {@link viewerParam} gives it.
|
||||
* name, the count of the dumps the viewer may see and when the latest was
|
||||
* added. Callers append the `WHERE`, and pass the viewer's id first, as
|
||||
* {@link viewerParam} gives it.
|
||||
*/
|
||||
const PLAYLIST_SELECT = `p.*, u.username as owner_username,
|
||||
${VISIBLE_DUMP_COUNT}
|
||||
${VISIBLE_DUMP_FIGURES}
|
||||
FROM playlists p JOIN users u ON u.id = p.user_id`;
|
||||
|
||||
/**
|
||||
* Returns what {@link VISIBLE_DUMP_COUNT}'s parameter takes for `viewerId`: a
|
||||
* signed-out viewer matches no user.
|
||||
* The `ORDER BY` of each of the `PLAYLIST_SORTS`, on a {@link PLAYLIST_SELECT}:
|
||||
* `comparePlaylists` (`shared/playlist.ts`) in SQL, which a test holds to it.
|
||||
*/
|
||||
const PLAYLIST_ORDER: Record<PlaylistSort, string> = {
|
||||
updated: "COALESCE(last_dump_at, p.created_at) DESC, p.created_at DESC",
|
||||
new: "p.created_at DESC",
|
||||
dumps: "dump_count DESC, p.created_at DESC",
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns what {@link VISIBLE_DUMP_FIGURES}'s parameter takes for `viewerId`:
|
||||
* a signed-out viewer matches no user.
|
||||
*/
|
||||
function viewerParam(viewerId: string | null | undefined): string {
|
||||
return viewerId ?? "";
|
||||
@@ -79,7 +91,8 @@ function playlistsOf(rows: unknown[]): Playlist[] {
|
||||
/**
|
||||
* Returns playlist `playlistId`, private or not. A caller that shows it to
|
||||
* someone must check `isPublic` first, as the OG middleware does. Its
|
||||
* `dumpCount` is `viewerId`'s, or a signed-out viewer's without one.
|
||||
* `dumpCount` and `lastDumpAt` are `viewerId`'s, or a signed-out viewer's
|
||||
* without one.
|
||||
*
|
||||
* @throws {APIException} 404 when there is no such playlist.
|
||||
*/
|
||||
@@ -99,13 +112,18 @@ export function getPlaylistById(
|
||||
|
||||
/**
|
||||
* Returns the playlist of `userId`'s whose slug is or was `slug`, private or
|
||||
* not, or throws a 404, as {@link getPlaylistById} does. Reached by a former
|
||||
* slug, it comes with its current one.
|
||||
* not, or throws a 404, as {@link getPlaylistById} does, counting its dumps
|
||||
* for `viewerId` likewise. Reached by a former slug, it comes with its current
|
||||
* one.
|
||||
*/
|
||||
export function getPlaylistByPath(userId: string, slug: string): Playlist {
|
||||
export function getPlaylistByPath(
|
||||
userId: string,
|
||||
slug: string,
|
||||
viewerId: string | null = null,
|
||||
): Playlist {
|
||||
const playlistId = resolveSlug("playlist", userId, slug);
|
||||
if (!playlistId) throw new APIException(404, "Playlist not found");
|
||||
return getPlaylistById(playlistId);
|
||||
return getPlaylistById(playlistId, viewerId);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -157,8 +175,8 @@ export function createPlaylist(
|
||||
* Returns a playlist with the dumps `requestingUserId` may see in it, in
|
||||
* order, for `GET /api/playlists/:playlistId`, and for
|
||||
* `GET /api/users/:key/playlists/:slug` once it has found the playlist by its
|
||||
* path. A null `requestingUserId` is a signed-out viewer. Its `dumpCount` is
|
||||
* the number of those dumps.
|
||||
* path, for the same viewer. A null `requestingUserId` is a signed-out viewer.
|
||||
* Its `dumpCount` is the number of those dumps.
|
||||
*
|
||||
* @throws {APIException} 404 when there is no such playlist, or it is private
|
||||
* and not theirs.
|
||||
@@ -168,7 +186,7 @@ export function getPlaylist(
|
||||
requestingUserId: string | null,
|
||||
): PlaylistWithDumps {
|
||||
const playlist = typeof playlistId === "string"
|
||||
? getPlaylistById(playlistId)
|
||||
? getPlaylistById(playlistId, requestingUserId)
|
||||
: playlistId;
|
||||
|
||||
if (!playlist.isPublic && requestingUserId !== playlist.userId) {
|
||||
@@ -228,15 +246,17 @@ export function searchPlaylists(
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a page of every public playlist, newest first, for
|
||||
* Returns a page of every public playlist, in the order `sort` names, for
|
||||
* `GET /api/playlists`, optionally only those in the playlist category
|
||||
* `categoryId`. The client's `/playlists` page lists them. Their dumps are
|
||||
* counted for `requestingUserId`, null being a signed-out viewer.
|
||||
* counted, and so ordered, for `requestingUserId`, null being a signed-out
|
||||
* viewer.
|
||||
*/
|
||||
export function listPublicPlaylists(
|
||||
requestingUserId: string | null,
|
||||
page: number,
|
||||
limit: number,
|
||||
sort: PlaylistSort,
|
||||
categoryId?: string,
|
||||
): Page<Playlist> {
|
||||
const offset = (page - 1) * limit;
|
||||
@@ -244,7 +264,7 @@ export function listPublicPlaylists(
|
||||
const rows = db.prepare(
|
||||
`SELECT ${PLAYLIST_SELECT} ${catJoin}
|
||||
WHERE p.is_public = 1
|
||||
ORDER BY p.created_at DESC LIMIT ? OFFSET ?;`,
|
||||
ORDER BY ${PLAYLIST_ORDER[sort]} LIMIT ? OFFSET ?;`,
|
||||
).all(viewerParam(requestingUserId), ...catParams, limit + 1, offset);
|
||||
return pageOf(playlistsOf(rows), limit);
|
||||
}
|
||||
@@ -259,13 +279,13 @@ export function listNewPublicPlaylists(from: Date, to: Date): Playlist[] {
|
||||
const rows = db.prepare(
|
||||
`SELECT ${PLAYLIST_SELECT}
|
||||
WHERE p.is_public = 1 AND p.created_at >= ? AND p.created_at < ?
|
||||
ORDER BY dump_count DESC, p.created_at DESC;`,
|
||||
ORDER BY ${PLAYLIST_ORDER.dumps};`,
|
||||
).all(viewerParam(null), from.toISOString(), to.toISOString());
|
||||
return playlistsOf(rows);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a page of `userId`'s playlists, newest first, for
|
||||
* Returns a page of `userId`'s playlists, in the order `sort` names, for
|
||||
* `GET /api/users/:key/playlists`, optionally only those in the playlist
|
||||
* category `categoryId`. The private ones are listed only when
|
||||
* `requestingUserId` is `userId`.
|
||||
@@ -275,6 +295,7 @@ export function listPlaylistsByUser(
|
||||
requestingUserId: string | null,
|
||||
page: number,
|
||||
limit: number,
|
||||
sort: PlaylistSort,
|
||||
categoryId?: string,
|
||||
): Page<Playlist> {
|
||||
const isOwner = requestingUserId === userId;
|
||||
@@ -285,7 +306,7 @@ export function listPlaylistsByUser(
|
||||
const rows = db.prepare(
|
||||
`SELECT ${PLAYLIST_SELECT} ${catJoin}
|
||||
WHERE p.user_id = ?${privacyFilter}
|
||||
ORDER BY p.created_at DESC LIMIT ? OFFSET ?;`,
|
||||
ORDER BY ${PLAYLIST_ORDER[sort]} LIMIT ? OFFSET ?;`,
|
||||
).all(
|
||||
viewerParam(requestingUserId),
|
||||
...catParams,
|
||||
@@ -620,7 +641,7 @@ export function getPlaylistMembershipsForDump(
|
||||
): PlaylistMembership[] {
|
||||
const rows = db.prepare(
|
||||
`SELECT p.*, u.username as owner_username,
|
||||
${VISIBLE_DUMP_COUNT},
|
||||
${VISIBLE_DUMP_FIGURES},
|
||||
EXISTS (SELECT 1 FROM playlist_dumps pd
|
||||
WHERE pd.playlist_id = p.id AND pd.dump_id = ?) as has_dump
|
||||
FROM playlists p JOIN users u ON u.id = p.user_id
|
||||
@@ -690,18 +711,19 @@ export function getPlaylistImageInfo(
|
||||
* Returns a playlist's dumps in order, all of them, for the callers to filter
|
||||
* per viewer.
|
||||
*/
|
||||
function getCurrentMembers(playlistId: string): PlaylistMember[] {
|
||||
function getCurrentMembers(playlistId: string): DatedPlaylistMember[] {
|
||||
const rows = db.prepare(
|
||||
`SELECT pd.dump_id, d.is_private, d.user_id
|
||||
`SELECT pd.dump_id, pd.added_at, d.is_private, d.user_id
|
||||
FROM playlist_dumps pd INNER JOIN dumps d ON d.id = pd.dump_id
|
||||
WHERE pd.playlist_id = ? ORDER BY pd.position ASC;`,
|
||||
).all(playlistId) as Array<
|
||||
{ dump_id: string; is_private: number; user_id: string }
|
||||
{ dump_id: string; added_at: string; is_private: number; user_id: string }
|
||||
>;
|
||||
return rows.map((r) => ({
|
||||
id: r.dump_id,
|
||||
isPrivate: Boolean(r.is_private),
|
||||
userId: r.user_id,
|
||||
addedAt: new Date(r.added_at),
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user