Skip to content

Node.js based forum software built for the modern web

added helpers.redirect

barisusakliddd4680ea7d4
diff --git a/src/controllers/categories.js b/src/controllers/categories.js
index 5264e0e5f4..18bbd57f8b 100644
--- a/src/controllers/categories.js
+++ b/src/controllers/categories.js
@@ -200,8 +200,7 @@ categoriesController.get = function(req, res, next) {
var topicCount = parseInt(results.categoryData.topic_count, 10);
if (topicIndex < 0 || topicIndex > Math.max(topicCount - 1, 0)) {
- var url = '/category/' + cid + '/' + req.params.slug + (topicIndex > topicCount ? '/' + topicCount : '');
- return res.locals.isAPI ? res.status(302).json(url) : res.redirect(url);
+ return helpers.redirect(res, '/category/' + cid + '/' + req.params.slug + (topicIndex > topicCount ? '/' + topicCount : ''));
}
userPrivileges = results.privileges;
diff --git a/src/controllers/groups.js b/src/controllers/groups.js
index 51200d0a40..7371c924e7 100644
--- a/src/controllers/groups.js
+++ b/src/controllers/groups.js
@@ -51,31 +51,35 @@ groupsController.details = function(req, res, next) {
}
}
], function(err, ok) {
- if (ok) {
- async.parallel({
- group: function(next) {
- groups.get(res.locals.groupName, {
- expand: true,
- uid: uid
- }, next);
- },
- posts: function(next) {
- groups.getLatestMemberPosts(res.locals.groupName, 10, uid, next);
- }
- }, function(err, results) {
- if (err) {
- return next(err);
- }
-
- if (!results.group) {
- return helpers.notFound(req, res);
- }
+ if (err) {
+ return next(err);
+ }
- res.render('groups/details', results);
- });
- } else {
- return res.locals.isAPI ? res.status(302).json('/groups') : res.redirect('/groups');
+ if (!ok) {
+ return helpers.redirect(res, '/groups');
}
+
+ async.parallel({
+ group: function(next) {
+ groups.get(res.locals.groupName, {
+ expand: true,
+ uid: uid
+ }, next);
+ },
+ posts: function(next) {
+ groups.getLatestMemberPosts(res.locals.groupName, 10, uid, next);
+ }
+ }, function(err, results) {
+ if (err) {
+ return next(err);
+ }
+
+ if (!results.group) {
+ return helpers.notFound(req, res);
+ }
+
+ res.render('groups/details', results);
+ });
});
};
diff --git a/src/controllers/helpers.js b/src/controllers/helpers.js
index dd5961839b..12a3327c55 100644
--- a/src/controllers/helpers.js
+++ b/src/controllers/helpers.js
@@ -45,6 +45,14 @@ helpers.notAllowed = function(req, res, error) {
}
};
+helpers.redirect = function(res, url) {
+ if (res.locals.isAPI) {
+ res.status(302).json(url);
+ } else {
+ res.redirect(url);
+ }
+};
+
helpers.buildCategoryBreadcrumbs = function(cid, callback) {
var breadcrumbs = [];
diff --git a/src/controllers/topics.js b/src/controllers/topics.js
index 1d756b4189..8429e19396 100644
--- a/src/controllers/topics.js
+++ b/src/controllers/topics.js
@@ -56,12 +56,8 @@ topicsController.get = function(req, res, next) {
var postCount = parseInt(results.topic.postcount, 10);
var pageCount = Math.max(1, Math.ceil((postCount - 1) / settings.postsPerPage));
- if (utils.isNumber(req.params.post_index)) {
- var url = '';
- if (req.params.post_index < 1 || req.params.post_index > postCount) {
- url = '/topic/' + req.params.topic_id + '/' + req.params.slug + (req.params.post_index > postCount ? '/' + postCount : '');
- return res.locals.isAPI ? res.status(302).json(url) : res.redirect(url);
- }
+ if (utils.isNumber(req.params.post_index) && (req.params.post_index < 1 || req.params.post_index > postCount)) {
+ return helpers.redirect(res, '/topic/' + req.params.topic_id + '/' + req.params.slug + (req.params.post_index > postCount ? '/' + postCount : ''));
}
if (settings.usePagination && (req.query.page < 1 || req.query.page > pageCount)) {
@@ -266,7 +262,7 @@ topicsController.get = function(req, res, next) {
});
topics.increaseViewCount(tid);
-
+
plugins.fireHook('filter:topic.build', {req: req, res: res, templateData: data}, function(err, data) {
if (err) {
return next(err);
diff --git a/src/middleware/middleware.js b/src/middleware/middleware.js
index 85cc10c908..62575d066b 100644
--- a/src/middleware/middleware.js
+++ b/src/middleware/middleware.js
@@ -60,12 +60,7 @@ middleware.redirectToAccountIfLoggedIn = function(req, res, next) {
if (err) {
return next(err);
}
-
- if (res.locals.isAPI) {
- res.status(302).json(nconf.get('relative_path') + '/user/' + userslug);
- } else {
- res.redirect(nconf.get('relative_path') + '/user/' + userslug);
- }
+ helpers.redirect(res, '/user/' + userslug);
});
};
@@ -85,13 +80,7 @@ middleware.addSlug = function(req, res, next) {
return next(err);
}
- var url = nconf.get('relative_path') + name + encodeURI(slug);
-
- if (res.locals.isAPI) {
- res.status(302).json(url);
- } else {
- res.redirect(url);
- }
+ helpers.redirect(res, name + encodeURI(slug));
});
}
diff --git a/src/routes/index.js b/src/routes/index.js
index a6c51b17c9..37ce391e54 100644
--- a/src/routes/index.js
+++ b/src/routes/index.js
@@ -218,7 +218,7 @@ function handleErrors(app, middleware) {
}
if (parseInt(err.status, 10) === 302 && err.path) {
- return res.locals.isAPI ? res.status(302).json(err) : res.redirect(err.path);
+ return res.locals.isAPI ? res.status(302).json(err.path) : res.redirect(err.path);
}
res.status(err.status || 500);