Adds comments to API

This commit is contained in:
Devon Campbell 2014-09-08 08:12:27 -04:00
parent 01916ef0ee
commit 51e9ee7c64

View file

@ -1,4 +1,4 @@
serveAPI = function(limitSegment){
postAPI = function(limitSegment){
var posts = [];
var limit = typeof limitSegment === 'undefined' ? 20 : limitSegment // default limit: 20 posts
@ -22,8 +22,42 @@ serveAPI = function(limitSegment){
if(twitterName = getTwitterNameById(post.userId))
properties.twitterName = twitterName;
var comments = [];
Comments.find({postId: post._id}, {sort: {postedAt: -1}, limit: 50}).forEach(function(comment) {
var commentProperties = {
body: comment.body,
author: comment.author,
date: comment.postedAt,
guid: comment._id,
parentCommentId: comment.parentCommentId
};
comments.push(commentProperties);
});
var commentsToDelete = [];
comments.forEach(function(comment, index) {
if (comment.parentCommentId) {
var parent = comments.filter(function(obj) {
return obj.guid === comment.parentCommentId;
})[0];
if (parent) {
parent.replies = parent.replies || [];
parent.replies.push(JSON.parse(JSON.stringify(comment)));
commentsToDelete.push(index)
}
}
});
commentsToDelete.reverse().forEach(function(index) {
comments.splice(index,1);
});
properties.comments = comments;
posts.push(properties);
});
return JSON.stringify(posts);
};
return JSON.stringify(posts);
};