added helpers.redirect

v1.18.x
barisusakli 10 years ago
parent f0ad2020df
commit ddd4680ea7

@ -200,8 +200,7 @@ categoriesController.get = function(req, res, next) {
var topicCount = parseInt(results.categoryData.topic_count, 10); var topicCount = parseInt(results.categoryData.topic_count, 10);
if (topicIndex < 0 || topicIndex > Math.max(topicCount - 1, 0)) { if (topicIndex < 0 || topicIndex > Math.max(topicCount - 1, 0)) {
var url = '/category/' + cid + '/' + req.params.slug + (topicIndex > topicCount ? '/' + topicCount : ''); return helpers.redirect(res, '/category/' + cid + '/' + req.params.slug + (topicIndex > topicCount ? '/' + topicCount : ''));
return res.locals.isAPI ? res.status(302).json(url) : res.redirect(url);
} }
userPrivileges = results.privileges; userPrivileges = results.privileges;

@ -51,31 +51,35 @@ groupsController.details = function(req, res, next) {
} }
} }
], function(err, ok) { ], function(err, ok) {
if (ok) { if (err) {
async.parallel({ return next(err);
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); if (!ok) {
}); return helpers.redirect(res, '/groups');
} else {
return res.locals.isAPI ? res.status(302).json('/groups') : res.redirect('/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);
});
}); });
}; };

@ -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) { helpers.buildCategoryBreadcrumbs = function(cid, callback) {
var breadcrumbs = []; var breadcrumbs = [];

@ -56,12 +56,8 @@ topicsController.get = function(req, res, next) {
var postCount = parseInt(results.topic.postcount, 10); var postCount = parseInt(results.topic.postcount, 10);
var pageCount = Math.max(1, Math.ceil((postCount - 1) / settings.postsPerPage)); var pageCount = Math.max(1, Math.ceil((postCount - 1) / settings.postsPerPage));
if (utils.isNumber(req.params.post_index)) { if (utils.isNumber(req.params.post_index) && (req.params.post_index < 1 || req.params.post_index > postCount)) {
var url = ''; return helpers.redirect(res, '/topic/' + req.params.topic_id + '/' + req.params.slug + (req.params.post_index > postCount ? '/' + postCount : ''));
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 (settings.usePagination && (req.query.page < 1 || req.query.page > pageCount)) { if (settings.usePagination && (req.query.page < 1 || req.query.page > pageCount)) {

@ -60,12 +60,7 @@ middleware.redirectToAccountIfLoggedIn = function(req, res, next) {
if (err) { if (err) {
return next(err); return next(err);
} }
helpers.redirect(res, '/user/' + userslug);
if (res.locals.isAPI) {
res.status(302).json(nconf.get('relative_path') + '/user/' + userslug);
} else {
res.redirect(nconf.get('relative_path') + '/user/' + userslug);
}
}); });
}; };
@ -85,13 +80,7 @@ middleware.addSlug = function(req, res, next) {
return next(err); return next(err);
} }
var url = nconf.get('relative_path') + name + encodeURI(slug); helpers.redirect(res, name + encodeURI(slug));
if (res.locals.isAPI) {
res.status(302).json(url);
} else {
res.redirect(url);
}
}); });
} }

@ -218,7 +218,7 @@ function handleErrors(app, middleware) {
} }
if (parseInt(err.status, 10) === 302 && err.path) { 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); res.status(err.status || 500);

Loading…
Cancel
Save