fix popular sort

v1.18.x
Barış Soner Uşaklı 8 years ago
parent 57c0861310
commit 8eb5564977

@ -9,15 +9,7 @@ module.exports = function (Topics) {
count = parseInt(count, 10) || 20; count = parseInt(count, 10) || 20;
if (term === 'alltime') { if (term === 'alltime') {
async.waterfall([ return getAllTimePopular(uid, count, callback);
function (next) {
getAllTimePopular(uid, count, next);
},
function (topics, next) {
sortTiedTopicsByViews(topics, next);
},
], callback);
return;
} }
async.waterfall([ async.waterfall([
@ -27,16 +19,19 @@ module.exports = function (Topics) {
function (tids, next) { function (tids, next) {
getTopics(tids, uid, count, next); getTopics(tids, uid, count, next);
}, },
function (topics, next) {
sortTiedTopicsByViews(topics, next);
},
], callback); ], callback);
}; };
function getAllTimePopular(uid, count, callback) { function getAllTimePopular(uid, count, callback) {
Topics.getTopicsFromSet('topics:posts', uid, 0, count - 1, function (err, data) { async.waterfall([
callback(err, data ? data.topics : null); 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) { function getTopics(tids, uid, count, callback) {
@ -47,9 +42,7 @@ module.exports = function (Topics) {
function (topics, next) { function (topics, next) {
tids = topics.filter(function (topic) { tids = topics.filter(function (topic) {
return topic && parseInt(topic.deleted, 10) !== 1; return topic && parseInt(topic.deleted, 10) !== 1;
}).sort(function (a, b) { }).sort(sortPopular).slice(0, count).map(function (topic) {
return b.postcount - a.postcount;
}).slice(0, count).map(function (topic) {
return topic.tid; return topic.tid;
}); });
privileges.topics.filterTids('read', tids, uid, next); privileges.topics.filterTids('read', tids, uid, next);
@ -60,11 +53,10 @@ module.exports = function (Topics) {
], callback); ], callback);
} }
function sortTiedTopicsByViews(topics, next) { function sortPopular(a, b) {
topics.sort(function (a, b) { if (parseInt(a.postcount, 10) !== parseInt(b.postcount, 10)) {
return parseInt(a.postcount, 10) !== parseInt(b.postcount, 10) ? 0 : parseInt(b.viewcount, 10) - parseInt(a.viewcount, 10); return b.postcount - a.postcount;
}); }
return parseInt(b.viewcount, 10) - parseInt(a.viewcount, 10);
next(null, topics);
} }
}; };

Loading…
Cancel
Save