optimize getUserGroups to work with multiple user ids

v1.18.x
barisusakli 11 years ago
parent 5b681678b1
commit b8279dc737

@ -438,7 +438,7 @@
}); });
}; };
Groups.getUserGroups = function(uid, callback) { Groups.getUserGroups = function(uids, callback) {
var ignoredGroups = ['registered-users']; var ignoredGroups = ['registered-users'];
db.getSetMembers('groups', function(err, groupNames) { db.getSetMembers('groups', function(err, groupNames) {
@ -466,19 +466,22 @@
return 'group:' + group.name + ':members'; return 'group:' + group.name + ':members';
}); });
async.map(uids, function(uid, next) {
db.isMemberOfSets(groupSets, uid, function(err, isMembers) { db.isMemberOfSets(groupSets, uid, function(err, isMembers) {
if (err) { if (err) {
return callback(err); return next(err);
} }
for(var i=isMembers.length - 1; i>=0; --i) { var memberOf = [];
if (!isMembers[i]) { isMembers.forEach(function(isMember, index) {
groupData.splice(i, 1); if (isMember) {
} memberOf.push(groupData[index]);
} }
});
callback(null, groupData); next(null, memberOf);
}); });
}, callback);
}); });
}); });
}; };

@ -222,21 +222,31 @@ var async = require('async'),
}; };
Posts.getUserInfoForPosts = function(uids, callback) { Posts.getUserInfoForPosts = function(uids, callback) {
user.getMultipleUserFields(uids, ['uid', 'username', 'userslug', 'reputation', 'postcount', 'picture', 'signature', 'banned'], function(err, userData) { async.parallel({
groups: function(next) {
groups.getUserGroups(uids, next);
},
userData: function(next) {
user.getMultipleUserFields(uids, ['uid', 'username', 'userslug', 'reputation', 'postcount', 'picture', 'signature', 'banned'], next);
}
}, function(err, results) {
if (err) { if (err) {
return callback(err); return callback(err);
} }
var userData = results.userData;
for(var i=0; i<userData.length; ++i) {
userData[i].groups = results.groups[i];
}
async.map(userData, function(userData, next) { async.map(userData, function(userData, next) {
var userInfo = { userData.uid = userData.uid || 0;
uid: userData.uid || 0, userData.username = userData.username || '[[global:guest]]';
username: userData.username || '[[global:guest]]', userData.userslug = userData.userslug || '';
userslug: userData.userslug || '', userData.reputation = userData.reputation || 0;
reputation: userData.reputation || 0, userData.postcount = userData.postcount || 0;
postcount: userData.postcount || 0, userData.banned = parseInt(userData.banned, 10) === 1;
banned: parseInt(userData.banned, 10) === 1, userData.picture = userData.picture || user.createGravatarURLFromEmail('');
picture: userData.picture || user.createGravatarURLFromEmail('')
};
async.parallel({ async.parallel({
signature: function(next) { signature: function(next) {
@ -247,18 +257,14 @@ var async = require('async'),
}, },
customProfileInfo: function(next) { customProfileInfo: function(next) {
plugins.fireHook('filter:posts.custom_profile_info', {profile: [], uid: userData.uid}, next); plugins.fireHook('filter:posts.custom_profile_info', {profile: [], uid: userData.uid}, next);
},
groups: function(next) {
groups.getUserGroups(userData.uid, next);
} }
}, function(err, results) { }, function(err, results) {
if (err) { if (err) {
return next(err); return next(err);
} }
userInfo.signature = results.signature; userData.signature = results.signature;
userInfo.custom_profile_info = results.customProfileInfo.profile; userData.custom_profile_info = results.customProfileInfo.profile;
userInfo.groups = results.groups; next(null, userData);
next(null, userInfo);
}); });
}, callback); }, callback);
}); });

Loading…
Cancel
Save