diff --git a/public/src/forum/footer.js b/public/src/forum/footer.js index dc89b15f01..34e61d8cf0 100644 --- a/public/src/forum/footer.js +++ b/public/src/forum/footer.js @@ -142,7 +142,7 @@ } }); - socket.emit('api:notifications.getCount', function(err, count) { + var updateNotifCount = function(count) { // Update notification icon, if necessary if (count > 0) { notifIcon.toggleClass('active', true); @@ -153,11 +153,15 @@ // Update the favicon + saved local count Tinycon.setBubble(count); localStorage.setItem('notifications:count', count); - }); + }; - if (localStorage.getItem('notifications:count') !== null) { - Tinycon.setBubble(parseInt(localStorage.getItem('notifications:count'), 10)); - } + socket.emit('api:notifications.getCount', function(err, count) { + if (!err) { + updateNotifCount(count); + } else { + updateNotifCount(0); + } + }); socket.on('event:new_notification', function() { notifIcon.toggleClass('active', true); @@ -176,8 +180,10 @@ // Update the favicon + local storage var savedCount = parseInt(localStorage.getItem('notifications:count'),10) || 0; - localStorage.setItem('notifications:count', savedCount+1); - Tinycon.setBubble(savedCount+1); + updateNotifCount(savedCount+1); + }); + socket.on('event:notifications.updateCount', function(count) { + updateNotifCount(count); }); // Chats Dropdown diff --git a/src/notifications.js b/src/notifications.js index 030478701b..722a711f47 100644 --- a/src/notifications.js +++ b/src/notifications.js @@ -147,11 +147,18 @@ var async = require('async'), Notifications.mark_read = function(nid, uid, callback) { if (parseInt(uid, 10) > 0) { Notifications.get(nid, uid, function(notif_data) { - db.sortedSetRemove('uid:' + uid + ':notifications:unread', nid); - db.sortedSetAdd('uid:' + uid + ':notifications:read', notif_data.datetime, nid); - if (callback) { - callback(); - } + async.parallel([ + function(next) { + db.sortedSetRemove('uid:' + uid + ':notifications:unread', nid, next); + }, + function(next) { + db.sortedSetAdd('uid:' + uid + ':notifications:read', notif_data.datetime, nid, next); + } + ], function(err) { + if (callback) { + callback(); + } + }); }); } }; diff --git a/src/routes/debug.js b/src/routes/debug.js index 14f87fa8a7..da831fb8b8 100644 --- a/src/routes/debug.js +++ b/src/routes/debug.js @@ -79,6 +79,10 @@ var DebugRoute = function(app) { }); }); + app.get('/test', function(req, res) { + user.pushNotifCount(2); + res.send(); + }); }); }; diff --git a/src/topics.js b/src/topics.js index 8e5f4a3f43..e21ac7d514 100644 --- a/src/topics.js +++ b/src/topics.js @@ -720,7 +720,7 @@ var async = require('async'), user.notifications.getUnreadByUniqueId(uid, 'topic:' + tid, function(err, nids) { notifications.mark_read_multiple(nids, uid, function() { - + user.pushNotifCount(uid); }); }); } diff --git a/src/user.js b/src/user.js index b45bf30275..7291971eb3 100644 --- a/src/user.js +++ b/src/user.js @@ -14,8 +14,9 @@ var bcrypt = require('bcrypt'), emailjsServer = emailjs.server.connect(meta.config['email:smtp:host'] || '127.0.0.1'), groups = require('./groups'), notifications = require('./notifications'), - topics = require('./topics'); + topics = require('./topics'), + websockets = require('./websockets'); (function(User) { '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 = { exists: function(socket, email, callback) { User.getUidByEmail(email, function(err, exists) {