Clients to have access to broadcast and send_message as it now uses data as dict, not python params

This commit is contained in:
James Barnsley
2018-03-20 21:47:59 +13:00
parent 48eae8b173
commit 4b5c8a4420
15 changed files with 1152 additions and 2014 deletions

View File

@ -230,23 +230,76 @@ class IrisCore(object):
}
def send_message(self, *args, **kwargs):
connection_id = kwargs.get('connection_id', None)
message = kwargs.get('message', None)
def send_message(self, *args, **kwargs):
callback = kwargs.get('callback', None)
data = kwargs.get('data', None)
if 'jsonrpc' not in message:
message['jsonrpc'] = '2.0'
logger.debug(data)
# Catch invalid recipient
if data['recipient'] not in self.connections:
error = 'Connection "'+data['recipient']+'" not found'
logger.error(error)
error = {
'message': error
}
if (callback):
callback(False, error)
else:
return error
# Sending of an error
if 'error' in data:
message = {
'jsonrpc': '2.0',
'error': data['error']
}
# Sending of a regular message
else:
message = {
'jsonrpc': '2.0',
'method': data['method'] if 'method' in data else None
}
if 'id' in data:
message['id'] = data['id']
if 'params' in data:
message['params'] = data['params']
if 'result' in data:
message['result'] = data['result']
# Dispatch the message
try:
self.connections[connection_id]['connection'].write_message(json_encode(message))
self.connections[data['recipient']]['connection'].write_message(json_encode(message))
response = {
'message': 'Sent message to '+data['recipient']
}
if (callback):
callback(response)
else:
return response
except:
logger.error('Failed to send message to '+ connection_id)
error = 'Failed to send message to '+ data['recipient']
logger.error(error)
error = {
'message': error
}
if (callback):
callback(False, error)
else:
return error
def broadcast(self, *args, **kwargs):
callback = kwargs.get('callback', None)
data = kwargs.get('data', None)
logger.debug(data)
if 'error' in data:
message = {
'jsonrpc': '2.0',
@ -315,20 +368,18 @@ class IrisCore(object):
}
self.connections[connection_id] = new_connection
self.send_message(
connection_id=connection_id,
message={
'method': 'pusher.connectionAdded',
'params': {
'connection': {
'connection_id': connection_id,
'client_id': client['client_id'],
'username': client['username'],
'ip': client['ip']
}
self.send_message(data={
'recipient': connection_id,
'method': 'pusher.connectionAdded',
'params': {
'connection': {
'connection_id': connection_id,
'client_id': client['client_id'],
'username': client['username'],
'ip': client['ip']
}
}
)
})
self.broadcast(data={
'method': 'pusher.connectionAdded',
@ -384,35 +435,6 @@ class IrisCore(object):
callback(False, error)
else:
return error
def deliver_message(self, *args, **kwargs):
callback = kwargs.get('callback', False)
data = kwargs.get('data', {})
if data['connection_id'] in self.connections:
self.send_message(
connection_id=data['connection_id'],
message=data['message']
)
response = {
'result': 'Sent message to '+data['connection_id']
}
if (callback):
callback(response)
else:
return response
else:
error = 'Connection "'+data['connection_id']+'" not found'
logger.error(error)
error = {
'message': error
}
if (callback):
callback(False, error)
else:
return error

View File

@ -135,7 +135,9 @@ class WebsocketHandler(tornado.websocket.WebSocketHandler):
request_response['result'] = response
# Respond to the original request
mem.iris.send_message(connection_id=self.connection_id, message=request_response)
data = request_response
data['recipient'] = self.connection_id
mem.iris.send_message(data=data)

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

View File

@ -4266,6 +4266,9 @@ input[type="submit"] {
left: 0;
right: 0; } }
.snapcast .group .clients {
padding-left: 20px; }
aside {
position: fixed;
top: 0;

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -38,7 +38,7 @@
// Release details
// These are automatically injected to built HTML
var build = "1520880835";
var build = "1521531954";
var version = "3.14.2";
// Construct the script tag

View File

@ -17,7 +17,14 @@ class AuthorizationModal_Send extends React.Component{
handleClick(e, connection_id){
e.preventDefault()
this.props.pusherActions.sendAuthorization(connection_id, this.props.authorization, this.props.me )
this.props.pusherActions.deliverMessage(
connection_id,
'spotify.authorizationReceived',
{
authorization: this.props.authorization,
me: this.props.me
}
);
this.props.uiActions.closeModal()
return false;
}

View File

@ -26,29 +26,36 @@ class PusherConnectionList extends React.Component{
}
render(){
if (!this.props.connected ) return <div className="pusher-connection-list grey-text">Not connected</div>
if (!this.props.connected){
return <div className="pusher-connection-list grey-text">Not connected</div>;
}
var connections = []
var connections = [];
for (var connection_id in this.props.connections){
if (this.props.connections.hasOwnProperty(connection_id)){
connections.push(this.props.connections[connection_id])
connections.push(this.props.connections[connection_id]);
}
}
if (connections.length <= 0) return <div className="pusher-connection-list grey-text">No connections</div>;
if (connections.length <= 0){
return <div className="pusher-connection-list grey-text">No connections</div>;
}
return (
<div className="pusher-connection-list">
{
connections.map((connection, index) => {
connections.map(connection => {
var is_me = false;
if (connection.connection_id == this.props.connection_id ) is_me = true;
if (connection.connection_id == this.props.connection_id){
is_me = true;
}
return (
<div className={ is_me ? 'connection cf me': 'connection cf'} key={connection.connection_id}>
<div className="col w30">{ connection.username } { is_me ? <span>(you)</span> : null }</div>
<div className={is_me ? 'connection cf me':'connection cf'} key={connection.connection_id}>
<div className="col w30">{ connection.username } {is_me ? <span>(you)</span> : null }</div>
<div className="col w70">
{ connection.ip }
<span className="grey-text"> ({ connection.connection_id })</span>
{connection.ip}
<span className="grey-text"> ({connection.connection_id})</span>
</div>
</div>
);

View File

@ -262,9 +262,9 @@ const MopidyMiddleware = (function(){
);
store.dispatch(pusherActions.deliverBroadcast(
'notification',
'ui.createNotification',
{
notification_type: 'info',
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)
}
@ -283,9 +283,9 @@ const MopidyMiddleware = (function(){
);
store.dispatch(pusherActions.deliverBroadcast(
'notification',
'ui.createNotification',
{
notification_type: 'info',
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)
}
@ -296,9 +296,9 @@ const MopidyMiddleware = (function(){
instruct(socket, store, 'playback.next');
store.dispatch(pusherActions.deliverBroadcast(
'notification',
'ui.createNotification',
{
notification_type: 'info',
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)
}
@ -308,21 +308,23 @@ const MopidyMiddleware = (function(){
case 'MOPIDY_STOP':
instruct(socket, store, 'playback.stop');
store.dispatch(pusherActions.deliverBroadcast({
type: 'notification',
notification_type: 'info',
content: store.getState().pusher.username +' stopped playback',
icon: (store.getState().core.current_track ? helpers.getTrackIcon(store.getState().core.current_track, store.getState().core) : false)
}));
store.dispatch(pusherActions.deliverBroadcast(
'ui.createNotification',
{
type: 'info',
content: store.getState().pusher.username +' stopped playback',
icon: (store.getState().core.current_track ? helpers.getTrackIcon(store.getState().core.current_track, store.getState().core) : false)
}
));
break
case 'MOPIDY_CHANGE_TRACK':
instruct(socket, store, 'playback.play', {tlid: action.tlid});
store.dispatch(pusherActions.deliverBroadcast(
'notification',
'ui.createNotification',
{
notification_type: 'info',
type: 'info',
content: store.getState().pusher.username +' changed track'
}
));
@ -331,9 +333,9 @@ const MopidyMiddleware = (function(){
case 'MOPIDY_REMOVE_TRACKS':
instruct(socket, store, 'tracklist.remove', {tlid: action.tlids});
store.dispatch(pusherActions.deliverBroadcast(
'notification',
'ui.createNotification',
{
notification_type: 'info',
type: 'info',
content: store.getState().pusher.username +' removed '+action.tlids.length+' tracks'
}
));
@ -401,11 +403,13 @@ const MopidyMiddleware = (function(){
case 'MOPIDY_SET_MUTE':
instruct(socket, store, 'mixer.setMute', [action.mute]);
store.dispatch(pusherActions.deliverBroadcast({
type: 'notification',
notification_type: 'info',
content: store.getState().pusher.username +(action.mute ? ' muted' : ' unmuted')+' playback'
}));
store.dispatch(pusherActions.deliverBroadcast(
'ui.createNotification',
{
type: 'info',
content: store.getState().pusher.username +(action.mute ? ' muted' : ' unmuted')+' playback'
}
));
break;
case 'MOPIDY_GET_VOLUME':
@ -554,12 +558,14 @@ const MopidyMiddleware = (function(){
break;
}
store.dispatch(pusherActions.deliverBroadcast({
type: 'notification',
notification_type: 'info',
content: store.getState().pusher.username +' is adding '+action.uris.length+' URIs to queue',
icon: (store.getState().core.current_track ? helpers.getTrackIcon(store.getState().core.current_track, store.getState().core) : false)
}));
store.dispatch(pusherActions.deliverBroadcast(
'ui.createNotification',
{
type: 'info',
content: store.getState().pusher.username +' is adding '+action.uris.length+' URIs to queue',
icon: (store.getState().core.current_track ? helpers.getTrackIcon(store.getState().core.current_track, store.getState().core) : false)
}
));
// split into batches
var uris = Object.assign([], action.uris)

View File

@ -44,21 +44,21 @@ export function getConnections(){
export function connectionAdded(connection){
return {
type: 'PUSHER_CONNECTION_ADDED',
connection: connection
connection: connection.connection
}
}
export function connectionChanged(connection){
return {
type: 'PUSHER_CONNECTION_CHANGED',
connection: connection
connection: connection.connection
}
}
export function connectionRemoved(connection){
return {
type: 'PUSHER_CONNECTION_REMOVED',
connection: connection
connection: connection.connection
}
}
@ -91,16 +91,13 @@ export function deliverBroadcast(method, params){
}
}
export function sendAuthorization(recipient_connectionid, authorization, me){
export function deliverMessage(recipient, method, params){
return {
type: 'PUSHER_DELIVER_MESSAGE',
data: {
connection_id: recipient_connectionid,
message: {
type: 'spotify_authorization',
authorization: authorization,
me: me
}
recipient: recipient,
method: method,
params: params
}
}
}
@ -144,6 +141,13 @@ export function getQueueMetadata(){
}
}
export function queueMetadataChanged(queue_metadata){
return {
type: 'PUSHER_QUEUE_METADATA_CHANGED',
queue_metadata: queue_metadata.queue_metadata
}
}
export function addQueueMetadata(tlids = [], from_uri = null){
return {
type: 'PUSHER_ADD_QUEUE_METADATA',

View File

@ -20,7 +20,7 @@ const PusherMiddleware = (function(){
const handleMessage = (ws, store, message) => {
if (store.getState().ui.log_pusher){
console.log('Pusher log', message);
console.log('Pusher log (incoming)', message);
}
// Response with request_id
@ -59,8 +59,6 @@ const PusherMiddleware = (function(){
));
} else {
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';
@ -90,8 +88,6 @@ const PusherMiddleware = (function(){
return;
}
console.log(action);
if (message.params){
store.dispatch(action(message.params));
} else {
@ -104,6 +100,10 @@ const PusherMiddleware = (function(){
const request = (store, method, params = null) => {
return new Promise((resolve, reject) => {
if (store.getState().ui.log_pusher){
console.log('Pusher log (outgoing)', {method: method, params: params});
}
var id = helpers.generateGuid();
var message = {
jsonrpc: '2.0',
@ -199,10 +199,10 @@ const PusherMiddleware = (function(){
store.dispatch(pusherActions.getConfig());
store.dispatch(pusherActions.getVersion());
store.dispatch(pusherActions.getRadio());
store.dispatch(pusherActions.getSnapcast());
//store.dispatch(pusherActions.getSnapcast());
store.dispatch(pusherActions.getQueueMetadata());
return next(action);
next(action);
break;
case 'PUSHER_INSTRUCT':
@ -221,7 +221,7 @@ const PusherMiddleware = (function(){
break
case 'PUSHER_DELIVER_MESSAGE':
request(store, 'deliver_message', action.data)
request(store, 'send_message', action.data)
.then(
response => {
store.dispatch(uiActions.createNotification({content: 'Message delivered'}));
@ -288,9 +288,7 @@ const PusherMiddleware = (function(){
break;
case 'PUSHER_SET_USERNAME':
request(store, 'set_username', {
username: action.username
})
request(store, 'set_username', {username: action.username})
.then(
response => {
response.type = 'PUSHER_USERNAME_CHANGED'
@ -367,11 +365,7 @@ const PusherMiddleware = (function(){
}
);
return next(action);
break
case 'PUSHER_SPOTIFY_AUTHORIZATION':
store.dispatch(uiActions.openModal('receive_authorization', {authorization: action.authorization, user: action.me}))
break
break;
case 'PUSHER_GET_RADIO':
request(store, 'get_radio')

View File

@ -235,6 +235,14 @@ export function refreshingToken(){
}
}
export function authorizationReceived(data){
console.log(data);
// This is just an alias to open the modal
return uiActions.openModal('receive_authorization', data);
}
export function importAuthorization(data){
return {
type: 'SPOTIFY_IMPORT_AUTHORIZATION',

View File

@ -146,6 +146,7 @@ export function getIcon(name){
**/
export function openModal(name, data){
console.log(name,data);
return {
type: 'OPEN_MODAL',
modal: {
@ -183,7 +184,7 @@ export function createNotification(data){
notification: Object.assign(
{
key: helpers.generateGuid(),
duration: 3,
duration: 5,
type: 'default',
title: null,
content: null,