From e1ee57bdadd3edea86f7d207f2e237c2bcfdfbc1 Mon Sep 17 00:00:00 2001 From: James Barnsley Date: Fri, 4 Aug 2017 22:33:49 +1200 Subject: [PATCH] Spotify.access will be checker for full access --- src/js/components/FollowButton.js | 7 +- src/js/components/Settings/Services.js | 122 +++++++++--------- src/js/components/Sidebar.js | 2 +- .../components/SpotifyAuthenticationFrame.js | 4 +- src/js/services/core/middleware.js | 4 +- src/js/services/localstorage/middleware.js | 19 +-- src/js/services/pusher/middleware.js | 6 + src/js/services/spotify/actions.js | 4 +- src/js/services/spotify/middleware.js | 10 +- src/js/services/spotify/reducer.js | 4 +- src/js/views/Album.js | 2 +- src/js/views/Artist.js | 2 +- src/js/views/Playlist.js | 2 +- src/js/views/User.js | 2 +- .../views/discover/DiscoverRecommendations.js | 2 +- 15 files changed, 100 insertions(+), 92 deletions(-) diff --git a/src/js/components/FollowButton.js b/src/js/components/FollowButton.js index e44d09c9..ed159449 100755 --- a/src/js/components/FollowButton.js +++ b/src/js/components/FollowButton.js @@ -15,7 +15,7 @@ class FollowButton extends React.Component{ } componentDidMount(){ - if (this.props.spotify_authorized && this.props.uri){ + if (this.props.spotify_access == 'full' && this.props.uri){ this.props.spotifyActions.following(this.props.uri) } } @@ -29,7 +29,7 @@ class FollowButton extends React.Component{ } render(){ - if (!this.props.spotify_authorized || !this.props.uri){ + if (this.props.spotify_access !== 'full' || !this.props.uri){ return false } @@ -56,7 +56,8 @@ class FollowButton extends React.Component{ const mapStateToProps = (state, ownProps) => { return { load_queue: state.ui.load_queue, - spotify_authorized: state.spotify.authorized + spotify_access: state.spotify.access, + spotify_authorized: state.spotify.authorization } } diff --git a/src/js/components/Settings/Services.js b/src/js/components/Settings/Services.js index 8c3c4dba..a67c3097 100755 --- a/src/js/components/Settings/Services.js +++ b/src/js/components/Settings/Services.js @@ -77,7 +77,7 @@ class Services extends React.Component{ var user = null if (this.props.spotify.me && this.props.spotify.authorization && this.props.spotify.authentication_provider == 'http_api'){ user = this.props.spotify.me - } else if (this.props.spotify.backend_username && this.props.spotify.backend_username){ + } else if (this.props.spotify.backend_username){ if (this.props.core.users && this.props.core.users['spotify:user:'+this.props.spotify.backend_username] !== undefined){ user = this.props.core.users['spotify:user:'+this.props.spotify.backend_username] } @@ -114,7 +114,7 @@ class Services extends React.Component{ } renderSendAuthorizationButton(){ - if (!this.props.spotify.authorized) return null + if (!this.props.spotify.authorization) return null return ( + ) } else { return ( @@ -147,7 +147,7 @@ class SpotifyAuthenticationFrame extends React.Component{ const mapStateToProps = (state, ownProps) => { return { authorization_url: state.spotify.authorization_url, - authorized: state.spotify.authorized, + authorized: state.spotify.authorization, authorizing: state.spotify.authorizing, refreshing_token: state.spotify.refreshing_token } diff --git a/src/js/services/core/middleware.js b/src/js/services/core/middleware.js index 0ec7d178..3e827292 100755 --- a/src/js/services/core/middleware.js +++ b/src/js/services/core/middleware.js @@ -201,7 +201,7 @@ const CoreMiddleware = (function(){ break case 'spotify': - if (store.getState().spotify.authorized && store.getState().spotify.me){ + if (store.getState().spotify.authorization && store.getState().spotify.me){ playlist.can_edit = (helpers.getFromUri('playlistowner',playlist.uri) == store.getState().spotify.me.id) } } @@ -225,7 +225,7 @@ const CoreMiddleware = (function(){ break case 'spotify': - if (store.getState().spotify.authorized && store.getState().spotify.me){ + if (store.getState().spotify.authorization && store.getState().spotify.me){ playlist.can_edit = (helpers.getFromUri('playlistowner',playlist.uri) == store.getState().spotify.me.id) } } diff --git a/src/js/services/localstorage/middleware.js b/src/js/services/localstorage/middleware.js index be21cd1c..8eb06e72 100755 --- a/src/js/services/localstorage/middleware.js +++ b/src/js/services/localstorage/middleware.js @@ -83,10 +83,10 @@ const localstorageMiddleware = (function(){ case 'SPOTIFY_AUTHORIZATION_GRANTED': var spotify = JSON.parse( localStorage.getItem('spotify') ); - if( !spotify ) spotify = {}; - Object.assign( - spotify,{ - authorized: true, + spotify = Object.assign( + {}, + (spotify ? spotify : {}), + { authorization: action.data, access_token: action.data.access_token, refresh_token: action.data.refresh_token, @@ -97,16 +97,19 @@ const localstorageMiddleware = (function(){ break; case 'SPOTIFY_AUTHORIZATION_REVOKED': + console.log('local',action) var spotify = JSON.parse( localStorage.getItem('spotify') ); - if( !spotify ) spotify = {}; - Object.assign( - spotify,{ - authorized: false, + spotify = Object.assign( + {}, + (spotify ? spotify : {}), + { + authorization: false, access_token: false, refresh_token: false, token_expiry: false } ); + console.log(spotify) localStorage.setItem('spotify', JSON.stringify(spotify)); break; diff --git a/src/js/services/pusher/middleware.js b/src/js/services/pusher/middleware.js index b1edafcb..9b38b3b4 100755 --- a/src/js/services/pusher/middleware.js +++ b/src/js/services/pusher/middleware.js @@ -335,6 +335,12 @@ const PusherMiddleware = (function(){ authorization_url: (action.config.authorization_url ? action.config.authorization_url : null), backend_username: (action.config.spotify_username ? action.config.spotify_username : null) })) + + // Get our backend_username user + if (store.getState().spotify.access !== 'none' && (!store.getState().core.users || !store.getState().core.users[action.config.spotify_username])){ + store.dispatch(spotifyActions.getUser(action.config.spotify_username)) + } + next( action ) break diff --git a/src/js/services/spotify/actions.js b/src/js/services/spotify/actions.js index dd627ed6..5774be93 100755 --- a/src/js/services/spotify/actions.js +++ b/src/js/services/spotify/actions.js @@ -112,7 +112,7 @@ function getToken( dispatch, getState ){ function refreshToken( dispatch, getState ){ return new Promise( (resolve, reject) => { - if (getState().spotify.authorized){ + if (getState().spotify.authorization && getState().spotify.authentication_provider == 'http_api'){ $.ajax({ method: 'GET', @@ -204,7 +204,7 @@ export function authorizationGranted( data ){ return { type: 'SPOTIFY_AUTHORIZATION_GRANTED', data: data } } -export function authorizationRevoked(){ +export function revokeAuthorization(){ return { type: 'SPOTIFY_AUTHORIZATION_REVOKED' } } diff --git a/src/js/services/spotify/middleware.js b/src/js/services/spotify/middleware.js index 1fd355c0..8791fb2d 100755 --- a/src/js/services/spotify/middleware.js +++ b/src/js/services/spotify/middleware.js @@ -68,7 +68,7 @@ const SpotifyMiddleware = (function(){ break case 'SPOTIFY_CREATE_PLAYLIST': - if( !store.getState().spotify.authorized ){ + if( !store.getState().spotify.authorization ){ store.dispatch( uiActions.createNotification( "Must be logged in to Spotify to do that", 'bad' ) ) return } @@ -78,7 +78,7 @@ const SpotifyMiddleware = (function(){ case 'SPOTIFY_REMOVE_PLAYLIST_TRACKS': var playlist = state.core.playlists[action.key] - if( !store.getState().spotify.authorized ){ + if( !store.getState().spotify.authorization ){ store.dispatch( uiActions.createNotification( "Must be logged in to Spotify to do that", 'bad' ) ) return } @@ -92,7 +92,7 @@ const SpotifyMiddleware = (function(){ case 'SPOTIFY_ADD_PLAYLIST_TRACKS': - if( !store.getState().spotify.authorized ){ + if( !store.getState().spotify.authorization ){ store.dispatch( uiActions.createNotification( "Must be logged in to Spotify to do that", 'bad' ) ) return } @@ -102,7 +102,7 @@ const SpotifyMiddleware = (function(){ case 'SPOTIFY_REORDER_PLAYLIST_TRACKS': - if( !store.getState().spotify.authorized ){ + if( !store.getState().spotify.authorization ){ store.dispatch( uiActions.createNotification( "Must be logged in to Spotify to do that", 'bad' ) ) return } @@ -117,7 +117,7 @@ const SpotifyMiddleware = (function(){ case 'SPOTIFY_SAVE_PLAYLIST': - if( !store.getState().spotify.authorized ){ + if( !store.getState().spotify.authorization ){ store.dispatch( uiActions.createNotification( "Must be logged in to Spotify to do that", 'bad' ) ) return } diff --git a/src/js/services/spotify/reducer.js b/src/js/services/spotify/reducer.js index 2f13b5d9..4eb0b4d7 100755 --- a/src/js/services/spotify/reducer.js +++ b/src/js/services/spotify/reducer.js @@ -18,7 +18,7 @@ export default function reducer(spotify = {}, action){ var spotify = Object.assign({},spotify,action.config) let access = 'none' - if (spotify.authentication_provider == 'http_api' && spotify.authorized){ + if (spotify.authentication_provider == 'http_api' && spotify.authorization){ access = 'full' } else if (spotify.authentication_provider == 'backend' && spotify.backend_username){ access = 'limited' @@ -27,7 +27,7 @@ export default function reducer(spotify = {}, action){ return spotify case 'PUSHER_SPOTIFY_TOKEN': - if( spotify.authorized ) return spotify; + if( spotify.authorization ) return spotify; return Object.assign({}, spotify, { authorizing: false, authorization: false, diff --git a/src/js/views/Album.js b/src/js/views/Album.js index 5ddaa78b..9bbfcd06 100755 --- a/src/js/views/Album.js +++ b/src/js/views/Album.js @@ -173,7 +173,7 @@ const mapStateToProps = (state, ownProps) => { album: (state.core.albums && typeof(state.core.albums[ownProps.params.uri]) !== 'undefined' ? state.core.albums[ownProps.params.uri] : false ), albums: state.core.albums, library_albums: state.core.library_albums, - spotify_authorized: state.spotify.authorized, + spotify_authorized: state.spotify.authorization, mopidy_connected: state.mopidy.connected }; } diff --git a/src/js/views/Artist.js b/src/js/views/Artist.js index c0a5d212..a34da95d 100755 --- a/src/js/views/Artist.js +++ b/src/js/views/Artist.js @@ -265,7 +265,7 @@ const mapStateToProps = (state, ownProps) => { artists: (state.core.artists ? state.core.artists : []), library_artists: (state.core.library_artists ? state.core.library_artists : []), albums: (state.core.albums ? state.core.albums : []), - spotify_authorized: state.spotify.authorized, + spotify_authorized: state.spotify.authorization, mopidy_connected: state.mopidy.connected } } diff --git a/src/js/views/Playlist.js b/src/js/views/Playlist.js index e994aed7..a3b6d975 100755 --- a/src/js/views/Playlist.js +++ b/src/js/views/Playlist.js @@ -217,7 +217,7 @@ const mapStateToProps = (state, ownProps) => { playlist: (state.core.playlists && typeof(state.core.playlists[uri]) !== 'undefined' ? state.core.playlists[uri] : false ), library_playlists: state.core.library_playlists, mopidy_connected: state.mopidy.connected, - spotify_authorized: state.spotify.authorized, + spotify_authorized: state.spotify.authorization, spotify_userid: state.spotify.me.id } } diff --git a/src/js/views/User.js b/src/js/views/User.js index 864de4ec..e8e8e7fd 100755 --- a/src/js/views/User.js +++ b/src/js/views/User.js @@ -121,7 +121,7 @@ class User extends React.Component{ const mapStateToProps = (state, ownProps) => { return { load_queue: state.ui.load_queue, - spotify_authorized: state.spotify.authorized, + spotify_authorized: state.spotify.authorization, spotify_authentication_provider: state.spotify.authentication_provider, spotify_backend_username: state.spotify.backend_username, me: state.spotify.me, diff --git a/src/js/views/discover/DiscoverRecommendations.js b/src/js/views/discover/DiscoverRecommendations.js index 90be6055..f85c330d 100755 --- a/src/js/views/discover/DiscoverRecommendations.js +++ b/src/js/views/discover/DiscoverRecommendations.js @@ -268,7 +268,7 @@ const mapStateToProps = (state, ownProps) => { artists: (state.core.artists ? state.core.artists : []), tracks: (state.core.tracks ? state.core.tracks : []), genres: (state.core.genres ? state.core.genres : []), - authorized: state.spotify.authorized, + authorized: state.spotify.authorization, load_queue: state.ui.load_queue, quick_search_results: (state.spotify.quick_search_results ? state.spotify.quick_search_results : {artists: [], tracks: []}), recommendations: (state.spotify.recommendations ? state.spotify.recommendations : {}),