diff --git a/packages/telescope-api/lib/server/api.js b/packages/telescope-api/lib/server/api.js index 9c9c69321..60b4935ae 100644 --- a/packages/telescope-api/lib/server/api.js +++ b/packages/telescope-api/lib/server/api.js @@ -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); -}; \ No newline at end of file + return JSON.stringify(posts); +};