mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-11 00:15:46 +01:00
fix: #11136, tests, and returning the proper number of arrays
This commit is contained in:
@@ -101,7 +101,7 @@ Categories.getModerators = async function (cid) {
|
|||||||
Categories.getModeratorUids = async function (cids) {
|
Categories.getModeratorUids = async function (cids) {
|
||||||
// Only check active categories
|
// Only check active categories
|
||||||
const disabled = (await Categories.getCategoriesFields(cids, ['disabled'])).map(obj => obj.disabled);
|
const disabled = (await Categories.getCategoriesFields(cids, ['disabled'])).map(obj => obj.disabled);
|
||||||
cids = cids.filter((_, idx) => !disabled[idx]);
|
// cids = cids.filter((_, idx) => !disabled[idx]);
|
||||||
|
|
||||||
const groupNames = cids.reduce((memo, cid) => {
|
const groupNames = cids.reduce((memo, cid) => {
|
||||||
memo.push(`cid:${cid}:privileges:moderate`);
|
memo.push(`cid:${cid}:privileges:moderate`);
|
||||||
@@ -124,9 +124,14 @@ Categories.getModeratorUids = async function (cids) {
|
|||||||
const uniqGroups = _.uniq(_.flatten(sets.groupNames));
|
const uniqGroups = _.uniq(_.flatten(sets.groupNames));
|
||||||
const groupUids = await groups.getMembersOfGroups(uniqGroups);
|
const groupUids = await groups.getMembersOfGroups(uniqGroups);
|
||||||
const map = _.zipObject(uniqGroups, groupUids);
|
const map = _.zipObject(uniqGroups, groupUids);
|
||||||
const moderatorUids = cids.map(
|
const moderatorUids = cids.map((cid, index) => {
|
||||||
(cid, index) => _.uniq(sets.uids[index].concat(_.flatten(sets.groupNames[index].map(g => map[g]))))
|
if (disabled[index]) {
|
||||||
);
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
return _.uniq(sets.uids[index].concat(_.flatten(sets.groupNames[index].map(g => map[g]))));
|
||||||
|
});
|
||||||
|
console.log('what', moderatorUids);
|
||||||
return moderatorUids;
|
return moderatorUids;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ const cache = require('../cache');
|
|||||||
module.exports = function (Categories) {
|
module.exports = function (Categories) {
|
||||||
Categories.update = async function (modified) {
|
Categories.update = async function (modified) {
|
||||||
const cids = Object.keys(modified);
|
const cids = Object.keys(modified);
|
||||||
|
console.log('updating', cids);
|
||||||
await Promise.all(cids.map(cid => updateCategory(cid, modified[cid])));
|
await Promise.all(cids.map(cid => updateCategory(cid, modified[cid])));
|
||||||
return cids;
|
return cids;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -826,17 +826,18 @@ describe('Categories', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Categories.getModeratorUids', () => {
|
describe.only('Categories.getModeratorUids', () => {
|
||||||
before((done) => {
|
let cid;
|
||||||
async.series([
|
|
||||||
async.apply(groups.create, { name: 'testGroup' }),
|
before(async () => {
|
||||||
async.apply(groups.join, 'cid:1:privileges:groups:moderate', 'testGroup'),
|
({ cid } = await Categories.create({ name: 'foobar' }));
|
||||||
async.apply(groups.join, 'testGroup', 1),
|
await groups.create({ name: 'testGroup' });
|
||||||
], done);
|
await groups.join(`cid:${cid}:privileges:groups:moderate`, 'testGroup');
|
||||||
|
await groups.join('testGroup', 1);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should retrieve all users with moderator bit in category privilege', (done) => {
|
it('should retrieve all users with moderator bit in category privilege', (done) => {
|
||||||
Categories.getModeratorUids([1, 2], (err, uids) => {
|
Categories.getModeratorUids([cid, 2], (err, uids) => {
|
||||||
assert.ifError(err);
|
assert.ifError(err);
|
||||||
assert.strictEqual(uids.length, 2);
|
assert.strictEqual(uids.length, 2);
|
||||||
assert(uids[0].includes('1'));
|
assert(uids[0].includes('1'));
|
||||||
@@ -851,7 +852,7 @@ describe('Categories', () => {
|
|||||||
async.apply(groups.join, 'cid:1:privileges:groups:moderate', 'testGroup2'),
|
async.apply(groups.join, 'cid:1:privileges:groups:moderate', 'testGroup2'),
|
||||||
async.apply(groups.join, 'testGroup2', 1),
|
async.apply(groups.join, 'testGroup2', 1),
|
||||||
function (next) {
|
function (next) {
|
||||||
Categories.getModeratorUids([1, 2], (err, uids) => {
|
Categories.getModeratorUids([cid, 2], (err, uids) => {
|
||||||
assert.ifError(err);
|
assert.ifError(err);
|
||||||
assert(uids[0].includes('1'));
|
assert(uids[0].includes('1'));
|
||||||
next();
|
next();
|
||||||
@@ -860,10 +861,18 @@ describe('Categories', () => {
|
|||||||
], done);
|
], done);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should not return moderators of disabled categories', async () => {
|
||||||
|
const payload = {};
|
||||||
|
payload[cid] = { disabled: 1 };
|
||||||
|
await Categories.update(payload);
|
||||||
|
const uids = await Categories.getModeratorUids([1, 2]);
|
||||||
|
assert(!uids[0].includes('1'));
|
||||||
|
});
|
||||||
|
|
||||||
after((done) => {
|
after((done) => {
|
||||||
async.series([
|
async.series([
|
||||||
async.apply(groups.leave, 'cid:1:privileges:groups:moderate', 'testGroup'),
|
async.apply(groups.leave, `cid:${cid}:privileges:groups:moderate`, 'testGroup'),
|
||||||
async.apply(groups.leave, 'cid:1:privileges:groups:moderate', 'testGroup2'),
|
async.apply(groups.leave, `cid:${cid}:privileges:groups:moderate`, 'testGroup2'),
|
||||||
async.apply(groups.destroy, 'testGroup'),
|
async.apply(groups.destroy, 'testGroup'),
|
||||||
async.apply(groups.destroy, 'testGroup2'),
|
async.apply(groups.destroy, 'testGroup2'),
|
||||||
], done);
|
], done);
|
||||||
|
|||||||
Reference in New Issue
Block a user