fix(subsonic): a scrobble's time is bounded as a native play's is (#8076)
The native events endpoint clamps a client's play time to between the account's creation and a few minutes past now. A Subsonic scrobble took its time as sent, so an app with a wrong clock dated plays years out, onto the wrong day in History and into Rediscover's ordering. The clamp moves to playevents.ClampClientTime and both surfaces use it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -98,3 +99,29 @@ func TestHandleScrobble_SubmissionTrueWritesCompletedPlay(t *testing.T) {
|
||||
t.Errorf("closed play_events = %d, want 1", closed)
|
||||
}
|
||||
}
|
||||
|
||||
// A scrobble's time is bounded like a native play's: an app whose clock is
|
||||
// years out can't date the play years out.
|
||||
func TestHandleScrobble_AWildClockIsBoundedToNow(t *testing.T) {
|
||||
pool, user, track := testScrobblePool(t)
|
||||
m := newScrobbleHandlers(pool)
|
||||
|
||||
q := url.Values{}
|
||||
q.Set("id", uuidToID(track.ID))
|
||||
q.Set("submission", "true")
|
||||
q.Set("time", strconv.FormatInt(time.Now().AddDate(5, 0, 0).UnixMilli(), 10))
|
||||
req := httptest.NewRequest(http.MethodGet, "/rest/scrobble?"+q.Encode(), nil)
|
||||
w := httptest.NewRecorder()
|
||||
m.handleScrobble(w, req.WithContext(context.WithValue(req.Context(), userCtxKey, user)))
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d", w.Code)
|
||||
}
|
||||
var startedAt time.Time
|
||||
if err := pool.QueryRow(context.Background(),
|
||||
"SELECT started_at FROM play_events WHERE user_id=$1", user.ID).Scan(&startedAt); err != nil {
|
||||
t.Fatalf("read play: %v", err)
|
||||
}
|
||||
if startedAt.After(time.Now().Add(time.Minute)) {
|
||||
t.Fatalf("play dated %v, want no later than now", startedAt)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user