From 459bc52338ddf6ebb1448f60e155db470e13a64e Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Wed, 18 Jan 2023 15:08:35 -0500 Subject: [PATCH] fix: #11136, tests, and returning the proper number of arrays --- src/categories/index.js | 13 +++++++++---- src/categories/update.js | 1 + test/categories.js | 31 ++++++++++++++++++++----------- 3 files changed, 30 insertions(+), 15 deletions(-) diff --git a/src/categories/index.js b/src/categories/index.js index 45ad28704a..a95d862e77 100644 --- a/src/categories/index.js +++ b/src/categories/index.js @@ -101,7 +101,7 @@ Categories.getModerators = async function (cid) { Categories.getModeratorUids = async function (cids) { // Only check active categories 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) => { memo.push(`cid:${cid}:privileges:moderate`); @@ -124,9 +124,14 @@ Categories.getModeratorUids = async function (cids) { const uniqGroups = _.uniq(_.flatten(sets.groupNames)); const groupUids = await groups.getMembersOfGroups(uniqGroups); const map = _.zipObject(uniqGroups, groupUids); - const moderatorUids = cids.map( - (cid, index) => _.uniq(sets.uids[index].concat(_.flatten(sets.groupNames[index].map(g => map[g])))) - ); + const moderatorUids = cids.map((cid, index) => { + if (disabled[index]) { + return []; + } + + return _.uniq(sets.uids[index].concat(_.flatten(sets.groupNames[index].map(g => map[g])))); + }); + console.log('what', moderatorUids); return moderatorUids; }; diff --git a/src/categories/update.js b/src/categories/update.js index d4be83edb8..ec1c2edb1e 100644 --- a/src/categories/update.js +++ b/src/categories/update.js @@ -11,6 +11,7 @@ const cache = require('../cache'); module.exports = function (Categories) { Categories.update = async function (modified) { const cids = Object.keys(modified); + console.log('updating', cids); await Promise.all(cids.map(cid => updateCategory(cid, modified[cid]))); return cids; }; diff --git a/test/categories.js b/test/categories.js index cd28ee33ef..284d0a0696 100644 --- a/test/categories.js +++ b/test/categories.js @@ -826,17 +826,18 @@ describe('Categories', () => { }); }); - describe('Categories.getModeratorUids', () => { - before((done) => { - async.series([ - async.apply(groups.create, { name: 'testGroup' }), - async.apply(groups.join, 'cid:1:privileges:groups:moderate', 'testGroup'), - async.apply(groups.join, 'testGroup', 1), - ], done); + describe.only('Categories.getModeratorUids', () => { + let cid; + + before(async () => { + ({ cid } = await Categories.create({ name: 'foobar' })); + await groups.create({ name: 'testGroup' }); + 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) => { - Categories.getModeratorUids([1, 2], (err, uids) => { + Categories.getModeratorUids([cid, 2], (err, uids) => { assert.ifError(err); assert.strictEqual(uids.length, 2); 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, 'testGroup2', 1), function (next) { - Categories.getModeratorUids([1, 2], (err, uids) => { + Categories.getModeratorUids([cid, 2], (err, uids) => { assert.ifError(err); assert(uids[0].includes('1')); next(); @@ -860,10 +861,18 @@ describe('Categories', () => { ], 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) => { async.series([ - async.apply(groups.leave, 'cid:1:privileges:groups:moderate', 'testGroup'), - async.apply(groups.leave, 'cid:1:privileges:groups:moderate', 'testGroup2'), + async.apply(groups.leave, `cid:${cid}:privileges:groups:moderate`, 'testGroup'), + async.apply(groups.leave, `cid:${cid}:privileges:groups:moderate`, 'testGroup2'), async.apply(groups.destroy, 'testGroup'), async.apply(groups.destroy, 'testGroup2'), ], done);