Revert "reverting groups.js to 4701eb1cee1a35b7f059f4362773bd9d67b7370c"

This reverts commit d8df9ab3ed.
v1.18.x
Julian Lam 11 years ago
parent e2d0d4db5c
commit bd2a1a3efa

@ -4,26 +4,31 @@
var async = require('async'), var async = require('async'),
winston = require('winston'), winston = require('winston'),
user = require('./user'), user = require('./user'),
db = require('./database'); db = require('./database'),
utils = require('../public/src/utils'),
Groups.list = function(options, callback) { filterGroups = function(groups, options) {
db.getSetMembers('groups', function (err, groupNames) {
if (groupNames.length > 0) {
async.map(groupNames, function (groupName, next) {
Groups.get(groupName, options, next);
}, function (err, groups) {
// Remove system, hidden, or deleted groups from this list // Remove system, hidden, or deleted groups from this list
if (!options.showAllGroups) { if (groups && !options.showAllGroups) {
groups = groups.filter(function (group) { return groups.filter(function (group) {
if (group.deleted || (group.hidden && !group.system) || (!options.showSystemGroups && group.system)) { if (group.deleted || (group.hidden && !group.system) || (!options.showSystemGroups && group.system)) {
return false; return false;
} else { } else {
return true; return true;
} }
}); });
} else {
return groups;
} }
};
callback(err, groups); Groups.list = function(options, callback) {
db.getSetMembers('groups', function (err, groupNames) {
if (groupNames.length > 0) {
async.map(groupNames, function (groupName, next) {
Groups.get(groupName, options, next);
}, function (err, groups) {
callback(err, filterGroups(groups, options));
}); });
} else { } else {
callback(null, []); callback(null, []);
@ -73,10 +78,12 @@
return callback(err); return callback(err);
} }
// User counts
results.base.count = numUsers || results.users.length; results.base.count = numUsers || results.users.length;
results.base.members = results.users; results.base.members = results.users;
results.base.memberCount = numUsers || results.users.length; results.base.memberCount = numUsers || results.users.length;
results.base.slug = utils.slugify(results.base.name);
results.base.deleted = !!parseInt(results.base.deleted, 10); results.base.deleted = !!parseInt(results.base.deleted, 10);
results.base.hidden = !!parseInt(results.base.hidden, 10); results.base.hidden = !!parseInt(results.base.hidden, 10);
results.base.system = !!parseInt(results.base.system, 10); results.base.system = !!parseInt(results.base.system, 10);
@ -87,6 +94,24 @@
}); });
}; };
Groups.search = function(query, options, callback) {
if (query.length) {
db.getSetMembers('groups', function(err, groups) {
groups = groups.filter(function(groupName) {
return groupName.match(new RegExp(utils.escapeRegexChars(query), 'i'));
});
async.map(groups, function(groupName, next) {
Groups.get(groupName, options, next);
}, function(err, groups) {
callback(err, filterGroups(groups, options));
});
});
} else {
callback(null, []);
}
};
Groups.isMember = function(uid, groupName, callback) { Groups.isMember = function(uid, groupName, callback) {
db.isSetMember('group:' + groupName + ':members', uid, callback); db.isSetMember('group:' + groupName + ':members', uid, callback);
}; };
@ -108,7 +133,20 @@
}; };
Groups.exists = function(name, callback) { Groups.exists = function(name, callback) {
db.isSetMember('groups', name, callback); name = utils.slugify(name);
db.getSetMembers('groups', function(err, groupNames) {
if (err) {
return callback(err);
}
var matches = groupNames.map(function(groupName) {
return utils.slugify(groupName);
}).filter(function(groupName) {
return groupName === name;
});
callback(null, matches.length > 0 ? true : false);
});
}; };
Groups.create = function(name, description, callback) { Groups.create = function(name, description, callback) {

Loading…
Cancel
Save