mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-07 14:35:47 +01:00
some changes to related code
This commit is contained in:
@@ -192,25 +192,22 @@ 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;
|
||||
topicData.locked = parseInt(topicData.locked, 10) === 1;
|
||||
topicData.pinned = parseInt(topicData.pinned, 10) === 1;
|
||||
|
||||
Topics.getRelatedTopics(topicData, uid, next);
|
||||
},
|
||||
function(related, next) {
|
||||
topicData.related = related || [];
|
||||
plugins.fireHook('filter:topic.get', {topic: topicData, uid: uid}, next);
|
||||
},
|
||||
function(data, next) {
|
||||
|
||||
@@ -5,7 +5,6 @@ var async = require('async'),
|
||||
|
||||
db = require('../database'),
|
||||
meta = require('../meta'),
|
||||
user = require('../user'),
|
||||
_ = require('underscore'),
|
||||
plugins = require('../plugins');
|
||||
|
||||
@@ -323,43 +322,33 @@ module.exports = function(Topics) {
|
||||
});
|
||||
};
|
||||
|
||||
Topics.getRelatedTopics = function(topicData, callback) {
|
||||
var maximumTopics = typeof meta.config.maximumRelatedTopics !== 'undefined' ? parseInt(meta.config.maximumRelatedTopics, 10) : 5;
|
||||
Topics.getRelatedTopics = function(topicData, uid, callback) {
|
||||
if (plugins.hasListeners('filter:topic.getRelatedTopics')) {
|
||||
return plugins.fireHook('filter:topic.getRelatedTopics', {topic: topicData, uid: uid}, callback);
|
||||
}
|
||||
|
||||
var maximumTopics = parseInt(meta.config.maximumRelatedTopics, 10) || 5;
|
||||
|
||||
if (!topicData.tags.length || maximumTopics === 0) {
|
||||
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;
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
async.map(topicData.tags, function (tag, next) {
|
||||
Topics.getTagTids(tag.value, 0, 5, next);
|
||||
}, next);
|
||||
},
|
||||
function (tids, next) {
|
||||
tids = _.shuffle(_.unique(_.flatten(tids))).slice(0, maximumTopics);
|
||||
Topics.getTopics(tids, uid, next);
|
||||
},
|
||||
function (topics, next) {
|
||||
topics = topics.filter(function(topic) {
|
||||
return topic && !topic.deleted && parseInt(topic.uid, 10) !== parseInt(uid, 10);
|
||||
});
|
||||
next(null, topics);
|
||||
}
|
||||
|
||||
related = _.shuffle(related).slice(0, maximumTopics);
|
||||
|
||||
topicData.related = related;
|
||||
callback(err, topicData);
|
||||
});
|
||||
});
|
||||
], callback);
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user