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
+24
View File
@@ -0,0 +1,24 @@
package playevents
import "time"
// clientClockFutureSkew is how far into the future a client's clock may run
// before its timestamp is treated as bogus and replaced with now.
const clientClockFutureSkew = 5 * time.Minute
// ClampClientTime bounds a play time a client supplied, on any surface (the
// native apps' events and a Subsonic app's scrobble): no earlier than the
// account's creation, since offline replays can be days old but no play
// predates the listener, and no later than now plus a small allowance for
// clock drift. An unbounded client clock would date plays anywhere, landing
// them on the wrong day in History and poisoning Rediscover's "not played in
// six months" ordering.
func ClampClientTime(at, userCreatedAt, now time.Time) time.Time {
if at.Before(userCreatedAt) {
return userCreatedAt
}
if at.After(now.Add(clientClockFutureSkew)) {
return now
}
return at
}
+25
View File
@@ -0,0 +1,25 @@
package playevents
import (
"testing"
"time"
)
func TestClampClientTime(t *testing.T) {
created := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)
now := time.Date(2026, 11, 26, 12, 0, 0, 0, time.UTC)
for _, c := range []struct {
name string
at time.Time
expect time.Time
}{
{"before the account existed", created.AddDate(-3, 0, 0), created},
{"an offline replay days old", now.AddDate(0, 0, -4), now.AddDate(0, 0, -4)},
{"a clock a little fast", now.Add(4 * time.Minute), now.Add(4 * time.Minute)},
{"a clock years ahead", now.AddDate(5, 0, 0), now},
} {
if got := ClampClientTime(c.at, created, now); !got.Equal(c.expect) {
t.Errorf("%s: got %v, want %v", c.name, got, c.expect)
}
}
}