🎧 Your Personal Streaming Service
Go airsonicmadsonicmedia-servermusicmusic-servernavidromeopensubsonicraspberry-piself-hostedstreamerstreaming-apistreaming-audiosubsonicsubsonic-server
Refactored NowPlaying
Deluan1cf8a0db4442
diff --git a/engine/mock_nowplaying_repo.go b/engine/mock_nowplaying_repo.goindex a52dfda18..718ee55c4 100644--- a/engine/mock_nowplaying_repo.go+++ b/engine/mock_nowplaying_repo.go@@ -12,6 +12,7 @@ func CreateMockNowPlayingRepo() *MockNowPlaying {type MockNowPlaying struct {NowPlayingRepositorydata []NowPlayingInfo+ t time.Timeerr bool}@@ -19,20 +20,19 @@ func (m *MockNowPlaying) SetError(err bool) {m.err = err}-func (m *MockNowPlaying) Enqueue(playerId int, playerName string, trackId, username string) error {+func (m *MockNowPlaying) Enqueue(info *NowPlayingInfo) error {if m.err {return errors.New("Error!")}- info := NowPlayingInfo{}- info.TrackId = trackId- info.Username = username- info.Start = time.Now()- info.PlayerId = playerId- info.PlayerName = playerNamem.data = append(m.data, NowPlayingInfo{})copy(m.data[1:], m.data[0:])- m.data[0] = info+ m.data[0] = *info++ if !m.t.IsZero() {+ m.data[0].Start = m.t+ m.t = time.Time{}+ }return nil}@@ -80,3 +80,7 @@ func (m *MockNowPlaying) ClearAll() {m.data = make([]NowPlayingInfo, 0)m.err = false}++func (m *MockNowPlaying) OverrideNow(t time.Time) {+ m.t = t+}diff --git a/engine/nowplaying.go b/engine/nowplaying.goindex e7ec8ac37..a7fee47a6 100644--- a/engine/nowplaying.go+++ b/engine/nowplaying.go@@ -15,7 +15,7 @@ type NowPlayingInfo struct {// This repo must have the semantics of a FIFO queue, for each playerIdtype NowPlayingRepository interface {// Insert at the head of the queue- Enqueue(playerId int, playerName string, trackId, username string) error+ Enqueue(*NowPlayingInfo) error// Removes and returns the element at the end of the queueDequeue(playerId int) (*NowPlayingInfo, error)diff --git a/engine/scrobbler.go b/engine/scrobbler.goindex 8d3fc6246..2de62a41b 100644--- a/engine/scrobbler.go+++ b/engine/scrobbler.go@@ -10,6 +10,11 @@ import ("github.com/deluan/gosonic/itunesbridge")+const (+ minSkipped = time.Duration(3) * time.Second+ maxSkipped = time.Duration(20) * time.Second+)+type Scrobbler interface {Register(playerId int, trackId string, playDate time.Time) (*domain.MediaFile, error)NowPlaying(playerId int, playerName, trackId, username string) (*domain.MediaFile, error)@@ -46,7 +51,7 @@ func (s *scrobbler) detectSkipped(playerId int, trackId string) {}}-func (s *scrobbler) Register(playerId int, trackId string, playDate time.Time) (*domain.MediaFile, error) {+func (s *scrobbler) Register(playerId int, trackId string, playTime time.Time) (*domain.MediaFile, error) {s.detectSkipped(playerId, trackId)mf, err := s.mfRepo.Get(trackId)@@ -58,7 +63,7 @@ func (s *scrobbler) Register(playerId int, trackId string, playDate time.Time) (return nil, errors.New(fmt.Sprintf(`Id "%s" not found`, trackId))}- if err := s.itunes.MarkAsPlayed(trackId, playDate); err != nil {+ if err := s.itunes.MarkAsPlayed(trackId, playTime); err != nil {return nil, err}return mf, nil@@ -74,5 +79,6 @@ func (s *scrobbler) NowPlaying(playerId int, playerName, trackId, username strinreturn nil, errors.New(fmt.Sprintf(`Id "%s" not found`, trackId))}- return mf, s.npRepo.Enqueue(playerId, playerName, trackId, username)+ info := &NowPlayingInfo{TrackId: trackId, Username: username, Start: time.Now(), PlayerId: playerId, PlayerName: playerName}+ return mf, s.npRepo.Enqueue(info)}diff --git a/engine/scrobbler_test.go b/engine/scrobbler_test.goindex 3478dd5ef..c9eca94b9 100644--- a/engine/scrobbler_test.go+++ b/engine/scrobbler_test.go@@ -80,21 +80,45 @@ func TestScrobbler(t *testing.T) {})})+}++func TestSkipping(t *testing.T) {+ Init(t, false)++ mfRepo := persistence.CreateMockMediaFileRepo()+ npRepo := engine.CreateMockNowPlayingRepo()+ itCtrl := &mockItunesControl{}++ scrobbler := engine.NewScrobbler(itCtrl, mfRepo, npRepo)+Convey("Given a DB with three songs", t, func() {mfRepo.SetData(`[{"Id":"1","Title":"Femme Fatale"},{"Id":"2","Title":"Here She Comes Now"},{"Id":"3","Title":"Lady Godiva's Operation"}]`, 3)itCtrl.skipped = make(map[string]time.Time)npRepo.ClearAll()Convey("When I play one song", func() {+ start := time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)+ npRepo.OverrideNow(start)scrobbler.NowPlaying(1, "DSub", "1", "deluan")- Convey("And I play the other song without scrobbling the first one", func() {+ Convey("And I skip it before 20 seconds", func() {+ npRepo.OverrideNow(start.Add(time.Duration(5) * time.Second))scrobbler.NowPlaying(1, "DSub", "2", "deluan")- mf, err := scrobbler.Register(1, "2", time.Now())Convey("Then the first song should be marked as skipped", func() {+ mf, err := scrobbler.Register(1, "2", start.Add(time.Duration(3)*time.Minute))So(mf.Id, ShouldEqual, "2")So(itCtrl.skipped, ShouldContainKey, "1")So(err, ShouldBeNil)})})+ SkipConvey("And I skip it after 20 seconds", func() {+ npRepo.OverrideNow(start.Add(time.Duration(30) * time.Second))+ scrobbler.NowPlaying(1, "DSub", "2", "deluan")+ Convey("Then the first song should be marked as skipped", func() {+ mf, err := scrobbler.Register(1, "2", start.Add(time.Duration(3)*time.Minute))+ So(mf.Id, ShouldEqual, "2")+ So(itCtrl.skipped, ShouldBeEmpty)+ So(err, ShouldBeNil)+ })+ })Convey("And I scrobble it before starting to play the other song", func() {mf, err := scrobbler.Register(1, "1", time.Now())Convey("Then the first song should NOT marked as skipped", func() {diff --git a/persistence/nowplaying_repository.go b/persistence/nowplaying_repository.goindex 7ac2b9486..48de8bbb3 100644--- a/persistence/nowplaying_repository.go+++ b/persistence/nowplaying_repository.go@@ -3,7 +3,6 @@ package persistenceimport ("encoding/json""fmt"- "time""github.com/deluan/gosonic/engine")@@ -26,15 +25,13 @@ func nowPlayingKeyName(playerId int) string {return fmt.Sprintf("%s:%d", nowPlayingKeyPrefix, playerId)}-func (r *nowPlayingRepository) Enqueue(playerId int, playerName, id, username string) error {- m := &engine.NowPlayingInfo{TrackId: id, Username: username, Start: time.Now(), PlayerId: playerId, PlayerName: playerName}-- h, err := json.Marshal(m)+func (r *nowPlayingRepository) Enqueue(info *engine.NowPlayingInfo) error {+ h, err := json.Marshal(info)if err != nil {return err}- keyName := []byte(nowPlayingKeyName(playerId))+ keyName := []byte(nowPlayingKeyName(info.PlayerId))_, err = Db().LPush(keyName, []byte(h))Db().LExpire(keyName, int64(engine.NowPlayingExpire.Seconds()))