diff --git a/internal/subsonic/scrobble_test.go b/internal/subsonic/scrobble_test.go index 11e38e62..a321705c 100644 --- a/internal/subsonic/scrobble_test.go +++ b/internal/subsonic/scrobble_test.go @@ -11,6 +11,7 @@ import ( "testing" "time" + "github.com/jackc/pgx/v5/pgtype" "github.com/jackc/pgx/v5/pgxpool" "git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq" @@ -125,3 +126,24 @@ func TestHandleScrobble_AWildClockIsBoundedToNow(t *testing.T) { t.Fatalf("play dated %v, want no later than now", startedAt) } } + +// A scrobble of a song the library doesn't hold answers "not found" (70), +// as stream and getSong do, and records nothing. +func TestHandleScrobble_AnUnknownSongIsNotFound(t *testing.T) { + pool, user, _ := testScrobblePool(t) + m := newScrobbleHandlers(pool) + unknown := uuidToID(pgtype.UUID{Bytes: [16]byte{0xee}, Valid: true}) + + for _, submission := range []string{"true", "false"} { + if code := call(t, m.handleScrobble, user, "id="+unknown+"&submission="+submission, nil); code != ErrDataNotFound { + t.Errorf("submission=%s: error %d, want %d", submission, code, ErrDataNotFound) + } + } + var n int + if err := pool.QueryRow(context.Background(), "SELECT count(*) FROM play_events WHERE user_id=$1", user.ID).Scan(&n); err != nil { + t.Fatal(err) + } + if n != 0 { + t.Errorf("play_events = %d, want none", n) + } +} diff --git a/internal/subsonic/stream.go b/internal/subsonic/stream.go index d8eac3ed..fa8b5765 100644 --- a/internal/subsonic/stream.go +++ b/internal/subsonic/stream.go @@ -200,8 +200,19 @@ func (m *mediaHandlers) handleScrobble(w http.ResponseWriter, r *http.Request) { } at = playevents.ClampClientTime(at, user.CreatedAt.Time, time.Now().UTC()) } + q := dbq.New(m.pool) + // A song the library doesn't hold is "not found" (70), as stream and + // getSong answer it, not a failure to record. + if _, err := q.GetTrackByID(r.Context(), trackID); err != nil { + if errors.Is(err, pgx.ErrNoRows) { + WriteFail(w, r, ErrDataNotFound, "Track not found") + return + } + WriteFail(w, r, ErrGeneric, "Failed to load track") + return + } clientID := params.Get("c") - source, err := m.recall.sourceFor(r.Context(), dbq.New(m.pool), user.ID, clientID, trackID) + source, err := m.recall.sourceFor(r.Context(), q, 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)