New NowPlaying and Skip detection implementation
This commit is contained in:
@@ -103,8 +103,8 @@ func (g *listGenerator) GetNowPlaying() (Entries, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
entries := make(Entries, len(*npInfo))
|
||||
for i, np := range *npInfo {
|
||||
entries := make(Entries, len(npInfo))
|
||||
for i, np := range npInfo {
|
||||
mf, err := g.mfRepository.Get(np.TrackId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -11,7 +11,7 @@ func CreateMockNowPlayingRepo() *MockNowPlaying {
|
||||
|
||||
type MockNowPlaying struct {
|
||||
NowPlayingRepository
|
||||
info NowPlayingInfo
|
||||
data []NowPlayingInfo
|
||||
err bool
|
||||
}
|
||||
|
||||
@@ -19,24 +19,44 @@ func (m *MockNowPlaying) SetError(err bool) {
|
||||
m.err = err
|
||||
}
|
||||
|
||||
func (m *MockNowPlaying) Set(id, username string, playerId int, playerName string) error {
|
||||
func (m *MockNowPlaying) Enqueue(playerId int, playerName string, trackId, username string) error {
|
||||
if m.err {
|
||||
return errors.New("Error!")
|
||||
}
|
||||
m.info.TrackId = id
|
||||
m.info.Username = username
|
||||
m.info.Start = time.Now()
|
||||
m.info.PlayerId = playerId
|
||||
m.info.PlayerName = playerName
|
||||
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
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *MockNowPlaying) Clear(playerId int) (*NowPlayingInfo, error) {
|
||||
r := m.info
|
||||
m.info = NowPlayingInfo{}
|
||||
return &r, nil
|
||||
func (m *MockNowPlaying) Dequeue(playerId int) (*NowPlayingInfo, error) {
|
||||
if len(m.data) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
l := len(m.data)
|
||||
info := m.data[l-1]
|
||||
m.data = m.data[:l-1]
|
||||
|
||||
return &info, nil
|
||||
}
|
||||
|
||||
func (m *MockNowPlaying) Current() NowPlayingInfo {
|
||||
return m.info
|
||||
func (m *MockNowPlaying) Head(playerId int) (*NowPlayingInfo, error) {
|
||||
if len(m.data) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
info := m.data[0]
|
||||
return &info, nil
|
||||
}
|
||||
|
||||
func (m *MockNowPlaying) ClearAll() {
|
||||
m.data = make([]NowPlayingInfo, 0)
|
||||
m.err = false
|
||||
}
|
||||
|
||||
+12
-3
@@ -12,8 +12,17 @@ type NowPlayingInfo struct {
|
||||
PlayerName string
|
||||
}
|
||||
|
||||
// This repo has the semantics of a FIFO queue, for each playerId
|
||||
type NowPlayingRepository interface {
|
||||
Set(trackId, username string, playerId int, playerName string) error
|
||||
Clear(playerId int) (*NowPlayingInfo, error)
|
||||
GetAll() (*[]NowPlayingInfo, error)
|
||||
// Insert at the head of the queue
|
||||
Enqueue(playerId int, playerName string, trackId, username string) error
|
||||
|
||||
// Returns the element at the head of the queue (last inserted one)
|
||||
Head(playerId int) (*NowPlayingInfo, error)
|
||||
|
||||
// Removes and returns the element at the end of the queue
|
||||
Dequeue(playerId int) (*NowPlayingInfo, error)
|
||||
|
||||
// Returns all heads from all playerIds
|
||||
GetAll() ([]*NowPlayingInfo, error)
|
||||
}
|
||||
|
||||
+31
-34
@@ -5,14 +5,14 @@ import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/astaxie/beego"
|
||||
"github.com/deluan/gosonic/domain"
|
||||
"github.com/deluan/gosonic/itunesbridge"
|
||||
)
|
||||
|
||||
type Scrobbler interface {
|
||||
Register(playerId int, trackId string, playDate time.Time) (*domain.MediaFile, error)
|
||||
NowPlaying(playerId int, trackId, username string, playerName string) (*domain.MediaFile, error)
|
||||
DetectSkipped(playerId int, trackId string, submission bool) (bool, error)
|
||||
NowPlaying(playerId int, playerName, trackId, username string) (*domain.MediaFile, error)
|
||||
}
|
||||
|
||||
func NewScrobbler(itunes itunesbridge.ItunesControl, mr domain.MediaFileRepository, npr NowPlayingRepository) Scrobbler {
|
||||
@@ -25,39 +25,20 @@ type scrobbler struct {
|
||||
npRepo NowPlayingRepository
|
||||
}
|
||||
|
||||
func (s *scrobbler) DetectSkipped(playerId int, trackId string, submission bool) (bool, error) {
|
||||
np, err := s.npRepo.Clear(playerId)
|
||||
if err != nil {
|
||||
return false, err
|
||||
func (s *scrobbler) Register(playerId int, trackId string, playDate time.Time) (*domain.MediaFile, error) {
|
||||
for {
|
||||
np, err := s.npRepo.Dequeue(playerId)
|
||||
if err != nil || np == nil || np.TrackId == trackId {
|
||||
break
|
||||
}
|
||||
err = s.itunes.MarkAsSkipped(np.TrackId, np.Start.Add(time.Duration(1)*time.Minute))
|
||||
if err != nil {
|
||||
beego.Warn("Error skipping track", np.TrackId)
|
||||
} else {
|
||||
beego.Debug("Skipped track", np.TrackId)
|
||||
}
|
||||
}
|
||||
|
||||
if np == nil {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
if (submission && np.TrackId != trackId) || (!submission) {
|
||||
return true, s.itunes.MarkAsSkipped(np.TrackId, time.Now())
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func (s *scrobbler) Register(playerId int, id string, playDate time.Time) (*domain.MediaFile, error) {
|
||||
mf, err := s.mfRepo.Get(id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if mf == nil {
|
||||
return nil, errors.New(fmt.Sprintf(`Id "%s" not found`, id))
|
||||
}
|
||||
|
||||
if err := s.itunes.MarkAsPlayed(id, playDate); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return mf, nil
|
||||
}
|
||||
|
||||
func (s *scrobbler) NowPlaying(playerId int, trackId, username string, playerName string) (*domain.MediaFile, error) {
|
||||
mf, err := s.mfRepo.Get(trackId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -67,5 +48,21 @@ func (s *scrobbler) NowPlaying(playerId int, trackId, username string, playerNam
|
||||
return nil, errors.New(fmt.Sprintf(`Id "%s" not found`, trackId))
|
||||
}
|
||||
|
||||
return mf, s.npRepo.Set(trackId, username, playerId, playerName)
|
||||
if err := s.itunes.MarkAsPlayed(trackId, playDate); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return mf, nil
|
||||
}
|
||||
|
||||
func (s *scrobbler) NowPlaying(playerId int, playerName, trackId, username string) (*domain.MediaFile, error) {
|
||||
mf, err := s.mfRepo.Get(trackId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if mf == nil {
|
||||
return nil, errors.New(fmt.Sprintf(`Id "%s" not found`, trackId))
|
||||
}
|
||||
|
||||
return mf, s.npRepo.Enqueue(playerId, playerName, trackId, username)
|
||||
}
|
||||
|
||||
+13
-12
@@ -54,7 +54,7 @@ func TestScrobbler(t *testing.T) {
|
||||
})
|
||||
|
||||
Convey("When I inform the song that is now playing", func() {
|
||||
mf, err := scrobbler.NowPlaying(1, "2", "deluan", "DSub")
|
||||
mf, err := scrobbler.NowPlaying(1, "DSub", "2", "deluan")
|
||||
|
||||
Convey("Then I get the song for that id back", func() {
|
||||
So(err, ShouldBeNil)
|
||||
@@ -62,7 +62,7 @@ func TestScrobbler(t *testing.T) {
|
||||
})
|
||||
|
||||
Convey("And it saves the song as the one current playing", func() {
|
||||
info := npRepo.Current()
|
||||
info, _ := npRepo.Head(1)
|
||||
So(info.TrackId, ShouldEqual, "2")
|
||||
So(info.Start, ShouldHappenBefore, time.Now())
|
||||
So(info.Username, ShouldEqual, "deluan")
|
||||
@@ -76,32 +76,33 @@ func TestScrobbler(t *testing.T) {
|
||||
|
||||
Reset(func() {
|
||||
itCtrl.played = make(map[string]time.Time)
|
||||
itCtrl.skipped = make(map[string]time.Time)
|
||||
})
|
||||
|
||||
})
|
||||
Convey("Given a DB with two songs", t, func() {
|
||||
mfRepo.SetData(`[{"Id":"1","Title":"Femme Fatale"},{"Id":"2","Title":"Here She Comes Now"}]`, 2)
|
||||
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() {
|
||||
scrobbler.NowPlaying(1, "1", "deluan", "DSub")
|
||||
Convey("And I start playing the other song without scrobbling the first one", func() {
|
||||
skip, err := scrobbler.DetectSkipped(1, "2", false)
|
||||
scrobbler.NowPlaying(1, "DSub", "1", "deluan")
|
||||
Convey("And I play the other song without scrobbling the first one", func() {
|
||||
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() {
|
||||
So(skip, ShouldBeTrue)
|
||||
So(mf.Id, ShouldEqual, "2")
|
||||
So(itCtrl.skipped, ShouldContainKey, "1")
|
||||
So(err, ShouldBeNil)
|
||||
})
|
||||
})
|
||||
Convey("And I scrobble it before starting to play the other song", func() {
|
||||
skip, err := scrobbler.DetectSkipped(1, "1", true)
|
||||
mf, err := scrobbler.Register(1, "1", time.Now())
|
||||
Convey("Then the first song should NOT marked as skipped", func() {
|
||||
So(skip, ShouldBeFalse)
|
||||
So(mf.Id, ShouldEqual, "1")
|
||||
So(itCtrl.skipped, ShouldBeEmpty)
|
||||
So(err, ShouldBeNil)
|
||||
})
|
||||
})
|
||||
Reset(func() {
|
||||
itCtrl.skipped = make(map[string]time.Time)
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user