Skip to content

Node.js based forum software built for the modern web

make one socket call to load unread counts

barisusakli5e5cafafd0ff
diff --git a/public/src/client/footer.js b/public/src/client/footer.js
index 5e39ac142c..288123c8e1 100644
--- a/public/src/client/footer.js
+++ b/public/src/client/footer.js
@@ -7,21 +7,13 @@ define('forum/footer', ['notifications', 'chat', 'components', 'translator'], fu
Chat.prepareDOM();
translator.prepareDOM();
- function updateUnreadTopicCount(err, count) {
- if (err) {
- return console.warn('Error updating unread count', err);
- }
-
+ function updateUnreadTopicCount(count) {
$('#unread-count i')
.toggleClass('unread-count', count > 0)
.attr('data-content', count > 20 ? '20+' : count);
}
- function updateUnreadChatCount(err, count) {
- if (err) {
- return console.warn('Error updating unread count', err);
- }
-
+ function updateUnreadChatCount(count) {
components.get('chat/icon')
.toggleClass('unread-count', count > 0)
.attr('data-content', count > 20 ? '20+' : count);
@@ -62,11 +54,20 @@ define('forum/footer', ['notifications', 'chat', 'components', 'translator'], fu
socket.on('event:new_post', onNewPost);
}
- socket.on('event:unread.updateCount', updateUnreadTopicCount);
- socket.emit('user.getUnreadCount', updateUnreadTopicCount);
+ if (app.user.uid) {
+ socket.emit('user.getUnreadCounts', function(err, data) {
+ if (err) {
+ return app.alert(err.message);
+ }
+ updateUnreadTopicCount(data.unreadTopicCount);
+ updateUnreadChatCount(data.unreadChatCount);
+ Notifications.updateNotifCount(data.unreadNotificationCount);
+ });
+ }
+
+ socket.on('event:unread.updateCount', updateUnreadTopicCount);
socket.on('event:unread.updateChatCount', updateUnreadChatCount);
- socket.emit('user.getUnreadChatCount', updateUnreadChatCount);
initUnreadTopics();
});
diff --git a/public/src/modules/notifications.js b/public/src/modules/notifications.js
index 03f5e7b2d5..cb6a4ef04e 100644
--- a/public/src/modules/notifications.js
+++ b/public/src/modules/notifications.js
@@ -64,14 +64,6 @@ define('notifications', ['sounds', 'translator', 'components'], function(sound,
Notifications.updateNotifCount(count);
}
- socket.emit('notifications.getCount', function(err, count) {
- if (!err) {
- Notifications.updateNotifCount(count);
- } else {
- Notifications.updateNotifCount(0);
- }
- });
-
socket.on('event:new_notification', function(notifData) {
app.alert({
alert_id: 'new_notif',
diff --git a/src/messaging.js b/src/messaging.js
index 99b01fec2f..7609ad24f9 100644
--- a/src/messaging.js
+++ b/src/messaging.js
@@ -314,7 +314,7 @@ var db = require('./database'),
if (err) {
return;
}
- sockets.in('uid_' + uid).emit('event:unread.updateChatCount', null, unreadCount);
+ sockets.in('uid_' + uid).emit('event:unread.updateChatCount', unreadCount);
});
};
diff --git a/src/socket.io/user.js b/src/socket.io/user.js
index ad7e4d3b52..08b5f11d0a 100644
--- a/src/socket.io/user.js
+++ b/src/socket.io/user.js
@@ -207,6 +207,17 @@ SocketUser.getUnreadChatCount = function(socket, data, callback) {
messaging.getUnreadCount(socket.uid, callback);
};
+SocketUser.getUnreadCounts = function(socket, data, callback) {
+ if (!socket.uid) {
+ return callback(null, {});
+ }
+ async.parallel({
+ unreadTopicCount: async.apply(topics.getTotalUnread, socket.uid),
+ unreadChatCount: async.apply(messaging.getUnreadCount, socket.uid),
+ unreadNotificationCount: async.apply(user.notifications.getUnreadCount, socket.uid)
+ }, callback);
+};
+
SocketUser.loadMore = function(socket, data, callback) {
if (!data || !data.set || parseInt(data.after, 10) < 0) {
return callback(new Error('[[error:invalid-data]]'));
diff --git a/src/topics/unread.js b/src/topics/unread.js
index d70915a7b7..294ac3cf17 100644
--- a/src/topics/unread.js
+++ b/src/topics/unread.js
@@ -142,7 +142,7 @@ module.exports = function(Topics) {
if (err) {
return callback(err);
}
- require('../socket.io').in('uid_' + uid).emit('event:unread.updateCount', null, count);
+ require('../socket.io').in('uid_' + uid).emit('event:unread.updateCount', count);
callback();
});
};