Files
NodeBB/src/topics/suggested.js

80 lines
1.8 KiB
JavaScript
Raw Normal View History

2015-02-25 12:43:56 -05:00
'use strict';
2016-11-22 15:29:58 +03:00
var async = require('async');
var _ = require('underscore');
2015-02-26 12:22:48 -05:00
2016-11-22 15:29:58 +03:00
var categories = require('../categories');
var search = require('../search');
2015-02-25 12:43:56 -05:00
module.exports = function (Topics) {
2015-02-25 12:43:56 -05:00
Topics.getSuggestedTopics = function (tid, uid, start, stop, callback) {
2015-02-25 12:43:56 -05:00
async.parallel({
tagTids: function (next) {
2015-02-25 12:43:56 -05:00
getTidsWithSameTags(tid, next);
},
searchTids: function (next) {
2015-02-25 12:46:00 -05:00
getSearchTids(tid, next);
2015-02-25 12:43:56 -05:00
},
categoryTids: function (next) {
2015-02-25 12:43:56 -05:00
getCategoryTids(tid, next);
2015-02-26 12:22:48 -05:00
}
}, function (err, results) {
2015-02-25 12:43:56 -05:00
if (err) {
return callback(err);
}
var tids = results.tagTids.concat(results.searchTids).concat(results.categoryTids);
tids = tids.filter(function (_tid, index, array) {
2015-02-25 12:43:56 -05:00
return parseInt(_tid, 10) !== parseInt(tid, 10) && array.indexOf(_tid) === index;
2016-11-22 15:29:58 +03:00
});
if (stop === -1) {
tids = tids.slice(start);
} else {
tids = tids.slice(start, stop + 1);
}
2015-02-26 12:22:48 -05:00
2015-02-25 12:43:56 -05:00
Topics.getTopics(tids, uid, callback);
});
};
function getTidsWithSameTags(tid, callback) {
async.waterfall([
function (next) {
2015-02-25 12:43:56 -05:00
Topics.getTopicTags(tid, next);
},
function (tags, next) {
async.map(tags, function (tag, next) {
2015-02-25 12:43:56 -05:00
Topics.getTagTids(tag, 0, -1, next);
}, next);
},
function (data, next) {
2015-02-25 12:43:56 -05:00
next(null, _.unique(_.flatten(data)));
}
2015-02-26 12:22:48 -05:00
], callback);
}
2015-02-25 12:43:56 -05:00
2015-02-25 12:46:00 -05:00
function getSearchTids(tid, callback) {
2015-02-25 12:43:56 -05:00
async.waterfall([
function (next) {
2015-02-26 12:22:48 -05:00
Topics.getTopicField(tid, 'title', next);
2015-02-25 12:43:56 -05:00
},
function (title, next) {
2015-05-11 16:01:22 -04:00
search.searchQuery('topic', title, [], [], next);
2015-02-25 12:43:56 -05:00
}
2015-02-26 12:22:48 -05:00
], callback);
2015-02-25 12:43:56 -05:00
}
function getCategoryTids(tid, callback) {
2016-11-22 15:29:58 +03:00
async.waterfall([
function (next) {
Topics.getTopicField(tid, 'cid', next);
},
function (cid, next) {
categories.getTopicIds(cid, 'cid:' + cid + ':tids', true, 0, 9, next);
2015-02-25 12:43:56 -05:00
}
2016-11-22 15:29:58 +03:00
], callback);
2015-02-25 12:43:56 -05:00
}
2015-02-26 12:22:48 -05:00
2015-02-25 12:43:56 -05:00
};