diff --git a/core/agents/lastfm/agent.go b/core/agents/lastfm/agent.go index 1d2637bd3..0e658219b 100644 --- a/core/agents/lastfm/agent.go +++ b/core/agents/lastfm/agent.go @@ -203,6 +203,11 @@ func (l *lastfmAgent) Scrobble(ctx context.Context, userId string, scrobbles []s return nil } +func (l *lastfmAgent) IsAuthorized(ctx context.Context, userId string) bool { + sk, err := l.sessionKeys.get(ctx, userId) + return err == nil && sk != "" +} + func init() { conf.AddHook(func() { if conf.Server.LastFM.Enabled { diff --git a/core/scrobbler/broker.go b/core/scrobbler/broker.go index 532ec2d79..0f0830724 100644 --- a/core/scrobbler/broker.go +++ b/core/scrobbler/broker.go @@ -66,9 +66,12 @@ func (s *broker) dispatchNowPlaying(ctx context.Context, userId string, trackId } // TODO Parallelize for name, constructor := range scrobblers { - log.Debug(ctx, "Sending NowPlaying info", "scrobbler", name, "track", t.Title, "artist", t.Artist) err := func() error { s := constructor(s.ds) + if !s.IsAuthorized(ctx, userId) { + return nil + } + log.Debug(ctx, "Sending NowPlaying info", "scrobbler", name, "track", t.Title, "artist", t.Artist) return s.NowPlaying(ctx, userId, t) }() if err != nil { @@ -104,13 +107,16 @@ func (s *broker) Submit(ctx context.Context, trackId string, playTime time.Time) scrobbles := []Scrobble{{MediaFile: *t, TimeStamp: playTime}} // TODO Parallelize for name, constructor := range scrobblers { - log.Debug(ctx, "Sending NowPlaying info", "scrobbler", name, "track", t.Title, "artist", t.Artist) err := func() error { s := constructor(s.ds) + if !s.IsAuthorized(ctx, u.ID) { + return nil + } + log.Debug(ctx, "Sending Scrobble", "scrobbler", name, "track", t.Title, "artist", t.Artist) return s.Scrobble(ctx, u.ID, scrobbles) }() if err != nil { - log.Error(ctx, "Error sending NowPlayingInfo", "scrobbler", name, "track", t.Title, "artist", t.Artist, err) + log.Error(ctx, "Error sending Scrobble", "scrobbler", name, "track", t.Title, "artist", t.Artist, err) return err } } diff --git a/core/scrobbler/broker_test.go b/core/scrobbler/broker_test.go index e558158e6..82faa05e5 100644 --- a/core/scrobbler/broker_test.go +++ b/core/scrobbler/broker_test.go @@ -22,7 +22,7 @@ var _ = Describe("Broker", func() { ctx = request.WithUser(ctx, model.User{ID: "u-1"}) ds = &tests.MockDataStore{} broker = GetBroker(ds) - fake = &fakeScrobbler{} + fake = &fakeScrobbler{Authorized: true} Register("fake", func(ds model.DataStore) Scrobbler { return fake }) @@ -44,9 +44,16 @@ var _ = Describe("Broker", func() { It("sends track to agent", func() { err := broker.NowPlaying(ctx, "player-1", "player-one", "123") Expect(err).ToNot(HaveOccurred()) + Expect(fake.NowPlayingCalled).To(BeTrue()) Expect(fake.UserID).To(Equal("u-1")) Expect(fake.Track.ID).To(Equal("123")) }) + It("does not send track to agent if user has not authorized", func() { + fake.Authorized = false + err := broker.NowPlaying(ctx, "player-1", "player-one", "123") + Expect(err).ToNot(HaveOccurred()) + Expect(fake.NowPlayingCalled).ToNot(BeTrue()) + }) }) Describe("GetNowPlaying", func() { @@ -86,21 +93,38 @@ var _ = Describe("Broker", func() { err := broker.Submit(ctx, "123", ts) Expect(err).ToNot(HaveOccurred()) + Expect(fake.ScrobbleCalled).To(BeTrue()) Expect(fake.UserID).To(Equal("u-1")) Expect(fake.Scrobbles[0].ID).To(Equal("123")) }) + + It("does not send track to agent if user has not authorized", func() { + fake.Authorized = false + err := broker.Submit(ctx, "123", time.Now()) + Expect(err).ToNot(HaveOccurred()) + Expect(fake.ScrobbleCalled).ToNot(BeTrue()) + }) + }) }) type fakeScrobbler struct { - UserID string - Track *model.MediaFile - Scrobbles []Scrobble - Error error + Authorized bool + NowPlayingCalled bool + ScrobbleCalled bool + UserID string + Track *model.MediaFile + Scrobbles []Scrobble + Error error +} + +func (f *fakeScrobbler) IsAuthorized(ctx context.Context, userId string) bool { + return f.Error == nil && f.Authorized } func (f *fakeScrobbler) NowPlaying(ctx context.Context, userId string, track *model.MediaFile) error { + f.NowPlayingCalled = true if f.Error != nil { return f.Error } @@ -110,6 +134,7 @@ func (f *fakeScrobbler) NowPlaying(ctx context.Context, userId string, track *mo } func (f *fakeScrobbler) Scrobble(ctx context.Context, userId string, scrobbles []Scrobble) error { + f.ScrobbleCalled = true if f.Error != nil { return f.Error } diff --git a/core/scrobbler/interfaces.go b/core/scrobbler/interfaces.go index 024794a03..158931c89 100644 --- a/core/scrobbler/interfaces.go +++ b/core/scrobbler/interfaces.go @@ -13,6 +13,7 @@ type Scrobble struct { } type Scrobbler interface { + IsAuthorized(ctx context.Context, userId string) bool NowPlaying(ctx context.Context, userId string, track *model.MediaFile) error Scrobble(ctx context.Context, userId string, scrobbles []Scrobble) error }