Changeset View
Changeset View
Standalone View
Standalone View
webroot/rsrc/js/application/aphlict/behavior-aphlict-listen.js
/** | /** | ||||
* @provides javelin-behavior-aphlict-listen | * @provides javelin-behavior-aphlict-listen | ||||
* @requires javelin-behavior | * @requires javelin-behavior | ||||
* javelin-aphlict | * javelin-aphlict | ||||
* javelin-stratcom | * javelin-stratcom | ||||
* javelin-request | * javelin-request | ||||
* javelin-uri | * javelin-uri | ||||
* javelin-dom | * javelin-dom | ||||
* javelin-json | * javelin-json | ||||
* javelin-router | * javelin-router | ||||
* javelin-util | * javelin-util | ||||
* javelin-leader | |||||
* phabricator-notification | * phabricator-notification | ||||
*/ | */ | ||||
JX.behavior('aphlict-listen', function(config) { | JX.behavior('aphlict-listen', function(config) { | ||||
var showing_reload = false; | var showing_reload = false; | ||||
JX.Stratcom.listen('aphlict-receive-message', null, function(e) { | JX.Stratcom.listen('aphlict-server-message', null, function(e) { | ||||
var message = e.getData(); | var message = e.getData(); | ||||
if (message.type != 'notification') { | if (message.type != 'notification') { | ||||
return; | return; | ||||
} | } | ||||
JX.Leader.callIfLeader(function() { | |||||
var request = new JX.Request( | var request = new JX.Request( | ||||
'/notification/individual/', | '/notification/individual/', | ||||
epriestley: With the adjustments above, we could also make `is_leader` available in these listeners, which… | |||||
onNotification); | onNotification); | ||||
Not Done Inline ActionsNot a big deal, but it would be vaguely nice to pass is_leader through here eventually to avoid this callback. I don't think it's worth including in this diff, but maybe the next time we touch this stuff. epriestley: Not a big deal, but it would be vaguely nice to pass `is_leader` through here eventually to… | |||||
var routable = request | var routable = request | ||||
.addData({key: message.key}) | .addData({key: message.key}) | ||||
.getRoutable(); | .getRoutable(); | ||||
routable | routable | ||||
.setType('notification') | .setType('notification') | ||||
.setPriority(250); | .setPriority(250); | ||||
JX.Router.getInstance().queue(routable); | JX.Router.getInstance().queue(routable); | ||||
}); | }); | ||||
}); | |||||
// Respond to a notification from the Aphlict notification server. We send | // Respond to a notification from the Aphlict notification server. We send | ||||
// a request to Phabricator to get notification details. | // a request to Phabricator to get notification details. | ||||
function onAphlictMessage(message) { | function onAphlictMessage(message) { | ||||
JX.Stratcom.invoke('aphlict-receive-message', null, message); | switch (message.type) { | ||||
case 'aphlict.server': | |||||
JX.Stratcom.invoke('aphlict-server-message', null, message.data); | |||||
break; | |||||
case 'notification.individual': | |||||
JX.Stratcom.invoke('aphlict-notification-message', null, message.data); | |||||
break; | |||||
} | |||||
} | } | ||||
// Respond to a response from Phabricator about a specific notification. | // Respond to a response from Phabricator about a specific notification. | ||||
function onNotification(response) { | function onNotification(response) { | ||||
if (!response.pertinent) { | if (!response.pertinent) { | ||||
return; | return; | ||||
} | } | ||||
JX.Leader.broadcast(null, { | |||||
type: 'notification.individual', | |||||
data: response | |||||
}); | |||||
} | |||||
JX.Stratcom.listen('aphlict-notification-message', null, function(e) { | |||||
JX.Stratcom.invoke('notification-panel-update', null, {}); | JX.Stratcom.invoke('notification-panel-update', null, {}); | ||||
Not Done Inline ActionsThis would become JX.Stratcom.listen('aphlict-notification-message', ...) or whatever. epriestley: This would become `JX.Stratcom.listen('aphlict-notification-message', ...)` or whatever. | |||||
var response = e.getData(); | |||||
// Show the notification itself. | // Show the notification itself. | ||||
new JX.Notification() | new JX.Notification() | ||||
.setContent(JX.$H(response.content)) | .setContent(JX.$H(response.content)) | ||||
.show(); | .show(); | ||||
// If the notification affected an object on this page, show a | // If the notification affected an object on this page, show a | ||||
// permanent reload notification if we aren't already. | // permanent reload notification if we aren't already. | ||||
if ((response.primaryObjectPHID in config.pageObjects) && !showing_reload) { | if ((response.primaryObjectPHID in config.pageObjects) && !showing_reload) { | ||||
var reload = new JX.Notification() | var reload = new JX.Notification() | ||||
.setContent('Page updated, click to reload.') | .setContent('Page updated, click to reload.') | ||||
.alterClassName('jx-notification-alert', true) | .alterClassName('jx-notification-alert', true) | ||||
.setDuration(0); | .setDuration(0); | ||||
reload.listen('activate', function() { JX.$U().go(); }); | reload.listen('activate', function() { JX.$U().go(); }); | ||||
reload.show(); | reload.show(); | ||||
showing_reload = true; | showing_reload = true; | ||||
} | } | ||||
} | }); | ||||
var client = new JX.Aphlict( | var client = new JX.Aphlict( | ||||
config.websocketURI, | config.websocketURI, | ||||
config.subscriptions); | config.subscriptions); | ||||
client | client | ||||
.setHandler(onAphlictMessage) | .setHandler(onAphlictMessage) | ||||
.start(); | .start(); | ||||
}); | }); |
With the adjustments above, we could also make is_leader available in these listeners, which would simplify them a bit (they wouldn't need to use a callback here).