From 76b257f7b8ee8059ebbace84fb84bf5edde8d2ca Mon Sep 17 00:00:00 2001 From: barisusakli Date: Wed, 6 Aug 2014 12:39:14 -0400 Subject: [PATCH] optimized User.getUsers --- src/groups.js | 4 ++++ src/user.js | 52 +++++++++++++++++++++++++-------------------------- 2 files changed, 30 insertions(+), 26 deletions(-) diff --git a/src/groups.js b/src/groups.js index 13e9917520..acbc2158a5 100644 --- a/src/groups.js +++ b/src/groups.js @@ -156,6 +156,10 @@ db.isSetMember('group:' + groupName + ':members', uid, callback); }; + Groups.isMembers = function(uids, groupName, callback) { + db.isSetMembers('group:' + groupName + ':members', uids, callback); + }; + Groups.isMemberOfGroups = function(uid, groups, callback) { groups = groups.map(function(groupName) { return 'group:' + groupName + ':members'; diff --git a/src/user.js b/src/user.js index f20686fc7c..3a6bcb4607 100644 --- a/src/user.js +++ b/src/user.js @@ -239,36 +239,32 @@ var bcrypt = require('bcryptjs'), }; User.getUsers = function(uids, callback) { - function loadUserInfo(user, callback) { - if (!user) { - return callback(null, user); + async.parallel({ + userData: function(next) { + User.getMultipleUserFields(uids, ['uid', 'username', 'userslug', 'picture', 'status', 'banned', 'postcount', 'reputation'], next); + }, + isAdmin: function(next) { + User.isAdministrator(uids, next); + }, + isOnline: function(next) { + db.isSortedSetMembers('users:online', uids, next); } - - async.waterfall([ - function(next) { - User.isAdministrator(user.uid, next); - }, - function(isAdmin, next) { - user.status = !user.status ? 'online' : user.status; - user.administrator = isAdmin; - user.banned = parseInt(user.banned, 10) === 1; - db.isSortedSetMember('users:online', user.uid, next); - }, - function(isMember, next) { - if (!isMember) { - user.status = 'offline'; - } - next(null, user); - } - ], callback); - } - - User.getMultipleUserFields(uids, ['uid', 'username', 'userslug', 'picture', 'status', 'banned', 'postcount', 'reputation'], function(err, usersData) { + }, function(err, results) { if (err) { return callback(err); } - async.map(usersData, loadUserInfo, callback); + results.userData.forEach(function(user, index) { + if (!user) { + return; + } + user.status = !user.status ? 'online' : user.status; + user.status = !results.isOnline[index] ? 'offline' : user.status; + user.administrator = results.isAdmin[index]; + user.banned = parseInt(user.banned, 10) === 1; + }); + + callback(err, results.userData); }); }; @@ -410,7 +406,11 @@ var bcrypt = require('bcryptjs'), }; User.isAdministrator = function(uid, callback) { - groups.isMember(uid, 'administrators', callback); + if (Array.isArray(uid)) { + groups.isMembers(uid, 'administrators', callback); + } else { + groups.isMember(uid, 'administrators', callback); + } }; User.isOnline = function(uid, callback) {