|
|
|
@ -5,6 +5,7 @@ var async = require('async'),
|
|
|
|
|
|
|
|
|
|
db = require('../database'),
|
|
|
|
|
meta = require('../meta'),
|
|
|
|
|
user = require('../user'),
|
|
|
|
|
_ = require('underscore'),
|
|
|
|
|
plugins = require('../plugins');
|
|
|
|
|
|
|
|
|
@ -322,4 +323,41 @@ module.exports = function(Topics) {
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Topics.getRelatedTopics = function(topicData, callback) {
|
|
|
|
|
if (!topicData.tags.length) {
|
|
|
|
|
return callback(null, topicData);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var related = [];
|
|
|
|
|
|
|
|
|
|
user.isAdministrator(topicData.threadTools.uid, function(err, isAdministrator) {
|
|
|
|
|
async.each(topicData.tags, function(tag, next) {
|
|
|
|
|
tag = tag.value;
|
|
|
|
|
|
|
|
|
|
Topics.getTagTids(tag, 0, 5, function(err, tids) {
|
|
|
|
|
Topics.getTopics(tids, topicData.threadTools.uid, function(err, topics) {
|
|
|
|
|
related = related.concat(topics.filter(function(topic) {
|
|
|
|
|
var doesntOwnTopic = parseInt(topic.uid, 10) !== parseInt(topicData.threadTools.uid, 10);
|
|
|
|
|
var isntSameTopic = parseInt(topic.tid, 10) !== parseInt(topicData.threadTools.topic.tid, 10);
|
|
|
|
|
|
|
|
|
|
return doesntOwnTopic && isntSameTopic;
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
next(err);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}, function(err) {
|
|
|
|
|
if (!isAdministrator) {
|
|
|
|
|
related = related.filter(function(topic) {
|
|
|
|
|
return topic && !topic.deleted;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
related = _.shuffle(related).slice(0, 5);
|
|
|
|
|
|
|
|
|
|
topicData.related = related;
|
|
|
|
|
callback(err, topicData);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
};
|