refactor: capabilities registration

Signed-off-by: Deluan <deluan@navidrome.org>
This commit is contained in:
Deluan 2025-12-24 12:44:12 -05:00
parent b2e1c216a0
commit e951a82265
4 changed files with 47 additions and 33 deletions

View File

@ -4,41 +4,13 @@ package plugins
// Capabilities are detected by checking which functions a plugin exports.
type Capability string
const (
// CapabilityMetadataAgent indicates the plugin can provide artist/album metadata.
// Detected when the plugin exports at least one of the metadata agent functions.
CapabilityMetadataAgent Capability = "MetadataAgent"
// CapabilityScrobbler indicates the plugin can receive scrobble events.
// Detected when the plugin exports at least one of the scrobbler functions.
CapabilityScrobbler Capability = "Scrobbler"
// CapabilityScheduler indicates the plugin can receive scheduled event callbacks.
// Detected when the plugin exports the scheduler callback function.
CapabilityScheduler Capability = "Scheduler"
)
// capabilityFunctions maps each capability to its required/optional functions.
// A plugin has a capability if it exports at least one of these functions.
var capabilityFunctions = map[Capability][]string{
CapabilityMetadataAgent: {
FuncGetArtistMBID,
FuncGetArtistURL,
FuncGetArtistBiography,
FuncGetSimilarArtists,
FuncGetArtistImages,
FuncGetArtistTopSongs,
FuncGetAlbumInfo,
FuncGetAlbumImages,
},
CapabilityScrobbler: {
FuncScrobblerIsAuthorized,
FuncScrobblerNowPlaying,
FuncScrobblerScrobble,
},
CapabilityScheduler: {
FuncSchedulerCallback,
},
var capabilityFunctions = map[Capability][]string{}
// registerCapability registers a capability with its associated functions.
func registerCapability(cap Capability, functions ...string) {
capabilityFunctions[cap] = functions
}
// functionExistsChecker is an interface for checking if a function exists in a plugin.

View File

@ -12,8 +12,19 @@ import (
"github.com/navidrome/navidrome/scheduler"
)
// CapabilityScheduler indicates the plugin can receive scheduled event callbacks.
// Detected when the plugin exports the scheduler callback function.
const CapabilityScheduler Capability = "Scheduler"
const FuncSchedulerCallback = "nd_scheduler_callback"
func init() {
registerCapability(
CapabilityScheduler,
FuncSchedulerCallback,
)
}
// timeAfterFunc is a variable for time.AfterFunc, allowing tests to override it.
var timeAfterFunc = time.AfterFunc

View File

@ -7,6 +7,10 @@ import (
"github.com/navidrome/navidrome/core/agents"
)
// CapabilityMetadataAgent indicates the plugin can provide artist/album metadata.
// Detected when the plugin exports at least one of the metadata agent functions.
const CapabilityMetadataAgent Capability = "MetadataAgent"
// Export function names (snake_case as per design)
const (
FuncGetArtistMBID = "nd_get_artist_mbid"
@ -19,6 +23,20 @@ const (
FuncGetAlbumImages = "nd_get_album_images"
)
func init() {
registerCapability(
CapabilityMetadataAgent,
FuncGetArtistMBID,
FuncGetArtistURL,
FuncGetArtistBiography,
FuncGetSimilarArtists,
FuncGetArtistImages,
FuncGetArtistTopSongs,
FuncGetAlbumInfo,
FuncGetAlbumImages,
)
}
// MetadataAgent is an adapter that wraps an Extism plugin and implements
// the agents interfaces for metadata retrieval.
type MetadataAgent struct {

View File

@ -9,6 +9,10 @@ import (
"github.com/navidrome/navidrome/model/request"
)
// CapabilityScrobbler indicates the plugin can receive scrobble events.
// Detected when the plugin exports at least one of the scrobbler functions.
const CapabilityScrobbler Capability = "Scrobbler"
// Scrobbler function names (snake_case as per design)
const (
FuncScrobblerIsAuthorized = "nd_scrobbler_is_authorized"
@ -16,6 +20,15 @@ const (
FuncScrobblerScrobble = "nd_scrobbler_scrobble"
)
func init() {
registerCapability(
CapabilityScrobbler,
FuncScrobblerIsAuthorized,
FuncScrobblerNowPlaying,
FuncScrobblerScrobble,
)
}
// ScrobblerPlugin is an adapter that wraps an Extism plugin and implements
// the scrobbler.Scrobbler interface for scrobbling to external services.
type ScrobblerPlugin struct {