group member search

only look in uids that are member of the group, added custom findUids
function to user.search
v1.18.x
barisusakli 10 years ago
parent 201c4b35da
commit 9f4f858ff9

@ -1098,65 +1098,42 @@ var async = require('async'),
Groups.searchMembers = function(data, callback) { Groups.searchMembers = function(data, callback) {
function userInGroup(userGroups) { function findUids(query, searchBy, startsWith, callback) {
for(var i=0; i<userGroups.length; ++i) { if (!query) {
if (userGroups[i].name === data.groupName) { return Groups.getMembers(data.groupName, 0, -1, callback);
return true;
}
}
return false;
} }
var searchResult;
var pagination;
if (!data.query) {
async.waterfall([ async.waterfall([
function(next) { function(next) {
user.getUidsFromSet('group:' + data.groupName + ':members', 0, -1, next); Groups.getMembers(data.groupName, 0, -1, next);
}, },
function(uids, next) { function(members, next) {
pagination = user.paginate(1, uids); user.getMultipleUserFields(members, ['uid'].concat(searchBy), next);
uids = pagination.data;
user.getUsers(uids, data.uid, next);
}, },
function(users, next) { function(users, next) {
next(null, { var uids = [];
users: users,
pagination: pagination.pagination for(var k=0; k<searchBy.length; ++k) {
}); for(var i=0; i<users.length; ++i) {
var field = users[i][searchBy[k]];
if ((startsWith && field.toLowerCase().startsWith(query)) || (!startsWith && field.toLowerCase().indexOf(query) !== -1)) {
uids.push(users[i].uid);
} }
], callback);
return;
} }
async.waterfall([
function(next) {
data.paginate = false;
user.search(data, next);
},
function(_searchResult, next) {
searchResult = _searchResult;
var uids = searchResult.users.map(function(user) {
return user && user.uid;
});
if (!uids.length) {
return callback(null, searchResult);
} }
Groups.getUserGroups(uids, next); if (searchBy.length > 1) {
}, uids = uids.filter(function(uid, index, array) {
function(groups, next) { return array.indexOf(uid) === index;
searchResult.users = searchResult.users.filter(function(user, index) {
return user && userInGroup(groups[index]);
}); });
}
pagination = user.paginate(data.page, searchResult.users); next(null, uids);
searchResult.pagination = pagination.pagination;
searchResult.users = pagination.data;
next(null, searchResult);
} }
], callback); ], callback);
}
data.findUids = findUids;
user.search(data, callback);
}; };
}(module.exports)); }(module.exports));

@ -26,7 +26,11 @@ module.exports = function(User) {
var searchResult = {}; var searchResult = {};
async.waterfall([ async.waterfall([
function(next) { function(next) {
if (data.findUids) {
data.findUids(query, searchBy, startsWith, next);
} else {
findUids(query, searchBy, startsWith, next); findUids(query, searchBy, startsWith, next);
}
}, },
function(uids, next) { function(uids, next) {
var filterBy = Array.isArray(data.filterBy) ? data.filterBy : []; var filterBy = Array.isArray(data.filterBy) ? data.filterBy : [];

Loading…
Cancel
Save