You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nodebb/src/topics/popular.js

53 lines
1.3 KiB
JavaScript

'use strict';
9 years ago
var async = require('async');
var privileges = require('../privileges');
module.exports = function(Topics) {
11 years ago
Topics.getPopular = function(term, uid, count, callback) {
count = parseInt(count, 10) || 20;
11 years ago
if (term === 'alltime') {
11 years ago
return getAllTimePopular(uid, count, callback);
11 years ago
}
async.waterfall([
function(next) {
Topics.getLatestTidsFromSet('topics:tid', 0, -1, term, next);
},
function(tids, next) {
11 years ago
getTopics(tids, uid, count, next);
}
], callback);
};
11 years ago
function getAllTimePopular(uid, count, callback) {
Topics.getTopicsFromSet('topics:posts', uid, 0, count - 1, function(err, data) {
11 years ago
callback(err, data ? data.topics : null);
});
}
11 years ago
function getTopics(tids, uid, count, callback) {
async.waterfall([
function(next) {
Topics.getTopicsFields(tids, ['tid', 'postcount', 'deleted'], next);
},
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) {
return topic.tid;
});
privileges.topics.filterTids('read', tids, uid, next);
},
function(tids, next) {
Topics.getTopicsByTids(tids, uid, next);
}
], callback);
}
};