search group members

v1.18.x
barisusakli 10 years ago
parent daf9999a29
commit d342e01ef1

@ -1096,4 +1096,67 @@ var async = require('async'),
next(null, groups);
};
Groups.searchMembers = function(data, callback) {
function userInGroup(userGroups) {
for(var i=0; i<userGroups.length; ++i) {
if (userGroups[i].name === data.groupName) {
return true;
}
}
return false;
}
var searchResult;
var pagination;
if (!data.query) {
async.waterfall([
function(next) {
user.getUidsFromSet('group:' + data.groupName + ':members', 0, -1, next);
},
function(uids, next) {
pagination = user.paginate(1, uids);
uids = pagination.data;
user.getUsers(uids, data.uid, next);
},
function(users, next) {
next(null, {
users: users,
pagination: pagination.pagination
});
}
], 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);
},
function(groups, next) {
searchResult.users = searchResult.users.filter(function(user, index) {
return user && userInGroup(groups[index]);
});
pagination = user.paginate(data.page, searchResult.users);
searchResult.pagination = pagination.pagination;
searchResult.users = pagination.data;
next(null, searchResult);
}
], callback);
};
}(module.exports));

@ -184,6 +184,15 @@ SocketGroups.search = function(socket, data, callback) {
groups.search(data.query || '', data.options || {}, callback);
};
SocketGroups.searchMembers = function(socket, data, callback) {
if (!data) {
return callback(null, []);
}
data.uid = socket.uid;
groups.searchMembers(data, callback);
};
SocketGroups.kick = function(socket, data, callback) {
if (!data) {
return callback(new Error('[[error:invalid-data]]'));

@ -15,6 +15,7 @@ module.exports = function(User) {
var startsWith = data.hasOwnProperty('startsWith') ? data.startsWith : true;
var page = data.page || 1;
var uid = data.uid || 0;
var paginate = data.hasOwnProperty('paginate') ? data.paginate : true;
if (searchBy.indexOf('ip') !== -1) {
return searchByIP(query, uid, callback);
@ -25,42 +26,50 @@ module.exports = function(User) {
return searchBy + ':uid';
});
var resultsPerPage = parseInt(meta.config.userSearchResultsPerPage, 10) || 20;
var start = Math.max(0, page - 1) * resultsPerPage;
var end = start + resultsPerPage;
var pageCount = 1;
var matchCount = 0;
var filterBy = Array.isArray(data.filterBy) ? data.filterBy : [];
var searchResult = {};
async.waterfall([
function(next) {
findUids(query, keys, startsWith, next);
},
function(uids, next) {
filterAndSortUids(uids, filterBy, data.sortBy, next);
},
function(uids, next) {
matchCount = uids.length;
uids = uids.slice(start, end);
searchResult.matchCount = uids.length;
if (paginate) {
var pagination = user.paginate(page, uids);
uids = pagination.data;
searchResult.pagination = pagination.pagination;
}
User.getUsers(uids, uid, next);
},
function(userData, next) {
var data = {
timing: (process.elapsedTimeSince(startTime) / 1000).toFixed(2),
users: userData,
matchCount: matchCount
};
var currentPage = Math.max(1, Math.ceil((start + 1) / resultsPerPage));
pageCount = Math.ceil(matchCount / resultsPerPage);
data.pagination = pagination.create(currentPage, pageCount);
searchResult.timing = (process.elapsedTimeSince(startTime) / 1000).toFixed(2);
searchResult.users = userData;
next(null, data);
next(null, searchResult);
}
], callback);
};
User.paginate = function(page, data) {
var resultsPerPage = parseInt(meta.config.userSearchResultsPerPage, 10) || 20;
var start = Math.max(0, page - 1) * resultsPerPage;
var end = start + resultsPerPage;
var pageCount = Math.ceil(data.length / resultsPerPage);
var currentPage = Math.max(1, Math.ceil((start + 1) / resultsPerPage));
return {
pagination: pagination.create(currentPage, pageCount),
data: data.slice(start, end)
};
};
function findUids(query, keys, startsWith, callback) {
db.getObjects(keys, function(err, hashes) {
if (err || !hashes) {

Loading…
Cancel
Save