Skip to content

Commit

Permalink
再生終了時に新規に曲をSpotifyのキューに追加する処理はhandleWaitTimerExpiredの方で行うことで、active d…
Browse files Browse the repository at this point in the history
…evice not foundのときに正しくINTERRUPT処理が行われるようにする
  • Loading branch information
p1ass committed Aug 6, 2020
1 parent 61feab1 commit df7a1f4
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 92 deletions.
35 changes: 18 additions & 17 deletions usecase/session_timer.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,12 @@ func (s *SessionTimerUseCase) startTrackEndTrigger(ctx context.Context, sessionI

func (s *SessionTimerUseCase) handleWaitTimerExpired(ctx context.Context, sessionID string, triggerAfterTrackEnd *entity.SyncCheckTimer, currentOperation currentOperation) error {
logger := log.New()
logger.Debugj(map[string]interface{}{"message": "currentOperation", "currentOperation": currentOperation})

playingInfo, err := s.playerCli.CurrentlyPlaying(ctx)
if err != nil {
logger.Errorj(map[string]interface{}{
"message": "startTrackEndTrigger: failed to get currently playing info",
"message": "handleWaitTimerExpired: failed to get currently playing info",
"sessionID": sessionID,
"error": err.Error(),
})
Expand All @@ -107,7 +108,7 @@ func (s *SessionTimerUseCase) handleWaitTimerExpired(ctx context.Context, sessio
sess, err := s.sessionRepo.FindByID(ctx, sessionID)
if err != nil {
logger.Errorj(map[string]interface{}{
"message": "startTrackEndTrigger: failed to get session",
"message": "handleWaitTimerExpired: failed to get session",
"sessionID": sessionID,
"error": err.Error(),
})
Expand All @@ -118,7 +119,7 @@ func (s *SessionTimerUseCase) handleWaitTimerExpired(ctx context.Context, sessio
s.handleInterrupt(sess)
if err := s.sessionRepo.Update(ctx, sess); err != nil {
logger.Errorj(map[string]interface{}{
"message": "startTrackEndTrigger: failed to update session after handleInterrupt",
"message": "handleWaitTimerExpired: failed to update session after IsPlayingCorrectTrack and handleInterrupt",
"sessionID": sessionID,
"error": err.Error(),
})
Expand All @@ -127,7 +128,20 @@ func (s *SessionTimerUseCase) handleWaitTimerExpired(ctx context.Context, sessio
return fmt.Errorf("session interrupt")
}

logger.Debugj(map[string]interface{}{"message": "currentOperation", "currentOperation": currentOperation})
track := sess.TrackURIShouldBeAddedWhenHandleTrackEnd()
if track != "" {
if err := s.playerCli.Enqueue(ctx, track, sess.DeviceID); err != nil {
s.handleInterrupt(sess)
if err := s.sessionRepo.Update(ctx, sess); err != nil {
logger.Errorj(map[string]interface{}{
"message": "handleWaitTimerExpired: failed to update session after Enqueue and handleInterrupt",
"sessionID": sessionID,
"error": err.Error(),
})
return fmt.Errorf("failed to enqueue track")
}
}
}

switch currentOperation {
case operationNextTrack:
Expand Down Expand Up @@ -210,19 +224,6 @@ func (s *SessionTimerUseCase) handleTrackEndTx(sessionID string) func(ctx contex
}, nil
}

track := sess.TrackURIShouldBeAddedWhenHandleTrackEnd()
if track != "" {
// TODO: Spotifyアプリを閉じた後、ずっとRelaymを開かないとINTERRUPTにならずにここまでたどり着いて
// active device not foundになってしまう
// そのときstateはPLAYのままなので表示がバグる
if err := s.playerCli.Enqueue(ctx, track, sess.DeviceID); err != nil {
return &handleTrackEndResponse{
nextTrack: false,
err: fmt.Errorf("call add queue api trackURI=%s: %w", track, err),
}, nil
}
}

logger.Debugj(map[string]interface{}{"message": "next track", "sessionID": sess.ID, "queueHead": sess.QueueHead})

return &handleTrackEndResponse{nextTrack: true, err: nil}, nil
Expand Down
133 changes: 58 additions & 75 deletions usecase/session_timer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,80 +119,6 @@ func TestSessionTimerUseCase_handleTrackEndTx(t *testing.T) {
wantNextTrack: true,
wantErr: false,
},
{
name: "次の曲が存在し、次に再生される曲の二曲先の曲が存在するときはNEXTTRACKイベントが送られて、次の再生状態に遷移し、同時に二曲先の曲がSpotifyのqueueに積まれる",
sessionID: "sessionID",
prepareMockPlayerFn: func(m *mock_spotify.MockPlayer) {
m.EXPECT().Enqueue(gomock.Any(), "spotify:track:3", "deviceID").Return(nil)
},
prepareMockPusherFn: func(m *mock_event.MockPusher) {
},
prepareMockUserRepoFn: func(m *mock_repository.MockUser) {},
prepareMockSessionRepoFn: func(m *mock_repository.MockSession) {
m.EXPECT().FindByIDForUpdate(gomock.Any(), "sessionID").Return(&entity.Session{
ID: "sessionID",
Name: "name",
CreatorID: "creatorID",
DeviceID: "deviceID",
StateType: entity.Play,
QueueHead: 0,
QueueTracks: []*entity.QueueTrack{
{
Index: 0,
URI: "spotify:track:asfafefea",
SessionID: "sessionID",
},
{
Index: 1,
URI: "spotify:track:06QTSGUEgcmKwiEJ0IMPig",
SessionID: "sessionID",
},
{
Index: 2,
URI: "spotify:track:2",
SessionID: "sessionID",
},
{
Index: 3,
URI: "spotify:track:3",
SessionID: "sessionID",
},
},
}, nil)
m.EXPECT().Update(gomock.Any(), &entity.Session{
ID: "sessionID",
Name: "name",
CreatorID: "creatorID",
DeviceID: "deviceID",
StateType: entity.Play,
QueueHead: 1,
QueueTracks: []*entity.QueueTrack{
{
Index: 0,
URI: "spotify:track:asfafefea",
SessionID: "sessionID",
},
{
Index: 1,
URI: "spotify:track:06QTSGUEgcmKwiEJ0IMPig",
SessionID: "sessionID",
},
{
Index: 2,
URI: "spotify:track:2",
SessionID: "sessionID",
},
{
Index: 3,
URI: "spotify:track:3",
SessionID: "sessionID",
},
},
}).Return(nil)
},
wantNextTrack: true,
wantErr: false,
},
{
name: "次の曲が存在するが、実際には違う曲が流れていた場合はINTERRUPTイベントが送られる",
sessionID: "sessionID",
Expand Down Expand Up @@ -455,7 +381,61 @@ func TestSessionTimerUseCase_handleWaitTimerExpired(t *testing.T) {
wantErr: false,
},
{
name: "Spotifyとの同期が取れていることが確認されると、currentOperationがNextTrackの時はイベントは送信されない",
name: "Spotifyとの同期が取れていることが確認されると、currentOperationがNextTrackの時はイベントが送信される",
sessionID: "sessionID",
currentOperation: "NextTrack",
prepareMockPlayerFn: func(m *mock_spotify.MockPlayer) {
m.EXPECT().CurrentlyPlaying(gomock.Any()).Return(&entity.CurrentPlayingInfo{
Playing: true,
Progress: 10000000,
Track: &entity.Track{
URI: "spotify:track:06QTSGUEgcmKwiEJ0IMPig",
ID: "06QTSGUEgcmKwiEJ0IMPig",
Name: "Borderland",
Duration: 213066000000,
Artists: []*entity.Artist{{Name: "MONOEYES"}},
URL: "https://open.spotify.com/track/06QTSGUEgcmKwiEJ0IMPig",
Album: &entity.Album{
Name: "Interstate 46 E.P.",
Images: []*entity.AlbumImage{
{
URL: "https://i.scdn.co/image/ab67616d0000b273b48630d6efcebca2596120c4",
Height: 640,
Width: 640,
},
},
},
},
}, nil)
},
prepareMockPusherFn: func(m *mock_event.MockPusher) {
m.EXPECT().Push(&event.PushMessage{
SessionID: "sessionID",
Msg: entity.NewEventNextTrack(1),
})
},
prepareMockUserRepoFn: func(m *mock_repository.MockUser) {},
prepareMockSessionRepoFn: func(m *mock_repository.MockSession) {
m.EXPECT().FindByID(gomock.Any(), "sessionID").Return(&entity.Session{
ID: "sessionID",
Name: "name",
CreatorID: "creatorID",
DeviceID: "deviceID",
StateType: "PLAY",
QueueHead: 1,
QueueTracks: []*entity.QueueTrack{
{Index: 0, URI: "spotify:track:5uQ0vKy2973Y9IUCd1wMEF"},
{Index: 1, URI: "spotify:track:06QTSGUEgcmKwiEJ0IMPig"},
},
ExpiredAt: time.Time{},
AllowToControlByOthers: false,
ProgressWhenPaused: 0,
}, nil)
},
wantErr: false,
},
{
name: "Spotifyとの同期が取れていることが確認されると、新しく追加すべき曲がSpotifyのキューに追加される",
sessionID: "sessionID",
currentOperation: "NextTrack",
prepareMockPlayerFn: func(m *mock_spotify.MockPlayer) {
Expand All @@ -481,6 +461,7 @@ func TestSessionTimerUseCase_handleWaitTimerExpired(t *testing.T) {
},
},
}, nil)
m.EXPECT().Enqueue(gomock.Any(), "spotify:track:track3", "deviceID").Return(nil)
},
prepareMockPusherFn: func(m *mock_event.MockPusher) {
m.EXPECT().Push(&event.PushMessage{
Expand All @@ -500,6 +481,8 @@ func TestSessionTimerUseCase_handleWaitTimerExpired(t *testing.T) {
QueueTracks: []*entity.QueueTrack{
{Index: 0, URI: "spotify:track:5uQ0vKy2973Y9IUCd1wMEF"},
{Index: 1, URI: "spotify:track:06QTSGUEgcmKwiEJ0IMPig"},
{Index: 2, URI: "spotify:track:track2"},
{Index: 3, URI: "spotify:track:track3"},
},
ExpiredAt: time.Time{},
AllowToControlByOthers: false,
Expand Down

0 comments on commit df7a1f4

Please sign in to comment.