diff --git a/src/controllers/categories.js b/src/controllers/categories.js index 6810e2ea7e..91f36139e6 100644 --- a/src/controllers/categories.js +++ b/src/controllers/categories.js @@ -91,48 +91,54 @@ categoriesController.get = function(req, res, next) { async.waterfall([ function(next) { - categories.getCategoryField(cid, 'disabled', next); + async.parallel({ + exists: function(next) { + categories.exists(cid, next); + }, + disabled: function(next) { + categories.getCategoryField(cid, 'disabled', next); + }, + privileges: function(next) { + privileges.categories.get(cid, uid, next); + }, + userSettings: function(next) { + user.getSettings(uid, next); + } + }, next); }, - function(disabled, next) { - if (parseInt(disabled, 10) === 1) { - return next(new Error('[[error:category-disabled]]')); + function(results, next) { + if (!results.exists || parseInt(results.disabled, 10) === 1) { + return notFound(req, res); } - privileges.categories.get(cid, uid, next); - }, - function (privileges, next) { - if (!privileges.read) { - return next(new Error('[[error:no-privileges]]')); + if (!results.privileges.read) { + return notAllowed(req, res); } - user.getSettings(uid, function(err, settings) { + var settings = results.userSettings; + + var topicIndex = 0; + if (!settings.usePagination) { + topicIndex = Math.max((req.params.topic_index || 1) - (settings.topicsPerPage - 1), 0); + } else if (!req.query.page) { + var index = Math.max(parseInt((req.params.topic_index || 0), 10), 0); + page = Math.ceil((index + 1) / settings.topicsPerPage); + } + + var start = (page - 1) * settings.topicsPerPage + topicIndex, + end = start + settings.topicsPerPage - 1; + + categories.getCategoryById(cid, start, end, uid, function (err, categoryData) { if (err) { return next(err); } - var topicIndex = 0; - if (!settings.usePagination) { - topicIndex = Math.max((req.params.topic_index || 1) - (settings.topicsPerPage - 1), 0); - } else if (!req.query.page) { - var index = Math.max(parseInt((req.params.topic_index || 0), 10), 0); - page = Math.ceil((index + 1) / settings.topicsPerPage); - } - - var start = (page - 1) * settings.topicsPerPage + topicIndex, - end = start + settings.topicsPerPage - 1; - - categories.getCategoryById(cid, start, end, uid, function (err, categoryData) { + categories.getRecentTopicReplies(categoryData.children, uid, function(err) { if (err) { return next(err); } - - categories.getRecentTopicReplies(categoryData.children, uid, function(err) { - if (err) { - return next(err); - } - categoryData.privileges = privileges; - next(null, categoryData); - }); + categoryData.privileges = results.privileges; + next(null, categoryData); }); }); }, @@ -179,7 +185,7 @@ categoriesController.get = function(req, res, next) { } ], function (err, data) { if (err) { - return res.locals.isAPI ? res.json(404, 'not-found') : res.redirect(nconf.get('relative_path') + '/404'); + return next(err); } if (data.link) { @@ -193,7 +199,7 @@ categoriesController.get = function(req, res, next) { } data.currentPage = page; - data['feeds:disableRSS'] = meta.config['feeds:disableRSS'] === '1' ? true : false; + data['feeds:disableRSS'] = parseInt(meta.config['feeds:disableRSS'], 10) === 1; data.csrf = req.csrfToken(); // Paginator for noscript @@ -204,8 +210,27 @@ categoriesController.get = function(req, res, next) { active: x === parseInt(page, 10) }); } + res.render('category', data); }); }; +function notFound(req, res) { + res.locals.isAPI ? res.json(404, 'not-found') : res.redirect(nconf.get('relative_path') + '/404'); +} + +function notAllowed(req, res) { + var uid = req.user ? req.user.uid : 0; + if (uid) { + res.locals.isAPI ? res.json(403, 'not-allowed') : res.redirect(nconf.get('relative_path') + '/403'); + } else { + if (res.locals.isAPI) { + res.json(401, 'not-authorized'); + } else { + req.session.returnTo = req.url; + res.redirect(nconf.get('relative_path') + '/login'); + } + } +} + module.exports = categoriesController;