Merge branch 'master' into develop

isekai-main
Julian Lam 2 years ago
commit eab5ab7ff9

@ -99,6 +99,10 @@ 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]);
const groupNames = cids.reduce((memo, cid) => {
memo.push(`cid:${cid}:privileges:moderate`);
memo.push(`cid:${cid}:privileges:groups:moderate`);
@ -120,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;
};

@ -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;
};

@ -32,6 +32,12 @@ try {
if (!semver.satisfies(version, defaultPackage.dependencies[packageName])) {
const e = new TypeError(`Incorrect dependency version: ${packageName}`);
e.code = 'DEP_WRONG_VERSION';
// delete the module from require cache so it doesn't break rest of the upgrade
// https://github.com/NodeBB/NodeBB/issues/11173
const resolvedModule = require.resolve(packageName);
if (require.cache[resolvedModule]) {
delete require.cache[resolvedModule];
}
throw e;
}
};

@ -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);

Loading…
Cancel
Save