Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

address review (codex): drop a stolen track from its previous album's tracklist

[._.]/ Adam Eivy264ebb15d367
diff --git a/server/routes/albums.js b/server/routes/albums.js
index 38cf09c35d..7e27a01f90 100644
--- a/server/routes/albums.js
+++ b/server/routes/albums.js
@@ -63,22 +63,45 @@ const patchSchema = z.object({
async function reconcileAlbumMembership(album) {
const desired = new Set(album.trackIds || []);
const all = await tracks.listTracks().catch(() => []);
+ const byId = new Map(all.map((t) => [t.id, t]));
const ops = [];
+ // A listed track that currently belongs to a DIFFERENT album is being moved
+ // here — drop it from that other album's ordered `trackIds` so the old album
+ // stops listing it (it now reports a different `albumId`). Call the album
+ // SERVICE (not this route) so the write can't re-enter reconcile and loop.
+ // Coalesce per-source-album removals into one update each.
+ const removalsByAlbum = new Map();
// Clear albumId on tracks that point here but are no longer listed.
for (const t of all) {
if (t.albumId === album.id && !desired.has(t.id)) {
ops.push(tracks.updateTrack(t.id, { albumId: '' }).catch(() => {}));
}
}
- // Stamp albumId on listed tracks that don't already point here.
- const byId = new Map(all.map((t) => [t.id, t]));
+ // Stamp albumId on listed tracks that don't already point here, and record any
+ // prior album they're being stolen from.
for (const id of desired) {
const t = byId.get(id);
if (t && t.albumId !== album.id) {
ops.push(tracks.updateTrack(id, { albumId: album.id }).catch(() => {}));
+ if (t.albumId) {
+ const set = removalsByAlbum.get(t.albumId) || new Set();
+ set.add(id);
+ removalsByAlbum.set(t.albumId, set);
+ }
}
}
await Promise.all(ops);
+ // Now prune each stolen track from its former album's list (one write per
+ // source album; skips the album being saved and any missing/deleted album).
+ await Promise.all([...removalsByAlbum.entries()].map(async ([srcAlbumId, stolen]) => {
+ if (srcAlbumId === album.id) return;
+ const src = await albums.getAlbum(srcAlbumId).catch(() => null);
+ if (!src) return;
+ const nextIds = (src.trackIds || []).filter((id) => !stolen.has(id));
+ if (nextIds.length !== (src.trackIds || []).length) {
+ await albums.updateAlbum(srcAlbumId, { trackIds: nextIds }).catch(() => {});
+ }
+ }));
}
router.get('/', asyncHandler(async (_req, res) => {
diff --git a/server/routes/albums.test.js b/server/routes/albums.test.js
index bcee7a665d..fbb61528dc 100644
--- a/server/routes/albums.test.js
+++ b/server/routes/albums.test.js
@@ -109,6 +109,17 @@ describe('albums routes', () => {
expect(tracks.listTracks).not.toHaveBeenCalled();
});
+ it('PATCH /:id stealing a track from another album drops it from that album\'s tracklist', async () => {
+ // track-7 currently belongs to album-2; adding it to album-1 must remove it
+ // from album-2's ordered trackIds (not just flip its albumId).
+ tracks.listTracks.mockResolvedValueOnce([{ id: 'track-7', albumId: 'album-2' }]);
+ albums.getAlbum.mockResolvedValueOnce({ id: 'album-2', trackIds: ['track-7', 'track-8'] });
+ const r = await request(app).patch('/api/albums/album-1').send({ trackIds: ['track-7'] });
+ expect(r.status).toBe(200);
+ expect(tracks.updateTrack).toHaveBeenCalledWith('track-7', { albumId: 'album-1' });
+ expect(albums.updateAlbum).toHaveBeenCalledWith('album-2', { trackIds: ['track-8'] });
+ });
+
it('DELETE /:id orphans the album\'s tracks (clears their albumId)', async () => {
tracks.listTracks.mockResolvedValueOnce([{ id: 'track-9', albumId: 'album-1' }]);
await request(app).delete('/api/albums/album-1');