From 8f4848cc69838d9dfed8347b8da402f91399ec87 Mon Sep 17 00:00:00 2001 From: Baris Soner Usakli Date: Sat, 6 Jul 2013 16:37:25 -0400 Subject: [PATCH] userList function for active users, moved it to server side too --- public/src/forum/topic.js | 21 +-------------------- src/websockets.js | 30 ++++++++++++++++++++---------- 2 files changed, 21 insertions(+), 30 deletions(-) diff --git a/public/src/forum/topic.js b/public/src/forum/topic.js index 2010135466..7f74eadb87 100644 --- a/public/src/forum/topic.js +++ b/public/src/forum/topic.js @@ -311,28 +311,9 @@ socket.on('api:get_users_in_room', function(data) { - - var anonymousCount = data.anonymousCount, - users = data.users, - usernames = [], - usercount = users.length; - - for (var i = 0, ii=users.length; i' + users[i].username + ''; - } - - // headexplosion.gif for fun, to see if I could do this in one line of code. feel free to refactor haha - var active = - ((usercount === 1) ? usernames[0] : '') - + ((usercount === 2 && anonymous === 0) ? usernames[0] + ' and ' + usernames[1] : '') - + ((usercount > 2 && anonymous === 0) ? usernames.join(', ').replace(/,([^,]*)$/, ", and$1") : '') - + (usercount > 1 && anonymous > 0 ? usernames.join(', ') : '') - + ((anonymousCount > 0) ? (usercount > 0 ? ' and ': '') + anonymousCount + ' guest' + (anonymousCount > 1 ? 's are': ' is') : '') - + (anonymousCount === 0 ? (usercount > 1 ? ' are' : ' is') : '') + ' browsing this thread'; - var activeEl = $('#thread_active_users'); if(activeEl.length) - activeEl.html(active); + activeEl.html(data); }); socket.on('event:rep_up', function(data) { diff --git a/src/websockets.js b/src/websockets.js index 5f9b0231c6..70d132c686 100644 --- a/src/websockets.js +++ b/src/websockets.js @@ -6,6 +6,7 @@ var SocketIO = require('socket.io').listen(global.server, { log:false }), posts = require('./posts.js'), favourites = require('./favourites.js'), utils = require('../public/src/utils.js'), + util = require('util'), topics = require('./topics.js'), categories = require('./categories.js'), notifications = require('./notifications.js'), @@ -103,19 +104,28 @@ var SocketIO = require('socket.io').listen(global.server, { log:false }), var uids = Object.keys(rooms[data.enter] || {}); var anonymousCount = io.sockets.clients(data.enter).length - uids.length; - if (uids.length === 0) { - io.sockets.in(data.enter).emit('api:get_users_in_room', { - users: [], - anonymousCount: anonymousCount - }); + function userList(users, anonymousCount, userCount) { + var usernames = []; + + for (var i = 0, ii=users.length; i' + users[i].username + ''; + } + + var joiner = anonymousCount + userCount == 1 ? 'is' : 'are', + userList = anonymousCount > 0 ? usernames.concat(util.format('%d guest%s', anonymousCount, anonymousCount > 1 ? 's' : '')) : usernames, + lastUser = userList.length > 1 ? ' and ' + userList.pop() : ''; + + return util.format('%s%s %s browsing this thread', userList.join(', '), lastUser, joiner); } - user.getMultipleUserFields(uids, ['username', 'userslug'], function(users) { - io.sockets.in(data.enter).emit('api:get_users_in_room', { - users: users, - anonymousCount: anonymousCount + + if (uids.length === 0) { + io.sockets.in(data.enter).emit('api:get_users_in_room', userList([], anonymousCount, 0)); + } else { + user.getMultipleUserFields(uids, ['username', 'userslug'], function(users) { + io.sockets.in(data.enter).emit('api:get_users_in_room', userList(users, anonymousCount, users.length)); }); - }); + } if (data.enter != 'admin') io.sockets.in('admin').emit('api:get_all_rooms', io.sockets.manager.rooms);