From ecf3ffa17051bb29a27c307ed3e2b7d618e949e2 Mon Sep 17 00:00:00 2001 From: psychobunny Date: Mon, 6 May 2013 17:09:08 +0000 Subject: [PATCH] fixed anon bug in users browsing this thread code, simplified the logic as well, fixed language --- public/templates/topic.tpl | 2 +- src/websockets.js | 30 +++++++++++++----------------- 2 files changed, 14 insertions(+), 18 deletions(-) diff --git a/public/templates/topic.tpl b/public/templates/topic.tpl index 29628a6641..02935ba553 100644 --- a/public/templates/topic.tpl +++ b/public/templates/topic.tpl @@ -61,7 +61,7 @@ socket.on('api:get_users_in_room', function(users) { + ((usercount === 2 && anonymous === 0) ? usernames[0] + ' and ' + usernames[1] : '') + ((usercount > 2 && anonymous === 0) ? usernames.join(', ').replace(/,([^,]*)$/, ", and$1") : '') + (usercount > 1 && anonymous > 0 ? usernames.join(', ') : '') - + ((anonymous > 0) ? (usercount > 0 ? ' and ': '') + anonymous + ' guest' + (anonymous.length > 1 ? 's are': ' is') : '') + + ((anonymous > 0) ? (usercount > 0 ? ' and ': '') + anonymous + ' guest' + (anonymous > 1 ? 's are': ' is') : '') + (anonymous === 0 ? (usercount > 1 ? ' are' : ' is') : '') + ' browsing this thread'; document.getElementById('thread_active_users').innerHTML = active; diff --git a/src/websockets.js b/src/websockets.js index e7dc1276b5..50373a7347 100644 --- a/src/websockets.js +++ b/src/websockets.js @@ -6,10 +6,7 @@ var SocketIO = require('socket.io').listen(global.server,{log:false}), (function(io) { var modules = null, users = {}, - rooms = { - 'users' : {}, - 'anonymous' : {} - }; + rooms = {} global.io = io; module.exports.init = function() { @@ -77,34 +74,33 @@ var SocketIO = require('socket.io').listen(global.server,{log:false}), if (data.leave !== null) socket.leave (data.leave); socket.join(data.enter); - rooms.users[data.enter] = rooms.users[data.enter] || {}; + rooms[data.enter] = rooms[data.enter] || {}; if (uid) { - rooms.users[data.enter][uid] = true; - if (rooms.users[data.leave]) { - delete rooms.users[data.leave][uid]; + rooms[data.enter][uid] = true; + if (rooms[data.leave]) { + delete rooms[data.leave][uid]; } - } else { - rooms.anonymous[data.enter] = (rooms.anonymous[data.enter] || 0) + 1; - rooms.anonymous[data.leave] = rooms.anonymous[data.leave] || 0; } - var uids = Object.keys(rooms.users[data.enter] || {}); + var uids = Object.keys(rooms[data.enter] || {}); + var anonymous = io.sockets.clients(data.enter).length - uids.length; if (uids.length == 0) { - socket.emit('api:get_users_in_room', { + io.sockets.in(data.enter).emit('api:get_users_in_room', { usernames: [], uids: [], - anonymous: rooms.anonymous[data.enter] || 0 + anonymous: anonymous }); } + + modules.user.get_usernames_by_uids(uids, function(usernames) { - socket.emit('api:get_users_in_room', { + io.sockets.in(data.enter).emit('api:get_users_in_room', { usernames: usernames, uids: uids, - anonymous: rooms.anonymous[data.enter] || 0 + anonymous: anonymous }); }); - });