From 49afe544e1b515bf6351db489c8d7a9973161871 Mon Sep 17 00:00:00 2001 From: barisusakli Date: Thu, 20 Aug 2015 14:23:20 -0400 Subject: [PATCH] recursive #3227 --- src/categories/recentreplies.js | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/src/categories/recentreplies.js b/src/categories/recentreplies.js index c0613ab5e4..d7a2a165a0 100644 --- a/src/categories/recentreplies.js +++ b/src/categories/recentreplies.js @@ -93,19 +93,28 @@ module.exports = function(Categories) { 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]; - } - }); + var posts = []; + getPostsRecursive(category, posts); - if (latestPost) { - category.posts = [latestPost]; + posts.sort(function(a, b) { + return b.timestamp - a.timestamp; + }); + if (posts.length) { + category.posts = [posts[0]]; } }); } + function getPostsRecursive(category, posts) { + category.posts.forEach(function(p) { + posts.push(p); + }); + + category.children.forEach(function(child) { + getPostsRecursive(child, posts); + }); + } + function assignPostsToCategories(categories, posts) { categories.forEach(function(category) { category.posts = posts.filter(function(post) {