mirror of
				https://github.com/NodeBB/NodeBB.git
				synced 2025-10-31 02:55:58 +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(); | 	Chat.prepareDOM(); | ||||||
| 	translator.prepareDOM(); | 	translator.prepareDOM(); | ||||||
|  |  | ||||||
| 	function updateUnreadTopicCount(err, count) { | 	function updateUnreadTopicCount(count) { | ||||||
| 		if (err) { |  | ||||||
| 			return console.warn('Error updating unread count', err); |  | ||||||
| 		} |  | ||||||
|  |  | ||||||
| 		$('#unread-count i') | 		$('#unread-count i') | ||||||
| 			.toggleClass('unread-count', count > 0) | 			.toggleClass('unread-count', count > 0) | ||||||
| 			.attr('data-content', count > 20 ? '20+' : count); | 			.attr('data-content', count > 20 ? '20+' : count); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	function updateUnreadChatCount(err, count) { | 	function updateUnreadChatCount(count) { | ||||||
| 		if (err) { |  | ||||||
| 			return console.warn('Error updating unread count', err); |  | ||||||
| 		} |  | ||||||
|  |  | ||||||
| 		components.get('chat/icon') | 		components.get('chat/icon') | ||||||
| 			.toggleClass('unread-count', count > 0) | 			.toggleClass('unread-count', count > 0) | ||||||
| 			.attr('data-content', count > 20 ? '20+' : count); | 			.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:new_post', onNewPost); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	socket.on('event:unread.updateCount', updateUnreadTopicCount); | 	if (app.user.uid) { | ||||||
| 	socket.emit('user.getUnreadCount', updateUnreadTopicCount); | 		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.on('event:unread.updateChatCount', updateUnreadChatCount); | ||||||
| 	socket.emit('user.getUnreadChatCount', updateUnreadChatCount); |  | ||||||
|  |  | ||||||
| 	initUnreadTopics(); | 	initUnreadTopics(); | ||||||
| }); | }); | ||||||
|   | |||||||
| @@ -64,14 +64,6 @@ define('notifications', ['sounds', 'translator', 'components'], function(sound, | |||||||
| 			Notifications.updateNotifCount(count); | 			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) { | 		socket.on('event:new_notification', function(notifData) { | ||||||
| 			app.alert({ | 			app.alert({ | ||||||
| 				alert_id: 'new_notif', | 				alert_id: 'new_notif', | ||||||
|   | |||||||
| @@ -314,7 +314,7 @@ var db = require('./database'), | |||||||
| 			if (err) { | 			if (err) { | ||||||
| 				return; | 				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); | 	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) { | SocketUser.loadMore = function(socket, data, callback) { | ||||||
| 	if (!data || !data.set || parseInt(data.after, 10) < 0) { | 	if (!data || !data.set || parseInt(data.after, 10) < 0) { | ||||||
| 		return callback(new Error('[[error:invalid-data]]')); | 		return callback(new Error('[[error:invalid-data]]')); | ||||||
|   | |||||||
| @@ -142,7 +142,7 @@ module.exports = function(Topics) { | |||||||
| 			if (err) { | 			if (err) { | ||||||
| 				return callback(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(); | 			callback(); | ||||||
| 		}); | 		}); | ||||||
| 	}; | 	}; | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user