navidrome/plugins/host_scrobbleretriever.go
Kendall Garner b0c6d2e444
feat(plugins): add scrobbles access to PDK (#5795)
* initial scrobble api

* feat: add scrobble retrieval api

* address feedback (1)

* fix spelling

* be explicit about get

* add primary key field, update index, remove rowid references

* use unix timestamp for input and output

* initial api, some testing

* add tests, add count retrieval

* add docs, test for rejected user

* add permission validation for scrobble retriever

* chore(plugins): fix typos in scrobble retriever

Rename newScrobbleRetreverService, and fix FromTImestamp/nonero in the
ScrobbleRetriever doc comments, which generate into the Go and Rust PDKs.
Also corrects two mislabelled test entries.

* fix(plugins): make scrobble pagination order deterministic

Sorting only by submission_time left the order of equal timestamps up to the
query planner, but the cursor skips ties by offset, so an unstable order can
repeat or drop scrobbles between pages. Break ties on scrobbles.id, which the
existing scrobbles_user_time index already yields for free.

Descending is now honoured for every combination of From/To rather than only
when both or neither is set. This changes the default for a lone ToTimestamp
from newest-first to oldest-first.

* refactor(plugins): return the next page's options from GetScrobbles

Paging previously meant reading NextTimestamp and Cursor off the response and
deciding where each belonged: NextTimestamp into FromTimestamp when ascending
or ToTimestamp when descending, and Cursor copied every time, including when 0.
Both are silent data-loss bugs when a plugin gets them wrong.

GetScrobbles now returns the options for the following page, or nil when the
range is exhausted, so a plugin passes the value straight back and repeats.
ScrobbleCursor and ScrobbleList are gone; the query itself is unchanged.

* docs(plugins): warn against setting ScrobbleOptions.Offset manually

The all-ties carry rule assumes Offset counts already-returned rows at the
boundary timestamp, which only holds for the options GetScrobbles returns.
A hand-built From+Offset combination can silently skip scrobbles, so document
the field as managed pagination state instead of a generic skip.

* docs(plugins): document the ScrobbleRetriever host service in the README

Covers the manifest permissions (including the users requirement), the four
host functions, the options/ref field tables, and the pagination loop with
its two gotchas: the host-managed offset and the adjusted range on the
returned next options.

* chore(plugins): regenerate scrobble retriever stub with nil-safe accessors

---------

Co-authored-by: Deluan Quintão <deluan@navidrome.org>
2026-08-08 22:13:29 -04:00

162 lines
4.4 KiB
Go

package plugins
import (
"context"
"fmt"
"github.com/Masterminds/squirrel"
"github.com/navidrome/navidrome/model"
"github.com/navidrome/navidrome/model/request"
"github.com/navidrome/navidrome/plugins/host"
)
// maxScrobbleItems caps how many scrobbles a single GetScrobbles call can return.
const maxScrobbleItems = 5000
type scrobbleRetrieverServiceImpl struct {
ds model.DataStore
users userAccess
}
func newScrobbleRetrieverService(ds model.DataStore, users userAccess) host.ScrobbleRetrieverService {
return &scrobbleRetrieverServiceImpl{
ds: ds,
users: users,
}
}
func (s *scrobbleRetrieverServiceImpl) getUserContext(ctx context.Context, username string) (context.Context, error) {
usr, err := s.users.resolve(ctx, s.ds, username)
if err != nil {
return nil, fmt.Errorf("scrobbleRetriever: %w", err)
}
ctx = request.WithUser(ctx, *usr)
return ctx, nil
}
func (s *scrobbleRetrieverServiceImpl) getFirstLastScrobble(ctx context.Context, username string, order string) (*int64, error) {
ctx, err := s.getUserContext(ctx, username)
if err != nil {
return nil, err
}
scrobbles, err := s.ds.Scrobble(ctx).GetAll(model.QueryOptions{Sort: "submission_time", Order: order, Max: 1})
if err != nil {
return nil, err
}
if len(scrobbles) == 0 {
return nil, nil
}
return &scrobbles[0].SubmissionTime, nil
}
func (s *scrobbleRetrieverServiceImpl) GetFirstTimestamp(ctx context.Context, username string) (*int64, error) {
return s.getFirstLastScrobble(ctx, username, "ASC")
}
func (s *scrobbleRetrieverServiceImpl) GetLastTimestamp(ctx context.Context, username string) (*int64, error) {
return s.getFirstLastScrobble(ctx, username, "DESC")
}
func (s *scrobbleRetrieverServiceImpl) GetScrobbles(ctx context.Context, username string, options host.ScrobbleOptions) ([]host.ScrobbleRef, *host.ScrobbleOptions, error) {
ctx, err := s.getUserContext(ctx, username)
if err != nil {
return nil, nil, err
}
if options.MaxItems < 1 || options.MaxItems > maxScrobbleItems {
options.MaxItems = maxScrobbleItems
}
options.Offset = max(options.Offset, 0)
order := "ASC"
if options.Descending {
order = "DESC"
}
// Fetch one more item than requested. The last item is the next timestamp to fetch
lookahead := options.MaxItems + 1
scrobbles, err := s.ds.Scrobble(ctx).GetAll(model.QueryOptions{
Max: lookahead,
Filters: scrobbleRangeFilters(options.FromTimestamp, options.ToTimestamp),
// The id tiebreak makes the order of equal timestamps stable, which is what
// lets Offset skip exactly the ties already returned
Sort: "scrobbles.submission_time, scrobbles.id",
Order: order,
Offset: options.Offset,
})
if err != nil {
return nil, nil, err
}
var next *host.ScrobbleOptions
targetLen := len(scrobbles)
if len(scrobbles) == lookahead {
nextTimestamp := scrobbles[lookahead-1].SubmissionTime
targetLen = lookahead - 1
ties := 0
for i := targetLen - 1; i >= 0; i-- {
if scrobbles[i].SubmissionTime != nextTimestamp {
break
}
ties++
}
// Every scrobble in this page shares the timestamp, so the ties skipped by the
// incoming offset are still ahead of us and must be carried over
if ties == targetLen {
ties += options.Offset
}
advanced := options
advanced.Offset = ties
if options.Descending {
advanced.ToTimestamp = &nextTimestamp
} else {
advanced.FromTimestamp = &nextTimestamp
}
next = &advanced
}
scrobbleRefs := make([]host.ScrobbleRef, targetLen)
for idx, scrobble := range scrobbles[:targetLen] {
scrobbleRefs[idx] = host.ScrobbleRef{
ID: scrobble.ID,
MediaFileID: scrobble.MediaFileID,
SubmissionTime: scrobble.SubmissionTime,
}
}
return scrobbleRefs, next, nil
}
func (s *scrobbleRetrieverServiceImpl) GetScrobbleCount(ctx context.Context, username string, options host.ScrobbleCountOptions) (int64, error) {
ctx, err := s.getUserContext(ctx, username)
if err != nil {
return 0, err
}
return s.ds.Scrobble(ctx).CountAll(model.QueryOptions{
Filters: scrobbleRangeFilters(options.FromTimestamp, options.ToTimestamp),
})
}
func scrobbleRangeFilters(from, to *int64) squirrel.And {
var filters squirrel.And
if from != nil {
filters = append(filters, squirrel.GtOrEq{"scrobbles.submission_time": *from})
}
if to != nil {
filters = append(filters, squirrel.LtOrEq{"scrobbles.submission_time": *to})
}
return filters
}
var _ host.ScrobbleRetrieverService = (*scrobbleRetrieverServiceImpl)(nil)