From 8bbd7d971b83371c1d61d7e9f47c1a4b41c99f7c Mon Sep 17 00:00:00 2001 From: barisusakli Date: Sat, 7 Feb 2015 00:52:53 -0500 Subject: [PATCH] fix index to post if sorting is by votes and pagination is used --- src/controllers/topics.js | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/controllers/topics.js b/src/controllers/topics.js index 83d873b583..498a3d81db 100644 --- a/src/controllers/topics.js +++ b/src/controllers/topics.js @@ -89,17 +89,23 @@ topicsController.get = function(req, res, next) { var postIndex = 0; page = parseInt(req.query.page, 10) || 1; req.params.post_index = parseInt(req.params.post_index, 10) || 0; + if (reverse && req.params.post_index === 1) { + req.params.post_index = 0; + } if (!settings.usePagination) { if (reverse) { - if (req.params.post_index === 1) { - req.params.post_index = 0; - } - postIndex = Math.max(postCount - (req.params.post_index || postCount) - (settings.postsPerPage - 1), 0); + postIndex = Math.max(0, postCount - (req.params.post_index || postCount) - (settings.postsPerPage - 1)); } else { - postIndex = Math.max((req.params.post_index || 1) - (settings.postsPerPage + 1), 0); + postIndex = Math.max(0, (req.params.post_index || 1) - (settings.postsPerPage + 1)); } } else if (!req.query.page) { - var index = Math.max(req.params.post_index - 1, 0) || 0; + var index = 0; + if (reverse) { + index = Math.max(0, postCount - (req.params.post_index || postCount)); + } else { + index = Math.max(0, req.params.post_index - 1) || 0; + } + page = Math.max(1, Math.ceil(index / settings.postsPerPage)); }