🎧 Your Personal Streaming Service
Go airsonicmadsonicmedia-servermusicmusic-servernavidromeopensubsonicraspberry-piself-hostedstreamerstreaming-apistreaming-audiosubsonicsubsonic-server
Optimize refresh events for scrobble endpoint
Deluan86271f04126f
diff --git a/server/events/events.go b/server/events/events.goindex 4410e7134..00d991e6b 100644--- a/server/events/events.go+++ b/server/events/events.go@@ -53,6 +53,9 @@ func (rr *RefreshResource) With(resource string, ids ...string) *RefreshResourceif rr.resources == nil {rr.resources = make(map[string][]string)}+ if len(ids) == 0 {+ rr.resources[resource] = append(rr.resources[resource], Any)+ }for i := range ids {rr.resources[resource] = append(rr.resources[resource], ids[i])}diff --git a/server/events/events_test.go b/server/events/events_test.goindex a5a7b05df..38a13ae48 100644--- a/server/events/events_test.go+++ b/server/events/events_test.go@@ -37,8 +37,8 @@ var _ = Describe("Events", func() {data := rr.Data(rr)Expect(data).To(Equal(`{"album":["al-1","al-2","al-3"],"artist":["ar-1","ar-2"],"song":["sg-1","sg-2"]}`))})- It("should send a * for when Any is used as id", func() {- rr.With("album", Any)+ It("should send a * when no ids are specified", func() {+ rr.With("album")data := rr.Data(rr)Expect(data).To(Equal(`{"album":["*"]}`))})diff --git a/server/subsonic/media_annotation.go b/server/subsonic/media_annotation.goindex 36ee28d86..8fd8b7c8c 100644--- a/server/subsonic/media_annotation.go+++ b/server/subsonic/media_annotation.go@@ -127,6 +127,9 @@ func (c *MediaAnnotationController) Scrobble(w http.ResponseWriter, r *http.RequplayerId := 1 // TODO Multiple players, based on playerName/username/clientIP(?)playerName := utils.ParamString(r, "c")username := utils.ParamString(r, "u")+ ctx := r.Context()+ event := &events.RefreshResource{}+ submissions := 0log.Debug(r, "Scrobbling tracks", "ids", ids, "times", times, "submission", submission)for i, id := range ids {@@ -137,19 +140,24 @@ func (c *MediaAnnotationController) Scrobble(w http.ResponseWriter, r *http.Requt = time.Now()}if submission {- _, err := c.scrobblerRegister(r.Context(), playerId, id, t)+ mf, err := c.scrobblerRegister(ctx, playerId, id, t)if err != nil {log.Error(r, "Error scrobbling track", "id", id, err)continue}+ submissions+++ event.With("song", mf.ID).With("album", mf.AlbumID).With("artist", mf.AlbumArtistID)} else {- _, err := c.scrobblerNowPlaying(r.Context(), playerId, playerName, id, username)+ _, err := c.scrobblerNowPlaying(ctx, playerId, playerName, id, username)if err != nil {log.Error(r, "Error setting current song", "id", id, err)continue}}}+ if submissions > 0 {+ c.broker.SendMessage(ctx, event)+ }return newResponse(), nil}@@ -177,7 +185,6 @@ func (c *MediaAnnotationController) scrobblerRegister(ctx context.Context, playeif err != nil {log.Error("Error while scrobbling", "trackId", trackId, "user", username, err)} else {- c.broker.SendMessage(ctx, &events.RefreshResource{})log.Info("Scrobbled", "title", mf.Title, "artist", mf.Artist, "user", username)}