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>
137 lines
5.7 KiB
Go
137 lines
5.7 KiB
Go
// Code generated by ndpgen. DO NOT EDIT.
|
|
//
|
|
// This file contains mock implementations for non-WASM builds.
|
|
// These mocks allow IDE support, compilation, and unit testing on non-WASM platforms.
|
|
// Plugin authors can use the exported mock instances to set expectations in tests.
|
|
//
|
|
//go:build !wasip1
|
|
|
|
package host
|
|
|
|
import (
|
|
"github.com/stretchr/testify/mock"
|
|
)
|
|
|
|
// ScrobbleCountOptions represents the ScrobbleCountOptions data structure.
|
|
// ScrobbleCountOptions carries optional parameters for counting user scrobbles
|
|
type ScrobbleCountOptions struct {
|
|
FromTimestamp *int64 `json:"fromTimestamp"`
|
|
ToTimestamp *int64 `json:"toTimestamp"`
|
|
}
|
|
|
|
// ScrobbleOptions represents the ScrobbleOptions data structure.
|
|
// ScrobbleOptions carries optional parameters for retrieving user scrobbles
|
|
type ScrobbleOptions struct {
|
|
FromTimestamp *int64 `json:"fromTimestamp"`
|
|
ToTimestamp *int64 `json:"toTimestamp"`
|
|
Descending bool `json:"descending"`
|
|
MaxItems int `json:"maxItems"`
|
|
Offset int `json:"offset"`
|
|
}
|
|
|
|
// ScrobbleRef represents the ScrobbleRef data structure.
|
|
// ScrobbleRef represents one instance of a scrobble (instance id, file id, submission time)
|
|
type ScrobbleRef struct {
|
|
ID int64 `json:"id"`
|
|
MediaFileID string `json:"mediaFileId"`
|
|
SubmissionTime int64 `json:"submissionTime"`
|
|
}
|
|
|
|
// mockScrobbleRetrieverService is the mock implementation for testing.
|
|
type mockScrobbleRetrieverService struct {
|
|
mock.Mock
|
|
}
|
|
|
|
// ScrobbleRetrieverMock is the auto-instantiated mock instance for testing.
|
|
// Use this to set expectations: host.ScrobbleRetrieverMock.On("MethodName", args...).Return(values...)
|
|
var ScrobbleRetrieverMock = &mockScrobbleRetrieverService{}
|
|
|
|
// GetFirstTimestamp is the mock method for ScrobbleRetrieverGetFirstTimestamp.
|
|
func (m *mockScrobbleRetrieverService) GetFirstTimestamp(username string) (*int64, error) {
|
|
args := m.Called(username)
|
|
var r0 *int64
|
|
if v := args.Get(0); v != nil {
|
|
r0 = v.(*int64)
|
|
}
|
|
return r0, args.Error(1)
|
|
}
|
|
|
|
// ScrobbleRetrieverGetFirstTimestamp delegates to the mock instance.
|
|
// GetFirstTimestamp returns the unix timestamp of the oldest scrobble for the user.
|
|
// If the user has no scrobbles, returns nil
|
|
func ScrobbleRetrieverGetFirstTimestamp(username string) (*int64, error) {
|
|
return ScrobbleRetrieverMock.GetFirstTimestamp(username)
|
|
}
|
|
|
|
// GetLastTimestamp is the mock method for ScrobbleRetrieverGetLastTimestamp.
|
|
func (m *mockScrobbleRetrieverService) GetLastTimestamp(username string) (*int64, error) {
|
|
args := m.Called(username)
|
|
var r0 *int64
|
|
if v := args.Get(0); v != nil {
|
|
r0 = v.(*int64)
|
|
}
|
|
return r0, args.Error(1)
|
|
}
|
|
|
|
// ScrobbleRetrieverGetLastTimestamp delegates to the mock instance.
|
|
// GetLastTimestamp returns the unix timestamp of the most recent scrobble for the user
|
|
// If the user has no scrobbles, return nil
|
|
func ScrobbleRetrieverGetLastTimestamp(username string) (*int64, error) {
|
|
return ScrobbleRetrieverMock.GetLastTimestamp(username)
|
|
}
|
|
|
|
// GetScrobbles is the mock method for ScrobbleRetrieverGetScrobbles.
|
|
func (m *mockScrobbleRetrieverService) GetScrobbles(username string, options ScrobbleOptions) ([]ScrobbleRef, *ScrobbleOptions, error) {
|
|
args := m.Called(username, options)
|
|
var r0 []ScrobbleRef
|
|
if v := args.Get(0); v != nil {
|
|
r0 = v.([]ScrobbleRef)
|
|
}
|
|
var r1 *ScrobbleOptions
|
|
if v := args.Get(1); v != nil {
|
|
r1 = v.(*ScrobbleOptions)
|
|
}
|
|
return r0, r1, args.Error(2)
|
|
}
|
|
|
|
// ScrobbleRetrieverGetScrobbles delegates to the mock instance.
|
|
// 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
|
|
func ScrobbleRetrieverGetScrobbles(username string, options ScrobbleOptions) ([]ScrobbleRef, *ScrobbleOptions, error) {
|
|
return ScrobbleRetrieverMock.GetScrobbles(username, options)
|
|
}
|
|
|
|
// GetScrobbleCount is the mock method for ScrobbleRetrieverGetScrobbleCount.
|
|
func (m *mockScrobbleRetrieverService) GetScrobbleCount(username string, options ScrobbleCountOptions) (int64, error) {
|
|
args := m.Called(username, options)
|
|
return args.Get(0).(int64), args.Error(1)
|
|
}
|
|
|
|
// ScrobbleRetrieverGetScrobbleCount delegates to the mock instance.
|
|
// 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
|
|
func ScrobbleRetrieverGetScrobbleCount(username string, options ScrobbleCountOptions) (int64, error) {
|
|
return ScrobbleRetrieverMock.GetScrobbleCount(username, options)
|
|
}
|