fix(subsonic): one listen is one play, and a daily mix played in a Subsonic app is credited to it
release / web (push) Successful in 1m23s
release / go (push) Successful in 1m55s
release / govulncheck (push) Successful in 19s
release / integration (push) Successful in 4m16s
release / android (push) Successful in 4m50s
release / Build signed APK (push) Successful in 5m29s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Successful in 17s
release / Verify release artifacts (tag releases only) (push) Skipped

A Subsonic app reports "now playing" when a song starts and "played" when
it ends. The played report used to close the now-playing row as a play of
its own and write a second one, so every listen counted twice, and as a
skip when the app sent the song's start time. The report now completes the
app's own row for that song. A late report for an earlier song no longer
cuts short the song playing now.

Subsonic's scrobble doesn't say where a song was played from, so Minstrel
remembers, per user and app, the daily mix the app last opened (for six
hours). A play of a song in that mix is credited to it: the mix's metrics
count it and its rotation moves on.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-25 21:48:17 -05:00
co-authored by Claude Opus 5.5
parent 7f7e946bad
commit 333523b30e
8 changed files with 477 additions and 88 deletions
+17 -6
View File
@@ -28,6 +28,9 @@ type mediaHandlers struct {
pool *pgxpool.Pool
events *playevents.Writer
logger *slog.Logger
// recall credits a scrobble to the daily mix the app last opened
// (see mixRecall); nil credits nothing.
recall *mixRecall
}
func newMediaHandlers(pool *pgxpool.Pool, events *playevents.Writer, logger *slog.Logger) *mediaHandlers {
@@ -175,10 +178,12 @@ func imageContentType(path string) string {
// handleScrobble translates Subsonic scrobble calls into native play_events.
//
// submission=false (now-playing): writes a play_started, leaves ended_at NULL.
// submission=true (completed): writes a synthetic completed play (start+end
// in one transaction). play_events WHERE ended_at IS NULL is the source of
// truth for "currently playing"; the prior in-memory nowPlayingMap is gone.
// submission=false (now playing) opens a play, leaving ended_at NULL.
// submission=true (played) completes that play when it is the same song,
// and otherwise writes a completed one; either way a listen is one row.
// play_events WHERE ended_at IS NULL is the source of truth for "currently
// playing". A song in the daily mix the app last opened is credited to
// that mix.
func (m *mediaHandlers) handleScrobble(w http.ResponseWriter, r *http.Request) {
params := r.URL.Query()
idStr := params.Get("id")
@@ -207,17 +212,23 @@ func (m *mediaHandlers) handleScrobble(w http.ResponseWriter, r *http.Request) {
}
}
clientID := params.Get("c")
source, err := m.recall.sourceFor(r.Context(), dbq.New(m.pool), user.ID, clientID, trackID)
if err != nil {
// Crediting a mix is a refinement; the play itself still counts.
m.logger.Warn("subsonic: scrobble mix lookup failed", "err", err)
source = ""
}
// Subsonic default is submission=true (completed play).
submission := strings.ToLower(params.Get("submission"))
switch submission {
case "true", "":
if err := m.events.RecordSyntheticCompletedPlay(r.Context(), user.ID, trackID, clientID, at); err != nil {
if err := m.events.RecordSyntheticCompletedPlay(r.Context(), user.ID, trackID, clientID, source, at); err != nil {
WriteFail(w, r, ErrGeneric, "Could not record play")
return
}
case "false":
if _, err := m.events.RecordPlayStarted(r.Context(), user.ID, trackID, clientID, at); err != nil {
if _, err := m.events.RecordPlayStartedWithSource(r.Context(), user.ID, trackID, clientID, source, "", at); err != nil {
WriteFail(w, r, ErrGeneric, "Could not record now-playing")
return
}