diff --git a/core/scrobbler/play_tracker.go b/core/scrobbler/play_tracker.go index 541a8e92b..d8212cabb 100644 --- a/core/scrobbler/play_tracker.go +++ b/core/scrobbler/play_tracker.go @@ -363,22 +363,29 @@ func (p *playTracker) ReportPlayback(ctx context.Context, params ReportPlaybackP case StateStopped: var loadedMF *model.MediaFile haveVerdict := false - if !params.IgnoreScrobble && player.ScrobbleEnabled { + if !params.IgnoreScrobble { mf, err := p.ds.MediaFile().GetWithParticipants(ctx, params.MediaId) if err != nil { - return err - } - loadedMF = mf - filtered = p.isFilteredOut(ctx, mf) - haveVerdict = true - trackDurationMs := int64(mf.Duration * 1000) - threshold := min(trackDurationMs*50/100, 240_000) - if params.PositionMs >= threshold { - err = p.incPlay(ctx, mf, now) - if err != nil { - log.Warn(ctx, "Error updating play counts", "id", mf.ID, "track", mf.Title, "user", user.UserName, err) + // If the track is not found, do not treat it as a fatal error. + // Log it and move on so that the session can still be closed + log.Warn(ctx, "Cannot find track for scrobbling", "id", params.MediaId, "user", user.UserName, err) + } else { + loadedMF = mf + filtered = p.isFilteredOut(ctx, mf) + haveVerdict = true + trackDurationMs := int64(mf.Duration * 1000) + threshold := min(trackDurationMs*50/100, 240_000) + if params.PositionMs >= threshold { + // To be consistent with Submit (/rest/scrobble?submission=true), play should be incremented even if + // scrobbling is disabled + err = p.incPlay(ctx, mf, now) + if err != nil { + log.Warn(ctx, "Error updating play counts", "id", mf.ID, "track", mf.Title, "user", user.UserName, err) + } + if player.ScrobbleEnabled { + p.dispatchScrobble(ctx, mf, now, filtered) + } } - p.dispatchScrobble(ctx, mf, now, filtered) } } p.sessionsMu.Lock() diff --git a/core/scrobbler/play_tracker_test.go b/core/scrobbler/play_tracker_test.go index 045827811..3075f9843 100644 --- a/core/scrobbler/play_tracker_test.go +++ b/core/scrobbler/play_tracker_test.go @@ -315,12 +315,15 @@ var _ = Describe("PlayTracker", func() { Expect(fake.ScrobbleCalled.Load()).To(BeFalse()) }) - It("does not send track to agent if player is not enabled to send scrobbles", func() { + It("does not send track to agent if player is not enabled to send scrobbles (but should still track internally)", func() { ctx = request.WithPlayer(ctx, model.Player{ScrobbleEnabled: false}) err := tracker.Submit(ctx, []Submission{{TrackID: "123", Timestamp: time.Now()}}) Expect(err).ToNot(HaveOccurred()) + Expect(track.PlayCount).To(Equal(int64(1))) + Expect(album.PlayCount).To(Equal(int64(1))) + Expect(fake.ScrobbleCalled.Load()).To(BeFalse()) }) @@ -811,7 +814,8 @@ var _ = Describe("PlayTracker", func() { MediaId: "123", PositionMs: 90000, State: "stopped", PlaybackRate: 1.0, ClientId: defaultClientId, }) Expect(err).ToNot(HaveOccurred()) - Expect(track.PlayCount).To(Equal(int64(0))) + Expect(track.PlayCount).To(Equal(int64(1))) + Consistently(func() bool { return fake.ScrobbleCalled.Load() }).Should(BeFalse()) }) It("scrobbles twice for two separate sessions of same song", func() {