feat(subsonic): wire contextual_likes capture/soft-delete into star/unstar

handleStar's track branch calls playevents.CaptureContextualLikeIfPlaying
when the underlying LikeTrack actually inserted a row. handleUnstar
calls SoftDeleteContextualLikes after every track-id unstar. Same
helpers as the api surface — single source of truth.

mediaHandlers struct gains a logger field threaded through Mount.
This commit is contained in:
2026-04-27 11:25:10 -04:00
parent fe1cc46752
commit bf7c5ad47d
5 changed files with 60 additions and 7 deletions
+47 -1
View File
@@ -55,7 +55,7 @@ func testStarPool(t *testing.T) (*pgxpool.Pool, dbq.User, dbq.Track, dbq.Album,
func newStarHandlers(pool *pgxpool.Pool) *mediaHandlers {
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
w := playevents.NewWriter(pool, logger, 30*time.Minute, 0.5, 30000)
return newMediaHandlers(pool, w)
return newMediaHandlers(pool, w, logger)
}
func TestHandleStar_TrackIDInsertsRow(t *testing.T) {
@@ -249,3 +249,49 @@ func TestHandleGetStarred2_CrossUserIsolation(t *testing.T) {
t.Errorf("bob's starred songs should be empty, got %d", len(songs))
}
}
func TestHandleStar_DuringNowPlaying_WritesContextualLike(t *testing.T) {
pool, user, track1, _, _ := testStarPool(t)
// Add a second track so star refers to a different track than the one playing.
q := dbq.New(pool)
a, _ := q.UpsertArtist(context.Background(), dbq.UpsertArtistParams{Name: "Y", SortName: "Y"})
al, _ := q.UpsertAlbum(context.Background(), dbq.UpsertAlbumParams{Title: "Y", SortTitle: "Y", ArtistID: a.ID})
track2, _ := q.UpsertTrack(context.Background(), dbq.UpsertTrackParams{
Title: "Star Me", AlbumID: al.ID, ArtistID: a.ID, FilePath: "/tmp/t2.flac", DurationMs: 100_000,
})
m := newStarHandlers(pool)
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
m.logger = logger
// Start now-playing for track1 (creates open play_event with vector).
q1 := url.Values{}
q1.Set("id", uuidToID(track1.ID))
q1.Set("submission", "false")
scrobReq := httptest.NewRequest(http.MethodGet, "/rest/scrobble?"+q1.Encode(), nil)
scrobCtx := context.WithValue(scrobReq.Context(), userCtxKey, user)
scrobResp := httptest.NewRecorder()
m.handleScrobble(scrobResp, scrobReq.WithContext(scrobCtx))
if scrobResp.Code != http.StatusOK {
t.Fatalf("scrobble: %d", scrobResp.Code)
}
// Star track2.
q2 := url.Values{}
q2.Set("id", uuidToID(track2.ID))
starReq := httptest.NewRequest(http.MethodGet, "/rest/star?"+q2.Encode(), nil)
starCtx := context.WithValue(starReq.Context(), userCtxKey, user)
starResp := httptest.NewRecorder()
m.handleStar(starResp, starReq.WithContext(starCtx))
if starResp.Code != http.StatusOK {
t.Fatalf("star: %d", starResp.Code)
}
// Verify contextual_likes row.
var count int
_ = pool.QueryRow(context.Background(),
"SELECT count(*) FROM contextual_likes WHERE user_id=$1 AND track_id=$2", user.ID, track2.ID).Scan(&count)
if count != 1 {
t.Errorf("contextual_likes count = %d, want 1", count)
}
}