pushNotifCount method for real-time updating of notification bell + favicon for multiple tabs (closes #219)

v1.18.x
Julian Lam 11 years ago
parent 538356846d
commit c313c4501b

@ -142,7 +142,7 @@
} }
}); });
socket.emit('api:notifications.getCount', function(err, count) { var updateNotifCount = function(count) {
// Update notification icon, if necessary // Update notification icon, if necessary
if (count > 0) { if (count > 0) {
notifIcon.toggleClass('active', true); notifIcon.toggleClass('active', true);
@ -153,11 +153,15 @@
// Update the favicon + saved local count // Update the favicon + saved local count
Tinycon.setBubble(count); Tinycon.setBubble(count);
localStorage.setItem('notifications:count', count); localStorage.setItem('notifications:count', count);
}); };
if (localStorage.getItem('notifications:count') !== null) { socket.emit('api:notifications.getCount', function(err, count) {
Tinycon.setBubble(parseInt(localStorage.getItem('notifications:count'), 10)); if (!err) {
} updateNotifCount(count);
} else {
updateNotifCount(0);
}
});
socket.on('event:new_notification', function() { socket.on('event:new_notification', function() {
notifIcon.toggleClass('active', true); notifIcon.toggleClass('active', true);
@ -176,8 +180,10 @@
// Update the favicon + local storage // Update the favicon + local storage
var savedCount = parseInt(localStorage.getItem('notifications:count'),10) || 0; var savedCount = parseInt(localStorage.getItem('notifications:count'),10) || 0;
localStorage.setItem('notifications:count', savedCount+1); updateNotifCount(savedCount+1);
Tinycon.setBubble(savedCount+1); });
socket.on('event:notifications.updateCount', function(count) {
updateNotifCount(count);
}); });
// Chats Dropdown // Chats Dropdown

@ -147,11 +147,18 @@ var async = require('async'),
Notifications.mark_read = function(nid, uid, callback) { Notifications.mark_read = function(nid, uid, callback) {
if (parseInt(uid, 10) > 0) { if (parseInt(uid, 10) > 0) {
Notifications.get(nid, uid, function(notif_data) { Notifications.get(nid, uid, function(notif_data) {
db.sortedSetRemove('uid:' + uid + ':notifications:unread', nid); async.parallel([
db.sortedSetAdd('uid:' + uid + ':notifications:read', notif_data.datetime, nid); function(next) {
if (callback) { db.sortedSetRemove('uid:' + uid + ':notifications:unread', nid, next);
callback(); },
} function(next) {
db.sortedSetAdd('uid:' + uid + ':notifications:read', notif_data.datetime, nid, next);
}
], function(err) {
if (callback) {
callback();
}
});
}); });
} }
}; };

@ -79,6 +79,10 @@ var DebugRoute = function(app) {
}); });
}); });
app.get('/test', function(req, res) {
user.pushNotifCount(2);
res.send();
});
}); });
}; };

@ -720,7 +720,7 @@ var async = require('async'),
user.notifications.getUnreadByUniqueId(uid, 'topic:' + tid, function(err, nids) { user.notifications.getUnreadByUniqueId(uid, 'topic:' + tid, function(err, nids) {
notifications.mark_read_multiple(nids, uid, function() { notifications.mark_read_multiple(nids, uid, function() {
user.pushNotifCount(uid);
}); });
}); });
} }

@ -14,8 +14,9 @@ var bcrypt = require('bcrypt'),
emailjsServer = emailjs.server.connect(meta.config['email:smtp:host'] || '127.0.0.1'), emailjsServer = emailjs.server.connect(meta.config['email:smtp:host'] || '127.0.0.1'),
groups = require('./groups'), groups = require('./groups'),
notifications = require('./notifications'), notifications = require('./notifications'),
topics = require('./topics'); topics = require('./topics'),
websockets = require('./websockets');
(function(User) { (function(User) {
'use strict'; 'use strict';
@ -864,6 +865,17 @@ var bcrypt = require('bcrypt'),
} }
}; };
User.pushNotifCount = function(uid) {
User.notifications.getUnreadCount(uid, function(err, count) {
console.log('unread count is', count);
if (!err) {
websockets.in('uid_' + uid).emit('event:notifications.updateCount', count);
} else {
winston.warn('[User.pushNotifCount] Count not retrieve unread notifications count to push to uid ' + uid + '\'s client(s)');
}
});
};
User.email = { User.email = {
exists: function(socket, email, callback) { exists: function(socket, email, callback) {
User.getUidByEmail(email, function(err, exists) { User.getUidByEmail(email, function(err, exists) {

Loading…
Cancel
Save