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:
2026-11-26 23:52:37 -05:00
co-authored by Claude Opus 5.5
parent 408c4f9c24
commit 4c0bd9df04
5 changed files with 80 additions and 22 deletions
+2 -21
View File
@@ -11,6 +11,7 @@ import (
"git.fabledsword.com/bvandeusen/minstrel/internal/apierror"
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
"git.fabledsword.com/bvandeusen/minstrel/internal/playevents"
)
type eventRequest struct {
@@ -59,26 +60,6 @@ type playStartedResponse struct {
SessionID string `json:"session_id"`
}
// eventFutureSkew is the tolerated client-clock drift into the future
// before a timestamp is treated as bogus and replaced with now.
const eventFutureSkew = 5 * time.Minute
// clampEventTime bounds a client-supplied event timestamp to sanity:
// no earlier than the account's creation (offline replays can
// legitimately be days old, but no play can predate the user) and no
// later than now + a small skew allowance. An unbounded client clock would
// let a skewed device write arbitrarily old plays, poisoning Rediscover's
// "not played in 6 months" ordering.
func clampEventTime(at, userCreatedAt, now time.Time) time.Time {
if at.Before(userCreatedAt) {
return userCreatedAt
}
if at.After(now.Add(eventFutureSkew)) {
return now
}
return at
}
type okResponse struct {
OK bool `json:"ok"`
}
@@ -100,7 +81,7 @@ func (h *handlers) handleEvents(w http.ResponseWriter, r *http.Request) {
writeErr(w, apierror.BadRequest("bad_request", "invalid `at` timestamp"))
return
}
at = clampEventTime(parsed.UTC(), user.CreatedAt.Time, time.Now().UTC())
at = playevents.ClampClientTime(parsed.UTC(), user.CreatedAt.Time, time.Now().UTC())
}
clientID := ""
if req.ClientID != nil {