Register skipped songs!
This commit is contained in:
@@ -31,6 +31,12 @@ func (m *MockNowPlaying) Set(id, username string, playerId int, playerName strin
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *MockNowPlaying) Clear(playerId int) (*NowPlayingInfo, error) {
|
||||
r := m.info
|
||||
m.info = NowPlayingInfo{}
|
||||
return &r, nil
|
||||
}
|
||||
|
||||
func (m *MockNowPlaying) Current() NowPlayingInfo {
|
||||
return m.info
|
||||
}
|
||||
|
||||
@@ -14,5 +14,6 @@ type NowPlayingInfo struct {
|
||||
|
||||
type NowPlayingRepository interface {
|
||||
Set(trackId, username string, playerId int, playerName string) error
|
||||
Clear(playerId int) (*NowPlayingInfo, error)
|
||||
GetAll() (*[]NowPlayingInfo, error)
|
||||
}
|
||||
|
||||
+22
-5
@@ -10,8 +10,9 @@ import (
|
||||
)
|
||||
|
||||
type Scrobbler interface {
|
||||
Register(trackId string, playDate time.Time) (*domain.MediaFile, error)
|
||||
NowPlaying(trackId, username string, playerName string) (*domain.MediaFile, error)
|
||||
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)
|
||||
}
|
||||
|
||||
func NewScrobbler(itunes itunesbridge.ItunesControl, mr domain.MediaFileRepository, npr NowPlayingRepository) Scrobbler {
|
||||
@@ -24,7 +25,23 @@ type scrobbler struct {
|
||||
npRepo NowPlayingRepository
|
||||
}
|
||||
|
||||
func (s *scrobbler) Register(id string, playDate time.Time) (*domain.MediaFile, error) {
|
||||
func (s *scrobbler) DetectSkipped(playerId int, trackId string, submission bool) (bool, error) {
|
||||
np, err := s.npRepo.Clear(playerId)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
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
|
||||
@@ -40,7 +57,7 @@ func (s *scrobbler) Register(id string, playDate time.Time) (*domain.MediaFile,
|
||||
return mf, nil
|
||||
}
|
||||
|
||||
func (s *scrobbler) NowPlaying(trackId, username string, playerName string) (*domain.MediaFile, error) {
|
||||
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
|
||||
@@ -50,5 +67,5 @@ func (s *scrobbler) NowPlaying(trackId, username string, playerName string) (*do
|
||||
return nil, errors.New(fmt.Sprintf(`Id "%s" not found`, trackId))
|
||||
}
|
||||
|
||||
return mf, s.npRepo.Set(trackId, username, 1, playerName)
|
||||
return mf, s.npRepo.Set(trackId, username, playerId, playerName)
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ func TestScrobbler(t *testing.T) {
|
||||
|
||||
Convey("When I scrobble an existing song", func() {
|
||||
now := time.Now()
|
||||
mf, err := scrobbler.Register("2", now)
|
||||
mf, err := scrobbler.Register(1, "2", now)
|
||||
|
||||
Convey("Then I get the scrobbled song back", func() {
|
||||
So(err, ShouldBeNil)
|
||||
@@ -42,7 +42,7 @@ func TestScrobbler(t *testing.T) {
|
||||
})
|
||||
|
||||
Convey("When the ID is not in the DB", func() {
|
||||
_, err := scrobbler.Register("3", time.Now())
|
||||
_, err := scrobbler.Register(1, "3", time.Now())
|
||||
|
||||
Convey("Then I receive an error", func() {
|
||||
So(err, ShouldNotBeNil)
|
||||
@@ -54,7 +54,7 @@ func TestScrobbler(t *testing.T) {
|
||||
})
|
||||
|
||||
Convey("When I inform the song that is now playing", func() {
|
||||
mf, err := scrobbler.NowPlaying("2", "deluan", "DSub")
|
||||
mf, err := scrobbler.NowPlaying(1, "2", "deluan", "DSub")
|
||||
|
||||
Convey("Then I get the song for that id back", func() {
|
||||
So(err, ShouldBeNil)
|
||||
@@ -79,13 +79,38 @@ func TestScrobbler(t *testing.T) {
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
Convey("Given a DB with two songs", t, func() {
|
||||
mfRepo.SetData(`[{"Id":"1","Title":"Femme Fatale"},{"Id":"2","Title":"Here She Comes Now"}]`, 2)
|
||||
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)
|
||||
Convey("Then the first song should be marked as skipped", func() {
|
||||
So(skip, ShouldBeTrue)
|
||||
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)
|
||||
Convey("Then the first song should NOT marked as skipped", func() {
|
||||
So(skip, ShouldBeFalse)
|
||||
So(itCtrl.skipped, ShouldBeEmpty)
|
||||
So(err, ShouldBeNil)
|
||||
})
|
||||
})
|
||||
Reset(func() {
|
||||
itCtrl.skipped = make(map[string]time.Time)
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
type mockItunesControl struct {
|
||||
itunesbridge.ItunesControl
|
||||
played map[string]time.Time
|
||||
error bool
|
||||
played map[string]time.Time
|
||||
skipped map[string]time.Time
|
||||
error bool
|
||||
}
|
||||
|
||||
func (m *mockItunesControl) MarkAsPlayed(id string, playDate time.Time) error {
|
||||
@@ -98,3 +123,14 @@ func (m *mockItunesControl) MarkAsPlayed(id string, playDate time.Time) error {
|
||||
m.played[id] = playDate
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *mockItunesControl) MarkAsSkipped(id string, skipDate time.Time) error {
|
||||
if m.error {
|
||||
return errors.New("ID not found")
|
||||
}
|
||||
if m.skipped == nil {
|
||||
m.skipped = make(map[string]time.Time)
|
||||
}
|
||||
m.skipped[id] = skipDate
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user