From 556e7066e85f3f9c632d8e8cdfe5f62794871c2c Mon Sep 17 00:00:00 2001
From: barisusakli <barisusakli@gmail.com>
Date: Sat, 15 Nov 2014 23:22:57 -0500
Subject: [PATCH] moved notFound notAllowed to helpers

---
 src/controllers/accounts.js   |  3 +++
 src/controllers/categories.js | 40 +++++------------------------------
 src/controllers/helpers.js    | 35 ++++++++++++++++++++++++++++++
 src/controllers/index.js      |  3 ++-
 src/controllers/topics.js     | 16 +++++++-------
 5 files changed, 53 insertions(+), 44 deletions(-)
 create mode 100644 src/controllers/helpers.js

diff --git a/src/controllers/accounts.js b/src/controllers/accounts.js
index 8a8aaf390a..f5279a9c08 100644
--- a/src/controllers/accounts.js
+++ b/src/controllers/accounts.js
@@ -566,6 +566,9 @@ accountsController.getChats = function(req, res, next) {
 		async.waterfall([
 			async.apply(user.getUidByUserslug, req.params.userslug),
 			function(toUid, next) {
+				if (!toUid) {
+					return notFound(res, '[[error:no-user]]');
+				}
 				async.parallel({
 					toUser: async.apply(user.getUserFields, toUid, ['uid', 'username']),
 					messages: async.apply(messaging.getMessages, req.user.uid, toUid, 'recent', false),
diff --git a/src/controllers/categories.js b/src/controllers/categories.js
index 81c2dcc459..bb50bc86f5 100644
--- a/src/controllers/categories.js
+++ b/src/controllers/categories.js
@@ -10,13 +10,9 @@ var categoriesController = {},
 	topics = require('../topics'),
 	meta = require('../meta'),
 	plugins = require('../plugins'),
+	helpers = require('./helpers'),
 	utils = require('../../public/src/utils');
 
-// todo: This might be better placed somewhere else
-var apiToRegular = function(url) {
-	return url.replace(/^\/api/, '');
-};
-
 categoriesController.recent = function(req, res, next) {
 	var uid = req.user ? req.user.uid : 0;
 	var end = (parseInt(meta.config.topicsPerList, 10) || 20) - 1;
@@ -106,7 +102,7 @@ categoriesController.get = function(req, res, next) {
 		userPrivileges;
 
 	if (req.params.topic_index && !utils.isNumber(req.params.topic_index)) {
-		return categoriesController.notFound(req, res);
+		return helpers.notFound(res);
 	}
 
 	async.waterfall([
@@ -128,15 +124,15 @@ categoriesController.get = function(req, res, next) {
 		},
 		function(results, next) {
 			if (!results.exists || (results.categoryData && parseInt(results.categoryData.disabled, 10) === 1)) {
-				return categoriesController.notFound(req, res);
+				return helpers.notFound(res);
 			}
 
 			if (cid + '/' + req.params.slug !== results.categoryData.slug) {
-				return categoriesController.notFound(req, res);
+				return helpers.notFound(res);
 			}
 
 			if (!results.privileges.read) {
-				return categoriesController.notAllowed(req, res);
+				return helpers.notAllowed(req, res);
 			}
 
 			var topicIndex = utils.isNumber(req.params.topic_index) ? parseInt(req.params.topic_index, 10) - 1 : 0;
@@ -260,32 +256,6 @@ categoriesController.get = function(req, res, next) {
 	});
 };
 
-categoriesController.notFound = function(req, res) {
-	if (res.locals.isAPI) {
-		res.status(404).json('not-found');
-	} else {
-		res.status(404).render('404');
-	}
-};
-
-categoriesController.notAllowed = function(req, res) {
-	var uid = req.user ? req.user.uid : 0;
 
-	if (uid) {
-		if (res.locals.isAPI) {
-			res.status(403).json('not-allowed');
-		} else {
-			res.status(403).render('403');
-		}
-	} else {
-		if (res.locals.isAPI) {
-			req.session.returnTo = apiToRegular(req.url);
-			res.status(401).json('not-authorized');
-		} else {
-			req.session.returnTo = req.url;
-			res.redirect(nconf.get('relative_path') + '/login');
-		}
-	}
-};
 
 module.exports = categoriesController;
diff --git a/src/controllers/helpers.js b/src/controllers/helpers.js
new file mode 100644
index 0000000000..f92e5b3d1b
--- /dev/null
+++ b/src/controllers/helpers.js
@@ -0,0 +1,35 @@
+'use strict';
+
+
+var helpers = {};
+
+helpers.notFound = function(res) {
+	if (res.locals.isAPI) {
+		res.status(404).json('not-found');
+	} else {
+		res.status(404).render('404');
+	}
+};
+
+helpers.notAllowed = function(req, res) {
+	var uid = req.user ? req.user.uid : 0;
+
+	if (uid) {
+		if (res.locals.isAPI) {
+			res.status(403).json('not-allowed');
+		} else {
+			res.status(403).render('403');
+		}
+	} else {
+		if (res.locals.isAPI) {
+			req.session.returnTo = req.url.replace(/^\/api/, '');
+			res.status(401).json('not-authorized');
+		} else {
+			req.session.returnTo = req.url;
+			res.redirect(nconf.get('relative_path') + '/login');
+		}
+	}
+};
+
+
+module.exports = helpers;
\ No newline at end of file
diff --git a/src/controllers/index.js b/src/controllers/index.js
index e92f856bc9..ec9919371d 100644
--- a/src/controllers/index.js
+++ b/src/controllers/index.js
@@ -9,6 +9,7 @@ var topicsController = require('./topics'),
 	staticController = require('./static'),
 	apiController = require('./api'),
 	adminController = require('./admin'),
+	helpers = require('./helpers'),
 
 	async = require('async'),
 	nconf = require('nconf'),
@@ -240,7 +241,7 @@ Controllers.outgoing = function(req, res, next) {
 
 Controllers.termsOfUse = function(req, res, next) {
 	if (!meta.config.termsOfUse) {
-		return categoriesController.notFound(req, res);
+		return helpers.notFound(res);
 	}
 	res.render('tos', {termsOfUse: meta.config.termsOfUse});
 };
diff --git a/src/controllers/topics.js b/src/controllers/topics.js
index f5555df140..6b0766ecaf 100644
--- a/src/controllers/topics.js
+++ b/src/controllers/topics.js
@@ -11,7 +11,7 @@ var topicsController = {},
 	topics = require('../topics'),
 	posts = require('../posts'),
 	privileges = require('../privileges'),
-	categoriesController = require('./categories'),
+	helpers = require('./helpers'),
 	utils = require('../../public/src/utils');
 
 topicsController.get = function(req, res, next) {
@@ -22,7 +22,7 @@ topicsController.get = function(req, res, next) {
 		userPrivileges;
 
 	if (req.params.post_index && !utils.isNumber(req.params.post_index)) {
-		return categoriesController.notFound(req, res);
+		return helpers.notFound(res);
 	}
 
 	async.waterfall([
@@ -43,15 +43,15 @@ topicsController.get = function(req, res, next) {
 			userPrivileges = results.privileges;
 
 			if (userPrivileges.disabled) {
-				return categoriesController.notFound(req, res);
+				return helpers.notFound(res);
 			}
 
 			if (tid + '/' + req.params.slug !== results.topic.slug) {
-				return categoriesController.notFound(req, res);
+				return helpers.notFound(res);
 			}
 
 			if (!userPrivileges.read) {
-				return categoriesController.notAllowed(req, res);
+				return helpers.notAllowed(req, res);
 			}
 
 			var settings = results.settings;
@@ -67,7 +67,7 @@ topicsController.get = function(req, res, next) {
 			}
 
 			if (settings.usePagination && (req.query.page < 1 || req.query.page > pageCount)) {
-				return categoriesController.notFound(req, res);
+				return helpers.notFound(res);
 			}
 
 			var set = 'tid:' + tid + ':posts',
@@ -110,13 +110,13 @@ topicsController.get = function(req, res, next) {
 
 			topics.getTopicWithPosts(tid, set, uid, start, end, reverse, function (err, topicData) {
 				if (err && err.message === '[[error:no-topic]]' && !topicData) {
-					return categoriesController.notFound(req, res);
+					return helpers.notFound(res);
 				}
 				if (err && !topicData) {
 					return next(err);
 				}
 				if (topicData.deleted && !userPrivileges.view_deleted) {
-					return categoriesController.notAllowed(req, res);
+					return helpers.notAllowed(req, res);
 				}
 
 				topicData.pageCount = pageCount;