diff --git a/src/controllers/mods.js b/src/controllers/mods.js index 52403a5950..ece6442889 100644 --- a/src/controllers/mods.js +++ b/src/controllers/mods.js @@ -76,27 +76,7 @@ modsController.flags.list = function (req, res, next) { }, next); }, function (data) { - // If res.locals.cids is populated, then slim down the categories list - if (res.locals.cids) { - data.categories = data.categories.filter(function (category) { - return res.locals.cids.indexOf(String(category.cid)) !== -1; - }); - } - - // Minimal returned set for templates.js - data.categories = data.categories.reduce(function (memo, cur) { - if (!res.locals.cids) { - memo[cur.cid] = cur.name; - return memo; - } - - // If mod, remove categories they can't moderate - if (res.locals.cids.indexOf(String(cur.cid)) !== -1) { - memo[cur.cid] = cur.name; - } - - return memo; - }, {}); + data.categories = filterCategories(res.locals.cids, data.categories); res.render('flags/list', { flags: data.flags.flags, @@ -117,6 +97,7 @@ modsController.flags.detail = function (req, res, next) { moderatedCids: async.apply(user.getModeratedCids, req.uid), flagData: async.apply(flags.get, req.params.flagId), assignees: async.apply(user.getAdminsandGlobalModsandModerators), + categories: async.apply(categories.buildForSelect, req.uid, 'read'), }, function (err, results) { if (err || !results.flagData) { return next(err || new Error('[[error:invalid-data]]')); @@ -124,6 +105,12 @@ modsController.flags.detail = function (req, res, next) { return next(new Error('[[error:no-privileges]]')); } + if (!results.isAdminOrGlobalMod && results.moderatedCids.length) { + res.locals.cids = results.moderatedCids; + } + + results.categories = filterCategories(res.locals.cids, results.categories); + if (results.flagData.type === 'user') { results.flagData.type_path = 'uid'; } else if (results.flagData.type === 'post') { @@ -142,10 +129,34 @@ modsController.flags.detail = function (req, res, next) { return memo; }, {}), title: '[[pages:flag-details, ' + req.params.flagId + ']]', + categories: results.categories, })); }); }; +function filterCategories(moderatedCids, categories) { + // If cids is populated, then slim down the categories list + if (moderatedCids) { + categories = categories.filter(function (category) { + return moderatedCids.includes(String(category.cid)); + }); + } + + return categories.reduce(function (memo, cur) { + if (!moderatedCids) { + memo[cur.cid] = cur.name; + return memo; + } + + // If mod, remove categories they can't moderate + if (moderatedCids.includes(String(cur.cid))) { + memo[cur.cid] = cur.name; + } + + return memo; + }, {}); +} + modsController.postQueue = function (req, res, next) { async.waterfall([ function (next) {