bubble replies from subcategories to the parents

v1.18.x
barisusakli 9 years ago
parent 42ebc15521
commit b2311edb94

@ -9,6 +9,7 @@ var _ = require('underscore');
var db = require('../database'); var db = require('../database');
var posts = require('../posts'); var posts = require('../posts');
var topics = require('../topics'); var topics = require('../topics');
var categories = require('../categories');
var privileges = require('../privileges'); var privileges = require('../privileges');
module.exports = function(Categories) { module.exports = function(Categories) {
@ -113,12 +114,26 @@ module.exports = function(Categories) {
topic.teaserPid = topic.teaserPid || topic.mainPid; topic.teaserPid = topic.teaserPid || topic.mainPid;
} }
}); });
topics.getTeasers(_topicData, next); var cids = _topicData.map(function(topic) {
return topic && topic.cid;
}).filter(function(cid, index, array) {
return cid && array.indexOf(cid) === index;
});
async.parallel({
categoryData: async.apply(categories.getCategoriesFields, cids, ['cid', 'parentCid']),
teasers: async.apply(topics.getTeasers, _topicData),
}, next);
}, },
function (teasers, next) { function (results, next) {
teasers.forEach(function(teaser, index) { var parentCids = {};
results.categoryData.forEach(function(category) {
parentCids[category.cid] = category.parentCid;
});
results.teasers.forEach(function(teaser, index) {
if (teaser) { if (teaser) {
teaser.cid = topicData[index].cid; teaser.cid = topicData[index].cid;
teaser.parentCid = parseInt(parentCids[teaser.cid]) || 0;
teaser.tid = teaser.uid = teaser.user.uid = undefined; teaser.tid = teaser.uid = teaser.user.uid = undefined;
teaser.topic = { teaser.topic = {
slug: topicData[index].slug, slug: topicData[index].slug,
@ -126,8 +141,8 @@ module.exports = function(Categories) {
}; };
} }
}); });
teasers = teasers.filter(Boolean); results.teasers = results.teasers.filter(Boolean);
next(null, teasers); next(null, results.teasers);
} }
], callback); ], callback);
} }
@ -135,7 +150,8 @@ module.exports = function(Categories) {
function assignTopicsToCategories(categories, topics) { function assignTopicsToCategories(categories, topics) {
categories.forEach(function(category) { categories.forEach(function(category) {
category.posts = topics.filter(function(topic) { category.posts = topics.filter(function(topic) {
return topic.cid && parseInt(topic.cid, 10) === parseInt(category.cid, 10); return topic.cid && (parseInt(topic.cid, 10) === parseInt(category.cid, 10) ||
parseInt(topic.parentCid, 10) === parseInt(category.cid, 10));
}).sort(function(a, b) { }).sort(function(a, b) {
return b.pid - a.pid; return b.pid - a.pid;
}).slice(0, parseInt(category.numRecentReplies, 10)); }).slice(0, parseInt(category.numRecentReplies, 10));

Loading…
Cancel
Save