diff --git a/src/categories/recentreplies.js b/src/categories/recentreplies.js index f495ab1463..c0613ab5e4 100644 --- a/src/categories/recentreplies.js +++ b/src/categories/recentreplies.js @@ -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) { - 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 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) { diff --git a/src/controllers/categories.js b/src/controllers/categories.js index bae16adc3b..6086640a5c 100644 --- a/src/controllers/categories.js +++ b/src/controllers/categories.js @@ -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); }); },