Skip to content

Commit

Permalink
closes #3227
Browse files Browse the repository at this point in the history
  • Loading branch information
barisusakli committed Aug 19, 2015
1 parent 91aa1d5 commit 7db4127
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
41 changes: 31 additions & 10 deletions src/categories/recentreplies.js
Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion src/controllers/categories.js
Expand Up @@ -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);
});
},
Expand Down

0 comments on commit 7db4127

Please sign in to comment.