mirror of
https://github.com/navidrome/navidrome.git
synced 2026-08-31 07:30:32 +00:00
* 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>
90 lines
4.5 KiB
Go
90 lines
4.5 KiB
Go
package host
|
|
|
|
import "context"
|
|
|
|
// ScrobbleRef represents one instance of a scrobble (instance id, file id, submission time)
|
|
type ScrobbleRef struct {
|
|
// The ID of the scrobble. Useful if duplicate scrobbles happen for the same time
|
|
ID int64 `json:"id"`
|
|
// The ID of the MediaFile submitted at this time
|
|
MediaFileID string `json:"mediaFileId"`
|
|
// The UNIX timestamp this scrobble was submitted
|
|
SubmissionTime int64 `json:"submissionTime"`
|
|
}
|
|
|
|
// ScrobbleOptions carries optional parameters for retrieving user scrobbles
|
|
type ScrobbleOptions struct {
|
|
// The starting unix timestamp to query for scrobbles (inclusive).
|
|
// If not specified, start from the first scrobble
|
|
FromTimestamp *int64 `json:"fromTimestamp,omitempty"`
|
|
// The ending unix timestamp to query for scrobbles (inclusive).
|
|
// If not specified, go up to the last scrobble
|
|
ToTimestamp *int64 `json:"toTimestamp,omitempty"`
|
|
// If true, return scrobbles from newest to oldest. Defaults to oldest first
|
|
Descending bool `json:"descending"`
|
|
// The maximum number of items to retrieve. This is capped at 5000, the
|
|
// default if not specified
|
|
MaxItems int `json:"maxItems"`
|
|
// Pagination state managed by GetScrobbles; only meaningful on the options it returns.
|
|
// Never set it or combine it with your own timestamps — scrobbles may be silently skipped
|
|
Offset int `json:"offset,omitempty"`
|
|
}
|
|
|
|
// ScrobbleCountOptions carries optional parameters for counting user scrobbles
|
|
type ScrobbleCountOptions struct {
|
|
// The starting unix timestamp to query for scrobbles (inclusive).
|
|
// If not specified, start from the first scrobble
|
|
FromTimestamp *int64 `json:"fromTimestamp,omitempty"`
|
|
// The ending unix timestamp to query for scrobbles (inclusive).
|
|
// If not specified, go up to the last scrobble
|
|
ToTimestamp *int64 `json:"toTimestamp,omitempty"`
|
|
}
|
|
|
|
// ScrobbleRetrieverService allows a plugin to retrieve scrobbles for one or more authorized users.
|
|
// It will only provide the media_file ID and submission time, which can be combined with the MatcherService
|
|
// to fetch deduped tracks
|
|
//
|
|
//nd:hostservice name=ScrobbleRetriever permission=scrobbleRetriever
|
|
type ScrobbleRetrieverService interface {
|
|
// GetFirstTimestamp returns the unix timestamp of the oldest scrobble for the user.
|
|
// If the user has no scrobbles, returns nil
|
|
//nd:hostfunc
|
|
GetFirstTimestamp(ctx context.Context, username string) (*int64, error)
|
|
|
|
// GetLastTimestamp returns the unix timestamp of the most recent scrobble for the user
|
|
// If the user has no scrobbles, return nil
|
|
//nd:hostfunc
|
|
GetLastTimestamp(ctx context.Context, username string) (*int64, error)
|
|
|
|
// GetScrobbles returns one page of scrobbles for a user.
|
|
//
|
|
// Parameters:
|
|
// - username: the user to query for scrobbles
|
|
// - options.FromTimestamp: If specified, the first UNIX timestamp to start fetching scrobbles (inclusive). Otherwise, start from the first scrobble
|
|
// - options.ToTimestamp: If specified, the last UNIX timestamp to fetch (inclusive). Otherwise, end at the last scrobble
|
|
// - options.Descending: If true, order from newest to oldest. Otherwise, oldest to newest
|
|
// - options.MaxItems: The maximum number of items to retrieve. The maximum value (and default) if not specified is 5000
|
|
// - options.Offset: Pagination state; only valid as received on the options returned by a previous call. Never set it manually
|
|
//
|
|
// Returns:
|
|
// - scrobbles: The scrobbles in the requested range, ordered by submission time
|
|
// (ties broken by scrobble ID) in the direction given by options.Descending
|
|
// - next: The options for the following page, or nil once no scrobbles remain.
|
|
// Pass it back to GetScrobbles unchanged and repeat until it is nil. It carries an
|
|
// adjusted FromTimestamp/ToTimestamp, so keep a copy if you still need the original range
|
|
//nd:hostfunc
|
|
GetScrobbles(ctx context.Context, username string, options ScrobbleOptions) (scrobbles []ScrobbleRef, next *ScrobbleOptions, err error)
|
|
|
|
// GetScrobbleCount returns the number of scrobbles for a user in a given range
|
|
//
|
|
// Parameters:
|
|
// - username: the user to query for scrobbles
|
|
// - options.FromTimestamp: If specified, the first UNIX timestamp to start fetching scrobbles (inclusive). Otherwise, start from the first scrobble
|
|
// - options.ToTimestamp: If specified, the last UNIX timestamp to fetch (inclusive). Otherwise, end at the last scrobble
|
|
//
|
|
// Returns:
|
|
// - the number of scrobbles in the given range, or 0
|
|
//nd:hostfunc
|
|
GetScrobbleCount(ctx context.Context, username string, options ScrobbleCountOptions) (int64, error)
|
|
}
|