mirror of
https://github.com/NodeBB/NodeBB.git
synced 2026-03-03 19:11:22 +01:00
fix popular sort
This commit is contained in:
@@ -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);
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user