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

78 lines
1.6 KiB
JavaScript

'use strict';
var async = require('async'),
db = require('../database'),
privileges = require('../privileges');
module.exports = function(Topics) {
var terms = {
daily: 'day',
weekly: 'week',
monthly: 'month'
};
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
}
var since = terms[term] || 'day';
async.waterfall([
function(next) {
Topics.getLatestTids(0, -1, since, next);
},
function(tids, next) {
11 years ago
getTopics(tids, uid, count, next);
},
function(topics, next) {
var tids = topics.map(function(topic) {
return topic.tid;
});
privileges.topics.filter('read', tids, uid, function(err, tids) {
if (err) {
return next(err);
}
topics = topics.filter(function(topic) {
return tids.indexOf(topic.tid) !== -1;
});
next(null, topics);
});
}
], callback);
};
11 years ago
function getAllTimePopular(uid, count, callback) {
Topics.getTopicsFromSet(uid, 'topics:posts', 0, count - 1, function(err, data) {
11 years ago
callback(err, data ? data.topics : null);
});
}
11 years ago
function getTopics(tids, uid, count, callback) {
var keys = tids.map(function(tid) {
return 'topic:' + tid;
});
db.getObjectsFields(keys, ['tid', 'postcount'], function(err, topics) {
if (err) {
return callback(err);
}
topics = topics.sort(function(a, b) {
return b.postcount - a.postcount;
}).slice(0, count).map(function(topic) {
return topic.tid;
});
Topics.getTopicsByTids(topics, uid, callback);
});
}
};