Handling new api structure with client-triggered broadcasts

This commit is contained in:
James Barnsley
2018-03-20 13:02:38 +13:00
parent 2cde9fa786
commit 8549e9db85
10 changed files with 160 additions and 95 deletions

View File

@ -76,7 +76,6 @@ const MopidyMiddleware = (function(){
break;
case 'event:playbackStateChanged':
console.log(type,data);
store.dispatch({
type: 'MOPIDY_PLAY_STATE',
play_state: data.new_state
@ -262,12 +261,14 @@ const MopidyMiddleware = (function(){
}
);
store.dispatch(pusherActions.deliverBroadcast({
type: 'notification',
notification_type: 'info',
content: store.getState().pusher.username +(store.getState().mopidy.play_state == 'paused' ? ' resumed' : ' started')+' playback',
icon: (store.getState().core.current_track ? helpers.getTrackIcon(store.getState().core.current_track, store.getState().core) : false)
}));
store.dispatch(pusherActions.deliverBroadcast(
'notification',
{
notification_type: 'info',
content: store.getState().pusher.username +(store.getState().mopidy.play_state == 'paused' ? ' resumed' : ' started')+' playback',
icon: (store.getState().core.current_track ? helpers.getTrackIcon(store.getState().core.current_track, store.getState().core) : false)
}
));
break;
case 'MOPIDY_PAUSE':
@ -281,23 +282,27 @@ const MopidyMiddleware = (function(){
}
);
store.dispatch(pusherActions.deliverBroadcast({
type: 'notification',
notification_type: 'info',
content: store.getState().pusher.username +' paused playback',
icon: (store.getState().core.current_track ? helpers.getTrackIcon(store.getState().core.current_track, store.getState().core) : false)
}));
store.dispatch(pusherActions.deliverBroadcast(
'notification',
{
notification_type: 'info',
content: store.getState().pusher.username +' paused playback',
icon: (store.getState().core.current_track ? helpers.getTrackIcon(store.getState().core.current_track, store.getState().core) : false)
}
));
break
case 'MOPIDY_NEXT':
instruct(socket, store, 'playback.next');
store.dispatch(pusherActions.deliverBroadcast({
type: 'notification',
notification_type: 'info',
content: store.getState().pusher.username +' skipped <em>'+store.getState().core.current_track.name+'</em>',
icon: (store.getState().core.current_track ? helpers.getTrackIcon(store.getState().core.current_track, store.getState().core) : false)
}));
store.dispatch(pusherActions.deliverBroadcast(
'notification',
{
notification_type: 'info',
content: store.getState().pusher.username +' skipped <em>'+store.getState().core.current_track.name+'</em>',
icon: (store.getState().core.current_track ? helpers.getTrackIcon(store.getState().core.current_track, store.getState().core) : false)
}
));
break
case 'MOPIDY_STOP':
@ -314,20 +319,24 @@ const MopidyMiddleware = (function(){
case 'MOPIDY_CHANGE_TRACK':
instruct(socket, store, 'playback.play', {tlid: action.tlid});
store.dispatch(pusherActions.deliverBroadcast({
type: 'notification',
notification_type: 'info',
content: store.getState().pusher.username +' changed track'
}));
store.dispatch(pusherActions.deliverBroadcast(
'notification',
{
notification_type: 'info',
content: store.getState().pusher.username +' changed track'
}
));
break;
case 'MOPIDY_REMOVE_TRACKS':
instruct(socket, store, 'tracklist.remove', {tlid: action.tlids});
store.dispatch(pusherActions.deliverBroadcast({
type: 'notification',
notification_type: 'info',
content: store.getState().pusher.username +' removed '+action.tlids.length+' tracks'
}));
store.dispatch(pusherActions.deliverBroadcast(
'notification',
{
notification_type: 'info',
content: store.getState().pusher.username +' removed '+action.tlids.length+' tracks'
}
));
break;
case 'MOPIDY_GET_REPEAT':
@ -439,7 +448,6 @@ const MopidyMiddleware = (function(){
instruct(socket, store, 'playback.getTimePosition')
.then(
response => {
console.log('playback.getTimePosition',response);
store.dispatch({
type: 'MOPIDY_TIME_POSITION',
time_position: response

View File

@ -60,10 +60,13 @@ export function getVersion(){
}
}
export function deliverBroadcast(data = null){
export function deliverBroadcast(method, params){
return {
type: 'PUSHER_DELIVER_BROADCAST',
data: data
data: {
method: method,
params: params
}
}
}

View File

@ -136,7 +136,10 @@ const PusherMiddleware = (function(){
socket.onopen = () => {
store.dispatch({
type: 'PUSHER_CONNECTED'
type: 'PUSHER_CONNECTED',
connection_id: connection.connection_id,
client_id: connection.client_id,
username: connection.username
});
};

View File

@ -162,16 +162,27 @@ function refreshToken(dispatch, getState){
$.ajax(config)
.then(
response => {
var token = response.spotify_token;
token.token_expiry = new Date().getTime() + (token.expires_in * 1000 );
token.source = 'mopidy';
dispatch({
type: 'SPOTIFY_TOKEN_REFRESHED',
access_token_provider: 'backend',
data: token
});
resolve(token);
(response, status, xhr) => {
if (response.error){
dispatch({ type: 'SPOTIFY_DISCONNECTED' })
reject({
config: config,
xhr: xhr,
status: status,
error: response.error
});
} else {
var token = response.result.spotify_token;
token.token_expiry = new Date().getTime() + (token.expires_in * 1000 );
token.source = 'mopidy';
dispatch({
type: 'SPOTIFY_TOKEN_REFRESHED',
access_token_provider: 'backend',
data: token
});
resolve(token);
}
},
(xhr, status, error) => {

View File

@ -203,10 +203,11 @@ export function closeNotification(key){
}
}
export function removeNotification(key){
export function removeNotification(key, manual = false){
return {
type: 'REMOVE_NOTIFICATION',
key: key
key: key,
manual: manual
}
}

View File

@ -86,14 +86,6 @@ const UIMiddleware = (function(){
case 'CLOSE_NOTIFICATION':
var notifications = Object.assign({}, store.getState().ui.notifications);
// If a broadcast, add to suppressed_broadcasts
if (notifications[action.key] && notifications[action.key].type == 'broadcast'){
store.dispatch({
type: 'SUPPRESS_BROADCAST',
key: action.key
})
}
// start a timeout to remove this notification
// This gives us time to animate out the notification before we remove the data
var timeout = setTimeout(
@ -106,6 +98,24 @@ const UIMiddleware = (function(){
next(action);
break;
case 'REMOVE_NOTIFICATION':
// Manual removal
if (action.manual){
var notifications = Object.assign({}, store.getState().ui.notifications);
// If a broadcast, add to suppressed_broadcasts
if (notifications[action.key] && notifications[action.key].type == 'broadcast'){
store.dispatch({
type: 'SUPPRESS_BROADCAST',
key: action.key
})
}
}
next(action);
break;
case 'BROADCASTS_LOADED':
var suppressed_broadcasts = []
if (typeof(store.getState().ui.suppressed_broadcasts) !== 'undefined'){