Merge pull request #796 from torhve/resync

Periodic hotlist sync. Fixes #692
This commit is contained in:
Lorenz Hübschle-Schneider 2016-07-29 14:34:47 +02:00 committed by GitHub
commit afe03fd9ea
2 changed files with 43 additions and 21 deletions

View File

@ -151,21 +151,33 @@ weechat.factory('connection',
_requestHotlist().then(function(hotlist) {
handlers.handleHotlistInfo(hotlist);
if (successCallback) {
successCallback();
}
});
// Schedule hotlist syncing every so often so that this
// client will have unread counts (mostly) in sync with
// other clients or terminal usage directly.
setInterval(function() {
if ($rootScope.connected) {
_requestHotlist().then(function(hotlist) {
handlers.handleHotlistInfo(hotlist);
});
}
}, 60000); // Sync hotlist every 60 second
// Fetch weechat time format for displaying timestamps
fetchConfValue('weechat.look.buffer_time_format',
function() {
// Will set models.wconfig['weechat.look.buffer_time_format']
_parseWeechatTimeFormat();
});
// Will set models.wconfig['weechat.look.buffer_time_format']
});
_requestSync();
$log.info("Connected to relay");
$rootScope.connected = true;
if (successCallback) {
successCallback();
}
},
function() {
handleWrongPassword();

View File

@ -340,23 +340,33 @@ weechat.factory('handlers', ['$rootScope', '$log', 'models', 'plugins', 'notific
* Handle answers to hotlist request
*/
var handleHotlistInfo = function(message) {
if (message.objects.length === 0) {
return;
// Hotlist includes only buffers with unread counts so first we
// iterate all our buffers and resets the counts.
_.each(models.getBuffers(), function(buffer) {
buffer.unread = 0;
buffer.notification = 0;
});
if (message.objects.length > 0) {
var hotlist = message.objects[0].content;
hotlist.forEach(function(l) {
var buffer = models.getBuffer(l.buffer);
// 1 is message
buffer.unread = l.count[1];
// 2 is private
buffer.notification = l.count[2];
// 3 is highlight
buffer.notification = l.count[3];
/* Since there is unread messages, we can guess
* what the last read line is and update it accordingly
*/
var unreadSum = _.reduce(l.count, function(memo, num) { return memo + num; }, 0);
buffer.lastSeen = buffer.lines.length - 1 - unreadSum;
});
}
var hotlist = message.objects[0].content;
hotlist.forEach(function(l) {
var buffer = models.getBuffer(l.buffer);
// 1 is message
buffer.unread += l.count[1];
// 2 is private
buffer.notification += l.count[2];
// 3 is highlight
buffer.notification += l.count[3];
/* Since there is unread messages, we can guess
* what the last read line is and update it accordingly
*/
var unreadSum = _.reduce(l.count, function(memo, num) { return memo + num; }, 0);
buffer.lastSeen = buffer.lines.length - 1 - unreadSum;
// the unread badges in the bufferlist doesn't update if we don't do this
setTimeout(function() {
$rootScope.$apply();
$rootScope.$emit('notificationChanged');
});
};