2016-10-27 16:58:11 +13:00
|
|
|
|
2017-08-02 10:53:30 +12:00
|
|
|
import ReactGA from 'react-ga'
|
|
|
|
|
|
2016-10-27 21:20:05 +13:00
|
|
|
var helpers = require('../../helpers.js')
|
2017-08-05 22:25:25 +12:00
|
|
|
var coreActions = require('../core/actions.js')
|
2017-02-06 17:02:43 +13:00
|
|
|
var uiActions = require('../ui/actions.js')
|
|
|
|
|
var pusherActions = require('./actions.js')
|
2017-11-02 21:20:56 +13:00
|
|
|
var lastfmActions = require('../lastfm/actions.js')
|
2017-02-06 17:02:43 +13:00
|
|
|
var spotifyActions = require('../spotify/actions.js')
|
2016-10-27 16:58:11 +13:00
|
|
|
|
|
|
|
|
const PusherMiddleware = (function(){
|
|
|
|
|
|
2017-03-27 14:23:38 +13:00
|
|
|
// container for the actual websocket
|
|
|
|
|
var socket = null
|
2017-01-09 09:29:56 +13:00
|
|
|
|
2017-03-27 14:23:38 +13:00
|
|
|
// requests pending
|
|
|
|
|
var deferredRequests = []
|
2016-10-27 16:58:11 +13:00
|
|
|
|
|
|
|
|
// handle all manner of socket messages
|
|
|
|
|
const handleMessage = (ws, store, message) => {
|
2017-02-19 21:27:43 +13:00
|
|
|
|
2017-10-26 19:26:38 +13:00
|
|
|
if (store.getState().ui.log_pusher){
|
2018-03-13 08:57:40 +13:00
|
|
|
console.log('Pusher log', message);
|
2017-09-19 16:57:59 +12:00
|
|
|
}
|
|
|
|
|
|
2017-10-26 19:26:38 +13:00
|
|
|
// Response with request_id
|
2018-03-13 08:57:40 +13:00
|
|
|
if (message.id !== undefined && message.id){
|
2017-09-19 16:57:59 +12:00
|
|
|
|
2017-10-26 19:26:38 +13:00
|
|
|
// Response matches a pending request
|
2018-03-13 08:57:40 +13:00
|
|
|
if (deferredRequests[message.id] !== undefined){
|
2017-10-26 19:26:38 +13:00
|
|
|
|
2018-03-13 08:57:40 +13:00
|
|
|
store.dispatch(uiActions.stopLoading(message.id));
|
2017-10-26 19:26:38 +13:00
|
|
|
|
|
|
|
|
// Response is an error
|
2018-03-13 08:57:40 +13:00
|
|
|
if (message.error !== undefined){
|
|
|
|
|
deferredRequests[message.id].reject(message.error);
|
2017-10-26 19:26:38 +13:00
|
|
|
|
|
|
|
|
// Successful response
|
|
|
|
|
} else {
|
2018-03-13 08:57:40 +13:00
|
|
|
deferredRequests[message.id].resolve(message.result);
|
2017-10-26 19:26:38 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Hmm, the response doesn't appear to be for us?
|
2017-02-18 22:04:05 +13:00
|
|
|
} else {
|
2017-09-19 16:57:59 +12:00
|
|
|
store.dispatch(coreActions.handleException(
|
2017-09-25 20:22:48 +13:00
|
|
|
'Pusher: Response received with no matching request',
|
2017-09-19 16:57:59 +12:00
|
|
|
message
|
|
|
|
|
));
|
2017-02-18 22:04:05 +13:00
|
|
|
}
|
2016-10-27 16:58:11 +13:00
|
|
|
|
2017-10-26 19:26:38 +13:00
|
|
|
// General broadcast received
|
2017-02-18 22:04:05 +13:00
|
|
|
} else {
|
2017-10-26 19:26:38 +13:00
|
|
|
|
|
|
|
|
// Broadcast of an error
|
2018-03-13 08:57:40 +13:00
|
|
|
if (message.error !== undefined){
|
2017-10-26 19:26:38 +13:00
|
|
|
store.dispatch(coreActions.handleException(
|
2018-03-13 08:57:40 +13:00
|
|
|
'Pusher: '+message.error.message,
|
2017-10-26 19:26:38 +13:00
|
|
|
message
|
|
|
|
|
));
|
|
|
|
|
} else {
|
2018-03-20 16:53:41 +13:00
|
|
|
|
|
|
|
|
console.log(message.method);
|
|
|
|
|
|
|
|
|
|
// Split our method into our action creator reference (eg pusherActions.createNotification(params))
|
|
|
|
|
var method = message.method.split('.');
|
|
|
|
|
var actions_library = method[0]+'Actions';
|
|
|
|
|
|
|
|
|
|
switch (method[0]){
|
|
|
|
|
case 'core':
|
|
|
|
|
var action = coreActions[method[1]];
|
|
|
|
|
break;
|
|
|
|
|
case 'ui':
|
|
|
|
|
var action = uiActions[method[1]];
|
|
|
|
|
break;
|
|
|
|
|
case 'pusher':
|
|
|
|
|
var action = pusherActions[method[1]];
|
|
|
|
|
break;
|
|
|
|
|
case 'lastfm':
|
|
|
|
|
var action = lastfmActions[method[1]];
|
|
|
|
|
break;
|
|
|
|
|
case 'mopidy':
|
|
|
|
|
var action = mopidyActions[method[1]];
|
|
|
|
|
break;
|
|
|
|
|
case 'spotify':
|
|
|
|
|
var action = spotifyActions[method[1]];
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!action){
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console.log(action);
|
|
|
|
|
|
|
|
|
|
if (message.params){
|
|
|
|
|
store.dispatch(action(message.params));
|
|
|
|
|
} else {
|
|
|
|
|
store.dispatch(action());
|
|
|
|
|
}
|
2017-10-26 19:26:38 +13:00
|
|
|
}
|
2017-01-09 09:29:56 +13:00
|
|
|
}
|
2016-10-27 21:20:05 +13:00
|
|
|
}
|
|
|
|
|
|
2018-03-13 08:57:40 +13:00
|
|
|
const request = (store, method, params = null) => {
|
2017-10-10 13:05:28 +13:00
|
|
|
return new Promise((resolve, reject) => {
|
2017-10-01 09:38:01 +13:00
|
|
|
|
2018-03-12 21:39:27 +13:00
|
|
|
var id = helpers.generateGuid();
|
2017-02-18 22:04:05 +13:00
|
|
|
var message = {
|
2018-03-13 08:57:40 +13:00
|
|
|
jsonrpc: '2.0',
|
2018-03-12 21:39:27 +13:00
|
|
|
id: id,
|
2018-03-13 08:57:40 +13:00
|
|
|
method: method
|
|
|
|
|
}
|
|
|
|
|
if (params){
|
|
|
|
|
message.params = params;
|
2017-02-18 22:04:05 +13:00
|
|
|
}
|
2017-10-26 19:26:38 +13:00
|
|
|
socket.send(JSON.stringify(message));
|
2017-03-27 14:23:38 +13:00
|
|
|
|
2018-03-12 21:39:27 +13:00
|
|
|
store.dispatch(uiActions.startLoading(id, 'pusher_'+method));
|
2017-10-01 09:38:01 +13:00
|
|
|
|
2017-10-06 09:07:28 +13:00
|
|
|
// Start our 15 second timeout
|
2017-10-01 09:38:01 +13:00
|
|
|
var timeout = setTimeout(
|
|
|
|
|
function(){
|
2018-03-12 21:39:27 +13:00
|
|
|
store.dispatch(uiActions.stopLoading(id));
|
|
|
|
|
reject({
|
|
|
|
|
id: id,
|
2018-03-13 08:57:40 +13:00
|
|
|
code: 32300,
|
|
|
|
|
message: "Request timed out"
|
2018-03-12 21:39:27 +13:00
|
|
|
});
|
2017-10-01 09:38:01 +13:00
|
|
|
},
|
2017-11-21 11:27:34 +13:00
|
|
|
30000
|
2017-10-01 09:38:01 +13:00
|
|
|
);
|
2017-01-09 09:29:56 +13:00
|
|
|
|
|
|
|
|
// add query to our deferred responses
|
2018-03-12 21:39:27 +13:00
|
|
|
deferredRequests[id] = {
|
2017-01-09 09:29:56 +13:00
|
|
|
resolve: resolve,
|
|
|
|
|
reject: reject
|
2017-10-26 19:26:38 +13:00
|
|
|
};
|
2017-01-09 09:29:56 +13:00
|
|
|
})
|
2016-11-29 15:36:01 +13:00
|
|
|
}
|
|
|
|
|
|
2016-10-27 16:58:11 +13:00
|
|
|
return store => next => action => {
|
2017-10-10 13:05:28 +13:00
|
|
|
switch(action.type){
|
2016-10-27 16:58:11 +13:00
|
|
|
|
|
|
|
|
case 'PUSHER_CONNECT':
|
|
|
|
|
|
2017-11-19 20:38:36 +13:00
|
|
|
// Stagnant socket, close it first
|
|
|
|
|
if (socket != null){
|
|
|
|
|
socket.close();
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-13 08:57:40 +13:00
|
|
|
store.dispatch({type: 'PUSHER_CONNECTING'});
|
2016-10-27 16:58:11 +13:00
|
|
|
|
|
|
|
|
var state = store.getState();
|
2016-10-28 09:14:29 +13:00
|
|
|
var connection = {
|
2017-11-19 20:38:36 +13:00
|
|
|
client_id: helpers.generateGuid(),
|
2017-02-18 22:04:05 +13:00
|
|
|
connection_id: helpers.generateGuid(),
|
2017-02-17 21:36:31 +13:00
|
|
|
username: 'Anonymous'
|
2016-10-28 09:14:29 +13:00
|
|
|
}
|
2017-11-19 20:38:36 +13:00
|
|
|
if (state.pusher.username){
|
|
|
|
|
connection.username = state.pusher.username;
|
|
|
|
|
}
|
|
|
|
|
connection.username = connection.username.replace(/\W/g, '');
|
2017-02-16 19:51:49 +13:00
|
|
|
|
2016-10-27 16:58:11 +13:00
|
|
|
socket = new WebSocket(
|
2017-03-28 18:23:58 +13:00
|
|
|
'ws'+(window.location.protocol === 'https:' ? 's' : '')+'://'+state.mopidy.host+':'+state.mopidy.port+'/iris/ws/',
|
2017-11-19 20:38:36 +13:00
|
|
|
[ connection.client_id, connection.connection_id, connection.username ]
|
2016-10-27 16:58:11 +13:00
|
|
|
);
|
|
|
|
|
|
2018-03-13 08:57:40 +13:00
|
|
|
socket.onopen = () => {
|
|
|
|
|
store.dispatch({
|
2018-03-20 13:02:38 +13:00
|
|
|
type: 'PUSHER_CONNECTED',
|
|
|
|
|
connection_id: connection.connection_id,
|
|
|
|
|
client_id: connection.client_id,
|
|
|
|
|
username: connection.username
|
2018-03-13 08:57:40 +13:00
|
|
|
});
|
2016-10-27 16:58:11 +13:00
|
|
|
};
|
|
|
|
|
|
2017-02-21 20:04:45 +13:00
|
|
|
socket.onclose = () => {
|
|
|
|
|
store.dispatch({
|
|
|
|
|
type: 'PUSHER_DISCONNECTED'
|
|
|
|
|
})
|
|
|
|
|
|
2017-10-26 19:26:38 +13:00
|
|
|
// attempt to reconnect every 5 seconds
|
2017-02-21 20:04:45 +13:00
|
|
|
setTimeout(() => {
|
|
|
|
|
store.dispatch(pusherActions.connect())
|
|
|
|
|
}, 5000);
|
|
|
|
|
};
|
|
|
|
|
|
2018-03-13 08:57:40 +13:00
|
|
|
socket.onmessage = (message) => {
|
|
|
|
|
var message = JSON.parse(message.data);
|
|
|
|
|
handleMessage(socket, store, message);
|
|
|
|
|
};
|
|
|
|
|
|
2016-10-27 16:58:11 +13:00
|
|
|
break;
|
|
|
|
|
|
2016-10-27 21:20:05 +13:00
|
|
|
case 'PUSHER_CONNECTED':
|
2018-03-12 21:39:27 +13:00
|
|
|
ReactGA.event({ category: 'Pusher', action: 'Connected', label: action.username});
|
2017-02-14 21:41:13 +13:00
|
|
|
|
2018-03-12 21:39:27 +13:00
|
|
|
store.dispatch(pusherActions.getConfig());
|
|
|
|
|
store.dispatch(pusherActions.getVersion());
|
|
|
|
|
store.dispatch(pusherActions.getRadio());
|
|
|
|
|
store.dispatch(pusherActions.getSnapcast());
|
|
|
|
|
store.dispatch(pusherActions.getQueueMetadata());
|
|
|
|
|
|
2016-10-29 22:40:02 +13:00
|
|
|
return next(action);
|
|
|
|
|
break;
|
|
|
|
|
|
2017-02-21 08:58:49 +13:00
|
|
|
case 'PUSHER_INSTRUCT':
|
2017-10-26 19:26:38 +13:00
|
|
|
request(action)
|
2017-02-21 08:58:49 +13:00
|
|
|
.then(
|
|
|
|
|
response => {
|
|
|
|
|
store.dispatch({ type: 'PUSHER_INSTRUCT', data: response.data })
|
2017-10-01 09:38:01 +13:00
|
|
|
},
|
|
|
|
|
error => {
|
|
|
|
|
store.dispatch(coreActions.handleException(
|
|
|
|
|
'Instruct failed',
|
|
|
|
|
error
|
|
|
|
|
));
|
2017-02-21 08:58:49 +13:00
|
|
|
}
|
2017-10-26 19:26:38 +13:00
|
|
|
);
|
2017-02-21 08:58:49 +13:00
|
|
|
break
|
|
|
|
|
|
|
|
|
|
case 'PUSHER_DELIVER_MESSAGE':
|
2017-03-27 14:23:38 +13:00
|
|
|
request(store, 'deliver_message', action.data)
|
2017-02-21 08:58:49 +13:00
|
|
|
.then(
|
|
|
|
|
response => {
|
2018-02-23 09:35:38 +13:00
|
|
|
store.dispatch(uiActions.createNotification({content: 'Message delivered'}));
|
2017-10-01 09:38:01 +13:00
|
|
|
},
|
|
|
|
|
error => {
|
|
|
|
|
store.dispatch(coreActions.handleException(
|
|
|
|
|
'Could not deliver message',
|
|
|
|
|
error
|
|
|
|
|
));
|
2017-02-21 08:58:49 +13:00
|
|
|
}
|
2017-10-26 19:26:38 +13:00
|
|
|
);
|
2017-02-21 08:58:49 +13:00
|
|
|
break
|
|
|
|
|
|
2017-03-09 08:04:23 +13:00
|
|
|
case 'PUSHER_DELIVER_BROADCAST':
|
2017-03-27 14:23:38 +13:00
|
|
|
request(store, 'broadcast', action.data)
|
2017-03-09 08:04:23 +13:00
|
|
|
break
|
|
|
|
|
|
2017-02-14 12:13:07 +13:00
|
|
|
case 'PUSHER_GET_QUEUE_METADATA':
|
2017-03-27 14:23:38 +13:00
|
|
|
request(store, 'get_queue_metadata')
|
2017-02-14 12:13:07 +13:00
|
|
|
.then(
|
|
|
|
|
response => {
|
2017-02-22 08:54:47 +13:00
|
|
|
response.type = 'PUSHER_QUEUE_METADATA'
|
2017-02-18 22:04:05 +13:00
|
|
|
store.dispatch(response)
|
2017-10-01 09:38:01 +13:00
|
|
|
},
|
|
|
|
|
error => {
|
|
|
|
|
store.dispatch(coreActions.handleException(
|
|
|
|
|
'Could not load queue metadata',
|
|
|
|
|
error
|
|
|
|
|
));
|
2017-02-14 12:13:07 +13:00
|
|
|
}
|
2017-10-26 19:26:38 +13:00
|
|
|
);
|
2017-02-14 12:13:07 +13:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 'PUSHER_ADD_QUEUE_METADATA':
|
2017-03-27 14:23:38 +13:00
|
|
|
request(store, 'add_queue_metadata', {
|
2017-02-18 22:04:05 +13:00
|
|
|
tlids: action.tlids,
|
|
|
|
|
added_from: action.from_uri,
|
|
|
|
|
added_by: store.getState().pusher.username
|
|
|
|
|
})
|
2017-02-14 12:13:07 +13:00
|
|
|
break;
|
|
|
|
|
|
2017-02-19 21:27:43 +13:00
|
|
|
case 'PUSHER_START_UPGRADE':
|
2017-08-02 10:53:30 +12:00
|
|
|
ReactGA.event({ category: 'Pusher', action: 'Upgrade', label: '' })
|
2017-03-27 14:23:38 +13:00
|
|
|
request(store, 'upgrade')
|
2017-10-01 09:38:01 +13:00
|
|
|
.then(
|
|
|
|
|
response => {
|
|
|
|
|
if (response.upgrade_successful){
|
2018-02-23 09:35:38 +13:00
|
|
|
store.dispatch(uiActions.createNotification({content: 'Upgrade complete'}));
|
2017-10-10 13:32:04 +13:00
|
|
|
} else {
|
2018-02-23 09:35:38 +13:00
|
|
|
store.dispatch(uiActions.createNotification({content: 'Upgrade failed, please upgrade manually', type: 'bad'}));
|
2017-10-01 09:38:01 +13:00
|
|
|
}
|
2017-02-18 22:04:05 +13:00
|
|
|
|
2017-10-01 09:38:01 +13:00
|
|
|
response.type = 'PUSHER_VERSION'
|
|
|
|
|
store.dispatch(response)
|
|
|
|
|
},
|
|
|
|
|
error => {
|
|
|
|
|
store.dispatch(coreActions.handleException(
|
|
|
|
|
'Could not start upgrade',
|
|
|
|
|
error
|
|
|
|
|
));
|
|
|
|
|
}
|
2017-10-26 19:26:38 +13:00
|
|
|
);
|
2016-10-28 09:14:29 +13:00
|
|
|
return next(action);
|
|
|
|
|
break;
|
|
|
|
|
|
2017-01-09 09:29:56 +13:00
|
|
|
case 'PUSHER_SET_USERNAME':
|
2017-03-27 14:23:38 +13:00
|
|
|
request(store, 'set_username', {
|
2017-02-18 22:04:05 +13:00
|
|
|
username: action.username
|
|
|
|
|
})
|
2017-10-26 19:26:38 +13:00
|
|
|
.then(
|
|
|
|
|
response => {
|
|
|
|
|
response.type = 'PUSHER_USERNAME_CHANGED'
|
|
|
|
|
store.dispatch(response)
|
|
|
|
|
},
|
|
|
|
|
error => {
|
|
|
|
|
store.dispatch(coreActions.handleException(
|
|
|
|
|
'Could not set username',
|
|
|
|
|
error
|
|
|
|
|
));
|
2017-01-09 09:29:56 +13:00
|
|
|
}
|
2017-10-26 19:26:38 +13:00
|
|
|
);
|
2016-10-28 10:48:55 +13:00
|
|
|
return next(action);
|
|
|
|
|
break;
|
|
|
|
|
|
2018-03-12 21:39:27 +13:00
|
|
|
case 'PUSHER_GET_VERSION':
|
|
|
|
|
request(store, 'get_version')
|
|
|
|
|
.then(
|
|
|
|
|
response => {
|
|
|
|
|
store.dispatch({
|
|
|
|
|
type: 'PUSHER_VERSION',
|
2018-03-13 08:57:40 +13:00
|
|
|
version: response.version
|
2018-03-12 21:39:27 +13:00
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
error => {
|
|
|
|
|
store.dispatch(coreActions.handleException(
|
|
|
|
|
'Could not load version',
|
|
|
|
|
error
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 'PUSHER_GET_CONFIG':
|
|
|
|
|
request(store, 'get_config')
|
|
|
|
|
.then(
|
|
|
|
|
response => {
|
|
|
|
|
store.dispatch({
|
|
|
|
|
type: 'PUSHER_CONFIG',
|
2018-03-13 08:57:40 +13:00
|
|
|
config: response.config
|
2018-03-12 21:39:27 +13:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var core = store.getState().core;
|
|
|
|
|
if (!core.country || !core.locale){
|
|
|
|
|
store.dispatch(coreActions.set({
|
2018-03-13 08:57:40 +13:00
|
|
|
country: response.config.country,
|
|
|
|
|
locale: response.config.locale
|
2018-03-12 21:39:27 +13:00
|
|
|
}))
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
error => {
|
|
|
|
|
store.dispatch(coreActions.handleException(
|
|
|
|
|
'Could not load config',
|
|
|
|
|
error
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
break;
|
|
|
|
|
|
2017-02-19 21:27:43 +13:00
|
|
|
case 'PUSHER_GET_CONNECTIONS':
|
2017-03-27 14:23:38 +13:00
|
|
|
request(store, 'get_connections')
|
2017-10-26 19:26:38 +13:00
|
|
|
.then(
|
2018-03-12 21:39:27 +13:00
|
|
|
response => {
|
|
|
|
|
store.dispatch({
|
|
|
|
|
type: 'PUSHER_CONNECTIONS',
|
2018-03-13 08:57:40 +13:00
|
|
|
connections: response.connections
|
2018-03-12 21:39:27 +13:00
|
|
|
})
|
2017-10-26 19:26:38 +13:00
|
|
|
},
|
|
|
|
|
error => {
|
|
|
|
|
store.dispatch(coreActions.handleException(
|
|
|
|
|
'Could not load connections',
|
|
|
|
|
error
|
|
|
|
|
));
|
2017-01-09 09:29:56 +13:00
|
|
|
}
|
2017-10-26 19:26:38 +13:00
|
|
|
);
|
2017-01-09 09:29:56 +13:00
|
|
|
return next(action);
|
|
|
|
|
break
|
2016-12-05 10:20:01 +13:00
|
|
|
|
2017-02-21 19:54:00 +13:00
|
|
|
case 'PUSHER_SPOTIFY_AUTHORIZATION':
|
2017-04-15 05:43:29 +12:00
|
|
|
store.dispatch(uiActions.openModal('receive_authorization', {authorization: action.authorization, user: action.me}))
|
2016-12-12 09:07:47 +13:00
|
|
|
break
|
|
|
|
|
|
2018-03-12 21:39:27 +13:00
|
|
|
case 'PUSHER_GET_RADIO':
|
|
|
|
|
request(store, 'get_radio')
|
|
|
|
|
.then(
|
|
|
|
|
response => {
|
|
|
|
|
store.dispatch({
|
|
|
|
|
type: 'PUSHER_RADIO',
|
2018-03-13 08:57:40 +13:00
|
|
|
radio: response.radio
|
2018-03-12 21:39:27 +13:00
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
error => {
|
|
|
|
|
store.dispatch(coreActions.handleException(
|
|
|
|
|
'Could not load radio',
|
|
|
|
|
error
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
break;
|
|
|
|
|
|
2017-02-21 05:46:28 +13:00
|
|
|
case 'PUSHER_START_RADIO':
|
2017-04-18 08:52:28 +12:00
|
|
|
case 'PUSHER_UPDATE_RADIO':
|
2017-08-02 10:53:30 +12:00
|
|
|
ReactGA.event({ category: 'Pusher', action: 'Start radio', label: action.uris.join() })
|
2017-04-18 08:52:28 +12:00
|
|
|
|
|
|
|
|
// start our UI process notification
|
2017-04-18 04:44:39 +12:00
|
|
|
if (action.type == 'PUSHER_UPDATE_RADIO'){
|
2017-08-22 08:09:35 +12:00
|
|
|
store.dispatch(uiActions.startProcess('PUSHER_RADIO_PROCESS', 'Updating radio'))
|
2017-04-18 04:44:39 +12:00
|
|
|
} else {
|
2017-08-22 08:09:35 +12:00
|
|
|
store.dispatch(uiActions.startProcess('PUSHER_RADIO_PROCESS', 'Starting radio'))
|
2017-04-18 04:44:39 +12:00
|
|
|
}
|
|
|
|
|
|
2016-12-20 16:58:20 +13:00
|
|
|
var data = {
|
2017-04-19 06:58:41 +12:00
|
|
|
update: (action.type == 'PUSHER_UPDATE_RADIO'),
|
2016-12-20 16:58:20 +13:00
|
|
|
seed_artists: [],
|
|
|
|
|
seed_genres: [],
|
|
|
|
|
seed_tracks: []
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-10 13:05:28 +13:00
|
|
|
for(var i = 0; i < action.uris.length; i++){
|
|
|
|
|
switch(helpers.uriType(action.uris[i] )){
|
2016-12-20 16:58:20 +13:00
|
|
|
case 'artist':
|
2017-10-10 13:05:28 +13:00
|
|
|
data.seed_artists.push(action.uris[i] );
|
2016-12-20 16:58:20 +13:00
|
|
|
break;
|
|
|
|
|
case 'track':
|
2017-10-10 13:05:28 +13:00
|
|
|
data.seed_tracks.push(action.uris[i] );
|
2016-12-20 16:58:20 +13:00
|
|
|
break;
|
2017-01-31 08:44:12 +13:00
|
|
|
case 'genre':
|
2017-10-10 13:05:28 +13:00
|
|
|
data.seed_genres.push(action.uris[i] );
|
2017-01-31 08:44:12 +13:00
|
|
|
break;
|
2016-12-20 16:58:20 +13:00
|
|
|
}
|
|
|
|
|
}
|
2017-03-09 08:04:23 +13:00
|
|
|
|
2017-04-18 08:52:28 +12:00
|
|
|
request(store, 'change_radio', data)
|
2017-10-01 09:38:01 +13:00
|
|
|
.then(
|
|
|
|
|
response => {
|
2018-02-28 09:15:34 +13:00
|
|
|
store.dispatch(uiActions.processFinishing('PUSHER_RADIO_PROCESS'));
|
2017-10-01 09:38:01 +13:00
|
|
|
if (response.status == 0){
|
2018-02-23 09:35:38 +13:00
|
|
|
store.dispatch(uiActions.createNotification({content: response.message, type: 'bad'}));
|
2017-10-01 09:38:01 +13:00
|
|
|
}
|
|
|
|
|
},
|
2017-11-17 08:32:12 +13:00
|
|
|
error => {
|
2018-02-28 09:15:34 +13:00
|
|
|
store.dispatch(uiActions.processFinishing('PUSHER_RADIO_PROCESS'));
|
2017-10-01 09:38:01 +13:00
|
|
|
store.dispatch(coreActions.handleException(
|
|
|
|
|
'Could not change radio',
|
|
|
|
|
error
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
)
|
2016-12-20 16:58:20 +13:00
|
|
|
break
|
|
|
|
|
|
2017-02-21 05:46:28 +13:00
|
|
|
case 'PUSHER_STOP_RADIO':
|
2018-02-23 09:35:38 +13:00
|
|
|
store.dispatch(uiActions.createNotification({content: 'Stopping radio'}));
|
|
|
|
|
ReactGA.event({ category: 'Pusher', action: 'Stop radio' });
|
2017-01-05 13:49:38 +13:00
|
|
|
|
2016-12-20 19:54:19 +13:00
|
|
|
var data = {
|
|
|
|
|
seed_artists: [],
|
|
|
|
|
seed_genres: [],
|
|
|
|
|
seed_tracks: []
|
|
|
|
|
}
|
2017-02-21 05:46:28 +13:00
|
|
|
|
2017-03-09 08:04:23 +13:00
|
|
|
// we don't need to wait for response, as change will be broadcast
|
2017-03-27 14:23:38 +13:00
|
|
|
request(store, 'stop_radio', data)
|
2017-02-21 05:46:28 +13:00
|
|
|
break
|
|
|
|
|
|
2017-08-02 10:53:30 +12:00
|
|
|
case 'PUSHER_RADIO_STARTED':
|
|
|
|
|
case 'PUSHER_RADIO_CHANGED':
|
2017-08-22 08:09:35 +12:00
|
|
|
if (action.radio && action.radio.enabled && store.getState().spotify.enabled){
|
2017-08-02 10:53:30 +12:00
|
|
|
store.dispatch(spotifyActions.resolveRadioSeeds(action.radio))
|
|
|
|
|
}
|
|
|
|
|
next(action)
|
|
|
|
|
break
|
|
|
|
|
|
2017-02-21 05:46:28 +13:00
|
|
|
case 'PUSHER_BROWSER_NOTIFICATION':
|
|
|
|
|
store.dispatch(uiActions.createBrowserNotification(action))
|
2016-12-20 19:54:19 +13:00
|
|
|
break
|
|
|
|
|
|
2018-02-23 09:35:38 +13:00
|
|
|
case 'PUSHER_NOTIFICATION':
|
|
|
|
|
var data = Object.assign(
|
|
|
|
|
{},
|
|
|
|
|
action, {
|
|
|
|
|
type: action.notification_type
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
store.dispatch(uiActions.createNotification(data));
|
|
|
|
|
break
|
|
|
|
|
|
2017-03-06 07:26:08 +13:00
|
|
|
case 'PUSHER_RESTART':
|
|
|
|
|
// Hard reload. This doesn't strictly clear the cache, but our compiler's
|
|
|
|
|
// cache buster should handle that
|
|
|
|
|
window.location.reload(true);
|
|
|
|
|
break
|
|
|
|
|
|
2017-08-02 10:53:30 +12:00
|
|
|
case 'PUSHER_VERSION':
|
|
|
|
|
ReactGA.event({ category: 'Pusher', action: 'Version', label: action.version.current })
|
|
|
|
|
|
|
|
|
|
if (action.version.upgrade_available){
|
2018-02-23 09:35:38 +13:00
|
|
|
store.dispatch(uiActions.createNotification({content: 'Version '+action.version.latest+' is available. See settings to upgrade.'}));
|
2017-08-02 10:53:30 +12:00
|
|
|
}
|
2017-10-10 13:05:28 +13:00
|
|
|
next(action )
|
2017-08-02 10:53:30 +12:00
|
|
|
break
|
|
|
|
|
|
2017-08-04 09:19:19 +12:00
|
|
|
case 'PUSHER_CONFIG':
|
2017-08-12 00:02:41 +12:00
|
|
|
store.dispatch(spotifyActions.set({
|
2017-08-04 09:19:19 +12:00
|
|
|
locale: (action.config.locale ? action.config.locale : null),
|
|
|
|
|
country: (action.config.country ? action.config.country : null),
|
2017-11-02 21:20:56 +13:00
|
|
|
authorization_url: (action.config.spotify_authorization_url ? action.config.spotify_authorization_url : null)
|
|
|
|
|
}))
|
|
|
|
|
store.dispatch(lastfmActions.set({
|
|
|
|
|
authorization_url: (action.config.lastfm_authorization_url ? action.config.lastfm_authorization_url : null)
|
2017-08-04 09:19:19 +12:00
|
|
|
}))
|
2017-08-04 22:33:49 +12:00
|
|
|
|
2017-10-10 13:05:28 +13:00
|
|
|
next(action )
|
2017-08-04 09:19:19 +12:00
|
|
|
break
|
|
|
|
|
|
2017-03-06 07:26:08 +13:00
|
|
|
case 'PUSHER_DEBUG':
|
2017-03-27 14:23:38 +13:00
|
|
|
request(store, action.message.method, action.message.data )
|
2017-10-26 19:26:38 +13:00
|
|
|
.then(
|
|
|
|
|
response => {
|
|
|
|
|
store.dispatch({type: 'DEBUG', response: response})
|
|
|
|
|
},
|
|
|
|
|
error => {
|
|
|
|
|
store.dispatch(coreActions.handleException(
|
|
|
|
|
'Could not debug',
|
|
|
|
|
error,
|
|
|
|
|
error.message
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
);
|
2017-03-06 07:26:08 +13:00
|
|
|
break;
|
|
|
|
|
|
2017-03-22 08:42:26 +13:00
|
|
|
case 'PUSHER_ERROR':
|
|
|
|
|
store.dispatch(uiActions.createNotification(action.message, 'bad'))
|
2017-08-02 10:53:30 +12:00
|
|
|
ReactGA.event({ category: 'Pusher', action: 'Error', label: action.message })
|
|
|
|
|
break
|
2017-03-22 08:42:26 +13:00
|
|
|
|
2018-03-12 21:39:27 +13:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Snapcast actions
|
|
|
|
|
**/
|
|
|
|
|
case 'PUSHER_GET_SNAPCAST':
|
|
|
|
|
request(store, 'snapcast_instruct', action.data)
|
|
|
|
|
.then(
|
|
|
|
|
response => {
|
|
|
|
|
store.dispatch({type: 'PUSHER_SNAPCAST', snapcast: response.result.server})
|
|
|
|
|
},
|
|
|
|
|
error => {
|
|
|
|
|
store.dispatch(coreActions.handleException(
|
|
|
|
|
'Could not get Snapcast server',
|
|
|
|
|
error,
|
|
|
|
|
error.message
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
case 'PUSHER_SET_SNAPCAST_CLIENT_VOLUME':
|
|
|
|
|
request(store, 'snapcast_instruct', action.data)
|
|
|
|
|
.then(
|
|
|
|
|
response => {
|
|
|
|
|
console.log(response)
|
|
|
|
|
store.dispatch({
|
|
|
|
|
type: 'PUSHER_SNAPCAST_CLIENT_UPDATED',
|
|
|
|
|
key: response.client.id,
|
|
|
|
|
client: response.client
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
error => {
|
|
|
|
|
store.dispatch(coreActions.handleException(
|
|
|
|
|
'ERR',
|
|
|
|
|
error,
|
|
|
|
|
error.message
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
break
|
|
|
|
|
|
2016-10-27 16:58:11 +13:00
|
|
|
// This action is irrelevant to us, pass it on to the next middleware
|
|
|
|
|
default:
|
|
|
|
|
return next(action);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
})();
|
|
|
|
|
|
2017-03-04 14:43:21 +01:00
|
|
|
export default PusherMiddleware
|