mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-07 06:25:50 +01:00
generic "related topics" functionality
This commit is contained in:
@@ -192,12 +192,19 @@ var async = require('async'),
|
||||
}, next);
|
||||
},
|
||||
function(results, next) {
|
||||
if (plugins.hasListeners('filter:topic.getRelatedTopics')) {
|
||||
plugins.fireHook('filter:topic.getRelatedTopics', results, next);
|
||||
} else {
|
||||
Topics.getRelatedTopics(results, next);
|
||||
}
|
||||
}, function(results, next) {
|
||||
topicData.posts = results.posts;
|
||||
topicData.category = results.category;
|
||||
topicData.thread_tools = results.threadTools.tools;
|
||||
topicData.tags = results.tags;
|
||||
topicData.isFollowing = results.isFollowing[0];
|
||||
topicData.bookmark = results.bookmark;
|
||||
topicData.related = results.related || [];
|
||||
|
||||
topicData.unreplied = parseInt(topicData.postcount, 10) === 1;
|
||||
topicData.deleted = parseInt(topicData.deleted, 10) === 1;
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
});
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user