removed checkPostIndex middleware

1 less call to get topic post count
This commit is contained in:
barisusakli
2014-09-20 18:51:32 -04:00
parent a19c306f49
commit b7fd4c22d4
3 changed files with 15 additions and 22 deletions

View File

@@ -36,6 +36,19 @@ topicsController.get = function(req, res, next) {
}, next);
},
function (results, next) {
var postCount = parseInt(results.postCount, 10) + 1;
if (utils.isNumber(req.params.post_index)) {
var url = '';
if (req.params.post_index > postCount) {
url = '/topic/' + req.params.topic_id + '/' + req.params.slug + '/' + postCount;
return res.locals.isAPI ? res.json(302, url) : res.redirect(url);
} else if (req.params.post_index < 1) {
url = '/topic/' + req.params.topic_id + '/' + req.params.slug;
return res.locals.isAPI ? res.json(302, url) : res.redirect(url);
}
}
userPrivileges = results.privileges;
if (userPrivileges.disabled) {
@@ -47,7 +60,6 @@ topicsController.get = function(req, res, next) {
}
var settings = results.settings;
var postCount = parseInt(results.postCount, 10) + 1;
var set = 'tid:' + tid + ':posts',
reverse = false;

View File

@@ -125,25 +125,6 @@ middleware.addSlug = function(req, res, next) {
next();
};
middleware.checkPostIndex = function(req, res, next) {
topics.getPostCount(req.params.topic_id, function(err, postCount) {
if (err) {
return next(err);
}
var postIndex = parseInt(req.params.post_index, 10);
postCount = parseInt(postCount, 10) + 1;
var url = '';
if (postIndex > postCount) {
url = '/topic/' + req.params.topic_id + '/' + req.params.slug + '/' + postCount;
return res.locals.isAPI ? res.json(302, url) : res.redirect(url);
} else if (postIndex < 1) {
url = '/topic/' + req.params.topic_id + '/' + req.params.slug;
return res.locals.isAPI ? res.json(302, url) : res.redirect(url);
}
next();
});
};
middleware.checkTopicIndex = function(req, res, next) {
db.sortedSetCard('categories:' + req.params.category_id + ':tid', function(err, topicCount) {
if (err) {

View File

@@ -54,8 +54,8 @@ function staticRoutes(app, middleware, controllers) {
function topicRoutes(app, middleware, controllers) {
app.get('/api/topic/teaser/:topic_id', controllers.topics.teaser);
app.get('/topic/:topic_id/:slug/:post_index?', middleware.applyCSRF, middleware.buildHeader, middleware.checkPostIndex, controllers.topics.get);
app.get('/api/topic/:topic_id/:slug/:post_index?', middleware.applyCSRF, middleware.checkPostIndex, controllers.topics.get);
app.get('/topic/:topic_id/:slug/:post_index?', middleware.applyCSRF, middleware.buildHeader, controllers.topics.get);
app.get('/api/topic/:topic_id/:slug/:post_index?', middleware.applyCSRF, controllers.topics.get);
app.get('/topic/:topic_id/:slug?', middleware.applyCSRF, middleware.buildHeader, middleware.addSlug, controllers.topics.get);
app.get('/api/topic/:topic_id/:slug?', middleware.applyCSRF, middleware.addSlug, controllers.topics.get);