mirror of
https://github.com/navidrome/navidrome.git
synced 2026-06-02 07:01:36 +00:00
* feat(plugins): add PlaybackReport to Scrobbler interface and all implementations * feat(plugins): add PlaybackReport worker and dispatch in PlayTracker * feat(plugins): add PlaybackReportRequest to plugin scrobbler capability * chore(plugins): regenerate PDK files with PlaybackReport * feat(plugins): add PlaybackReport to test scrobbler plugin * feat(plugins): add PlaybackReport to plugin scrobbler adapter * refactor(plugins): fix double DB fetch in StateStopped and batch getActiveScrobblers - Hoist mf from scrobble branch so PlaybackReport reuses it instead of fetching again from DB - Call getActiveScrobblers once per drain batch instead of per-entry * chore(plugins): include generated scrobbler schema with PlaybackReport * fix(plugins): skip PlaybackReport for plugins that don't export it Plugins detected as scrobblers only need to export one scrobbler function. Older plugins that don't export nd_scrobbler_playback_report would cause noisy error logs on every reportPlayback call. Now errFunctionNotFound and errNotImplemented are treated as no-ops. * refactor: rename NowPlayingInfo to PlaybackReport Signed-off-by: Deluan <deluan@navidrome.org> * refactor: rename stopNowPlayingWorker to stopBackgroundWorkers Signed-off-by: Deluan <deluan@navidrome.org> * refactor: move NowPlaying and PlaybackReport logic to separate worker files Signed-off-by: Deluan <deluan@navidrome.org> * refactor(scrobbler): rename NowPlayingInfo to PlaybackSession and add expired state Rename NowPlayingInfo struct to PlaybackSession to better reflect its role as a complete playback session representation. Add UserId field to make sessions self-contained, removing redundant userId parameters from PlaybackReport interface method and internal dispatch functions. Introduce StateExpired internal state that fires when a session cache entry expires without an explicit stop, ensuring plugins always receive a terminal event regardless of client behavior. * fix(scrobbler): update playback state description to include 'expired' Signed-off-by: Deluan <deluan@navidrome.org> * fix(scrobbler): resolve data race in OnExpiration callback Capture conf.Server.EnableNowPlaying at construction time instead of reading it from the background ttlcache eviction goroutine. The previous code raced with test config cleanup that writes to the same field concurrently. * fix(scrobbler): return error when media file lookup fails in StateStopped Simplify the MediaFile population logic in the stopped case to return an error if the track cannot be found. A stop report with an empty MediaFile is useless to plugins, and returning the error allows clients to retry or alert the user when auto-scrobble is enabled. * refactor(scrobbler): use session data directly in PlaybackReport adapter Use info.Username from PlaybackSession instead of extracting it from context in the plugin adapter, since the session is now self-contained. Add debug/trace logging for session expiration and enqueue the expired report with a user-enriched context so downstream handlers can identify the user. --------- Signed-off-by: Deluan <deluan@navidrome.org>
105 lines
3.0 KiB
Go
105 lines
3.0 KiB
Go
// Test scrobbler plugin for Navidrome plugin system integration tests.
|
|
// Build with: tinygo build -o ../test-scrobbler.wasm -target wasip1 -buildmode=c-shared ./main.go
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"strconv"
|
|
|
|
"github.com/navidrome/navidrome/plugins/pdk/go/pdk"
|
|
"github.com/navidrome/navidrome/plugins/pdk/go/scrobbler"
|
|
)
|
|
|
|
func init() {
|
|
scrobbler.Register(&testScrobbler{})
|
|
}
|
|
|
|
type testScrobbler struct{}
|
|
|
|
// IsAuthorized checks if a user is authorized.
|
|
func (t *testScrobbler) IsAuthorized(scrobbler.IsAuthorizedRequest) (bool, error) {
|
|
return checkAuthConfig(), nil
|
|
}
|
|
|
|
// NowPlaying sends a now playing notification.
|
|
func (t *testScrobbler) NowPlaying(input scrobbler.NowPlayingRequest) error {
|
|
// Check for configured error
|
|
if err := checkConfigError(); err != nil {
|
|
return err
|
|
}
|
|
|
|
// Log the now playing (for potential debugging)
|
|
artistName := ""
|
|
if len(input.Track.Artists) > 0 {
|
|
artistName = input.Track.Artists[0].Name
|
|
}
|
|
pdk.Log(pdk.LogInfo, "NowPlaying: "+input.Track.Title+" by "+artistName)
|
|
return nil
|
|
}
|
|
|
|
// Scrobble submits a scrobble.
|
|
func (t *testScrobbler) Scrobble(input scrobbler.ScrobbleRequest) error {
|
|
// Check for configured error
|
|
if err := checkConfigError(); err != nil {
|
|
return err
|
|
}
|
|
|
|
// Log the scrobble (for potential debugging)
|
|
artistName := ""
|
|
if len(input.Track.Artists) > 0 {
|
|
artistName = input.Track.Artists[0].Name
|
|
}
|
|
pdk.Log(pdk.LogInfo, "Scrobble: "+input.Track.Title+" by "+artistName)
|
|
return nil
|
|
}
|
|
|
|
// PlaybackReport receives a playback state report.
|
|
func (t *testScrobbler) PlaybackReport(input scrobbler.PlaybackReportRequest) error {
|
|
if err := checkConfigError(); err != nil {
|
|
return err
|
|
}
|
|
|
|
artistName := ""
|
|
if len(input.Track.Artists) > 0 {
|
|
artistName = input.Track.Artists[0].Name
|
|
}
|
|
pdk.Log(pdk.LogInfo, "PlaybackReport: "+input.Track.Title+" by "+artistName+" state="+input.State)
|
|
return nil
|
|
}
|
|
|
|
// checkConfigError checks if the plugin is configured to return an error.
|
|
// If "error" config is set, it returns the appropriate ScrobblerError.
|
|
// Error types: "not_authorized", "retry_later", "unrecoverable"
|
|
func checkConfigError() error {
|
|
errMsg, hasErr := pdk.GetConfig("error")
|
|
if !hasErr || errMsg == "" {
|
|
return nil
|
|
}
|
|
errType, _ := pdk.GetConfig("error_type")
|
|
switch errType {
|
|
case scrobbler.ScrobblerErrorNotAuthorized.Error():
|
|
return fmt.Errorf("%w: %s", scrobbler.ScrobblerErrorNotAuthorized, errMsg)
|
|
case scrobbler.ScrobblerErrorRetryLater.Error():
|
|
return fmt.Errorf("%w: %s", scrobbler.ScrobblerErrorRetryLater, errMsg)
|
|
default:
|
|
return fmt.Errorf("%w: %s", scrobbler.ScrobblerErrorUnrecoverable, errMsg)
|
|
}
|
|
}
|
|
|
|
// checkAuthConfig returns whether the plugin is configured to authorize users.
|
|
// If "authorized" config is set to "false", users are not authorized.
|
|
// Default is true (authorized).
|
|
func checkAuthConfig() bool {
|
|
authStr, hasAuth := pdk.GetConfig("authorized")
|
|
if !hasAuth {
|
|
return true // Default: authorized
|
|
}
|
|
auth, err := strconv.ParseBool(authStr)
|
|
if err != nil {
|
|
return true // Default on parse error
|
|
}
|
|
return auth
|
|
}
|
|
|
|
func main() {}
|