2016-10-27 16:58:11 +13:00
|
|
|
|
2017-08-02 10:53:30 +12:00
|
|
|
import ReactGA from 'react-ga'
|
|
|
|
|
|
2018-08-20 08:48:46 +12:00
|
|
|
var helpers = require('../../helpers')
|
|
|
|
|
var coreActions = require('../core/actions')
|
|
|
|
|
var uiActions = require('../ui/actions')
|
|
|
|
|
var mopidyActions = require('../mopidy/actions')
|
|
|
|
|
var pusherActions = require('./actions')
|
|
|
|
|
var lastfmActions = require('../lastfm/actions')
|
2018-08-21 21:08:11 +12:00
|
|
|
var geniusActions = require('../genius/actions')
|
2018-08-20 08:48:46 +12:00
|
|
|
var spotifyActions = require('../spotify/actions')
|
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-20 21:47:59 +13:00
|
|
|
console.log('Pusher log (incoming)', message);
|
2017-09-19 16:57:59 +12:00
|
|
|
}
|
|
|
|
|
|
2018-04-23 09:07:48 +12:00
|
|
|
// Pull our ID. JSON-RPC nests the ID under the error object,
|
|
|
|
|
// so make sure we handle that.
|
|
|
|
|
// TODO: Use this as our measure of a successful response vs error
|
|
|
|
|
var id = null;
|
|
|
|
|
if (message.id){
|
|
|
|
|
id = message.id;
|
|
|
|
|
} else if (message.error && message.error.id){
|
|
|
|
|
id = message.error.id;
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-26 19:26:38 +13:00
|
|
|
// Response with request_id
|
2018-04-23 09:07:48 +12:00
|
|
|
if (id){
|
2017-09-19 16:57:59 +12:00
|
|
|
|
2017-10-26 19:26:38 +13:00
|
|
|
// Response matches a pending request
|
2018-04-23 09:07:48 +12:00
|
|
|
if (deferredRequests[id] !== undefined){
|
2017-10-26 19:26:38 +13:00
|
|
|
|
2018-04-23 09:07:48 +12:00
|
|
|
store.dispatch(uiActions.stopLoading(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){
|
2018-04-23 09:07:48 +12:00
|
|
|
deferredRequests[id].reject(message.error);
|
2017-10-26 19:26:38 +13:00
|
|
|
|
|
|
|
|
// Successful response
|
|
|
|
|
} else {
|
2018-04-23 09:07:48 +12:00
|
|
|
deferredRequests[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,
|
2018-04-23 09:07:48 +12:00
|
|
|
message,
|
|
|
|
|
(message.error.data !== undefined && message.error.data.description !== undefined ? message.error.data.description : null)
|
2017-10-26 19:26:38 +13:00
|
|
|
));
|
2018-04-09 21:13:12 +12:00
|
|
|
|
2017-10-26 19:26:38 +13:00
|
|
|
} else {
|
2018-03-20 16:53:41 +13:00
|
|
|
|
2018-03-21 08:45:22 +13:00
|
|
|
switch (message.method){
|
|
|
|
|
case 'connection_added':
|
|
|
|
|
store.dispatch(pusherActions.connectionAdded(message.params.connection));
|
2018-03-20 16:53:41 +13:00
|
|
|
break;
|
2018-03-21 08:45:22 +13:00
|
|
|
case 'connection_changed':
|
|
|
|
|
store.dispatch(pusherActions.connectionChanged(message.params.connection));
|
2018-03-20 16:53:41 +13:00
|
|
|
break;
|
2018-03-21 08:45:22 +13:00
|
|
|
case 'connection_removed':
|
|
|
|
|
store.dispatch(pusherActions.connectionRemoved(message.params.connection));
|
2018-03-20 16:53:41 +13:00
|
|
|
break;
|
2018-03-21 08:45:22 +13:00
|
|
|
case 'queue_metadata_changed':
|
|
|
|
|
store.dispatch(pusherActions.queueMetadataChanged(message.params.queue_metadata));
|
2018-03-20 16:53:41 +13:00
|
|
|
break;
|
2018-03-21 08:45:22 +13:00
|
|
|
case 'spotify_token_changed':
|
|
|
|
|
store.dispatch(spotifyActions.tokenChanged(message.params.spotify_token));
|
2018-03-20 16:53:41 +13:00
|
|
|
break;
|
2018-09-30 20:09:12 +13:00
|
|
|
case 'share_configuration_received':
|
2018-07-13 16:48:41 +12:00
|
|
|
store.dispatch(uiActions.createNotification({
|
2018-09-30 20:09:12 +13:00
|
|
|
type: 'share-configuration-received',
|
|
|
|
|
configuration: message.params,
|
2018-07-13 16:48:41 +12:00
|
|
|
sticky: true
|
|
|
|
|
}));
|
2018-03-21 08:45:22 +13:00
|
|
|
break;
|
|
|
|
|
case 'notification':
|
|
|
|
|
store.dispatch(uiActions.createNotification(message.params.notification));
|
2018-03-20 16:53:41 +13:00
|
|
|
break;
|
2018-04-01 21:26:42 +12:00
|
|
|
case 'radio_started':
|
|
|
|
|
store.dispatch(pusherActions.radioStarted(message.params.radio));
|
|
|
|
|
break;
|
|
|
|
|
case 'radio_changed':
|
|
|
|
|
store.dispatch(pusherActions.radioChanged(message.params.radio));
|
|
|
|
|
break;
|
|
|
|
|
case 'radio_stopped':
|
|
|
|
|
store.dispatch(pusherActions.radioStopped());
|
|
|
|
|
break;
|
2018-04-21 21:59:40 +12:00
|
|
|
case 'reload':
|
2018-04-09 21:13:12 +12:00
|
|
|
window.location.reload(true);
|
|
|
|
|
break;
|
2018-04-21 21:59:40 +12:00
|
|
|
case 'upgrading':
|
2018-09-17 08:18:07 +12:00
|
|
|
case 'upgrade_started':
|
|
|
|
|
store.dispatch(mopidyActions.upgrading());
|
2018-04-21 21:59:40 +12:00
|
|
|
store.dispatch(uiActions.createNotification({content: 'Upgrading...', type: 'info'}));
|
2018-04-17 15:58:39 +12:00
|
|
|
break;
|
2018-04-21 21:59:40 +12:00
|
|
|
case 'restarting':
|
2018-09-17 08:18:07 +12:00
|
|
|
store.dispatch(mopidyActions.restarting());
|
2018-04-17 15:58:39 +12:00
|
|
|
store.dispatch(uiActions.createNotification({content: 'Restarting...', type: 'info'}));
|
|
|
|
|
break;
|
2018-03-20 16:53:41 +13:00
|
|
|
}
|
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
|
|
|
}
|
2018-04-23 09:07:48 +12:00
|
|
|
|
|
|
|
|
if (store.getState().ui.log_pusher){
|
|
|
|
|
console.log('Pusher log (outgoing)', message);
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
2018-04-23 09:07:48 +12:00
|
|
|
// Start our 30 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
|
|
|
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-06-05 13:43:02 +12:00
|
|
|
if (store.getState().ui.allow_reporting){
|
|
|
|
|
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.getRadio());
|
|
|
|
|
store.dispatch(pusherActions.getQueueMetadata());
|
2018-09-18 21:38:37 +12:00
|
|
|
|
|
|
|
|
// Give things a few moments to setup before we check for version.
|
|
|
|
|
// This is because the server makes a GitHub request, which creates a [very] small delay
|
|
|
|
|
// in subsequent requests.
|
|
|
|
|
setTimeout(
|
|
|
|
|
function(){
|
|
|
|
|
store.dispatch(pusherActions.getVersion());
|
|
|
|
|
},
|
|
|
|
|
2000
|
|
|
|
|
);
|
2018-03-20 21:47:59 +13:00
|
|
|
next(action);
|
2016-10-29 22:40:02 +13:00
|
|
|
break;
|
|
|
|
|
|
2018-09-24 16:10:42 +12:00
|
|
|
case 'PUSHER_REQUEST':
|
2018-09-24 13:21:08 +12:00
|
|
|
request(store, action.method, action.params)
|
2017-02-21 08:58:49 +13:00
|
|
|
.then(
|
|
|
|
|
response => {
|
2018-09-24 13:21:08 +12:00
|
|
|
if (action.response_callback){
|
|
|
|
|
store.dispatch(action.response_callback.call(this, response));
|
|
|
|
|
}
|
2017-10-01 09:38:01 +13:00
|
|
|
},
|
2018-09-24 13:21:08 +12:00
|
|
|
error => {
|
|
|
|
|
if (action.error_callback){
|
|
|
|
|
store.dispatch(action.error_callback.call(this, error));
|
|
|
|
|
} else {
|
|
|
|
|
store.dispatch(coreActions.handleException(
|
2018-09-24 16:10:42 +12:00
|
|
|
'Pusher request failed',
|
|
|
|
|
error,
|
|
|
|
|
action.method,
|
|
|
|
|
action
|
2018-09-24 13:21:08 +12:00
|
|
|
));
|
|
|
|
|
}
|
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':
|
2018-03-20 21:47:59 +13:00
|
|
|
request(store, 'send_message', action.data)
|
2017-02-21 08:58:49 +13:00
|
|
|
.then(
|
|
|
|
|
response => {
|
2018-09-30 20:09:12 +13:00
|
|
|
store.dispatch(uiActions.createNotification({type: 'info', 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-01-09 09:29:56 +13:00
|
|
|
case 'PUSHER_SET_USERNAME':
|
2018-03-20 21:47:59 +13:00
|
|
|
request(store, 'set_username', {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
|
|
|
|
|
));
|
2018-06-06 21:11:27 +12:00
|
|
|
|
|
|
|
|
// Forced change to local state, even if server-end failed
|
|
|
|
|
// Useful for changing when not yet connected (ie Initial setup on
|
|
|
|
|
// non-standard ports, etc)
|
|
|
|
|
if (action.force){
|
|
|
|
|
response.type = 'PUSHER_USERNAME_CHANGED'
|
|
|
|
|
store.dispatch(response)
|
|
|
|
|
}
|
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
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
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);
|
2018-03-20 21:47:59 +13:00
|
|
|
break;
|
2016-12-12 09:07:47 +13:00
|
|
|
|
2018-03-12 21:39:27 +13:00
|
|
|
case 'PUSHER_GET_RADIO':
|
|
|
|
|
request(store, 'get_radio')
|
|
|
|
|
.then(
|
|
|
|
|
response => {
|
|
|
|
|
store.dispatch({
|
2018-04-01 21:26:42 +12:00
|
|
|
type: 'PUSHER_RADIO_LOADED',
|
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':
|
2018-06-05 13:43:02 +12:00
|
|
|
if (store.getState().ui.allow_reporting){
|
|
|
|
|
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 = {
|
2018-04-01 21:26:42 +12:00
|
|
|
reset: (action.type == 'PUSHER_START_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
|
|
|
|
2018-04-01 21:26:42 +12:00
|
|
|
if (action.type == 'PUSHER_START_RADIO'){
|
|
|
|
|
store.dispatch(pusherActions.deliverBroadcast(
|
|
|
|
|
'notification',
|
|
|
|
|
{
|
|
|
|
|
notification: {
|
|
|
|
|
type: 'info',
|
|
|
|
|
content: store.getState().pusher.username + ' is starting radio mode'
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
}
|
2018-04-01 21:26:42 +12:00
|
|
|
store.dispatch(pusherActions.radioChanged(response.radio));
|
2017-10-01 09:38:01 +13:00
|
|
|
},
|
2017-11-17 08:32:12 +13:00
|
|
|
error => {
|
2018-04-01 21:26:42 +12: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'}));
|
2018-06-05 13:43:02 +12:00
|
|
|
|
|
|
|
|
if (store.getState().ui.allow_reporting){
|
|
|
|
|
ReactGA.event({ category: 'Pusher', action: 'Stop radio' });
|
|
|
|
|
}
|
2017-01-05 13:49:38 +13:00
|
|
|
|
2018-04-01 21:26:42 +12:00
|
|
|
store.dispatch(pusherActions.deliverBroadcast(
|
|
|
|
|
'notification',
|
|
|
|
|
{
|
|
|
|
|
notification: {
|
|
|
|
|
type: 'info',
|
|
|
|
|
content: store.getState().pusher.username + ' stopped radio mode'
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
));
|
|
|
|
|
|
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-27 14:23:38 +13:00
|
|
|
request(store, 'stop_radio', data)
|
2018-04-01 21:26:42 +12:00
|
|
|
.then(
|
|
|
|
|
response => {
|
|
|
|
|
store.dispatch(pusherActions.radioStopped());
|
|
|
|
|
}, error => {
|
|
|
|
|
store.dispatch(coreActions.handleException(
|
|
|
|
|
'Could not stop radio',
|
|
|
|
|
error
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
);
|
2017-08-02 10:53:30 +12:00
|
|
|
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
|
|
|
|
|
|
2018-04-21 21:59:40 +12:00
|
|
|
case 'PUSHER_RELOAD':
|
2017-03-06 07:26:08 +13:00
|
|
|
// Hard reload. This doesn't strictly clear the cache, but our compiler's
|
|
|
|
|
// cache buster should handle that
|
|
|
|
|
window.location.reload(true);
|
|
|
|
|
break
|
|
|
|
|
|
2018-04-21 21:59:40 +12:00
|
|
|
case 'PUSHER_RESTART':
|
2018-04-16 15:51:35 +12:00
|
|
|
request(store, 'restart')
|
|
|
|
|
.then(
|
|
|
|
|
response => {
|
2018-04-21 21:59:40 +12:00
|
|
|
store.dispatch(mopidyActions.restarting());
|
2018-04-16 15:51:35 +12:00
|
|
|
},
|
|
|
|
|
error => {
|
2018-04-17 15:58:39 +12:00
|
|
|
store.dispatch(uiActions.createNotification({content: error.message, description: (error.description ? error.description : null), type: 'bad'}));
|
2018-04-16 15:51:35 +12:00
|
|
|
}
|
|
|
|
|
);
|
2018-04-16 21:58:38 +12:00
|
|
|
next(action);
|
2018-04-16 09:01:42 +12:00
|
|
|
break
|
|
|
|
|
|
2018-04-17 15:58:39 +12:00
|
|
|
case 'PUSHER_UPGRADE':
|
2018-06-05 13:43:02 +12:00
|
|
|
if (store.getState().ui.allow_reporting){
|
|
|
|
|
ReactGA.event({ category: 'Pusher', action: 'Upgrade', label: '' });
|
|
|
|
|
}
|
2018-04-17 15:58:39 +12:00
|
|
|
request(store, 'upgrade')
|
2018-04-16 21:58:38 +12:00
|
|
|
.then(
|
|
|
|
|
response => {
|
2018-04-21 21:59:40 +12:00
|
|
|
store.dispatch(mopidyActions.upgrading());
|
2018-04-16 21:58:38 +12:00
|
|
|
},
|
|
|
|
|
error => {
|
2018-04-23 09:07:48 +12:00
|
|
|
store.dispatch(uiActions.createNotification({content: error.message, description: (error.description ? error.description : null), type: 'bad'}));
|
2018-04-16 21:58:38 +12:00
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
break;
|
|
|
|
|
|
2017-08-02 10:53:30 +12:00
|
|
|
case 'PUSHER_VERSION':
|
2018-06-05 13:43:02 +12:00
|
|
|
if (store.getState().ui.allow_reporting){
|
|
|
|
|
ReactGA.event({ category: 'Pusher', action: 'Version', label: action.version.current });
|
|
|
|
|
}
|
2018-04-16 21:58:38 +12:00
|
|
|
next(action);
|
2017-08-02 10:53:30 +12:00
|
|
|
break
|
|
|
|
|
|
2017-08-04 09:19:19 +12:00
|
|
|
case 'PUSHER_CONFIG':
|
2018-04-21 11:24:11 +12:00
|
|
|
|
|
|
|
|
// Set default country/locale (unless we've already been configured)
|
|
|
|
|
var spotify = store.getState().spotify;
|
2018-04-23 09:07:48 +12:00
|
|
|
var spotify_updated = false;
|
2018-04-21 11:24:11 +12:00
|
|
|
var spotify_updates = {};
|
|
|
|
|
|
|
|
|
|
if (!spotify.country && action.config.country){
|
|
|
|
|
spotify_updates.country = action.config.country;
|
2018-04-23 09:07:48 +12:00
|
|
|
spotify_updated = true;
|
2018-04-21 11:24:11 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!spotify.locale && action.config.locale){
|
|
|
|
|
spotify_updates.locale = action.config.locale;
|
2018-04-23 09:07:48 +12:00
|
|
|
spotify_updated = true;
|
2018-04-21 11:24:11 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (action.config.spotify_authorization_url){
|
2018-04-24 20:33:26 +12:00
|
|
|
spotify_updates.authorization_url = action.config.spotify_authorization_url;
|
2018-04-23 09:07:48 +12:00
|
|
|
spotify_updated = true;
|
2018-04-21 11:24:11 +12:00
|
|
|
}
|
|
|
|
|
|
2018-04-23 09:07:48 +12:00
|
|
|
if (spotify_updated){
|
2018-04-21 11:24:11 +12:00
|
|
|
store.dispatch(spotifyActions.set(spotify_updates));
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-02 21:20:56 +13:00
|
|
|
store.dispatch(lastfmActions.set({
|
|
|
|
|
authorization_url: (action.config.lastfm_authorization_url ? action.config.lastfm_authorization_url : null)
|
2018-03-26 10:36:13 +13:00
|
|
|
}));
|
2018-08-16 08:05:06 +12:00
|
|
|
|
|
|
|
|
store.dispatch(geniusActions.set({
|
|
|
|
|
authorization_url: (action.config.genius_authorization_url ? action.config.genius_authorization_url : null)
|
|
|
|
|
}));
|
2017-08-04 22:33:49 +12:00
|
|
|
|
2018-03-26 10:36:13 +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':
|
2018-06-05 13:43:02 +12:00
|
|
|
store.dispatch(uiActions.createNotification(action.message, 'bad'));
|
|
|
|
|
if (store.getState().ui.allow_reporting){
|
|
|
|
|
ReactGA.event({ category: 'Pusher', action: 'Error', label: action.message });
|
|
|
|
|
}
|
2017-08-02 10:53:30 +12:00
|
|
|
break
|
2017-03-22 08:42:26 +13:00
|
|
|
|
2018-03-12 21:39:27 +13:00
|
|
|
|
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
|