mirror of
https://github.com/navidrome/navidrome.git
synced 2026-11-19 15:23:36 +00:00
refactor(agents): remove typed-nil workarounds from Last.fm and ListenBrainz (#6476)
This commit is contained in:
parent
a6f3b91f87
commit
3679b6215d
@ -49,10 +49,11 @@ type lastfmAgent struct {
|
||||
getInfoMutex sync.Mutex
|
||||
}
|
||||
|
||||
func lastFMEnabled() bool {
|
||||
return conf.Server().LastFM.Enabled && conf.Server().LastFM.ApiKey != "" && conf.Server().LastFM.Secret != ""
|
||||
}
|
||||
|
||||
func lastFMConstructor(ds model.DataStore) *lastfmAgent {
|
||||
if !conf.Server().LastFM.Enabled || conf.Server().LastFM.ApiKey == "" || conf.Server().LastFM.Secret == "" {
|
||||
return nil
|
||||
}
|
||||
l := &lastfmAgent{
|
||||
ds: ds,
|
||||
languages: conf.Server().LastFM.Languages,
|
||||
@ -431,22 +432,16 @@ func (l *lastfmAgent) PlaybackReport(context.Context, scrobbler.PlaybackSession)
|
||||
func init() {
|
||||
conf.AddHook(func() {
|
||||
agents.Register(lastFMAgentName, func(ds model.DataStore) agents.Interface {
|
||||
// This is a workaround for the fact that a (Interface)(nil) is not the same as a (*lastfmAgent)(nil)
|
||||
// See https://go.dev/doc/faq#nil_error
|
||||
a := lastFMConstructor(ds)
|
||||
if a != nil {
|
||||
return a
|
||||
if !lastFMEnabled() {
|
||||
return nil
|
||||
}
|
||||
return nil
|
||||
return lastFMConstructor(ds)
|
||||
})
|
||||
scrobbler.Register(lastFMAgentName, func(ds model.DataStore) scrobbler.Scrobbler {
|
||||
// Same as above - this is a workaround for the fact that a (Scrobbler)(nil) is not the same as a (*lastfmAgent)(nil)
|
||||
// See https://go.dev/doc/faq#nil_error
|
||||
a := lastFMConstructor(ds)
|
||||
if a != nil {
|
||||
return a
|
||||
if !lastFMEnabled() {
|
||||
return nil
|
||||
}
|
||||
return nil
|
||||
return lastFMConstructor(ds)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
@ -47,24 +47,21 @@ var _ = Describe("lastfmAgent", func() {
|
||||
Expect(agent.languages).To(Equal([]string{"pt", "en"}))
|
||||
})
|
||||
})
|
||||
When("Agent is disabled", func() {
|
||||
It("returns nil", func() {
|
||||
conf.Update(func(c *conf.Options) { c.LastFM.Enabled = false })
|
||||
Expect(lastFMConstructor(ds)).To(BeNil())
|
||||
})
|
||||
})
|
||||
When("ApiKey is empty", func() {
|
||||
It("returns nil", func() {
|
||||
conf.Update(func(c *conf.Options) { c.LastFM.ApiKey = "" })
|
||||
Expect(lastFMConstructor(ds)).To(BeNil())
|
||||
})
|
||||
})
|
||||
When("Secret is empty", func() {
|
||||
It("returns nil", func() {
|
||||
conf.Update(func(c *conf.Options) { c.LastFM.Secret = "" })
|
||||
Expect(lastFMConstructor(ds)).To(BeNil())
|
||||
})
|
||||
})
|
||||
|
||||
Describe("lastFMEnabled", func() {
|
||||
It("is true when enabled with an api key and secret", func() {
|
||||
Expect(lastFMEnabled()).To(BeTrue())
|
||||
})
|
||||
DescribeTable("is false when the configuration is incomplete",
|
||||
func(change func(*conf.Options)) {
|
||||
conf.Update(change)
|
||||
Expect(lastFMEnabled()).To(BeFalse())
|
||||
},
|
||||
Entry("disabled", func(c *conf.Options) { c.LastFM.Enabled = false }),
|
||||
Entry("no api key", func(c *conf.Options) { c.LastFM.ApiKey = "" }),
|
||||
Entry("no secret", func(c *conf.Options) { c.LastFM.Secret = "" }),
|
||||
)
|
||||
})
|
||||
|
||||
Describe("GetArtistBiography", func() {
|
||||
|
||||
@ -231,23 +231,10 @@ func init() {
|
||||
conf.AddHook(func() {
|
||||
if conf.Server().ListenBrainz.Enabled {
|
||||
scrobbler.Register(listenBrainzAgentName, func(ds model.DataStore) scrobbler.Scrobbler {
|
||||
// This is a workaround for the fact that a (Interface)(nil) is not the same as a (*listenBrainzAgent)(nil)
|
||||
// See https://go.dev/doc/faq#nil_error
|
||||
a := listenBrainzConstructor(ds)
|
||||
if a != nil {
|
||||
return a
|
||||
}
|
||||
return nil
|
||||
return listenBrainzConstructor(ds)
|
||||
})
|
||||
|
||||
agents.Register(listenBrainzAgentName, func(ds model.DataStore) agents.Interface {
|
||||
// This is a workaround for the fact that a (Interface)(nil) is not the same as a (*listenBrainzAgent)(nil)
|
||||
// See https://go.dev/doc/faq#nil_error
|
||||
a := listenBrainzConstructor(ds)
|
||||
if a != nil {
|
||||
return a
|
||||
}
|
||||
return nil
|
||||
return listenBrainzConstructor(ds)
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user