fix(likes): the web marks every copy of a liked song, not only the one tapped (#8126)

A like is on the song: the server likes every copy sharing its song_key,
but the web's optimistic update marked only the tapped track, so the
same song on another album or a compilation showed an empty heart until
the liked ids were fetched again. A track like or unlike now answers 200
with the ids of every copy it changed, the asked one first, and the web
marks them all. Clients that ignore the body (Android) are unaffected.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-28 11:58:13 -05:00
co-authored by Claude Opus 5.5
parent 1c4307e6ef
commit 698ff23105
4 changed files with 87 additions and 20 deletions
+12
View File
@@ -85,6 +85,18 @@ describe('likes mutations', () => {
expect(cached?.track_ids).toContain('t1');
});
test('a like marks every copy of the song the server names, and an unlike clears them', async () => {
(api.post as ReturnType<typeof vi.fn>).mockResolvedValueOnce({ track_ids: ['t2', 't2-best-of'] });
await likeEntity(client, 'track', 't2');
let cached = client.getQueryData(qk.likedIds()) as { track_ids: string[] } | undefined;
expect(cached?.track_ids).toEqual(['t1', 't2', 't2-best-of']);
(api.del as ReturnType<typeof vi.fn>).mockResolvedValueOnce({ track_ids: ['t2-best-of', 't2'] });
await unlikeEntity(client, 'track', 't2-best-of');
cached = client.getQueryData(qk.likedIds()) as { track_ids: string[] } | undefined;
expect(cached?.track_ids).toEqual(['t1']);
});
test('a failed unlike puts back only its own like', async () => {
(api.del as ReturnType<typeof vi.fn>).mockRejectedValueOnce(new Error('boom'));
await expect(unlikeEntity(client, 'track', 't1')).rejects.toThrow('boom');
+30 -13
View File
@@ -82,35 +82,52 @@ function wasLiked(client: QueryClient, kind: EntityKind, id: string): boolean |
return client.getQueryData<LikedIdsResponse>(qk.likedIds())?.[SET_KEY_BY_KIND[kind]].includes(id);
}
// likeEntity shows the like straight away, then tells the server. If the
// server refuses, only this id goes back to how it was: a like or unlike made
// while the request was out keeps its own state. unlikeEntity is the same.
// A track like or unlike answers with every copy of the song it changed (the
// same song on another album or a compilation); album and artist likes
// answer nothing.
type TrackLikeResponse = { track_ids?: string[] } | null;
// likeEntity shows the like straight away, then tells the server, and marks
// the song's other copies once the server names them. If the server refuses,
// only this id goes back to how it was: a like or unlike made while the
// request was out keeps its own state. unlikeEntity is the same.
export async function likeEntity(
client: QueryClient,
kind: EntityKind,
id: string
): Promise<void> {
const before = wasLiked(client, kind, id);
setLiked(client, kind, id, true);
try {
await api.post(`/api/likes/${PATH_BY_KIND[kind]}/${id}`, undefined);
} catch (err) {
if (before !== undefined) setLiked(client, kind, id, before);
throw err;
}
await changeLike(client, kind, id, true, () =>
api.post<TrackLikeResponse>(`/api/likes/${PATH_BY_KIND[kind]}/${id}`, undefined)
);
}
export async function unlikeEntity(
client: QueryClient,
kind: EntityKind,
id: string
): Promise<void> {
await changeLike(client, kind, id, false, () =>
api.del<TrackLikeResponse>(`/api/likes/${PATH_BY_KIND[kind]}/${id}`)
);
}
async function changeLike(
client: QueryClient,
kind: EntityKind,
id: string,
liked: boolean,
send: () => Promise<TrackLikeResponse>
): Promise<void> {
const before = wasLiked(client, kind, id);
setLiked(client, kind, id, false);
setLiked(client, kind, id, liked);
let res: TrackLikeResponse;
try {
await api.del(`/api/likes/${PATH_BY_KIND[kind]}/${id}`);
res = await send();
} catch (err) {
if (before !== undefined) setLiked(client, kind, id, before);
throw err;
}
for (const copy of res?.track_ids ?? []) {
if (copy !== id) setLiked(client, kind, copy, liked);
}
}