mirror of
https://github.com/navidrome/navidrome.git
synced 2025-04-14 03:07:24 +03:00
Refactored NowPlaying
Also added a test case for skipping range
This commit is contained in:
parent
3c8f6e9a65
commit
1cf8a0db44
@ -12,6 +12,7 @@ func CreateMockNowPlayingRepo() *MockNowPlaying {
|
||||
type MockNowPlaying struct {
|
||||
NowPlayingRepository
|
||||
data []NowPlayingInfo
|
||||
t time.Time
|
||||
err 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 = playerName
|
||||
|
||||
m.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
|
||||
}
|
||||
|
@ -15,7 +15,7 @@ type NowPlayingInfo struct {
|
||||
// This repo must have the semantics of a FIFO queue, for each playerId
|
||||
type 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 queue
|
||||
Dequeue(playerId int) (*NowPlayingInfo, error)
|
||||
|
@ -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 strin
|
||||
return 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)
|
||||
}
|
||||
|
@ -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() {
|
||||
|
@ -3,7 +3,6 @@ package persistence
|
||||
import (
|
||||
"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()))
|
||||
|
Loading…
x
Reference in New Issue
Block a user