diff --git a/src/controllers/mods.js b/src/controllers/mods.js index 5d2d904663..0079412f87 100644 --- a/src/controllers/mods.js +++ b/src/controllers/mods.js @@ -8,17 +8,17 @@ var adminFlagsController = require('./admin/flags'); var modsController = {}; modsController.flagged = function (req, res, next) { - async.parallel([ - async.apply(user.isAdminOrGlobalMod, req.uid), - async.apply(user.isModeratorOfAnyCategory, req.uid) - ], function (err, results) { - if (err || !(results[0] || results[1])) { + async.parallel({ + isAdminOrGlobalMod: async.apply(user.isAdminOrGlobalMod, req.uid), + moderatedCids: async.apply(user.getModeratedCids, req.uid) + }, function (err, results) { + if (err || !(results.isAdminOrGlobalMod || !!results.moderatedCids.length)) { return next(err); } - if (!results[0] && results[1]) { - res.locals.cids = results[1]; - } + if (!results.isAdminOrGlobalMod && results.moderatedCids.length) { + res.locals.cids = results.moderatedCids; + } adminFlagsController.get(req, res, next); }); diff --git a/src/user.js b/src/user.js index 996d1abd0a..7cc01c89f3 100644 --- a/src/user.js +++ b/src/user.js @@ -235,19 +235,8 @@ var meta = require('./meta'); }; User.isModeratorOfAnyCategory = function (uid, callback) { - // Checks all active categories and determines whether passed-in uid is a mod of any of them - db.getSortedSetRange('categories:cid', 0, -1, function (err, cids) { - async.filter(cids, function (cid, next) { - User.isModerator(uid, cid, function (err, isMod) { - if (err) { - // do nothing because async doesn't support errors in filter yet - } - - next(!!isMod); - }); - }, function (result) { - callback(err, result); - }); + User.getModeratedCids(uid, function (err, cids) { + callback(err, Array.isArray(cids) ? !!cids.length : false); }); }; @@ -328,6 +317,25 @@ var meta = require('./meta'); ], callback); }; + User.getModeratedCids = function (uid, callback) { + var cids; + async.waterfall([ + function (next) { + db.getSortedSetRange('categories:cid', 0, -1, next); + }, + function (_cids, next) { + cids = _cids; + User.isModerator(uid, cids, next); + }, + function (isMods, next) { + cids = cids.filter(function (cid, index) { + return cid && isMods[index]; + }); + next(null, cids); + } + ], callback); + }; + User.addInterstitials = function (callback) { plugins.registerHook('core', { hook: 'filter:register.interstitial',