Searching as provider-based processes
This commit is contained in:
@ -480,24 +480,30 @@ const MopidyMiddleware = (function(){
|
||||
**/
|
||||
|
||||
|
||||
case 'SEARCH_STARTED':
|
||||
store.dispatch({
|
||||
type: 'MOPIDY_CLEAR_SEARCH_RESULTS'
|
||||
});
|
||||
next(action)
|
||||
break
|
||||
|
||||
|
||||
case 'MOPIDY_GET_SEARCH_RESULTS':
|
||||
|
||||
// Flush out our previous results
|
||||
store.dispatch({type: 'MOPIDY_CLEAR_SEARCH_RESULTS'})
|
||||
|
||||
var uri_schemes_to_ignore = ['spotify:']
|
||||
var uri_schemes = Object.assign([], store.getState().ui.search_settings.uri_schemes)
|
||||
for (var i = 0; i < uri_schemes.length; i++){
|
||||
if (uri_schemes_to_ignore.includes(uri_schemes[i])){
|
||||
uri_schemes.splice(i,1)
|
||||
}
|
||||
}
|
||||
var uri_scheme = uri_schemes.shift()
|
||||
|
||||
store.dispatch(uiActions.startProcess(
|
||||
'MOPIDY_GET_SEARCH_RESULTS_PROCESSOR',
|
||||
'Searching Mopidy',
|
||||
'Searching '+uri_scheme.replace(':',''),
|
||||
{
|
||||
context: action.context,
|
||||
query: action.query,
|
||||
limit: action.limit,
|
||||
total: uri_schemes.length,
|
||||
uri_schemes: store.getState().ui.search_settings.uri_schemes
|
||||
total: (uri_schemes.length + 1),
|
||||
uri_scheme: uri_scheme,
|
||||
uri_schemes: uri_schemes
|
||||
}
|
||||
));
|
||||
break
|
||||
@ -505,95 +511,126 @@ const MopidyMiddleware = (function(){
|
||||
|
||||
case 'MOPIDY_GET_SEARCH_RESULTS_PROCESSOR':
|
||||
|
||||
var uri_schemes = Object.assign([], action.data.uri_schemes)
|
||||
var uri_scheme = uri_schemes.shift()
|
||||
// No more schemes, so we're done!
|
||||
if (!action.data.uri_scheme){
|
||||
store.dispatch(uiActions.processFinished('MOPIDY_GET_SEARCH_RESULTS_PROCESSOR'))
|
||||
return
|
||||
}
|
||||
|
||||
// Update UI for this round
|
||||
store.dispatch(uiActions.updateProcess(
|
||||
'MOPIDY_GET_SEARCH_RESULTS_PROCESSOR',
|
||||
'Searching '+action.data.uri_scheme.replace(':','')
|
||||
))
|
||||
|
||||
// Construct our next batch's task
|
||||
var next_uri_schemes = Object.assign([], action.data.uri_schemes)
|
||||
var next_uri_scheme = next_uri_schemes.shift()
|
||||
|
||||
switch (action.data.context){
|
||||
|
||||
// Albums
|
||||
case 'albums':
|
||||
instruct( socket, store, 'library.search', {query: {album: [action.data.query]}, uris: [uri_scheme]})
|
||||
case 'albums':
|
||||
|
||||
instruct( socket, store, 'library.search', {query: {album: [action.data.query]}, uris: [action.data.uri_scheme]})
|
||||
.then( response => {
|
||||
console.log(response)
|
||||
if (response.length <= 0) return
|
||||
|
||||
// collate all our different sources into one array
|
||||
var albums_uris = []
|
||||
if (response[0].tracks){
|
||||
for (var i = 0; i < response[0].tracks.length; i++){
|
||||
if (response[0].tracks[i].album){
|
||||
var album = response[0].tracks[i].album
|
||||
if (album.uri){
|
||||
albums_uris.push(album.uri)
|
||||
if (response.length > 0){
|
||||
|
||||
// collate all our different sources into one array
|
||||
var albums = []
|
||||
if (response[0].tracks){
|
||||
for (var i = 0; i < response[0].tracks.length; i++){
|
||||
if (response[0].tracks[i].album !== undefined && response[0].tracks[i].album.uri !== undefined){
|
||||
albums.push(response[0].tracks[i].album)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: limit uris at the loop, rather than post loop for performance
|
||||
var albums_uris = helpers.arrayOf('uri',albums)
|
||||
albums_uris = helpers.removeDuplicates(albums_uris)
|
||||
|
||||
store.dispatch({
|
||||
type: 'ALBUMS_LOADED',
|
||||
albums: albums
|
||||
})
|
||||
|
||||
/*
|
||||
// load each album
|
||||
// Do we really need to do this? The tracks.album seems like the full object...
|
||||
for (var i = 0; i < albums_uris.length; i++){
|
||||
store.dispatch(mopidyActions.getAlbum(albums_uris[i]))
|
||||
}
|
||||
*/
|
||||
|
||||
// and plug in their URIs
|
||||
store.dispatch({
|
||||
type: 'MOPIDY_SEARCH_RESULTS_LOADED',
|
||||
context: action.data.context,
|
||||
results: albums_uris
|
||||
})
|
||||
}
|
||||
|
||||
// TODO: limit uris at the loop, rather than post loop for performance
|
||||
albums_uris = helpers.removeDuplicates(albums_uris).splice(0, action.limit)
|
||||
|
||||
// load each album
|
||||
for (var i = 0; i < albums_uris.length; i++){
|
||||
store.dispatch(mopidyActions.getAlbum(albums_uris[i]))
|
||||
}
|
||||
|
||||
// and plug in their URIs
|
||||
store.dispatch({
|
||||
type: 'MOPIDY_SEARCH_RESULTS_LOADED',
|
||||
context: action.data.context,
|
||||
results: albums_uris
|
||||
})
|
||||
|
||||
store.dispatch(uiActions.processFinished(
|
||||
store.dispatch(uiActions.runProcess(
|
||||
'MOPIDY_GET_SEARCH_RESULTS_PROCESSOR',
|
||||
'Searching '+uri_scheme,
|
||||
{
|
||||
context: action.data.context,
|
||||
query: action.data.query,
|
||||
limit: action.data.limit,
|
||||
uri_schemes: uri_schemes
|
||||
})
|
||||
)
|
||||
uri_scheme: next_uri_scheme,
|
||||
uri_schemes: next_uri_schemes
|
||||
}
|
||||
))
|
||||
})
|
||||
break
|
||||
|
||||
// Artists
|
||||
case 'artists':
|
||||
instruct( socket, store, 'library.search', {query: {artist: [action.data.query]}, uris: [uri_scheme]})
|
||||
instruct( socket, store, 'library.search', {query: {artist: [action.data.query]}, uris: [action.data.uri_scheme]})
|
||||
.then( response => {
|
||||
if (response.length <= 0) return
|
||||
|
||||
var artists_uris = []
|
||||
if (response[0].tracks){
|
||||
for (var i = 0; i < response[0].tracks.length; i++){
|
||||
if (response[0].tracks[i].artists){
|
||||
for (var j = 0; j < response[0].tracks[i].artists.length; j++){
|
||||
var artist = response[0].tracks[i].artists[j]
|
||||
if (artist.uri){
|
||||
artists_uris.push(artist.uri)
|
||||
if (response.length > 0){
|
||||
var artists_uris = []
|
||||
if (response[0].tracks){
|
||||
for (var i = 0; i < response[0].tracks.length; i++){
|
||||
if (response[0].tracks[i].artists){
|
||||
for (var j = 0; j < response[0].tracks[i].artists.length; j++){
|
||||
var artist = response[0].tracks[i].artists[j]
|
||||
if (artist.uri){
|
||||
artists_uris.push(artist.uri)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: limit uris at the loop, rather than post loop for performance
|
||||
artists_uris = helpers.removeDuplicates(artists_uris)
|
||||
|
||||
// load each artist
|
||||
for (var i = 0; i < artists_uris.length; i++){
|
||||
store.dispatch(mopidyActions.getArtist(artists_uris[i]))
|
||||
}
|
||||
|
||||
// and plug in their URIs
|
||||
store.dispatch({
|
||||
type: 'MOPIDY_SEARCH_RESULTS_LOADED',
|
||||
context: action.data.context,
|
||||
results: artists_uris
|
||||
})
|
||||
}
|
||||
|
||||
// TODO: limit uris at the loop, rather than post loop for performance
|
||||
artists_uris = helpers.removeDuplicates(artists_uris).splice(0, action.limit)
|
||||
|
||||
// load each artist
|
||||
for (var i = 0; i < artists_uris.length; i++){
|
||||
store.dispatch(mopidyActions.getArtist(artists_uris[i]))
|
||||
}
|
||||
|
||||
// and plug in their URIs
|
||||
store.dispatch({
|
||||
type: 'MOPIDY_SEARCH_RESULTS_LOADED',
|
||||
context: action.data.context,
|
||||
results: artists_uris
|
||||
})
|
||||
|
||||
store.dispatch(uiActions.processFinished('MOPIDY_GET_SEARCH_RESULTS_PROCESSOR'))
|
||||
store.dispatch(uiActions.runProcess(
|
||||
'MOPIDY_GET_SEARCH_RESULTS_PROCESSOR',
|
||||
{
|
||||
context: action.data.context,
|
||||
query: action.data.query,
|
||||
limit: action.data.limit,
|
||||
uri_scheme: next_uri_scheme,
|
||||
uri_schemes: next_uri_schemes
|
||||
}
|
||||
))
|
||||
})
|
||||
break
|
||||
|
||||
@ -611,7 +648,7 @@ const MopidyMiddleware = (function(){
|
||||
}
|
||||
}
|
||||
|
||||
playlists_uris = playlists_uris.splice(0, action.limit)
|
||||
playlists_uris = playlists_uris
|
||||
|
||||
// load each playlist
|
||||
for (var i = 0; i < playlists_uris.length; i++){
|
||||
@ -625,26 +662,35 @@ const MopidyMiddleware = (function(){
|
||||
results: playlists_uris
|
||||
})
|
||||
|
||||
dispatch(uiActions.processFinished('MOPIDY_GET_SEARCH_RESULTS_PROCESSOR'))
|
||||
store.dispatch(uiActions.processFinished('MOPIDY_GET_SEARCH_RESULTS_PROCESSOR'))
|
||||
})
|
||||
break
|
||||
|
||||
// Tracks
|
||||
case 'tracks':
|
||||
instruct( socket, store, 'library.search', {query: {any: [action.data.query]}, uris: [uri_scheme]})
|
||||
instruct( socket, store, 'library.search', {query: {any: [action.data.query]}, uris: [action.data.uri_scheme]})
|
||||
.then( response => {
|
||||
if (response.length <= 0) return
|
||||
if (typeof(response[0].tracks) === 'undefined') return
|
||||
|
||||
var tracks = response[0].tracks.splice(0,action.limit)
|
||||
if (response.length > 0 && response[0].tracks !== undefined){
|
||||
var tracks = response[0].tracks
|
||||
|
||||
store.dispatch({
|
||||
type: 'MOPIDY_SEARCH_RESULTS_LOADED',
|
||||
context: action.data.context,
|
||||
results: tracks
|
||||
});
|
||||
store.dispatch({
|
||||
type: 'MOPIDY_SEARCH_RESULTS_LOADED',
|
||||
context: action.data.context,
|
||||
results: tracks
|
||||
});
|
||||
}
|
||||
|
||||
store.dispatch(uiActions.processFinished('MOPIDY_GET_SEARCH_RESULTS_PROCESSOR'))
|
||||
store.dispatch(uiActions.runProcess(
|
||||
'MOPIDY_GET_SEARCH_RESULTS_PROCESSOR',
|
||||
{
|
||||
context: action.data.context,
|
||||
query: action.data.query,
|
||||
limit: action.data.limit,
|
||||
uri_scheme: next_uri_scheme,
|
||||
uri_schemes: next_uri_schemes
|
||||
}
|
||||
))
|
||||
})
|
||||
break
|
||||
|
||||
|
||||
@ -147,6 +147,7 @@ export default function reducer(mopidy = {}, action){
|
||||
**/
|
||||
|
||||
case 'MOPIDY_CLEAR_SEARCH_RESULTS':
|
||||
console.log(action)
|
||||
return Object.assign({}, mopidy, { search_results: {} });
|
||||
|
||||
case 'MOPIDY_SEARCH_RESULTS_LOADED':
|
||||
@ -158,7 +159,7 @@ export default function reducer(mopidy = {}, action){
|
||||
var search_results = {}
|
||||
}
|
||||
|
||||
if (search_results.results){
|
||||
if (search_results[action.context]){
|
||||
search_results[action.context] = [...search_results[action.context], ...action.results]
|
||||
} else {
|
||||
search_results[action.context] = action.results
|
||||
|
||||
@ -448,6 +448,8 @@ export function getSearchResults(type = 'album,artist,playlist,track', query, li
|
||||
|
||||
dispatch(uiActions.startProcess('SPOTIFY_GET_SEARCH_RESULTS_PROCESSOR','Searching Spotify'))
|
||||
|
||||
type = type.replace(/s+$/, "")
|
||||
|
||||
var url = 'search?q='+query
|
||||
url += '&type='+type
|
||||
url += '&country='+getState().core.country
|
||||
@ -457,59 +459,65 @@ export function getSearchResults(type = 'album,artist,playlist,track', query, li
|
||||
sendRequest( dispatch, getState, url )
|
||||
.then( response => {
|
||||
|
||||
dispatch({
|
||||
type: 'ARTISTS_LOADED',
|
||||
artists: response.artists.items
|
||||
});
|
||||
|
||||
dispatch({
|
||||
type: 'ALBUMS_LOADED',
|
||||
albums: response.albums.items
|
||||
});
|
||||
|
||||
var playlists = []
|
||||
for (var i = 0; i < response.playlists.items.length; i++){
|
||||
playlists.push(Object.assign(
|
||||
{},
|
||||
response.playlists.items[i],
|
||||
{
|
||||
can_edit: (getState().spotify.me && response.playlists.items[i].owner.id == getState().spotify.me.id),
|
||||
tracks_total: response.playlists.items[i].tracks.total
|
||||
}
|
||||
))
|
||||
if (response.tracks !== undefined){
|
||||
dispatch({
|
||||
type: 'SPOTIFY_SEARCH_RESULTS_LOADED',
|
||||
context: 'tracks',
|
||||
results: response.tracks.items,
|
||||
more: response.tracks.next,
|
||||
});
|
||||
}
|
||||
|
||||
if (response.artists !== undefined){
|
||||
dispatch({
|
||||
type: 'ARTISTS_LOADED',
|
||||
artists: response.artists.items
|
||||
});
|
||||
dispatch({
|
||||
type: 'SPOTIFY_SEARCH_RESULTS_LOADED',
|
||||
context: 'artists',
|
||||
results: helpers.arrayOf('uri',response.artists.items),
|
||||
more: response.artists.next,
|
||||
});
|
||||
}
|
||||
|
||||
if (response.albums !== undefined){
|
||||
dispatch({
|
||||
type: 'ALBUMS_LOADED',
|
||||
albums: response.albums.items
|
||||
});
|
||||
dispatch({
|
||||
type: 'SPOTIFY_SEARCH_RESULTS_LOADED',
|
||||
context: 'albums',
|
||||
results: helpers.arrayOf('uri',response.albums.items),
|
||||
more: response.albums.next,
|
||||
});
|
||||
}
|
||||
dispatch({
|
||||
type: 'PLAYLISTS_LOADED',
|
||||
playlists: playlists
|
||||
});
|
||||
|
||||
dispatch({
|
||||
type: 'SPOTIFY_SEARCH_RESULTS_LOADED',
|
||||
context: 'playlists',
|
||||
results: helpers.arrayOf('uri',playlists),
|
||||
more: response.playlists.next
|
||||
});
|
||||
if (response.playlists !== undefined){
|
||||
var playlists = []
|
||||
for (var i = 0; i < response.playlists.items.length; i++){
|
||||
playlists.push(Object.assign(
|
||||
{},
|
||||
response.playlists.items[i],
|
||||
{
|
||||
can_edit: (getState().spotify.me && response.playlists.items[i].owner.id == getState().spotify.me.id),
|
||||
tracks_total: response.playlists.items[i].tracks.total
|
||||
}
|
||||
))
|
||||
}
|
||||
dispatch({
|
||||
type: 'PLAYLISTS_LOADED',
|
||||
playlists: playlists
|
||||
});
|
||||
|
||||
dispatch({
|
||||
type: 'SPOTIFY_SEARCH_RESULTS_LOADED',
|
||||
context: 'artists',
|
||||
results: helpers.arrayOf('uri',response.artists.items),
|
||||
more: response.artists.next,
|
||||
});
|
||||
|
||||
dispatch({
|
||||
type: 'SPOTIFY_SEARCH_RESULTS_LOADED',
|
||||
context: 'albums',
|
||||
results: helpers.arrayOf('uri',response.albums.items),
|
||||
more: response.albums.next,
|
||||
});
|
||||
|
||||
dispatch({
|
||||
type: 'SPOTIFY_SEARCH_RESULTS_LOADED',
|
||||
context: 'tracks',
|
||||
results: response.tracks.items,
|
||||
more: response.tracks.next,
|
||||
});
|
||||
dispatch({
|
||||
type: 'SPOTIFY_SEARCH_RESULTS_LOADED',
|
||||
context: 'playlists',
|
||||
results: helpers.arrayOf('uri',playlists),
|
||||
more: response.playlists.next
|
||||
});
|
||||
}
|
||||
|
||||
dispatch(uiActions.processFinished('SPOTIFY_GET_SEARCH_RESULTS_PROCESSOR'))
|
||||
});
|
||||
|
||||
@ -36,7 +36,7 @@ class Search extends React.Component{
|
||||
this.props.mopidyActions.getSearchResults(this.props.view, this.props.params.query)
|
||||
}
|
||||
|
||||
if (this.props.mopidy_connected && this.props.search_settings.uri_schemes && this.props.search_settings.uri_schemes.includes('spotify')){
|
||||
if (this.props.mopidy_connected && this.props.search_settings.uri_schemes && this.props.search_settings.uri_schemes.includes('spotify:')){
|
||||
this.props.spotifyActions.getSearchResults(this.props.view, this.props.params.query)
|
||||
}
|
||||
}
|
||||
@ -46,9 +46,21 @@ class Search extends React.Component{
|
||||
this.props.mopidyActions.getSearchResults(newProps.view, newProps.params.query)
|
||||
}
|
||||
|
||||
if (!this.props.spotify_connected && newProps.spotify_connected && newProps.search_settings.uri_schemes.includes('spotify')){
|
||||
if (!this.props.spotify_connected && newProps.spotify_connected && newProps.search_settings.uri_schemes.includes('spotify:')){
|
||||
this.props.spotifyActions.getSearchResults(newProps.view, newProps.params.query)
|
||||
}
|
||||
|
||||
// Search changed
|
||||
if (this.props.params.query !== newProps.params.query || this.props.view !== newProps.view){
|
||||
|
||||
if (this.props.mopidy_connected && this.props.search_settings.uri_schemes){
|
||||
this.props.mopidyActions.getSearchResults(newProps.view, newProps.params.query)
|
||||
}
|
||||
|
||||
if (this.props.mopidy_connected && this.props.search_settings.uri_schemes && this.props.search_settings.uri_schemes.includes('spotify:')){
|
||||
this.props.spotifyActions.getSearchResults(newProps.view, newProps.params.query)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
loadMore(type){
|
||||
|
||||
Reference in New Issue
Block a user