Skip to content

Commit

Permalink
optimize: token expire
Browse files Browse the repository at this point in the history
  • Loading branch information
anhoder committed Oct 7, 2023
1 parent 5cb1a7d commit 0e0b3ee
Show file tree
Hide file tree
Showing 15 changed files with 44 additions and 49 deletions.
6 changes: 2 additions & 4 deletions internal/ui/menu_add_to_user_playlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ func (m *AddToUserPlaylistMenu) BeforeEnterMenuHook() model.Hook {
err error
)
res, err = m.spotifox.spotifyClient.CurrentUsersPlaylists(context.Background(), spotify.Limit(m.limit))
if utils.CheckSpotifyErr(err) == utils.NeedLogin {
page, _ := m.spotifox.ToLoginPage(EnterMenuCallback(main))
if catched, page := m.spotifox.HandleResCode(utils.CheckSpotifyErr(err), EnterMenuCallback(main)); catched {
return false, page
}
if err != nil {
Expand Down Expand Up @@ -109,8 +108,7 @@ func (m *AddToUserPlaylistMenu) BottomOutHook() model.Hook {
err error
)
res, err = m.spotifox.spotifyClient.CurrentUsersPlaylists(context.Background(), spotify.Limit(m.limit), spotify.Offset(m.offset))
if utils.CheckSpotifyErr(err) == utils.NeedLogin {
page, _ := m.spotifox.ToLoginPage(BottomOutHookCallback(main, m))
if catched, page := m.spotifox.HandleResCode(utils.CheckSpotifyErr(err), BottomOutHookCallback(main, m)); catched {
return false, page
}
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions internal/ui/menu_album_detail.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ func (m *AlbumDetailMenu) BeforeEnterMenuHook() model.Hook {
}

res, err := m.spotifox.spotifyClient.GetAlbumTracks(context.Background(), m.album.ID, spotify.Limit(50))
if utils.CheckSpotifyErr(err) == utils.NeedLogin {
page, _ := m.spotifox.ToLoginPage(EnterMenuCallback(main))
if catched, page := m.spotifox.HandleResCode(utils.CheckSpotifyErr(err), EnterMenuCallback(main)); catched {
return false, page
}
if err != nil {
Expand Down
6 changes: 2 additions & 4 deletions internal/ui/menu_artist_album.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ func (m *ArtistAlbumMenu) BeforeEnterMenuHook() model.Hook {
}

res, err := m.spotifox.spotifyClient.GetArtistAlbums(context.Background(), m.artistId, []spotify.AlbumType{AllType}, spotify.Limit(m.limit))
if utils.CheckSpotifyErr(err) == utils.NeedLogin {
page, _ := m.spotifox.ToLoginPage(EnterMenuCallback(main))
if catched, page := m.spotifox.HandleResCode(utils.CheckSpotifyErr(err), EnterMenuCallback(main)); catched {
return false, page
}
if err != nil {
Expand Down Expand Up @@ -89,8 +88,7 @@ func (m *ArtistAlbumMenu) BottomOutHook() model.Hook {

m.offset += m.limit
res, err := m.spotifox.spotifyClient.GetArtistAlbums(context.Background(), m.artistId, []spotify.AlbumType{AllType}, spotify.Limit(m.limit), spotify.Offset(m.offset))
if utils.CheckSpotifyErr(err) == utils.NeedLogin {
page, _ := m.spotifox.ToLoginPage(BottomOutHookCallback(main, m))
if catched, page := m.spotifox.HandleResCode(utils.CheckSpotifyErr(err), BottomOutHookCallback(main, m)); catched {
return false, page
}
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions internal/ui/menu_artist_song.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ func (m *ArtistSongMenu) BeforeEnterMenuHook() model.Hook {
country = m.spotifox.user.Country
}
res, err := m.spotifox.spotifyClient.GetArtistsTopTracks(context.Background(), m.artistId, country)
if utils.CheckSpotifyErr(err) == utils.NeedLogin {
page, _ := m.spotifox.ToLoginPage(EnterMenuCallback(main))
if catched, page := m.spotifox.HandleResCode(utils.CheckSpotifyErr(err), EnterMenuCallback(main)); catched {
return false, page
}
if err != nil {
Expand Down
6 changes: 2 additions & 4 deletions internal/ui/menu_featured_playlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ func (m *FeaturedPlaylistMenu) BeforeEnterMenuHook() model.Hook {
}

msg, res, err := m.spotifox.spotifyClient.FeaturedPlaylists(context.Background(), spotify.Limit(m.limit))
if utils.CheckSpotifyErr(err) == utils.NeedLogin {
page, _ := m.spotifox.ToLoginPage(EnterMenuCallback(main))
if catched, page := m.spotifox.HandleResCode(utils.CheckSpotifyErr(err), EnterMenuCallback(main)); catched {
return false, page
}
if err != nil {
Expand Down Expand Up @@ -87,8 +86,7 @@ func (m *FeaturedPlaylistMenu) BottomOutHook() model.Hook {

m.offset += m.limit
_, res, err := m.spotifox.spotifyClient.FeaturedPlaylists(context.Background(), spotify.Limit(m.limit), spotify.Offset(m.offset))
if utils.CheckSpotifyErr(err) == utils.NeedLogin {
page, _ := m.spotifox.ToLoginPage(BottomOutHookCallback(main, m))
if catched, page := m.spotifox.HandleResCode(utils.CheckSpotifyErr(err), BottomOutHookCallback(main, m)); catched {
return false, page
}
if err != nil {
Expand Down
6 changes: 2 additions & 4 deletions internal/ui/menu_liked_songs.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ func (m *LikedSongsMenu) BeforeEnterMenuHook() model.Hook {
return false, page
}
res, err := m.spotifox.spotifyClient.CurrentUsersTracks(context.Background(), spotify.Limit(m.limit))
if utils.CheckSpotifyErr(err) == utils.NeedLogin {
page, _ := m.spotifox.ToLoginPage(EnterMenuCallback(main))
if catched, page := m.spotifox.HandleResCode(utils.CheckSpotifyErr(err), EnterMenuCallback(main)); catched {
return false, page
}
if err != nil {
Expand Down Expand Up @@ -85,8 +84,7 @@ func (m *LikedSongsMenu) BottomOutHook() model.Hook {

m.offset += m.limit
res, err := m.spotifox.spotifyClient.CurrentUsersTracks(context.Background(), spotify.Limit(m.limit), spotify.Offset(m.offset))
if utils.CheckSpotifyErr(err) == utils.NeedLogin {
page, _ := m.spotifox.ToLoginPage(BottomOutHookCallback(main, m))
if catched, page := m.spotifox.HandleResCode(utils.CheckSpotifyErr(err), BottomOutHookCallback(main, m)); catched {
return false, page
}
if err != nil {
Expand Down
6 changes: 2 additions & 4 deletions internal/ui/menu_playlist_detail.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ func (m *PlaylistDetailMenu) BeforeEnterMenuHook() model.Hook {
return false, page
}
res, err := m.spotifox.spotifyClient.GetPlaylistItems(context.Background(), m.playlistId, spotify.Limit(m.limit))
if utils.CheckSpotifyErr(err) == utils.NeedLogin {
page, _ := m.spotifox.ToLoginPage(EnterMenuCallback(main))
if catched, page := m.spotifox.HandleResCode(utils.CheckSpotifyErr(err), EnterMenuCallback(main)); catched {
return false, page
}
if err != nil {
Expand Down Expand Up @@ -89,8 +88,7 @@ func (m *PlaylistDetailMenu) BottomOutHook() model.Hook {

m.offset += m.limit
res, err := m.spotifox.spotifyClient.GetPlaylistItems(context.Background(), m.playlistId, spotify.Limit(m.limit), spotify.Offset(m.offset))
if utils.CheckSpotifyErr(err) == utils.NeedLogin {
page, _ := m.spotifox.ToLoginPage(BottomOutHookCallback(main, m))
if catched, page := m.spotifox.HandleResCode(utils.CheckSpotifyErr(err), BottomOutHookCallback(main, m)); catched {
return false, page
}
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions internal/ui/menu_search_result.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,7 @@ func (m *SearchResultMenu) BottomOutHook() model.Hook {
m.offset += types.SearchPageSize

res, err := m.spotifox.spotifyClient.Search(context.Background(), m.keyword, m.searchType, spotify.Limit(types.SearchPageSize), spotify.Offset(m.offset))
if utils.CheckSpotifyErr(err) == utils.NeedLogin {
page, _ := m.spotifox.ToLoginPage(BottomOutHookCallback(main, m))
if catched, page := m.spotifox.HandleResCode(utils.CheckSpotifyErr(err), BottomOutHookCallback(main, m)); catched {
return false, page
}
if err != nil {
Expand Down
6 changes: 2 additions & 4 deletions internal/ui/menu_user_artist.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ func (m *UserArtistMenu) BeforeEnterMenuHook() model.Hook {
}

res, err := m.spotifox.spotifyClient.CurrentUsersFollowedArtists(context.Background(), spotify.Limit(m.limit))
if utils.CheckSpotifyErr(err) == utils.NeedLogin {
page, _ := m.spotifox.ToLoginPage(EnterMenuCallback(main))
if catched, page := m.spotifox.HandleResCode(utils.CheckSpotifyErr(err), EnterMenuCallback(main)); catched {
return false, page
}
if err != nil {
Expand Down Expand Up @@ -88,8 +87,7 @@ func (m *UserArtistMenu) BottomOutHook() model.Hook {

m.offset += m.limit
res, err := m.spotifox.spotifyClient.CurrentUsersFollowedArtists(context.Background(), spotify.Limit(m.limit), spotify.Offset(m.offset))
if utils.CheckSpotifyErr(err) == utils.NeedLogin {
page, _ := m.spotifox.ToLoginPage(BottomOutHookCallback(main, m))
if catched, page := m.spotifox.HandleResCode(utils.CheckSpotifyErr(err), BottomOutHookCallback(main, m)); catched {
return false, page
}
if err != nil {
Expand Down
6 changes: 2 additions & 4 deletions internal/ui/menu_user_playlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ func (m *UserPlaylistMenu) BeforeEnterMenuHook() model.Hook {
res, err = m.spotifox.spotifyClient.GetPlaylistsForUser(context.Background(), m.userId, spotify.Limit(m.limit))
}

if utils.CheckSpotifyErr(err) == utils.NeedLogin {
page, _ := m.spotifox.ToLoginPage(EnterMenuCallback(main))
if catched, page := m.spotifox.HandleResCode(utils.CheckSpotifyErr(err), EnterMenuCallback(main)); catched {
return false, page
}
if err != nil {
Expand Down Expand Up @@ -106,8 +105,7 @@ func (m *UserPlaylistMenu) BottomOutHook() model.Hook {
res, err = m.spotifox.spotifyClient.GetPlaylistsForUser(context.Background(), m.userId, spotify.Limit(m.limit), spotify.Offset(m.offset))
}

if utils.CheckSpotifyErr(err) == utils.NeedLogin {
page, _ := m.spotifox.ToLoginPage(BottomOutHookCallback(main, m))
if catched, page := m.spotifox.HandleResCode(utils.CheckSpotifyErr(err), BottomOutHookCallback(main, m)); catched {
return false, page
}
if err != nil {
Expand Down
6 changes: 2 additions & 4 deletions internal/ui/menu_user_top_songs.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ func (m *UserTopSongsMenu) BeforeEnterMenuHook() model.Hook {
return false, page
}
res, err := m.spotifox.spotifyClient.CurrentUsersTopTracks(context.Background(), spotify.Limit(m.limit))
if utils.CheckSpotifyErr(err) == utils.NeedLogin {
page, _ := m.spotifox.ToLoginPage(EnterMenuCallback(main))
if catched, page := m.spotifox.HandleResCode(utils.CheckSpotifyErr(err), EnterMenuCallback(main)); catched {
return false, page
}
if err != nil {
Expand All @@ -76,8 +75,7 @@ func (m *UserTopSongsMenu) BottomOutHook() model.Hook {

m.offset += m.limit
res, err := m.spotifox.spotifyClient.CurrentUsersTopTracks(context.Background(), spotify.Limit(m.limit), spotify.Offset(m.offset))
if utils.CheckSpotifyErr(err) == utils.NeedLogin {
page, _ := m.spotifox.ToLoginPage(BottomOutHookCallback(main, m))
if catched, page := m.spotifox.HandleResCode(utils.CheckSpotifyErr(err), BottomOutHookCallback(main, m)); catched {
return false, page
}
if err != nil {
Expand Down
9 changes: 4 additions & 5 deletions internal/ui/operate.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,11 +371,10 @@ func addSongToUserPlaylist(m *Spotifox, isAdd bool) model.Page {
playlist := me.playlists[menu.RealDataIndex(main.SelectedIndex())]

_, err := m.spotifyClient.AddTracksToPlaylist(context.Background(), playlist.ID, me.song.ID)
if utils.CheckSpotifyErr(err) == utils.NeedLogin {
page, _ := m.ToLoginPage(func() model.Page {
addSongToUserPlaylist(m, isAdd)
return nil
})
if catched, page := m.HandleResCode(utils.CheckSpotifyErr(err), func() model.Page {
addSongToUserPlaylist(m, isAdd)
return nil
}); catched {
return page
}
if err != nil {
Expand Down
9 changes: 4 additions & 5 deletions internal/ui/search_page.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,10 @@ func (s *SearchPage) enterHandler() (model.Page, tea.Cmd) {
}

res, err := s.spotifox.spotifyClient.Search(context.Background(), s.wordsInput.Value(), s.searchType, spotify.Limit(types.SearchPageSize))
if utils.CheckSpotifyErr(err) == utils.NeedLogin {
page, _ := s.spotifox.ToLoginPage(func() model.Page {
s.enterHandler()
return nil
})
if catched, page := s.spotifox.HandleResCode(utils.CheckSpotifyErr(err), func() model.Page {
s.enterHandler()
return nil
}); catched {
return page, func() tea.Msg { return page.Msg() }
}
if err != nil {
Expand Down
15 changes: 15 additions & 0 deletions internal/ui/spotifox.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,21 @@ func NewSpotifox(app *model.App) *Spotifox {
return s
}

func (s *Spotifox) HandleResCode(code utils.ResCode, callback LoginCallback) (bool, model.Page) {
utils.Logger().Printf("[INFO] code: %+v", code)
switch code {
case utils.NeedLogin:
page, _ := s.ToLoginPage(callback)
return true, page
case utils.TokenExpired:
// refresh token
s.login.AfterLogin = callback
page, _ := s.login.handleLoginSuccess()
return true, page
}
return false, nil
}

func (s *Spotifox) ToLoginPage(callback LoginCallback) (model.Page, tea.Cmd) {
s.login.AfterLogin = callback
if s.user != nil && s.user.Username != "" && len(s.user.AuthBlob) > 0 {
Expand Down
3 changes: 2 additions & 1 deletion utils/response_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const (
UnknownError
NeedLogin
NeedReconnect
TokenExpired
)

func CheckSpotifyErr(err error) ResCode {
Expand All @@ -26,7 +27,7 @@ func CheckSpotifyErr(err error) ResCode {
return NeedLogin
}
if errors.Is(err, auth.ErrTokenExpired) {
return NeedLogin
return TokenExpired
}
return UnknownError
}
Expand Down

0 comments on commit 0e0b3ee

Please sign in to comment.