v2: global player, infinite scroll, image picker, threaded comments
This commit is contained in:
@@ -24,7 +24,7 @@ import {
|
||||
} from "./ws-service.ts";
|
||||
|
||||
const DUMP_SELECT_COLS =
|
||||
"id, kind, title, comment, user_id, created_at, url, rich_content, file_name, file_mime, file_size, vote_count";
|
||||
"id, kind, title, comment, user_id, created_at, url, rich_content, file_name, file_mime, file_size, vote_count, is_private";
|
||||
|
||||
function getPlaylistById(playlistId: string): Playlist {
|
||||
const row = db.prepare(`SELECT * FROM playlists WHERE id = ?;`).get(
|
||||
@@ -75,32 +75,54 @@ export function getPlaylist(
|
||||
throw new APIException(APIErrorCode.NOT_FOUND, 404, "Playlist not found");
|
||||
}
|
||||
|
||||
const dumpCols = DUMP_SELECT_COLS.split(", ").map((c) => `d.${c}`).join(", ");
|
||||
const isOwner = requestingUserId === playlist.userId;
|
||||
|
||||
// For public playlists (or when viewed by non-owner), filter out private dumps
|
||||
const rows = db.prepare(
|
||||
`SELECT ${DUMP_SELECT_COLS.split(", ").map((c) => `d.${c}`).join(", ")}
|
||||
`SELECT ${dumpCols}
|
||||
FROM dumps d
|
||||
INNER JOIN playlist_dumps pd ON d.id = pd.dump_id
|
||||
WHERE pd.playlist_id = ?
|
||||
AND (d.is_private = 0 OR d.user_id = ?)
|
||||
ORDER BY pd.position ASC;`,
|
||||
).all(playlistId);
|
||||
).all(playlistId, requestingUserId ?? "");
|
||||
|
||||
const dumps: Dump[] = rows.filter(isDumpRow).map(dumpRowToApi);
|
||||
// Owners always see their own private dumps; strip them for non-owners regardless
|
||||
const visibleDumps = isOwner
|
||||
? dumps
|
||||
: dumps.filter((d) => !d.isPrivate);
|
||||
|
||||
return { ...playlist, dumps };
|
||||
return { ...playlist, dumps: visibleDumps };
|
||||
}
|
||||
|
||||
export function listPlaylistsByUser(
|
||||
userId: string,
|
||||
requestingUserId: string | null,
|
||||
): Playlist[] {
|
||||
page: number,
|
||||
limit: number,
|
||||
): { items: Playlist[]; total: number } {
|
||||
const isOwner = requestingUserId === userId;
|
||||
const offset = (page - 1) * limit;
|
||||
|
||||
const countSql = isOwner
|
||||
? `SELECT COUNT(*) as count FROM playlists WHERE user_id = ?;`
|
||||
: `SELECT COUNT(*) as count FROM playlists WHERE user_id = ? AND is_public = 1;`;
|
||||
const sql = isOwner
|
||||
? `SELECT p.*, (SELECT COUNT(*) FROM playlist_dumps pd WHERE pd.playlist_id = p.id) as dump_count
|
||||
FROM playlists p WHERE p.user_id = ? ORDER BY p.created_at DESC;`
|
||||
FROM playlists p WHERE p.user_id = ? ORDER BY p.created_at DESC LIMIT ? OFFSET ?;`
|
||||
: `SELECT p.*, (SELECT COUNT(*) FROM playlist_dumps pd WHERE pd.playlist_id = p.id) as dump_count
|
||||
FROM playlists p WHERE p.user_id = ? AND p.is_public = 1 ORDER BY p.created_at DESC;`;
|
||||
FROM playlists p WHERE p.user_id = ? AND p.is_public = 1 ORDER BY p.created_at DESC LIMIT ? OFFSET ?;`;
|
||||
|
||||
const rows = db.prepare(sql).all(userId);
|
||||
return rows.filter(isPlaylistRow).map(playlistRowToApi);
|
||||
const totalRow = db.prepare(countSql).get(userId) as
|
||||
| { count: number }
|
||||
| undefined;
|
||||
const rows = db.prepare(sql).all(userId, limit, offset);
|
||||
return {
|
||||
items: rows.filter(isPlaylistRow).map(playlistRowToApi),
|
||||
total: totalRow?.count ?? 0,
|
||||
};
|
||||
}
|
||||
|
||||
export function updatePlaylist(
|
||||
@@ -179,11 +201,11 @@ export function addDumpToPlaylist(
|
||||
throw new APIException(APIErrorCode.UNAUTHORIZED, 403, "Forbidden");
|
||||
}
|
||||
|
||||
const maxRow = db.prepare(
|
||||
`SELECT MAX(position) as max_pos FROM playlist_dumps WHERE playlist_id = ?;`,
|
||||
).get(playlistId) as { max_pos: number | null } | undefined;
|
||||
const minRow = db.prepare(
|
||||
`SELECT MIN(position) as min_pos FROM playlist_dumps WHERE playlist_id = ?;`,
|
||||
).get(playlistId) as { min_pos: number | null } | undefined;
|
||||
|
||||
const nextPos = (maxRow?.max_pos ?? -1) + 1;
|
||||
const nextPos = (minRow?.min_pos ?? 1) - 1;
|
||||
const addedAt = new Date().toISOString();
|
||||
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user