Skip to content

Node.js based forum software built for the modern web

fix popular sort

Barış Soner Uşaklı8eb5564977e6
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);
}
};