move all music per-user state into the sidecar; platform = auth + proxy only
Favorites / now-playing / playlists were being served by the platform router straight from Postgres, which violated the intended split (officer = auth + proxy; officer-music = the whole /api/music/* contract). Move them into the sidecar so it owns ALL music endpoints — library AND user state. - sidecar (index.ts): serves /favorites, /now-playing, /playlists[/:id[/items]] backed by Postgres (the same officerdb queries other sidecars already use). The authenticated user id arrives in X-Officer-User; the sidecar is loopback- only, so it trusts the header (401 if absent). HTTP-contract comment updated. - platform (router.ts): reduced to a pure auth+proxy catch-all — it now injects X-Officer-User from the authenticated ctx user and forwards the request body (favorites/now-playing/playlist writes carry JSON) in addition to Range/query. No schema change — the tables are unchanged, only WHERE they're served moves. Verified live: booted the real sidecar against the live DB and exercised the endpoints with the X-Officer-User header — 401-without-header, favorites round- trip, and full playlist CRUD with ownership scoping all pass. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -21,9 +21,43 @@ import {
|
||||
onIndexProgress,
|
||||
buildReport,
|
||||
} from './indexer';
|
||||
import {
|
||||
getMusicFavorites,
|
||||
addMusicFavorite,
|
||||
removeMusicFavorite,
|
||||
getNowPlaying,
|
||||
setNowPlaying,
|
||||
clearNowPlaying,
|
||||
getPlaylists,
|
||||
getPlaylist,
|
||||
createPlaylist,
|
||||
renamePlaylist,
|
||||
deletePlaylist,
|
||||
addPlaylistItems,
|
||||
setPlaylistItems,
|
||||
type FavoriteKind,
|
||||
} from 'officerdb';
|
||||
|
||||
const DATA_PATH = process.env.DATA_PATH ?? join(process.cwd(), 'data');
|
||||
|
||||
// ── Per-user state validation ──
|
||||
// The authenticated user id arrives in X-Officer-User (the platform proxy injects it after auth; we're
|
||||
// loopback-only so we trust it). Favorite/playlist `key`s are opaque paths we never interpret.
|
||||
const userIdOf = (req: Request): number | null => {
|
||||
const n = Number(req.headers.get('x-officer-user'));
|
||||
return Number.isInteger(n) && n > 0 ? n : null;
|
||||
};
|
||||
const FAVORITE_KINDS = new Set<FavoriteKind>(['track', 'album', 'artist']);
|
||||
const isKind = (k: unknown): k is FavoriteKind => typeof k === 'string' && FAVORITE_KINDS.has(k as FavoriteKind);
|
||||
const PLAYLIST_NAME_MAX = 200;
|
||||
const cleanName = (v: unknown): string | null => {
|
||||
if (typeof v !== 'string') return null;
|
||||
const n = v.trim();
|
||||
return n && n.length <= PLAYLIST_NAME_MAX ? n : null;
|
||||
};
|
||||
const asKeys = (v: unknown): string[] | null =>
|
||||
Array.isArray(v) && v.every((k) => typeof k === 'string' && k) ? (v as string[]) : null;
|
||||
|
||||
// The music sidecar (officer-music). Same philosophy as the other officer-* sidecars: a singleton
|
||||
// process that registers with the API server. It OWNS an audio-streaming HTTP server (all path
|
||||
// resolution + streaming + ffprobe duration happens here); the platform API is just a thin proxy that
|
||||
@@ -59,6 +93,22 @@ const DATA_PATH = process.env.DATA_PATH ?? join(process.cwd(), 'data');
|
||||
// `event: done` (IndexReport) and the stream closes.
|
||||
// GET /health "ok".
|
||||
//
|
||||
// ── Per-user state (USER data in Postgres, not library data). User id in X-Officer-User, injected by
|
||||
// the platform proxy after auth; `key`s are opaque paths (track homePath / album|artist rel). ──
|
||||
// GET /favorites { tracks[], albums[], artists[] } (keys, newest first).
|
||||
// POST /favorites { kind, key } add (idempotent). kind ∈ track|album|artist.
|
||||
// DELETE /favorites?kind=&key= remove.
|
||||
// GET /now-playing last snapshot, or null.
|
||||
// PUT /now-playing { homePath, dir?, title?, artist?, album?, durationSec?, positionSec? } upsert.
|
||||
// DELETE /now-playing clear.
|
||||
// GET /playlists [{ id, name, count, createdAt, updatedAt }] (recent first).
|
||||
// POST /playlists { name } create → 201 row; 409 if name taken.
|
||||
// GET /playlists/:id { id, name, items:[keys], … }; 404 if not the user's.
|
||||
// PATCH /playlists/:id { name } rename; 404 / 409.
|
||||
// DELETE /playlists/:id delete (items cascade); 404.
|
||||
// POST /playlists/:id/items { keys[] } append → { count }; 404.
|
||||
// PUT /playlists/:id/items { keys[] } replace whole list (reorder/remove) → { count }; 404.
|
||||
//
|
||||
// `<rel>` = album folder path relative to the Music root (e.g. "Albums/AC-DC/[1980] Back in Black").
|
||||
// `v` = per-album version stamp; unchanged `v` ⇒ nothing changed ⇒ the phone can skip re-downloading.
|
||||
// ─────────────────────────────────────────────────────────────────────────────────────────────────
|
||||
@@ -101,7 +151,10 @@ const server = Bun.serve({
|
||||
server.timeout(req, 1800);
|
||||
}
|
||||
const json = (data: unknown, init?: ResponseInit) =>
|
||||
new Response(JSON.stringify(data), { ...init, headers: { 'Content-Type': 'application/json', ...init?.headers } });
|
||||
new Response(JSON.stringify(data), {
|
||||
...init,
|
||||
headers: { 'Content-Type': 'application/json', ...init?.headers },
|
||||
});
|
||||
|
||||
if (url.pathname === '/health') return new Response('ok');
|
||||
|
||||
@@ -191,7 +244,9 @@ const server = Bun.serve({
|
||||
if (!(await Bun.file(posterPath).exists())) return new Response('Not found', { status: 404 });
|
||||
const v = await albumVersion(rel);
|
||||
if (v && req.headers.get('if-none-match') === v) return new Response(null, { status: 304 });
|
||||
return new Response(Bun.file(posterPath), { headers: { 'Content-Type': 'image/jpeg', ...(v ? { ETag: v } : {}) } });
|
||||
return new Response(Bun.file(posterPath), {
|
||||
headers: { 'Content-Type': 'image/jpeg', ...(v ? { ETag: v } : {}) },
|
||||
});
|
||||
}
|
||||
|
||||
// Track lyrics (external .lrc/.txt sidecar or embedded, cached during indexing). Try synced first.
|
||||
@@ -247,6 +302,107 @@ const server = Bun.serve({
|
||||
});
|
||||
}
|
||||
|
||||
// ── Per-user state (favorites / now-playing / playlists) ──────────────────────────────────────
|
||||
// USER data backed by Postgres (NOT library data). The user id comes from X-Officer-User (see above).
|
||||
const P = url.pathname;
|
||||
if (P === '/favorites' || P === '/now-playing' || P === '/playlists' || P.startsWith('/playlists/')) {
|
||||
const uid = userIdOf(req);
|
||||
if (uid === null) return json({ error: 'unauthenticated' }, { status: 401 });
|
||||
const m = req.method;
|
||||
|
||||
if (P === '/favorites') {
|
||||
if (m === 'GET') return json(await getMusicFavorites(uid));
|
||||
if (m === 'POST') {
|
||||
const { kind, key } = (await req.json().catch(() => ({}))) as { kind?: unknown; key?: unknown };
|
||||
if (!isKind(kind) || typeof key !== 'string' || !key)
|
||||
return json({ error: 'kind and key required' }, { status: 400 });
|
||||
await addMusicFavorite(uid, kind, key);
|
||||
return json({ ok: true });
|
||||
}
|
||||
if (m === 'DELETE') {
|
||||
const kind = url.searchParams.get('kind');
|
||||
const key = url.searchParams.get('key');
|
||||
if (!isKind(kind) || !key) return json({ error: 'kind and key required' }, { status: 400 });
|
||||
await removeMusicFavorite(uid, kind, key);
|
||||
return json({ ok: true });
|
||||
}
|
||||
return new Response('Method not allowed', { status: 405 });
|
||||
}
|
||||
|
||||
if (P === '/now-playing') {
|
||||
if (m === 'GET') return json(await getNowPlaying(uid));
|
||||
if (m === 'PUT') {
|
||||
const b = (await req.json().catch(() => ({}))) as Record<string, unknown>;
|
||||
if (typeof b.homePath !== 'string' || !b.homePath)
|
||||
return json({ error: 'homePath required' }, { status: 400 });
|
||||
const str = (v: unknown) => (typeof v === 'string' ? v : undefined);
|
||||
const num = (v: unknown) => (typeof v === 'number' && Number.isFinite(v) ? v : undefined);
|
||||
await setNowPlaying(uid, {
|
||||
homePath: b.homePath,
|
||||
dir: str(b.dir),
|
||||
title: str(b.title),
|
||||
artist: str(b.artist),
|
||||
album: str(b.album),
|
||||
durationSec: num(b.durationSec),
|
||||
positionSec: num(b.positionSec),
|
||||
});
|
||||
return json({ ok: true });
|
||||
}
|
||||
if (m === 'DELETE') {
|
||||
await clearNowPlaying(uid);
|
||||
return json({ ok: true });
|
||||
}
|
||||
return new Response('Method not allowed', { status: 405 });
|
||||
}
|
||||
|
||||
if (P === '/playlists') {
|
||||
if (m === 'GET') return json(await getPlaylists(uid));
|
||||
if (m === 'POST') {
|
||||
const { name } = (await req.json().catch(() => ({}))) as { name?: unknown };
|
||||
const n = cleanName(name);
|
||||
if (!n) return json({ error: 'name required (1-200 chars)' }, { status: 400 });
|
||||
const row = await createPlaylist(uid, n);
|
||||
return row
|
||||
? json(row, { status: 201 })
|
||||
: json({ error: 'a playlist with that name already exists' }, { status: 409 });
|
||||
}
|
||||
return new Response('Method not allowed', { status: 405 });
|
||||
}
|
||||
|
||||
// /playlists/:id and /playlists/:id/items
|
||||
const match = P.match(/^\/playlists\/(\d+)(\/items)?$/);
|
||||
if (!match) return json({ error: 'not found' }, { status: 404 });
|
||||
const id = Number(match[1]);
|
||||
|
||||
if (match[2]) {
|
||||
// /playlists/:id/items — POST append, PUT replace (reorder/remove)
|
||||
if (m !== 'POST' && m !== 'PUT') return new Response('Method not allowed', { status: 405 });
|
||||
const { keys } = (await req.json().catch(() => ({}))) as { keys?: unknown };
|
||||
const ks = asKeys(keys);
|
||||
if (!ks) return json({ error: 'keys[] required' }, { status: 400 });
|
||||
const count = await (m === 'POST' ? addPlaylistItems(uid, id, ks) : setPlaylistItems(uid, id, ks));
|
||||
return count === null ? json({ error: 'not found' }, { status: 404 }) : json({ count });
|
||||
}
|
||||
|
||||
if (m === 'GET') {
|
||||
const pl = await getPlaylist(uid, id);
|
||||
return pl ? json(pl) : json({ error: 'not found' }, { status: 404 });
|
||||
}
|
||||
if (m === 'PATCH') {
|
||||
const { name } = (await req.json().catch(() => ({}))) as { name?: unknown };
|
||||
const n = cleanName(name);
|
||||
if (!n) return json({ error: 'name required (1-200 chars)' }, { status: 400 });
|
||||
if (!(await getPlaylist(uid, id))) return json({ error: 'not found' }, { status: 404 });
|
||||
const ok = await renamePlaylist(uid, id, n);
|
||||
return ok ? json({ ok: true }) : json({ error: 'a playlist with that name already exists' }, { status: 409 });
|
||||
}
|
||||
if (m === 'DELETE') {
|
||||
const ok = await deletePlaylist(uid, id);
|
||||
return ok ? json({ ok: true }) : json({ error: 'not found' }, { status: 404 });
|
||||
}
|
||||
return new Response('Method not allowed', { status: 405 });
|
||||
}
|
||||
|
||||
return new Response('Not found', { status: 404 });
|
||||
},
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user