userList function for active users, moved it to server side too

v1.18.x
Baris Soner Usakli 12 years ago
parent 12f78f5d56
commit 8f4848cc69

@ -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<ii; ++i) {
usernames[i] = '<strong>' + '<a href="/users/'+users[i].userslug+'">' + users[i].username + '</a></strong>';
}
// 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) {

@ -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<ii; ++i) {
usernames[i] = '<strong>' + '<a href="/users/'+users[i].userslug+'">' + users[i].username + '</a></strong>';
}
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);

Loading…
Cancel
Save