mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-01 11:35:55 +01:00
pushNotifCount method for real-time updating of notification bell + favicon for multiple tabs (closes #219)
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
@@ -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) {
|
||||
notifications.mark_read_multiple(nids, uid, function() {
|
||||
|
||||
user.pushNotifCount(uid);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
14
src/user.js
14
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) {
|
||||
|
||||
Reference in New Issue
Block a user