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/suggested.js

74 lines
1.7 KiB
JavaScript

10 years ago
'use strict';
var async = require('async'),
_ = require('underscore'),
10 years ago
10 years ago
categories = require('../categories'),
search = require('../search'),
10 years ago
db = require('../database');
10 years ago
module.exports = function(Topics) {
Topics.getSuggestedTopics = function(tid, uid, start, stop, callback) {
10 years ago
async.parallel({
tagTids: function(next) {
getTidsWithSameTags(tid, next);
},
searchTids: function(next) {
getSearchTids(tid, next);
10 years ago
},
categoryTids: function(next) {
getCategoryTids(tid, next);
10 years ago
}
10 years ago
}, function(err, results) {
if (err) {
return callback(err);
}
var tids = results.tagTids.concat(results.searchTids).concat(results.categoryTids);
tids = tids.filter(function(_tid, index, array) {
return parseInt(_tid, 10) !== parseInt(tid, 10) && array.indexOf(_tid) === index;
}).slice(start, stop + 1);
10 years ago
10 years ago
Topics.getTopics(tids, uid, callback);
});
};
function getTidsWithSameTags(tid, callback) {
async.waterfall([
function(next) {
Topics.getTopicTags(tid, next);
},
function(tags, next) {
async.map(tags, function(tag, next) {
Topics.getTagTids(tag, 0, -1, next);
}, next);
},
function(data, next) {
next(null, _.unique(_.flatten(data)));
}
10 years ago
], callback);
}
10 years ago
function getSearchTids(tid, callback) {
10 years ago
async.waterfall([
function(next) {
10 years ago
Topics.getTopicField(tid, 'title', next);
10 years ago
},
function(title, next) {
search.searchQuery('topic', title, next);
}
10 years ago
], callback);
10 years ago
}
function getCategoryTids(tid, callback) {
Topics.getTopicField(tid, 'cid', function(err, cid) {
if (err || !cid) {
return callback(err, []);
}
categories.getTopicIds('cid:' + cid + ':tids', true, 0, 9, callback);
10 years ago
});
10 years ago
}
10 years ago
10 years ago
};