mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-07 06:25:50 +01:00
First pass at #1331 - Groups.search() + Tests
This commit is contained in:
@@ -244,7 +244,7 @@
|
||||
|
||||
tags : ['a', 'abbr', 'acronym', 'address', 'applet', 'area', 'article', 'aside', 'audio', 'b', 'base', 'basefont', 'bdi', 'bdo', 'big', 'blockquote', 'body', 'br', 'button', 'canvas', 'caption', 'center', 'cite', 'code', 'col', 'colgroup', 'command', 'datalist', 'dd', 'del', 'details', 'dfn', 'dialog', 'dir', 'div', 'dl', 'dt', 'em', 'embed', 'fieldset', 'figcaption', 'figure', 'font', 'footer', 'form', 'frame', 'frameset', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'head', 'header', 'hr', 'html', 'i', 'iframe', 'img', 'input', 'ins', 'kbd', 'keygen', 'label', 'legend', 'li', 'link', 'map', 'mark', 'menu', 'meta', 'meter', 'nav', 'noframes', 'noscript', 'object', 'ol', 'optgroup', 'option', 'output', 'p', 'param', 'pre', 'progress', 'q', 'rp', 'rt', 'ruby', 's', 'samp', 'script', 'section', 'select', 'small', 'source', 'span', 'strike', 'strong', 'style', 'sub', 'summary', 'sup', 'table', 'tbody', 'td', 'textarea', 'tfoot', 'th', 'thead', 'time', 'title', 'tr', 'track', 'tt', 'u', 'ul', 'var', 'video', 'wbr'],
|
||||
|
||||
getTagsExcept : function(excludeTags) {
|
||||
getTagsExcept: function(excludeTags) {
|
||||
var tagsToReturn = utils.tags.slice();
|
||||
excludeTags.forEach(function(tag) {
|
||||
var index = tagsToReturn.indexOf(tag);
|
||||
@@ -253,6 +253,10 @@
|
||||
}
|
||||
});
|
||||
return tagsToReturn;
|
||||
},
|
||||
|
||||
escapeRegexChars: function(text) {
|
||||
return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -4,7 +4,23 @@
|
||||
var async = require('async'),
|
||||
winston = require('winston'),
|
||||
user = require('./user'),
|
||||
db = require('./database');
|
||||
db = require('./database'),
|
||||
utils = require('../public/src/utils'),
|
||||
|
||||
filterGroups = function(groups, options) {
|
||||
// Remove system, hidden, or deleted groups from this list
|
||||
if (groups && !options.showAllGroups) {
|
||||
return groups.filter(function (group) {
|
||||
if (group.deleted || (group.hidden && !group.system) || (!options.showSystemGroups && group.system)) {
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
});
|
||||
} else {
|
||||
return groups;
|
||||
}
|
||||
};
|
||||
|
||||
Groups.list = function(options, callback) {
|
||||
db.getSetMembers('groups', function (err, groupNames) {
|
||||
@@ -12,18 +28,7 @@
|
||||
async.map(groupNames, function (groupName, next) {
|
||||
Groups.get(groupName, options, next);
|
||||
}, function (err, groups) {
|
||||
// Remove system, hidden, or deleted groups from this list
|
||||
if (!options.showAllGroups) {
|
||||
groups = groups.filter(function (group) {
|
||||
if (group.deleted || (group.hidden && !group.system) || (!options.showSystemGroups && group.system)) {
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
callback(err, groups);
|
||||
callback(err, filterGroups(groups, options));
|
||||
});
|
||||
} else {
|
||||
callback(null, []);
|
||||
@@ -87,6 +92,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) {
|
||||
db.isSetMember('group:' + groupName + ':members', uid, callback);
|
||||
};
|
||||
|
||||
@@ -73,6 +73,27 @@ describe('Groups', function() {
|
||||
});
|
||||
});
|
||||
|
||||
describe('.search()', function() {
|
||||
it('should return the "Test" group when searched for', function(done) {
|
||||
Groups.search('test', {}, function(err, groups) {
|
||||
assert.equal(1, groups.length);
|
||||
assert.strictEqual('Test', groups[0].name);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should return the "Hidden" group when "showAllGroups" option is passed in', function(done) {
|
||||
Groups.search('hidden', {
|
||||
showAllGroups: true
|
||||
}, function(err, groups) {
|
||||
console.log(groups);
|
||||
assert.equal(1, groups.length);
|
||||
assert.strictEqual('Hidden', groups[0].name);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('.isMember()', function() {
|
||||
it('should return boolean true when a user is in a group', function(done) {
|
||||
Groups.isMember(1, 'Test', function(err, isMember) {
|
||||
|
||||
Reference in New Issue
Block a user