Skip to content

Node.js based forum software built for the modern web

optimize getUserGroups to work with multiple user ids

barisusaklib8279dc7377e
diff --git a/src/groups.js b/src/groups.js
index acbc2158a5..b47b069b8a 100644
--- a/src/groups.js
+++ b/src/groups.js
@@ -438,7 +438,7 @@
});
};
- Groups.getUserGroups = function(uid, callback) {
+ Groups.getUserGroups = function(uids, callback) {
var ignoredGroups = ['registered-users'];
db.getSetMembers('groups', function(err, groupNames) {
@@ -466,19 +466,22 @@
return 'group:' + group.name + ':members';
});
- db.isMemberOfSets(groupSets, uid, function(err, isMembers) {
- if (err) {
- return callback(err);
- }
-
- for(var i=isMembers.length - 1; i>=0; --i) {
- if (!isMembers[i]) {
- groupData.splice(i, 1);
+ async.map(uids, function(uid, next) {
+ db.isMemberOfSets(groupSets, uid, function(err, isMembers) {
+ if (err) {
+ return next(err);
}
- }
- callback(null, groupData);
- });
+ var memberOf = [];
+ isMembers.forEach(function(isMember, index) {
+ if (isMember) {
+ memberOf.push(groupData[index]);
+ }
+ });
+
+ next(null, memberOf);
+ });
+ }, callback);
});
});
};
diff --git a/src/posts.js b/src/posts.js
index 3ebb14d859..7c7d5ff849 100644
--- a/src/posts.js
+++ b/src/posts.js
@@ -222,21 +222,31 @@ var async = require('async'),
};
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) {
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) {
- var userInfo = {
- uid: userData.uid || 0,
- username: userData.username || '[[global:guest]]',
- userslug: userData.userslug || '',
- reputation: userData.reputation || 0,
- postcount: userData.postcount || 0,
- banned: parseInt(userData.banned, 10) === 1,
- picture: userData.picture || user.createGravatarURLFromEmail('')
- };
+ userData.uid = userData.uid || 0;
+ userData.username = userData.username || '[[global:guest]]';
+ userData.userslug = userData.userslug || '';
+ userData.reputation = userData.reputation || 0;
+ userData.postcount = userData.postcount || 0;
+ userData.banned = parseInt(userData.banned, 10) === 1;
+ userData.picture = userData.picture || user.createGravatarURLFromEmail('');
async.parallel({
signature: function(next) {
@@ -247,18 +257,14 @@ var async = require('async'),
},
customProfileInfo: function(next) {
plugins.fireHook('filter:posts.custom_profile_info', {profile: [], uid: userData.uid}, next);
- },
- groups: function(next) {
- groups.getUserGroups(userData.uid, next);
}
}, function(err, results) {
if (err) {
return next(err);
}
- userInfo.signature = results.signature;
- userInfo.custom_profile_info = results.customProfileInfo.profile;
- userInfo.groups = results.groups;
- next(null, userInfo);
+ userData.signature = results.signature;
+ userData.custom_profile_info = results.customProfileInfo.profile;
+ next(null, userData);
});
}, callback);
});