async.each in user.js

v1.18.x
Baris Soner Usakli 12 years ago
parent c47db0e908
commit f2d7f856aa

@ -310,14 +310,15 @@
]); ]);
socket.on('api:get_users_in_room', function(users) { socket.on('api:get_users_in_room', function(data) {
var anonymous = users.anonymous,
usernames = users.usernames, var anonymousCount = data.anonymousCount,
userslugs = users.userslugs, users = data.users,
usercount = usernames.length; usernames = [],
usercount = users.length;
for (var i = 0, ii=usercount; i<ii; i++) {
usernames[i] = '<strong>' + '<a href="/users/'+userslugs[i]+'">' + usernames[i] + '</a></strong>'; 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 // headexplosion.gif for fun, to see if I could do this in one line of code. feel free to refactor haha
@ -326,8 +327,8 @@
+ ((usercount === 2 && anonymous === 0) ? usernames[0] + ' and ' + usernames[1] : '') + ((usercount === 2 && anonymous === 0) ? usernames[0] + ' and ' + usernames[1] : '')
+ ((usercount > 2 && anonymous === 0) ? usernames.join(', ').replace(/,([^,]*)$/, ", and$1") : '') + ((usercount > 2 && anonymous === 0) ? usernames.join(', ').replace(/,([^,]*)$/, ", and$1") : '')
+ (usercount > 1 && anonymous > 0 ? usernames.join(', ') : '') + (usercount > 1 && anonymous > 0 ? usernames.join(', ') : '')
+ ((anonymous > 0) ? (usercount > 0 ? ' and ': '') + anonymous + ' guest' + (anonymous > 1 ? 's are': ' is') : '') + ((anonymousCount > 0) ? (usercount > 0 ? ' and ': '') + anonymousCount + ' guest' + (anonymousCount > 1 ? 's are': ' is') : '')
+ (anonymous === 0 ? (usercount > 1 ? ' are' : ' is') : '') + ' browsing this thread'; + (anonymousCount === 0 ? (usercount > 1 ? ' are' : ' is') : '') + ' browsing this thread';
var activeEl = $('#thread_active_users'); var activeEl = $('#thread_active_users');
if(activeEl.length) if(activeEl.length)

@ -57,11 +57,7 @@ var RDB = require('./redis.js'),
function getActiveUsers(next) { function getActiveUsers(next) {
user.getMultipleUserFields(active_users, ['username', 'userslug'], function(users) { user.getMultipleUserFields(active_users, ['username', 'userslug'], function(users) {
var activeUserData = []; next(null, users);
for(var uid in users) {
activeUserData.push(users[uid]);
}
next(null, activeUserData);
}); });
} }
@ -206,15 +202,10 @@ var RDB = require('./redis.js'),
Categories.getModerators = function(cid, callback) { Categories.getModerators = function(cid, callback) {
RDB.smembers('cid:' + cid + ':moderators', function(err, mods) { RDB.smembers('cid:' + cid + ':moderators', function(err, mods) {
if (mods.length === 0) return callback([]); if (mods.length === 0)
return callback([]);
user.getMultipleUserFields(mods, ['username'], function(details) { user.getMultipleUserFields(mods, ['username'], function(moderators) {
var moderators = [];
for(u in details) {
if (details.hasOwnProperty(u)) {
moderators.push({ username: details[u].username });
}
}
callback(moderators); callback(moderators);
}); });
}); });

@ -40,21 +40,27 @@ var utils = require('./../public/src/utils.js'),
return; return;
} }
var data = {}, var returnData = [];
loaded = 0;
uuids = uids.filter(function(value, index, self) { uuids = uids.filter(function(value, index, self) {
return self.indexOf(value) === index; return self.indexOf(value) === index;
}); });
for (var i=0, ii=uuids.length; i<ii; i++) { function iterator(uid, callback) {
(function(user_id) { User.getUserFields(uid, fields, function(userData) {
User.getUserFields(user_id, fields, function(user_data) { returnData.push(userData);
data[user_id] = user_data; callback(null);
loaded ++; });
if (loaded == uuids.length) callback(data);
});
}(uuids[i]));
} }
async.each(uuids, iterator, function(err) {
if(!err) {
callback(returnData);
} else {
console.log(err);
callback(null);
}
});
} }
User.getUserData = function(uid, callback) { User.getUserData = function(uid, callback) {
@ -411,23 +417,25 @@ var utils = require('./../public/src/utils.js'),
}); });
} }
User.getDataForUsers = function(userIds, callback) { User.getDataForUsers = function(uids, callback) {
var returnData = []; var returnData = [];
if(!userIds || userIds.length === 0) { if(!uids || !Array.isArray(uids) || uids.length === 0) {
callback(returnData); callback(returnData);
return; return;
} }
for(var i = 0, ii = userIds.length; i < ii; ++i) { function iterator(uids, callback) {
User.getUserData(userIds[i], function(userData) { User.getUserData(uid, function(userData) {
returnData.push(userData); returnData.push(userData);
if(returnData.length == userIds.length) { callback(null);
callback(returnData);
}
}); });
} }
async.each(uids, iterator, function(err) {
callback(returnData);
});
} }
User.sendPostNotificationToFollowers = function(uid, tid, pid) { User.sendPostNotificationToFollowers = function(uid, tid, pid) {
@ -455,13 +463,9 @@ var utils = require('./../public/src/utils.js'),
}); });
} }
// @todo check why this function checks for a callback - if there's no callback,
// it effectively does nothing
User.exists = function(userslug, callback) { User.exists = function(userslug, callback) {
User.get_uid_by_userslug(userslug, function(exists) { User.get_uid_by_userslug(userslug, function(exists) {
if (callback) { callback(!!exists);
callback(!!exists);
}
}); });
}; };

@ -101,28 +101,20 @@ var SocketIO = require('socket.io').listen(global.server, { log:false }),
} }
var uids = Object.keys(rooms[data.enter] || {}); var uids = Object.keys(rooms[data.enter] || {});
var anonymous = io.sockets.clients(data.enter).length - uids.length; var anonymousCount = io.sockets.clients(data.enter).length - uids.length;
if (uids.length == 0) { if (uids.length === 0) {
io.sockets.in(data.enter).emit('api:get_users_in_room', { io.sockets.in(data.enter).emit('api:get_users_in_room', {
usernames: [], users: [],
uids: [], anonymousCount: anonymousCount
anonymous: anonymous
}); });
} }
user.getMultipleUserFields(uids, ['username', 'userslug'], function(users) {
user.get_usernames_by_uids(uids, function(usernames) { io.sockets.in(data.enter).emit('api:get_users_in_room', {
user.get_userslugs_by_uids(uids, function(userslugs) { users: users,
anonymousCount: anonymousCount
io.sockets.in(data.enter).emit('api:get_users_in_room', {
usernames: usernames,
userslugs: userslugs,
uids: uids,
anonymous: anonymous
});
}); });
}); });
if (data.enter != 'admin') io.sockets.in('admin').emit('api:get_all_rooms', io.sockets.manager.rooms); if (data.enter != 'admin') io.sockets.in('admin').emit('api:get_all_rooms', io.sockets.manager.rooms);

Loading…
Cancel
Save