You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nodebb/src/groups/search.js

132 lines
3.1 KiB
JavaScript

'use strict';
8 years ago
var async = require('async');
var user = require('../user');
var db = require('./../database');
module.exports = function (Groups) {
Groups.search = function (query, options, callback) {
if (!query) {
return callback(null, []);
}
query = query.toLowerCase();
async.waterfall([
async.apply(db.getObjectValues, 'groupslug:groupname'),
function (groupNames, next) {
// Ephemeral groups and the registered-users groups are searchable
8 years ago
groupNames = Groups.getEphemeralGroups().concat(groupNames).concat('registered-users');
groupNames = groupNames.filter(function (name) {
return name.toLowerCase().indexOf(query) !== -1 && name !== 'administrators' && !Groups.isPrivilegeGroup(name);
});
groupNames = groupNames.slice(0, 100);
Groups.getGroupsData(groupNames, next);
},
function (groupsData, next) {
groupsData = groupsData.filter(Boolean);
if (options.filterHidden) {
groupsData = groupsData.filter(function (group) {
return !group.hidden;
});
}
Groups.sort(options.sort, groupsData, next);
},
], callback);
};
Groups.sort = function (strategy, groups, next) {
switch (strategy) {
8 years ago
case 'count':
groups = groups.sort(function (a, b) {
return a.slug > b.slug;
}).sort(function (a, b) {
return b.memberCount - a.memberCount;
});
break;
8 years ago
case 'date':
groups = groups.sort(function (a, b) {
return b.createtime - a.createtime;
});
break;
8 years ago
case 'alpha': // intentional fall-through
default:
groups = groups.sort(function (a, b) {
return a.slug > b.slug ? 1 : -1;
});
}
next(null, groups);
};
Groups.searchMembers = function (data, callback) {
function findUids(query, searchBy, callback) {
query = query.toLowerCase();
async.waterfall([
function (next) {
Groups.getMembers(data.groupName, 0, -1, next);
},
function (members, next) {
10 years ago
user.getUsersFields(members, ['uid'].concat([searchBy]), next);
},
function (users, next) {
var uids = [];
for (var i = 0; i < users.length; i += 1) {
var field = users[i][searchBy];
if (field.toLowerCase().startsWith(query)) {
uids.push(users[i].uid);
}
}
next(null, uids);
},
], callback);
}
10 years ago
if (!data.query) {
Groups.getOwnersAndMembers(data.groupName, data.uid, 0, 19, function (err, users) {
10 years ago
if (err) {
return callback(err);
}
callback(null, { users: users });
10 years ago
});
return;
}
data.findUids = findUids;
10 years ago
var results;
async.waterfall([
function (next) {
10 years ago
user.search(data, next);
},
function (_results, next) {
10 years ago
results = _results;
var uids = results.users.map(function (user) {
10 years ago
return user && user.uid;
});
Groups.ownership.isOwners(uids, data.groupName, next);
},
function (isOwners, next) {
results.users.forEach(function (user, index) {
10 years ago
if (user) {
user.isOwner = isOwners[index];
}
});
results.users.sort(function (a, b) {
10 years ago
if (a.isOwner && !b.isOwner) {
return -1;
} else if (!a.isOwner && b.isOwner) {
return 1;
}
return 0;
});
10 years ago
next(null, results);
},
10 years ago
], callback);
};
};