mirror of
				https://github.com/NodeBB/NodeBB.git
				synced 2025-10-30 10:35:55 +01:00 
			
		
		
		
	make one socket call to load unread counts
This commit is contained in:
		| @@ -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(); | ||||
| }); | ||||
|   | ||||
| @@ -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', | ||||
|   | ||||
| @@ -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); | ||||
| 		}); | ||||
| 	}; | ||||
|  | ||||
|   | ||||
| @@ -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]]')); | ||||
|   | ||||
| @@ -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(); | ||||
| 		}); | ||||
| 	}; | ||||
|   | ||||
		Reference in New Issue
	
	Block a user