Polishing spotify actions, removing middleware; Mopidy current track selection

This commit is contained in:
James Barnsley
2016-10-21 09:56:07 +13:00
parent daab2133df
commit afb6bb7a0d
19 changed files with 444 additions and 102 deletions

View File

@ -74,6 +74,49 @@ export function removeAuthorization(){
return { type: 'SPOTIFY_REMOVE_AUTHORIZATION' }
}
export function refreshToken(){
return (dispatch, getState) => {
dispatch({ type: 'SPOTIFY_TOKEN_REFRESHING' });
$.ajax({
method: 'GET',
url: '//jamesbarnsley.co.nz/spotmop.php?action=refresh&refresh_token='+getState().spotify.refresh_token,
dataType: "json",
timeout: 10000
})
.then( response => {
response.expires = new Date().getTime() + 3600000;
dispatch({
type: 'SPOTIFY_TOKEN_REFRESHED',
data: response
});
});
}
}
/**
* Get current user
**/
export function getMe(){
return (dispatch, getState) => {
// flush out the previous store value
dispatch({ type: 'SPOTIFY_ME_LOADED', data: false });
sendRequest( getState().spotify.access_token, 'me' )
.then( response => {
dispatch({
type: 'SPOTIFY_ME_LOADED',
data: response
});
});
}
}
/**
* Get a single artist
@ -86,7 +129,6 @@ export function getArtist( uri ){
// flush out the previous store value
dispatch({ type: 'SPOTIFY_ARTIST_LOADED', data: false });
var id = uri.replace('spotify:artist:','');
var artist = {};
// while we're fiddling about, go get the albums
@ -95,12 +137,12 @@ export function getArtist( uri ){
// get both the artist and the top tracks
$.when(
sendRequest( false, 'artists/'+id )
sendRequest( false, 'artists/'+ getFromUri('artistid', uri) )
.then( response => {
Object.assign(artist, response);
}),
sendRequest( false, 'artists/'+id+'/top-tracks?country='+getState().spotify.country )
sendRequest( false, 'artists/'+ getFromUri('artistid', uri) +'/top-tracks?country='+getState().spotify.country )
.then( response => {
Object.assign(artist, response);
})
@ -116,7 +158,7 @@ export function getArtist( uri ){
export function getArtistAlbums( uri ){
return dispatch => {
sendRequest( false, 'artists/'+ uri.replace('spotify:artist:','') +'/albums' )
sendRequest( false, 'artists/'+ getFromUri('artistid', uri) +'/albums' )
.then( response => {
dispatch({
type: 'SPOTIFY_ARTIST_ALBUMS_LOADED',
@ -132,15 +174,15 @@ export function getArtistAlbums( uri ){
* @oaram uri string
**/
export function getAlbum( uri ){
return dispatch => {
return dispatch => {
// flush out the previous store value
// flush out the previous store value
dispatch({ type: 'SPOTIFY_ALBUM_LOADED', data: false });
sendRequest( false, 'albums/'+ uri.replace('spotify:album:','') )
sendRequest( false, 'albums/'+ getFromUri('albumid', uri) )
.then( response => {
// inject the parent album object into each track for consistent track objects
// inject the parent album object into each track for consistent track objects
for( var i = 0; i < response.tracks.items.length; i++ ){
response.tracks.items[i].album = {
name: response.name,
@ -149,7 +191,28 @@ export function getAlbum( uri ){
}
dispatch({
type: 'SPOTIFY_ALBUM_LOADED',
type: 'SPOTIFY_ALBUM_LOADED',
data: response
});
});
}
}
/**
* Single playlist
*
* @oaram uri string
**/
export function getPlaylist( uri ){
return (dispatch, getState) => {
// flush out the previous store value
dispatch({ type: 'SPOTIFY_PLAYLIST_LOADED', data: false });
sendRequest( getState().spotify.access_token, 'users/'+ getFromUri('userid',uri) +'/playlists/'+ getFromUri('playlistid',uri) +'?market='+getState().spotify.country )
.then( response => {
dispatch({
type: 'SPOTIFY_PLAYLIST_LOADED',
data: response
});
});
@ -157,6 +220,22 @@ export function getAlbum( uri ){
}
export function getLibraryPlaylists(){
return (dispatch, getState) => {
dispatch({ type: 'SPOTIFY_LIBRARY_PLAYLISTS_LOADED', data: false });
sendRequest( getState().spotify.access_token, 'me/playlists' )
.then( response => {
dispatch({
type: 'SPOTIFY_LIBRARY_PLAYLISTS_LOADED',
data: response
});
});
}
}
export function getLibraryArtists(){
return (dispatch, getState) => {
@ -193,7 +272,7 @@ export function getLibraryTracks(){
dispatch({ type: 'SPOTIFY_LIBRARY_TRACKS_LOADED', data: false });
sendRequest( getState().spotify.access_token, 'me/tracks' )
sendRequest( getState().spotify.access_token, 'me/tracks?limit=50' )
.then( response => {
dispatch({
type: 'SPOTIFY_LIBRARY_TRACKS_LOADED',
@ -202,3 +281,47 @@ export function getLibraryTracks(){
});
}
}
/**
* Get an element from a URI
* @param element = string, the element we wish to extract
* @param uri = string
**/
function getFromUri( element, uri ){
var exploded = uri.split(':');
if( element == 'userid' && exploded[1] == 'user' )
return exploded[2];
if( element == 'playlistid' && exploded[3] == 'playlist' )
return exploded[4];
if( element == 'artistid' && exploded[1] == 'artist' )
return exploded[2];
if( element == 'albumid' && exploded[1] == 'album' )
return exploded[2];
if( element == 'trackid' && exploded[1] == 'track' )
return exploded[2];
return null;
}
/**
* Identify what kind of asset a URI is (playlist, album, etc)
* @param uri = string
* @return string
**/
function uriType( uri ){
var exploded = uri.split(':');
if( exploded[0] == 'spotify' && exploded[1] == 'track' )
return 'track';
if( exploded[0] == 'spotify' && exploded[1] == 'artist' )
return 'artist';
if( exploded[0] == 'spotify' && exploded[1] == 'album' )
return 'album';
if( exploded[0] == 'spotify' && exploded[1] == 'user' && exploded[3] == 'playlist' )
return 'playlist';
if( exploded[0] == 'spotify' && exploded[1] == 'user' && exploded.length == 3 )
return 'user';
return null;
}

View File

@ -3,54 +3,30 @@ import actions from './actions'
const SpotifyMiddleware = (function(){
/**
* Send an ajax request to the Spotify API
*
* @param endpoint string = the url to query (ie /albums/:uri)
* @param method string
* @param data mixed = request payload
* @return ajax promise
**/
const sendRequest = (token, endpoint, method = 'GET', data = false) => {
var options = {
method: method,
cache: true,
url: 'https://api.spotify.com/v1/'+endpoint,
data: data
};
if( token ){
options.headers = {
Authorization: 'Bearer '+ token
}
}
return $.ajax( options );
}
/**
* The actual middleware inteceptor
**/
return store => next => action => {
return next(action);
/*
var state = store.getState();
switch(action.type){
case 'SPOTIFY_AUTHORIZATION_GRANTED':
sendRequest( action.data.access_token, 'me' )
.then( (response) => {
var data = action.data;
data.me = response;
store.dispatch({ type: 'SPOTIFY_AUTHORIZATION_COMPLETE', data: data })
});
case 'SPOTIFY_AUTHORIZATION_GRANTEDXXX':
// proceed as usual
next(action)
// now we've been granted,
store.dispatch({ type: 'SPOTIFY_AUTHORIZATION_COMPLETE', data: data })
break;
// This action is irrelevant to us, pass it on to the next middleware
default:
return next(action);
}
*/
}
})();

View File

@ -5,16 +5,12 @@ export default function reducer(spotify = {}, action){
case 'SPOTIFY_CONNECTING':
return Object.assign({}, spotify, { connected: false, connecting: true });
case 'SPOTIFY_START_AUTHORIZATION':
return Object.assign({}, spotify, { authorizing: true });
case 'SPOTIFY_AUTHORIZATION_COMPLETE':
case 'SPOTIFY_AUTHORIZATION_GRANTED':
return Object.assign({}, spotify, {
authorizing: false,
authorized: true,
authorization: action.data,
me: action.data.me,
token: action.data.access_token
access_token: action.data.access_token
});
case 'SPOTIFY_REMOVE_AUTHORIZATION':
@ -22,16 +18,32 @@ export default function reducer(spotify = {}, action){
authorizing: false,
authorization: false,
authorized: false,
token: false,
access_token: false,
me: false
});
case 'SPOTIFY_CONNECTED':
return Object.assign({}, spotify, { connected: true, connecting: false });
case 'SPOTIFY_TOKEN_REFRESHING':
return Object.assign({}, spotify, { refreshing_token: true });
case 'SPOTIFY_TOKEN_REFRESHED':
return Object.assign({}, spotify, {
refreshing_token: false,
authorization: action.data,
access_token: action.data.access_token
});
case 'SPOTIFY_DISCONNECTED':
return Object.assign({}, spotify, { connected: false, connecting: false });
case 'SPOTIFY_ME_LOADED':
return Object.assign({}, spotify, { me: action.data });
case 'SPOTIFY_PLAYLIST_LOADED':
return Object.assign({}, spotify, { playlist: action.data });
case 'SPOTIFY_ALBUM_LOADED':
return Object.assign({}, spotify, { album: action.data });
@ -41,12 +53,18 @@ export default function reducer(spotify = {}, action){
case 'SPOTIFY_ARTIST_ALBUMS_LOADED':
return Object.assign({}, spotify, { artist_albums: action.data });
case 'SPOTIFY_LIBRARY_PLAYLISTS_LOADED':
return Object.assign({}, spotify, { libraryPlaylists: action.data });
case 'SPOTIFY_LIBRARY_ARTISTS_LOADED':
return Object.assign({}, spotify, { libraryArtists: action.data });
case 'SPOTIFY_LIBRARY_ALBUMS_LOADED':
return Object.assign({}, spotify, { libraryAlbums: action.data });
case 'SPOTIFY_LIBRARY_TRACKS_LOADED':
return Object.assign({}, spotify, { libraryTracks: action.data });
default:
return spotify
}