optimized User.getUsers

v1.18.x
barisusakli 11 years ago
parent 59d4d2880f
commit 76b257f7b8

@ -156,6 +156,10 @@
db.isSetMember('group:' + groupName + ':members', uid, callback); 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.isMemberOfGroups = function(uid, groups, callback) {
groups = groups.map(function(groupName) { groups = groups.map(function(groupName) {
return 'group:' + groupName + ':members'; return 'group:' + groupName + ':members';

@ -239,36 +239,32 @@ var bcrypt = require('bcryptjs'),
}; };
User.getUsers = function(uids, callback) { User.getUsers = function(uids, callback) {
function loadUserInfo(user, callback) { async.parallel({
if (!user) { userData: function(next) {
return callback(null, user); 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);
} }
}, function(err, results) {
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) {
if (err) { if (err) {
return callback(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) { 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) { User.isOnline = function(uid, callback) {

Loading…
Cancel
Save