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
+13 -4
View File
@@ -50,10 +50,18 @@ func (h *handlers) handleLikeTrack(w http.ResponseWriter, r *http.Request) {
}
// The like is on the song: every copy it reached changes on
// the client too.
for _, t := range eventbus.LedBy(id, liked) {
changed := eventbus.LedBy(id, liked)
for _, t := range changed {
h.publishLikeEvent(user.ID, t, "track", true)
}
w.WriteHeader(http.StatusNoContent)
writeJSON(w, http.StatusOK, trackLikeView{TrackIDs: uuidsToStrings(changed)})
}
// trackLikeView answers a track like or unlike: the copies of the song it
// changed, the one asked for first, so the client can mark them all at once
// rather than only the copy that was tapped.
type trackLikeView struct {
TrackIDs []string `json:"track_ids"`
}
func (h *handlers) handleUnlikeTrack(w http.ResponseWriter, r *http.Request) {
@@ -76,10 +84,11 @@ func (h *handlers) handleUnlikeTrack(w http.ResponseWriter, r *http.Request) {
h.logger.Error("api: soft-delete contextual_likes", "err", err)
// Don't fail the response — soft-delete is best-effort.
}
for _, t := range eventbus.LedBy(id, unliked) {
changed := eventbus.LedBy(id, unliked)
for _, t := range changed {
h.publishLikeEvent(user.ID, t, "track", false)
}
w.WriteHeader(http.StatusNoContent)
writeJSON(w, http.StatusOK, trackLikeView{TrackIDs: uuidsToStrings(changed)})
}
func (h *handlers) handleLikeAlbum(w http.ResponseWriter, r *http.Request) {
+32 -3
View File
@@ -5,6 +5,7 @@ import (
"encoding/json"
"net/http"
"net/http/httptest"
"slices"
"testing"
"github.com/go-chi/chi/v5"
@@ -46,11 +47,11 @@ func TestLikeTrack_Idempotent(t *testing.T) {
track := seedTrack(t, pool, album.ID, artist.ID, "Something", 3, 183_000)
w := callLike(h, user, http.MethodPost, "/api/likes/tracks/"+uuidToString(track.ID))
if w.Code != http.StatusNoContent {
if w.Code != http.StatusOK {
t.Fatalf("first like: status = %d body=%s", w.Code, w.Body.String())
}
w = callLike(h, user, http.MethodPost, "/api/likes/tracks/"+uuidToString(track.ID))
if w.Code != http.StatusNoContent {
if w.Code != http.StatusOK {
t.Errorf("second like (idempotent): status = %d", w.Code)
}
var count int
@@ -89,11 +90,39 @@ func TestUnlikeTrack_IdempotentEvenWhenAbsent(t *testing.T) {
track := seedTrack(t, pool, album.ID, artist.ID, "Something", 3, 183_000)
w := callLike(h, user, http.MethodDelete, "/api/likes/tracks/"+uuidToString(track.ID))
if w.Code != http.StatusNoContent {
if w.Code != http.StatusOK {
t.Errorf("status = %d", w.Code)
}
}
// A like is on the song, so its answer names every copy it reached, and an
// unlike every copy it cleared, the tapped one first.
func TestLikeTrack_AnswersEveryCopyOfTheSong(t *testing.T) {
h, pool := testHandlers(t)
truncateLibrary(t, pool)
user := seedUser(t, pool, "alice", "x", false)
artist := seedArtist(t, pool, "Copper Lanterns")
album := seedAlbum(t, pool, artist.ID, "Harbour Street", 1969)
best := seedAlbum(t, pool, artist.ID, "Best Of", 1975)
first := seedTrack(t, pool, album.ID, artist.ID, "Something", 3, 183_000)
copied := seedTrack(t, pool, best.ID, artist.ID, "Something", 1, 183_000)
if _, err := pool.Exec(context.Background(), "UPDATE tracks SET song_id = $1 WHERE id = $2", first.ID, copied.ID); err != nil {
t.Fatal(err)
}
want := []string{uuidToString(copied.ID), uuidToString(first.ID)}
for _, method := range []string{http.MethodPost, http.MethodDelete} {
w := callLike(h, user, method, "/api/likes/tracks/"+uuidToString(copied.ID))
var got trackLikeView
if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil {
t.Fatalf("%s: decode %q: %v", method, w.Body.String(), err)
}
if !slices.Equal(got.TrackIDs, want) {
t.Errorf("%s: track_ids = %v, want %v", method, got.TrackIDs, want)
}
}
}
func TestListLikedTracks_PaginatedAndSortedDescByLikedAt(t *testing.T) {
h, pool := testHandlers(t)
truncateLibrary(t, pool)