Skip to content

Node.js based forum software built for the modern web

#2032

barisusakli55cbaf6a19c6
diff --git a/src/controllers/categories.js b/src/controllers/categories.js
index 91f36139e6..b23e4d4afb 100644
--- a/src/controllers/categories.js
+++ b/src/controllers/categories.js
@@ -108,11 +108,11 @@ categoriesController.get = function(req, res, next) {
},
function(results, next) {
if (!results.exists || parseInt(results.disabled, 10) === 1) {
- return notFound(req, res);
+ return categoriesController.notFound(req, res);
}
if (!results.privileges.read) {
- return notAllowed(req, res);
+ return categoriesController.notAllowed(req, res);
}
var settings = results.userSettings;
@@ -215,11 +215,11 @@ categoriesController.get = function(req, res, next) {
});
};
-function notFound(req, res) {
+categoriesController.notFound = function(req, res) {
res.locals.isAPI ? res.json(404, 'not-found') : res.redirect(nconf.get('relative_path') + '/404');
-}
+};
-function notAllowed(req, res) {
+categoriesController.notAllowed = function(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');
@@ -231,6 +231,6 @@ function notAllowed(req, res) {
res.redirect(nconf.get('relative_path') + '/login');
}
}
-}
+};
module.exports = categoriesController;
diff --git a/src/controllers/topics.js b/src/controllers/topics.js
index d0857b862a..de91eb75dd 100644
--- a/src/controllers/topics.js
+++ b/src/controllers/topics.js
@@ -6,12 +6,13 @@ var topicsController = {},
validator = require('validator'),
nconf = require('nconf'),
qs = require('querystring'),
- user = require('./../user'),
- meta = require('./../meta'),
- topics = require('./../topics'),
+ user = require('../user'),
+ meta = require('../meta'),
+ topics = require('../topics'),
posts = require('../posts'),
privileges = require('../privileges'),
- utils = require('./../../public/src/utils');
+ categoriesController = require('./categories'),
+ utils = require('../../public/src/utils');
topicsController.get = function(req, res, next) {
var tid = req.params.topic_id,
@@ -22,16 +23,10 @@ topicsController.get = function(req, res, next) {
async.waterfall([
function (next) {
- privileges.topics.get(tid, uid, next);
- },
- function (privileges, next) {
- if (!privileges.read || privileges.disabled) {
- return next(new Error('[[error:no-privileges]]'));
- }
-
- userPrivileges = privileges;
-
async.parallel({
+ privileges: function(next) {
+ privileges.topics.get(tid, uid, next);
+ },
postCount: function(next) {
topics.getPostCount(tid, next);
},
@@ -41,6 +36,16 @@ topicsController.get = function(req, res, next) {
}, next);
},
function (results, next) {
+ userPrivileges = results.privileges;
+
+ if (userPrivileges.disabled) {
+ return categoriesController.notFound(req, res);
+ }
+
+ if (!userPrivileges.read) {
+ return categoriesController.notAllowed(req, res);
+ }
+
var settings = results.settings;
var postCount = parseInt(results.postCount, 10) + 1;
var set = 'tid:' + tid + ':posts',
@@ -82,16 +87,22 @@ topicsController.get = function(req, res, next) {
end = start + settings.postsPerPage - 1;
topics.getTopicWithPosts(tid, set, uid, start, end, reverse, function (err, topicData) {
- if (topicData) {
- if (topicData.deleted && !userPrivileges.view_deleted) {
- return next(new Error('[[error:no-topic]]'));
- }
- topicData.currentPage = page;
- if(page > 1) {
- topicData.posts.splice(0, 1);
- }
+ if (err && err.message === '[[error:no-topic]]' && !topicData) {
+ return categoriesController.notFound(req, res);
+ }
+ if (err && !topicData) {
+ return next(err);
}
- next(err, topicData);
+ if (topicData.deleted && !userPrivileges.view_deleted) {
+ return categoriesController.notAllowed(req, res);
+ }
+
+ topicData.currentPage = page;
+ if(page > 1) {
+ topicData.posts.splice(0, 1);
+ }
+
+ next(null, topicData);
});
},
function (topicData, next) {
@@ -191,7 +202,7 @@ topicsController.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);
}
data.privileges = userPrivileges;
@@ -214,6 +225,7 @@ topicsController.get = function(req, res, next) {
active: x === parseInt(page, 10)
});
}
+
res.render('topic', data);
});
};