v1.18.x
barisusakli 10 years ago
parent 91aa1d55dc
commit 7db41275a3

@ -56,9 +56,10 @@ module.exports = function(Categories) {
}
},
function(posts, next) {
categoryData.forEach(function(category) {
assignPostsToCategory(category, posts);
});
assignPostsToCategories(categoryData, posts);
bubbleUpChildrenPosts(categoryData);
next();
}
], callback);
@ -87,13 +88,33 @@ module.exports = function(Categories) {
], callback);
}
function assignPostsToCategory(category, posts) {
function bubbleUpChildrenPosts(categoryData) {
categoryData.forEach(function(category) {
if (category.posts.length) {
return;
}
var latestPost;
category.children.forEach(function(children) {
if (children.posts.length && (!latestPost || (latestPost && latestPost.timestamp < children.posts[0].timestamp))) {
latestPost = children.posts[0];
}
});
if (latestPost) {
category.posts = [latestPost];
}
});
}
function assignPostsToCategories(categories, posts) {
categories.forEach(function(category) {
category.posts = posts.filter(function(post) {
return post.category && (parseInt(post.category.cid, 10) === parseInt(category.cid, 10) ||
parseInt(post.category.parentCid, 10) === parseInt(category.cid, 10));
}).sort(function(a, b) {
return b.timestamp - a.timestamp;
}).slice(0, parseInt(category.numRecentReplies, 10));
});
}
function getRecentTopicPids(category, callback) {

@ -191,7 +191,9 @@ categoriesController.get = function(req, res, next) {
});
},
function(categoryData, next) {
categories.getRecentTopicReplies(categoryData.children, req.uid, function(err) {
var allCategories = [];
categories.flattenCategories(allCategories, [categoryData]);
categories.getRecentTopicReplies(allCategories, req.uid, function(err) {
next(err, categoryData);
});
},

Loading…
Cancel
Save