From 8eb5564977e69408df6bdd4cbd1a7aff5c7a2c15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Sun, 28 May 2017 16:48:47 -0400 Subject: [PATCH] fix popular sort --- src/topics/popular.js | 40 ++++++++++++++++------------------------ 1 file changed, 16 insertions(+), 24 deletions(-) diff --git a/src/topics/popular.js b/src/topics/popular.js index b85e66d213..85d25abeaf 100644 --- a/src/topics/popular.js +++ b/src/topics/popular.js @@ -9,15 +9,7 @@ module.exports = function (Topics) { count = parseInt(count, 10) || 20; if (term === 'alltime') { - async.waterfall([ - function (next) { - getAllTimePopular(uid, count, next); - }, - function (topics, next) { - sortTiedTopicsByViews(topics, next); - }, - ], callback); - return; + return getAllTimePopular(uid, count, callback); } async.waterfall([ @@ -27,16 +19,19 @@ module.exports = function (Topics) { function (tids, next) { getTopics(tids, uid, count, next); }, - function (topics, next) { - sortTiedTopicsByViews(topics, next); - }, ], callback); }; function getAllTimePopular(uid, count, callback) { - Topics.getTopicsFromSet('topics:posts', uid, 0, count - 1, function (err, data) { - callback(err, data ? data.topics : null); - }); + async.waterfall([ + function (next) { + Topics.getTopicsFromSet('topics:posts', uid, 0, count - 1, next); + }, + function (data, next) { + data.topics.sort(sortPopular); + next(null, data.topics); + }, + ], callback); } function getTopics(tids, uid, count, callback) { @@ -47,9 +42,7 @@ module.exports = function (Topics) { function (topics, next) { tids = topics.filter(function (topic) { return topic && parseInt(topic.deleted, 10) !== 1; - }).sort(function (a, b) { - return b.postcount - a.postcount; - }).slice(0, count).map(function (topic) { + }).sort(sortPopular).slice(0, count).map(function (topic) { return topic.tid; }); privileges.topics.filterTids('read', tids, uid, next); @@ -60,11 +53,10 @@ module.exports = function (Topics) { ], callback); } - function sortTiedTopicsByViews(topics, next) { - topics.sort(function (a, b) { - return parseInt(a.postcount, 10) !== parseInt(b.postcount, 10) ? 0 : parseInt(b.viewcount, 10) - parseInt(a.viewcount, 10); - }); - - next(null, topics); + function sortPopular(a, b) { + if (parseInt(a.postcount, 10) !== parseInt(b.postcount, 10)) { + return b.postcount - a.postcount; + } + return parseInt(b.viewcount, 10) - parseInt(a.viewcount, 10); } };