mirror of
https://github.com/vale981/Vulcan
synced 2025-03-06 10:01:40 -05:00
Adds comments to API
This commit is contained in:
parent
01916ef0ee
commit
51e9ee7c64
1 changed files with 37 additions and 3 deletions
|
@ -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);
|
||||
};
|
||||
|
|
Loading…
Add table
Reference in a new issue