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

80 lines
1.8 KiB
JavaScript

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