From bd640a1107594706df1e746f089f850a16fbfae0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?=
 <barisusakli@gmail.com>
Date: Wed, 3 Jul 2019 12:48:26 -0400
Subject: [PATCH] Topic await errors (#7727)

* feat: derp await

* feat: restore user.getSettings

* feat: cleanup

* feat: make tid const

* feat: cleanup

* fix: tests

* feat: deprecate filter:controllers.topic.get

* feat: rewrite post-index logic

* feat: change calculation to match others

* fix: use .async

* feat: remove try/catch wrapper
---
 public/src/client/topic.js             |  15 +-
 public/src/client/topic/posts.js       |   2 +-
 public/src/modules/navigator.js        |  39 +---
 src/categories/topics.js               |   4 +-
 src/controllers/helpers.js             |   2 +
 src/controllers/topics.js              | 295 ++++++++++---------------
 src/database/mongo/sorted.js           |  24 +-
 src/database/postgres/sorted.js        |  16 ++
 src/database/redis/sorted.js           |  20 +-
 src/plugins/hooks.js                   |   4 +-
 src/posts/index.js                     |  19 +-
 src/posts/topics.js                    |   5 +-
 src/socket.io/topics/infinitescroll.js |  19 +-
 src/topics/index.js                    |   2 +-
 src/topics/posts.js                    |  21 +-
 test/categories.js                     |   4 +-
 test/database/sorted.js                |   9 +
 test/posts.js                          |  18 +-
 18 files changed, 234 insertions(+), 284 deletions(-)

diff --git a/public/src/client/topic.js b/public/src/client/topic.js
index 9c7254088d..a26f9f6a95 100644
--- a/public/src/client/topic.js
+++ b/public/src/client/topic.js
@@ -61,7 +61,7 @@ define('forum/topic', [
 		addDropupHandler();
 		addRepliesHandler();
 
-		navigator.init('[component="post"]', ajaxify.data.postcount, Topic.toTop, Topic.toBottom, Topic.navigatorCallback, Topic.calculateIndex);
+		navigator.init('[component="post"]', ajaxify.data.postcount, Topic.toTop, Topic.toBottom, Topic.navigatorCallback);
 
 		handleBookmark(tid);
 
@@ -110,9 +110,7 @@ define('forum/topic', [
 			if (err) {
 				return app.alertError(err.message);
 			}
-			if (config.topicPostSort !== 'oldest_to_newest') {
-				postCount = 2;
-			}
+
 			navigator.scrollBottom(postCount - 1);
 		});
 	};
@@ -122,7 +120,7 @@ define('forum/topic', [
 		var bookmark = ajaxify.data.bookmark || storage.getItem('topic:' + tid + ':bookmark');
 		var postIndex = ajaxify.data.postIndex;
 
-		if (postIndex > 0) {
+		if (postIndex > 1) {
 			if (components.get('post/anchor', postIndex - 1).length) {
 				return navigator.scrollToPostIndex(postIndex - 1, true, 0);
 			}
@@ -200,13 +198,6 @@ define('forum/topic', [
 		}
 	}
 
-	Topic.calculateIndex = function (index, elementCount) {
-		if (index !== 1 && config.topicPostSort !== 'oldest_to_newest') {
-			return elementCount - index + 2;
-		}
-		return index;
-	};
-
 	Topic.navigatorCallback = function (index, elementCount) {
 		var path = ajaxify.removeRelativePath(window.location.pathname.slice(1));
 		if (!path.startsWith('topic')) {
diff --git a/public/src/client/topic/posts.js b/public/src/client/topic/posts.js
index a7eaabf657..e03322f72e 100644
--- a/public/src/client/topic/posts.js
+++ b/public/src/client/topic/posts.js
@@ -199,7 +199,7 @@ define('forum/topic/posts', [
 				components.get('topic').append(html);
 			}
 
-			infinitescroll.removeExtra($('[component="post"]'), direction, config.postsPerPage * 2);
+			infinitescroll.removeExtra($('[component="post"]'), direction, Math.max(20, config.postsPerPage * 2));
 
 			$(window).trigger('action:posts.loaded', { posts: data.posts });
 
diff --git a/public/src/modules/navigator.js b/public/src/modules/navigator.js
index 9ab970fd57..552c0e86f6 100644
--- a/public/src/modules/navigator.js
+++ b/public/src/modules/navigator.js
@@ -25,7 +25,7 @@ define('navigator', ['forum/pagination', 'components'], function (pagination, co
 		$(window).off('keydown', onKeyDown);
 	});
 
-	navigator.init = function (selector, count, toTop, toBottom, callback, calculateIndex) {
+	navigator.init = function (selector, count, toTop, toBottom, callback) {
 		index = 1;
 		navigator.selector = selector;
 		navigator.callback = callback;
@@ -62,10 +62,6 @@ define('navigator', ['forum/pagination', 'components'], function (pagination, co
 				}
 
 				var index = parseInt(input.val(), 10);
-				if (typeof calculateIndex === 'function') {
-					index = calculateIndex(index, count);
-				}
-
 				var url = generateUrl(index);
 				input.val('');
 				$('.pagination-block .dropdown-toggle').trigger('click');
@@ -135,10 +131,9 @@ define('navigator', ['forum/pagination', 'components'], function (pagination, co
 				return app.alertError(err.message);
 			}
 
-			var relIndex = getRelativeIndex();
 			var date = new Date(timestamp);
 			var ds = date.toLocaleString(config.userLang, { month: 'long' });
-			touchTooltipEl.find('.text').translateText('[[global:pagination.out_of, ' + relIndex + ', ' + count + ']]');
+			touchTooltipEl.find('.text').translateText('[[global:pagination.out_of, ' + index + ', ' + count + ']]');
 			if (timestamp > Date.now() - (30 * 24 * 60 * 60 * 1000)) {
 				touchTooltipEl.find('.time').text(ds + ' ' + date.getDate());
 			} else {
@@ -277,26 +272,12 @@ define('navigator', ['forum/pagination', 'components'], function (pagination, co
 			return;
 		}
 		index = index > count ? count : index;
-		var relIndex = getRelativeIndex();
-		paginationTextEl.translateHtml('[[global:pagination.out_of, ' + relIndex + ', ' + count + ']]');
-		var fraction = (relIndex - 1) / (count - 1 || 1);
+		paginationTextEl.translateHtml('[[global:pagination.out_of, ' + index + ', ' + count + ']]');
+		var fraction = (index - 1) / (count - 1 || 1);
 		paginationBlockMeterEl.val(fraction);
 		paginationBlockProgressEl.width((fraction * 100) + '%');
 	};
 
-	function getRelativeIndex() {
-		var relIndex = index;
-		if (relIndex === 1) {
-			return 1;
-		}
-		if (ajaxify.data.template.topic) {
-			if (config.topicPostSort === 'most_votes' || config.topicPostSort === 'newest_to_oldest') {
-				relIndex = ajaxify.data.postcount - index + 2;
-			}
-		}
-		return relIndex;
-	}
-
 	navigator.scrollUp = function () {
 		var $window = $(window);
 
@@ -377,18 +358,8 @@ define('navigator', ['forum/pagination', 'components'], function (pagination, co
 		}
 
 		var scrollMethod = inTopic ? navigator.scrollToPostIndex : navigator.scrollToTopicIndex;
-		if (inTopic) {
-			if (config.topicPostSort === 'most_votes' || config.topicPostSort === 'newest_to_oldest') {
-				index = ajaxify.data.postcount - index;
-			}
-		} else if (inCategory) {
-			if (config.categoryTopicSort === 'most_posts' || config.categoryTopicSort === 'oldest_to_newest') {
-				index = ajaxify.data.topic_count - index;
-			}
-		}
-
-		var page = Math.max(1, Math.ceil((index + 1) / config.postsPerPage));
 
+		var page = 1 + Math.floor(index / config.postsPerPage);
 		if (parseInt(page, 10) !== ajaxify.data.pagination.currentPage) {
 			pagination.loadPage(page, function () {
 				scrollMethod(index, highlight, duration);
diff --git a/src/categories/topics.js b/src/categories/topics.js
index 0da5928505..a0d1f330b9 100644
--- a/src/categories/topics.js
+++ b/src/categories/topics.js
@@ -89,9 +89,7 @@ module.exports = function (Categories) {
 				var stop = data.stop === -1 ? data.stop : start + normalTidsToGet - 1;
 
 				if (Array.isArray(set)) {
-					var weights = set.map(function (s, index) {
-						return index ? 0 : 1;
-					});
+					const weights = set.map((s, index) => (index ? 0 : 1));
 					db[direction === 'highest-to-lowest' ? 'getSortedSetRevIntersect' : 'getSortedSetIntersect']({ sets: set, start: start, stop: stop, weights: weights }, next);
 				} else {
 					db[direction === 'highest-to-lowest' ? 'getSortedSetRevRange' : 'getSortedSetRange'](set, start, stop, next);
diff --git a/src/controllers/helpers.js b/src/controllers/helpers.js
index 9ce66d7bd6..394644d7b2 100644
--- a/src/controllers/helpers.js
+++ b/src/controllers/helpers.js
@@ -322,3 +322,5 @@ function recursive(category, categoriesData, level) {
 		});
 	}
 }
+
+helpers.async = require('../promisify')(helpers);
diff --git a/src/controllers/topics.js b/src/controllers/topics.js
index 702688c774..960b91d4cd 100644
--- a/src/controllers/topics.js
+++ b/src/controllers/topics.js
@@ -8,7 +8,7 @@ var winston = require('winston');
 var user = require('../user');
 var meta = require('../meta');
 var topics = require('../topics');
-var posts = require('../posts').async;
+var posts = require('../posts');
 var privileges = require('../privileges');
 var plugins = require('../plugins');
 var helpers = require('./helpers');
@@ -18,191 +18,140 @@ var analytics = require('../analytics');
 
 var topicsController = module.exports;
 
-topicsController.get = function getTopic(req, res, callback) {
-	var tid = req.params.topic_id;
-	var currentPage = parseInt(req.query.page, 10) || 1;
-	var pageCount = 1;
-	var postCount = 0;
-	var topicData;
-	var userPrivileges;
-	var settings;
-	var rssToken;
+topicsController.get = async function getTopic(req, res, callback) {
+	const tid = req.params.topic_id;
 
 	if ((req.params.post_index && !utils.isNumber(req.params.post_index) && req.params.post_index !== 'unread') || !utils.isNumber(tid)) {
 		return callback();
 	}
+	let postIndex = parseInt(req.params.post_index, 10) || 1;
+	const [
+		userPrivileges,
+		settings,
+		topicData,
+		rssToken,
+	] = await Promise.all([
+		privileges.async.topics.get(tid, req.uid),
+		user.async.getSettings(req.uid),
+		topics.async.getTopicData(tid),
+		user.async.auth.getFeedToken(req.uid),
+	]);
 
-	async.waterfall([
-		function (next) {
-			async.parallel({
-				privileges: function (next) {
-					privileges.topics.get(tid, req.uid, next);
-				},
-				settings: function (next) {
-					user.getSettings(req.uid, next);
-				},
-				topic: function (next) {
-					topics.getTopicData(tid, next);
-				},
-				rssToken: function (next) {
-					user.auth.getFeedToken(req.uid, next);
-				},
-			}, next);
-		},
-		function (results, next) {
-			topicData = results.topic;
-			if (!topicData) {
-				return callback();
-			}
+	var currentPage = parseInt(req.query.page, 10) || 1;
+	const pageCount = Math.max(1, Math.ceil((topicData && topicData.postcount) / settings.postsPerPage));
+	if (!topicData || userPrivileges.disabled || (settings.usePagination && (currentPage < 1 || currentPage > pageCount))) {
+		return callback();
+	}
 
-			userPrivileges = results.privileges;
-			rssToken = results.rssToken;
+	if (!userPrivileges['topics:read'] || (topicData.deleted && !userPrivileges.view_deleted)) {
+		return helpers.notAllowed(req, res);
+	}
 
-			if (!userPrivileges['topics:read'] || (results.topic.deleted && !userPrivileges.view_deleted)) {
-				return helpers.notAllowed(req, res);
-			}
+	if (!res.locals.isAPI && (!req.params.slug || topicData.slug !== tid + '/' + req.params.slug) && (topicData.slug && topicData.slug !== tid + '/')) {
+		return helpers.redirect(res, '/topic/' + topicData.slug + (postIndex ? '/' + postIndex : '') + (currentPage > 1 ? '?page=' + currentPage : ''));
+	}
 
-			if (!res.locals.isAPI && (!req.params.slug || results.topic.slug !== tid + '/' + req.params.slug) && (results.topic.slug && results.topic.slug !== tid + '/')) {
-				var url = '/topic/' + results.topic.slug;
-				if (req.params.post_index) {
-					url += '/' + req.params.post_index;
-				}
-				if (currentPage > 1) {
-					url += '?page=' + currentPage;
-				}
-				return helpers.redirect(res, url);
-			}
+	if (postIndex === 'unread') {
+		postIndex = await topics.async.getUserBookmark(tid, req.uid);
+	}
 
-			settings = results.settings;
-			postCount = results.topic.postcount;
-			pageCount = Math.max(1, Math.ceil(postCount / settings.postsPerPage));
+	if (utils.isNumber(postIndex) && (postIndex < 1 || postIndex > topicData.postcount)) {
+		return helpers.redirect(res, '/topic/' + req.params.topic_id + '/' + req.params.slug + (postIndex > topicData.postcount ? '/' + topicData.postcount : ''));
+	}
+	postIndex = Math.max(1, postIndex);
+	const sort = req.query.sort || settings.topicPostSort;
+	const set = sort === 'most_votes' ? 'tid:' + tid + ':posts:votes' : 'tid:' + tid + ':posts';
+	const reverse = sort === 'newest_to_oldest' || sort === 'most_votes';
+	if (settings.usePagination && !req.query.page) {
+		currentPage = calculatePageFromIndex(postIndex, settings);
+	}
+	const { start, stop } = calculateStartStop(currentPage, postIndex, settings);
+
+	await topics.async.getTopicWithPosts(topicData, set, req.uid, start, stop, reverse);
+
+	topics.modifyPostsByPrivilege(topicData, userPrivileges);
+
+	const hookData = await plugins.async.fireHook('filter:controllers.topic.get', { topicData: topicData, uid: req.uid });
+	await Promise.all([
+		buildBreadcrumbs(hookData.topicData),
+		addTags(topicData, req, res),
+	]);
+
+	topicData.privileges = userPrivileges;
+	topicData.topicStaleDays = meta.config.topicStaleDays;
+	topicData['reputation:disabled'] = meta.config['reputation:disabled'];
+	topicData['downvote:disabled'] = meta.config['downvote:disabled'];
+	topicData['feeds:disableRSS'] = meta.config['feeds:disableRSS'];
+	topicData.bookmarkThreshold = meta.config.bookmarkThreshold;
+	topicData.postEditDuration = meta.config.postEditDuration;
+	topicData.postDeleteDuration = meta.config.postDeleteDuration;
+	topicData.scrollToMyPost = settings.scrollToMyPost;
+	topicData.allowMultipleBadges = meta.config.allowMultipleBadges === 1;
+	topicData.rssFeedUrl = nconf.get('relative_path') + '/topic/' + topicData.tid + '.rss';
+	if (req.loggedIn) {
+		topicData.rssFeedUrl += '?uid=' + req.uid + '&token=' + rssToken;
+	}
 
-			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 : ''));
-			}
+	topicData.postIndex = postIndex;
+	topicData.pagination = pagination.create(currentPage, pageCount, req.query);
+	topicData.pagination.rel.forEach(function (rel) {
+		rel.href = nconf.get('url') + '/topic/' + topicData.slug + rel.href;
+		res.locals.linkTags.push(rel);
+	});
 
-			if (settings.usePagination && (currentPage < 1 || currentPage > pageCount)) {
-				return callback();
-			}
+	incrementViewCount(req, tid);
 
-			if (req.params.post_index === 'unread') {
-				topics.getUserBookmark(tid, req.uid, next);
-			} else {
-				next(null, req.params.post_index);
-			}
-		},
-		function (post_index, next) {
-			req.params.post_index = post_index;
-
-			var set = 'tid:' + tid + ':posts';
-			var reverse = false;
-			// `sort` qs has priority over user setting
-			var sort = req.query.sort || settings.topicPostSort;
-			if (sort === 'newest_to_oldest') {
-				reverse = true;
-			} else if (sort === 'most_votes') {
-				reverse = true;
-				set = 'tid:' + tid + ':posts:votes';
-			}
+	markAsRead(req, tid);
 
-			var postIndex = 0;
+	analytics.increment(['pageviews:byCid:' + topicData.category.cid]);
 
-			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 (req.params.post_index !== 0) {
-					currentPage = 1;
-				}
-				if (reverse) {
-					postIndex = Math.max(0, postCount - (req.params.post_index || postCount) - Math.ceil(settings.postsPerPage / 2));
-				} else {
-					postIndex = Math.max(0, (req.params.post_index || 1) - Math.ceil(settings.postsPerPage / 2));
-				}
-			} else if (!req.query.page) {
-				var index;
-				if (reverse) {
-					index = Math.max(0, postCount - (req.params.post_index || postCount) + 2);
-				} else {
-					index = Math.max(0, req.params.post_index) || 0;
-				}
-
-				currentPage = Math.max(1, Math.ceil(index / settings.postsPerPage));
-			}
+	res.render('topic', topicData);
+};
+
+function calculatePageFromIndex(postIndex, settings) {
+	return 1 + Math.floor((postIndex - 1) / settings.postsPerPage);
+}
 
-			var start = ((currentPage - 1) * settings.postsPerPage) + postIndex;
-			var stop = start + settings.postsPerPage - 1;
+function calculateStartStop(page, postIndex, settings) {
+	var startSkip = 0;
 
-			topics.getTopicWithPosts(topicData, set, req.uid, start, stop, reverse, next);
-		},
-		function (topicData, next) {
-			if (topicData.category.disabled) {
-				return callback();
-			}
+	if (!settings.usePagination) {
+		if (postIndex !== 0) {
+			page = 1;
+		}
+		startSkip = Math.max(0, postIndex - Math.ceil(settings.postsPerPage / 2));
+	}
 
-			topics.modifyPostsByPrivilege(topicData, userPrivileges);
+	const start = ((page - 1) * settings.postsPerPage) + startSkip;
+	const stop = start + settings.postsPerPage - 1;
+	return { start: Math.max(0, start), stop: Math.max(0, stop) };
+}
 
-			plugins.fireHook('filter:controllers.topic.get', { topicData: topicData, uid: req.uid }, next);
-		},
-		function (data, next) {
-			buildBreadcrumbs(data.topicData, next);
-		},
-		async function (topicData) {
-			await addTags(topicData, req, res);
-			return topicData;
-		},
-		function (topicData) {
-			topicData.privileges = userPrivileges;
-			topicData.topicStaleDays = meta.config.topicStaleDays;
-			topicData['reputation:disabled'] = meta.config['reputation:disabled'];
-			topicData['downvote:disabled'] = meta.config['downvote:disabled'];
-			topicData['feeds:disableRSS'] = meta.config['feeds:disableRSS'];
-			topicData.bookmarkThreshold = meta.config.bookmarkThreshold;
-			topicData.postEditDuration = meta.config.postEditDuration;
-			topicData.postDeleteDuration = meta.config.postDeleteDuration;
-			topicData.scrollToMyPost = settings.scrollToMyPost;
-			topicData.allowMultipleBadges = meta.config.allowMultipleBadges === 1;
-			topicData.rssFeedUrl = nconf.get('relative_path') + '/topic/' + topicData.tid + '.rss';
-			if (req.loggedIn) {
-				topicData.rssFeedUrl += '?uid=' + req.uid + '&token=' + rssToken;
-			}
+function incrementViewCount(req, tid) {
+	if (req.uid >= 0) {
+		req.session.tids_viewed = req.session.tids_viewed || {};
+		if (!req.session.tids_viewed[tid] || req.session.tids_viewed[tid] < Date.now() - 3600000) {
+			topics.increaseViewCount(tid);
+			req.session.tids_viewed[tid] = Date.now();
+		}
+	}
+}
 
-			topicData.postIndex = req.params.post_index;
-			topicData.pagination = pagination.create(currentPage, pageCount, req.query);
-			topicData.pagination.rel.forEach(function (rel) {
-				rel.href = nconf.get('url') + '/topic/' + topicData.slug + rel.href;
-				res.locals.linkTags.push(rel);
-			});
-
-			if (req.uid >= 0) {
-				req.session.tids_viewed = req.session.tids_viewed || {};
-				if (!req.session.tids_viewed[tid] || req.session.tids_viewed[tid] < Date.now() - 3600000) {
-					topics.increaseViewCount(tid);
-					req.session.tids_viewed[tid] = Date.now();
-				}
+function markAsRead(req, tid) {
+	if (req.loggedIn) {
+		topics.markAsRead([tid], req.uid, function (err, markedRead) {
+			if (err) {
+				return winston.error(err);
 			}
-
-			if (req.loggedIn) {
-				topics.markAsRead([tid], req.uid, function (err, markedRead) {
-					if (err) {
-						return winston.error(err);
-					}
-					if (markedRead) {
-						topics.pushUnreadCount(req.uid);
-						topics.markTopicNotificationsRead([tid], req.uid);
-					}
-				});
+			if (markedRead) {
+				topics.pushUnreadCount(req.uid);
+				topics.markTopicNotificationsRead([tid], req.uid);
 			}
+		});
+	}
+}
 
-			analytics.increment(['pageviews:byCid:' + topicData.category.cid]);
-
-			res.render('topic', topicData);
-		},
-	], callback);
-};
-
-function buildBreadcrumbs(topicData, callback) {
+async function buildBreadcrumbs(topicData) {
 	var breadcrumbs = [
 		{
 			text: topicData.category.name,
@@ -212,22 +161,12 @@ function buildBreadcrumbs(topicData, callback) {
 			text: topicData.title,
 		},
 	];
-
-	async.waterfall([
-		function (next) {
-			helpers.buildCategoryBreadcrumbs(topicData.category.parentCid, next);
-		},
-		function (crumbs, next) {
-			topicData.breadcrumbs = crumbs.concat(breadcrumbs);
-			next(null, topicData);
-		},
-	], callback);
+	const parentCrumbs = await helpers.async.buildCategoryBreadcrumbs(topicData.category.parentCid);
+	topicData.breadcrumbs = parentCrumbs.concat(breadcrumbs);
 }
 
 async function addTags(topicData, req, res) {
-	var postAtIndex = topicData.posts.find(function (postData) {
-		return parseInt(postData.index, 10) === parseInt(Math.max(0, req.params.post_index - 1), 10);
-	});
+	var postAtIndex = topicData.posts.find(p => parseInt(p.index, 10) === parseInt(Math.max(0, req.params.post_index - 1), 10));
 
 	var description = '';
 	if (postAtIndex && postAtIndex.content) {
@@ -300,7 +239,7 @@ async function addTags(topicData, req, res) {
 }
 
 async function addOGImageTags(res, topicData, postAtIndex) {
-	const uploads = postAtIndex ? await posts.uploads.listWithSizes(postAtIndex.pid) : [];
+	const uploads = postAtIndex ? await posts.async.uploads.listWithSizes(postAtIndex.pid) : [];
 	const images = uploads.map((upload) => {
 		upload.name = nconf.get('url') + nconf.get('upload_url') + '/files/' + upload.name;
 		return upload;
diff --git a/src/database/mongo/sorted.js b/src/database/mongo/sorted.js
index b70be28198..874b9b7a01 100644
--- a/src/database/mongo/sorted.js
+++ b/src/database/mongo/sorted.js
@@ -208,6 +208,14 @@ module.exports = function (db, module) {
 	}
 
 	module.sortedSetsRanks = function (keys, values, callback) {
+		sortedSetsRanks(module.sortedSetRank, keys, values, callback);
+	};
+
+	module.sortedSetsRevRanks = function (keys, values, callback) {
+		sortedSetsRanks(module.sortedSetRevRank, keys, values, callback);
+	};
+
+	function sortedSetsRanks(method, keys, values, callback) {
 		if (!Array.isArray(keys) || !keys.length) {
 			return callback(null, []);
 		}
@@ -217,12 +225,20 @@ module.exports = function (db, module) {
 		}
 
 		async.map(data, function (item, next) {
-			getSortedSetRank(false, item.key, item.value, next);
+			method(item.key, item.value, next);
 		}, callback);
-	};
+	}
 
 	module.sortedSetRanks = function (key, values, callback) {
-		module.getSortedSetRange(key, 0, -1, function (err, sortedSet) {
+		sortedSetRanks(module.getSortedSetRange, key, values, callback);
+	};
+
+	module.sortedSetRevRanks = function (key, values, callback) {
+		sortedSetRanks(module.getSortedSetRevRange, key, values, callback);
+	};
+
+	function sortedSetRanks(method, key, values, callback) {
+		method(key, 0, -1, function (err, sortedSet) {
 			if (err) {
 				return callback(err);
 			}
@@ -237,7 +253,7 @@ module.exports = function (db, module) {
 
 			callback(null, result);
 		});
-	};
+	}
 
 	module.sortedSetScore = function (key, value, callback) {
 		if (!key) {
diff --git a/src/database/postgres/sorted.js b/src/database/postgres/sorted.js
index 2a5610fbfa..9f06248298 100644
--- a/src/database/postgres/sorted.js
+++ b/src/database/postgres/sorted.js
@@ -323,6 +323,14 @@ SELECT (SELECT r
 		getSortedSetRank('ASC', keys, values, callback);
 	};
 
+	module.sortedSetsRevRanks = function (keys, values, callback) {
+		if (!Array.isArray(keys) || !keys.length) {
+			return callback(null, []);
+		}
+
+		getSortedSetRank('DESC', keys, values, callback);
+	};
+
 	module.sortedSetRanks = function (key, values, callback) {
 		if (!Array.isArray(values) || !values.length) {
 			return callback(null, []);
@@ -331,6 +339,14 @@ SELECT (SELECT r
 		getSortedSetRank('ASC', new Array(values.length).fill(key), values, callback);
 	};
 
+	module.sortedSetRevRanks = function (key, values, callback) {
+		if (!Array.isArray(values) || !values.length) {
+			return callback(null, []);
+		}
+
+		getSortedSetRank('DESC', new Array(values.length).fill(key), values, callback);
+	};
+
 	module.sortedSetScore = function (key, value, callback) {
 		if (!key) {
 			return callback(null, null);
diff --git a/src/database/redis/sorted.js b/src/database/redis/sorted.js
index 8f5202ebc9..ebed45130a 100644
--- a/src/database/redis/sorted.js
+++ b/src/database/redis/sorted.js
@@ -149,6 +149,10 @@ module.exports = function (redisClient, module) {
 		redisClient.zrank(key, value, callback);
 	};
 
+	module.sortedSetRevRank = function (key, value, callback) {
+		redisClient.zrevrank(key, value, callback);
+	};
+
 	module.sortedSetsRanks = function (keys, values, callback) {
 		var batch = redisClient.batch();
 		for (var i = 0; i < values.length; i += 1) {
@@ -157,6 +161,14 @@ module.exports = function (redisClient, module) {
 		batch.exec(callback);
 	};
 
+	module.sortedSetsRevRanks = function (keys, values, callback) {
+		var batch = redisClient.batch();
+		for (var i = 0; i < values.length; i += 1) {
+			batch.zrevrank(keys[i], values[i]);
+		}
+		batch.exec(callback);
+	};
+
 	module.sortedSetRanks = function (key, values, callback) {
 		var batch = redisClient.batch();
 		for (var i = 0; i < values.length; i += 1) {
@@ -165,8 +177,12 @@ module.exports = function (redisClient, module) {
 		batch.exec(callback);
 	};
 
-	module.sortedSetRevRank = function (key, value, callback) {
-		redisClient.zrevrank(key, value, callback);
+	module.sortedSetRevRanks = function (key, values, callback) {
+		var batch = redisClient.batch();
+		for (var i = 0; i < values.length; i += 1) {
+			batch.zrevrank(key, values[i]);
+		}
+		batch.exec(callback);
 	};
 
 	module.sortedSetScore = function (key, value, callback) {
diff --git a/src/plugins/hooks.js b/src/plugins/hooks.js
index 73c5cdf449..06a948bc6f 100644
--- a/src/plugins/hooks.js
+++ b/src/plugins/hooks.js
@@ -6,7 +6,7 @@ const utils = require('../utils');
 
 module.exports = function (Plugins) {
 	Plugins.deprecatedHooks = {
-
+		'filter:controllers.topic.get': 'filter:topic.build',
 	};
 
 	Plugins.internals = {
@@ -34,7 +34,7 @@ module.exports = function (Plugins) {
 
 		var method;
 
-		if (Object.keys(Plugins.deprecatedHooks).includes(data.hook)) {
+		if (Plugins.deprecatedHooks[data.hook]) {
 			winston.warn('[plugins/' + id + '] Hook `' + data.hook + '` is deprecated, ' +
 				(Plugins.deprecatedHooks[data.hook] ?
 					'please use `' + Plugins.deprecatedHooks[data.hook] + '` instead.' :
diff --git a/src/posts/index.js b/src/posts/index.js
index 3f18c2c9e9..d8b83a4c9d 100644
--- a/src/posts/index.js
+++ b/src/posts/index.js
@@ -88,8 +88,9 @@ Posts.getPostSummariesFromSet = function (set, uid, start, stop, callback) {
 Posts.getPidIndex = function (pid, tid, topicPostSort, callback) {
 	async.waterfall([
 		function (next) {
-			var set = topicPostSort === 'most_votes' ? 'tid:' + tid + ':posts:votes' : 'tid:' + tid + ':posts';
-			db.sortedSetRank(set, pid, next);
+			const set = topicPostSort === 'most_votes' ? 'tid:' + tid + ':posts:votes' : 'tid:' + tid + ':posts';
+			const reverse = topicPostSort === 'newest_to_oldest' || topicPostSort === 'most_votes';
+			db[reverse ? 'sortedSetRevRank' : 'sortedSetRank'](set, pid, next);
 		},
 		function (index, next) {
 			if (!utils.isNumber(index)) {
@@ -111,21 +112,17 @@ Posts.getPostIndices = function (posts, uid, callback) {
 		},
 		function (settings, next) {
 			var byVotes = settings.topicPostSort === 'most_votes';
-			var sets = posts.map(function (post) {
-				return byVotes ? 'tid:' + post.tid + ':posts:votes' : 'tid:' + post.tid + ':posts';
-			});
+			var sets = posts.map(p => (byVotes ? 'tid:' + p.tid + ':posts:votes' : 'tid:' + p.tid + ':posts'));
+			const reverse = settings.topicPostSort === 'newest_to_oldest' || settings.topicPostSort === 'most_votes';
 
 			var uniqueSets = _.uniq(sets);
-			var method = 'sortedSetsRanks';
+			var method = reverse ? 'sortedSetsRevRanks' : 'sortedSetsRanks';
 			if (uniqueSets.length === 1) {
-				method = 'sortedSetRanks';
+				method = reverse ? 'sortedSetRevRanks' : 'sortedSetRanks';
 				sets = uniqueSets[0];
 			}
 
-			var pids = posts.map(function (post) {
-				return post.pid;
-			});
-
+			const pids = posts.map(post => post.pid);
 			db[method](sets, pids, next);
 		},
 		function (indices, next) {
diff --git a/src/posts/topics.js b/src/posts/topics.js
index 219be7b0d3..afd60f1a23 100644
--- a/src/posts/topics.js
+++ b/src/posts/topics.js
@@ -60,10 +60,7 @@ module.exports = function (Posts) {
 						Posts.getPostIndices(postData, uid, next);
 					},
 					topics: function (next) {
-						var tids = postData.map(function (post) {
-							return post ? post.tid : null;
-						});
-
+						const tids = postData.map(post => post && post.tid);
 						topics.getTopicsFields(tids, ['slug'], next);
 					},
 				}, next);
diff --git a/src/socket.io/topics/infinitescroll.js b/src/socket.io/topics/infinitescroll.js
index dd5897b0c5..11523d79b5 100644
--- a/src/socket.io/topics/infinitescroll.js
+++ b/src/socket.io/topics/infinitescroll.js
@@ -34,26 +34,17 @@ module.exports = function (SocketTopics) {
 
 				userPrivileges = results.privileges;
 
-				var set = 'tid:' + data.tid + ':posts';
-				if (data.topicPostSort === 'most_votes') {
-					set = 'tid:' + data.tid + ':posts:votes';
-				}
+				var set = data.topicPostSort === 'most_votes' ? 'tid:' + data.tid + ':posts:votes' : 'tid:' + data.tid + ':posts';
 				var reverse = data.topicPostSort === 'newest_to_oldest' || data.topicPostSort === 'most_votes';
 				var start = Math.max(0, parseInt(data.after, 10));
 
-				var infScrollPostsPerPage = Math.max(0, Math.min(meta.config.postsPerPage || 20, parseInt(data.count, 10) || meta.config.postsPerPage || 20) - 1);
+				var infScrollPostsPerPage = Math.max(0, Math.min(meta.config.postsPerPage || 20, parseInt(data.count, 10) || meta.config.postsPerPage || 20));
 
-				if (data.direction > 0) {
-					if (reverse) {
-						start = results.topic.postcount - start;
-					}
-				} else if (reverse) {
-					start = results.topic.postcount - start - infScrollPostsPerPage;
-				} else {
-					start -= infScrollPostsPerPage;
+				if (data.direction === -1) {
+					start -= (infScrollPostsPerPage + 1);
 				}
 
-				var stop = start + (infScrollPostsPerPage);
+				var stop = start + infScrollPostsPerPage - 1;
 
 				start = Math.max(0, start);
 				stop = Math.max(0, stop);
diff --git a/src/topics/index.js b/src/topics/index.js
index 5d68e23dcf..accdc9041e 100644
--- a/src/topics/index.js
+++ b/src/topics/index.js
@@ -246,7 +246,7 @@ function getMainPostAndReplies(topic, set, uid, start, stop, reverse, callback)
 				replies = posts.slice(1);
 			}
 
-			Topics.calculatePostIndices(replies, start, topic.postcount, reverse);
+			Topics.calculatePostIndices(replies, start);
 
 			Topics.addPostData(posts, uid, next);
 		},
diff --git a/src/topics/posts.js b/src/topics/posts.js
index b0358374cc..c37bd38b88 100644
--- a/src/topics/posts.js
+++ b/src/topics/posts.js
@@ -27,19 +27,12 @@ module.exports = function (Topics) {
 	Topics.getTopicPosts = function (tid, set, start, stop, uid, reverse, callback) {
 		async.waterfall([
 			function (next) {
-				async.parallel({
-					posts: function (next) {
-						posts.getPostsFromSet(set, start, stop, uid, reverse, next);
-					},
-					postCount: function (next) {
-						Topics.getTopicField(tid, 'postcount', next);
-					},
-				}, next);
+				posts.getPostsFromSet(set, start, stop, uid, reverse, next);
 			},
-			function (results, next) {
-				Topics.calculatePostIndices(results.posts, start, results.postCount, reverse);
+			function (posts, next) {
+				Topics.calculatePostIndices(posts, start);
 
-				Topics.addPostData(results.posts, uid, next);
+				Topics.addPostData(posts, uid, next);
 			},
 		], callback);
 	};
@@ -185,11 +178,9 @@ module.exports = function (Topics) {
 		], callback);
 	};
 
-	Topics.calculatePostIndices = function (posts, start, postCount, reverse) {
+	Topics.calculatePostIndices = function (posts, start) {
 		posts.forEach(function (post, index) {
-			if (reverse) {
-				post.index = postCount - (start + index + 1);
-			} else {
+			if (post) {
 				post.index = start + index + 1;
 			}
 		});
diff --git a/test/categories.js b/test/categories.js
index 334697da86..26f6c4157d 100644
--- a/test/categories.js
+++ b/test/categories.js
@@ -124,7 +124,7 @@ describe('Categories', function () {
 				start: 0,
 				stop: 10,
 				uid: 0,
-				sort: 'oldest-to-newest',
+				sort: 'oldest_to_newest',
 			}, function (err, result) {
 				assert.equal(err, null);
 
@@ -144,7 +144,7 @@ describe('Categories', function () {
 				stop: 10,
 				uid: 0,
 				targetUid: 1,
-				sort: 'oldest-to-newest',
+				sort: 'oldest_to_newest',
 			}, function (err, result) {
 				assert.equal(err, null);
 				assert(Array.isArray(result.topics));
diff --git a/test/database/sorted.js b/test/database/sorted.js
index ea8a7feb25..4d5acfef52 100644
--- a/test/database/sorted.js
+++ b/test/database/sorted.js
@@ -589,6 +589,15 @@ describe('Sorted Set methods', function () {
 				done();
 			});
 		});
+
+		it('should return the ranks of values in a sorted set in reverse', function (done) {
+			db.sortedSetRevRanks('sortedSetTest1', ['value2', 'value1', 'value3', 'value4'], function (err, ranks) {
+				assert.equal(err, null);
+				assert.equal(arguments.length, 2);
+				assert.deepEqual(ranks, [1, 2, 0, null]);
+				done();
+			});
+		});
 	});
 
 	describe('sortedSetScore()', function () {
diff --git a/test/posts.js b/test/posts.js
index cca10542e0..de2aa50bd3 100644
--- a/test/posts.js
+++ b/test/posts.js
@@ -812,12 +812,28 @@ describe('Post\'s', function () {
 		});
 
 		it('should get pid index', function (done) {
-			socketPosts.getPidIndex({ uid: voterUid }, { pid: pid, tid: topicData.tid, topicPostSort: 'oldest-to-newest' }, function (err, index) {
+			socketPosts.getPidIndex({ uid: voterUid }, { pid: pid, tid: topicData.tid, topicPostSort: 'oldest_to_newest' }, function (err, index) {
 				assert.ifError(err);
 				assert.equal(index, 2);
 				done();
 			});
 		});
+
+		it('should get pid index in reverse', function (done) {
+			topics.reply({
+				uid: voterUid,
+				tid: topicData.tid,
+				content: 'raw content',
+			}, function (err, postData) {
+				assert.ifError(err);
+
+				socketPosts.getPidIndex({ uid: voterUid }, { pid: postData.pid, tid: topicData.tid, topicPostSort: 'newest_to_oldest' }, function (err, index) {
+					assert.ifError(err);
+					assert.equal(index, 1);
+					done();
+				});
+			});
+		});
 	});
 
 	describe('filterPidsByCid', function () {