Commit 4aa123d5 authored by JC Brand's avatar JC Brand

Add timestamps to resources

So that when a higher priority resource goes offline, we can fall back to the
right chat status if at the next priority level there are multiple resources.

https://github.com/jcbrand/converse.js/commit/789654d54e7a8c80b35934f526e05ee7802c790f#comments
parent 7cfd77b8
This diff is collapsed.
...@@ -859,8 +859,9 @@ ...@@ -859,8 +859,9 @@
var resources = this.get('resources'); var resources = this.get('resources');
if (!_.isObject(resources)) { resources = {}; } if (!_.isObject(resources)) { resources = {}; }
resources[resource] = { resources[resource] = {
'priority': _.isNaN(Number(priority)) ? 0 : Number(priority), 'priority': _.isNaN(parseInt(priority)) ? 0 : parseInt(priority),
'status': chat_status 'status': chat_status,
'timestamp': moment().format()
}; };
this.set({'resources': resources}); this.set({'resources': resources});
return resources; return resources;
...@@ -884,10 +885,17 @@ ...@@ -884,10 +885,17 @@
getHighestPriorityStatus: function () { getHighestPriorityStatus: function () {
/* Return the chat status assigned to the resource with the /* Return the chat status assigned to the resource with the
* highest priority. * highest priority.
*
* If multiple resources have the same priority, take the
* newest one.
*/ */
var resources = this.get('resources'); var resources = this.get('resources');
if (_.isObject(resources) && _.size(resources)) { if (_.isObject(resources) && _.size(resources)) {
var val = _.flow(_.values, _.partial(_.sortBy, _, 'priority'), _.reverse)(resources)[0]; var val = _.flow(
_.values,
_.partial(_.sortBy, _, ['priority', 'timestamp']),
_.reverse
)(resources)[0];
if (!_.isUndefined(val)) { if (!_.isUndefined(val)) {
return val.status; return val.status;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment