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>
104 lines
2.9 KiB
Go
104 lines
2.9 KiB
Go
package subsonic
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
|
|
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
|
)
|
|
|
|
// mixRecallFor is how long after an app opens a daily mix its plays are
|
|
// still credited to that mix: long enough for a mix of a few hours played
|
|
// straight through, short enough that tomorrow's listening isn't credited
|
|
// to yesterday's mix.
|
|
const mixRecallFor = 6 * time.Hour
|
|
|
|
// mixRecall remembers, for each user and app, the daily mix that app last
|
|
// opened with getPlaylist. A Subsonic scrobble says which song was played
|
|
// but not where it was played from, so this is how a play of For You in a
|
|
// Subsonic app is credited to For You: its metrics count it and its
|
|
// rotation moves on.
|
|
//
|
|
// It lives in memory. After a restart, plays go uncredited until the app
|
|
// opens a mix again, which only loses the credit, never the play.
|
|
type mixRecall struct {
|
|
mu sync.Mutex
|
|
last map[recallKey]recalledMix
|
|
now func() time.Time
|
|
}
|
|
|
|
type recallKey struct {
|
|
user pgtype.UUID
|
|
client string
|
|
}
|
|
|
|
type recalledMix struct {
|
|
variant string
|
|
at time.Time
|
|
}
|
|
|
|
func newMixRecall() *mixRecall {
|
|
return &mixRecall{last: map[recallKey]recalledMix{}, now: time.Now}
|
|
}
|
|
|
|
// remember notes that the user's app (the Subsonic c parameter) opened a
|
|
// daily mix of the given variant. Entries past mixRecallFor are dropped
|
|
// on the way, so the map holds at most one entry per user and app.
|
|
func (m *mixRecall) remember(user pgtype.UUID, client, variant string) {
|
|
if m == nil {
|
|
return
|
|
}
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
now := m.now()
|
|
for k, v := range m.last {
|
|
if now.Sub(v.at) > mixRecallFor {
|
|
delete(m.last, k)
|
|
}
|
|
}
|
|
m.last[recallKey{user, client}] = recalledMix{variant: variant, at: now}
|
|
}
|
|
|
|
// variant is the daily mix the user's app opened within mixRecallFor, or
|
|
// "" when it opened none. A nil mixRecall remembers nothing.
|
|
func (m *mixRecall) variant(user pgtype.UUID, client string) string {
|
|
if m == nil {
|
|
return ""
|
|
}
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
v, ok := m.last[recallKey{user, client}]
|
|
if !ok || m.now().Sub(v.at) > mixRecallFor {
|
|
return ""
|
|
}
|
|
return v.variant
|
|
}
|
|
|
|
// sourceFor is the play source to record for a Subsonic play of trackID:
|
|
// the daily mix the app last opened, when the track is in that mix's
|
|
// current snapshot, and otherwise "". A song played from an album after
|
|
// opening a mix is not in the mix, so it isn't credited to it.
|
|
func (m *mixRecall) sourceFor(ctx context.Context, q *dbq.Queries, user pgtype.UUID, client string, trackID pgtype.UUID) (string, error) {
|
|
variant := m.variant(user, client)
|
|
if variant == "" {
|
|
return "", nil
|
|
}
|
|
_, err := q.GetSystemPickKindForTrack(ctx, dbq.GetSystemPickKindForTrackParams{
|
|
UserID: user,
|
|
SystemVariant: &variant,
|
|
TrackID: trackID,
|
|
})
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return "", nil
|
|
}
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return variant, nil
|
|
}
|