mirror of
https://github.com/vale981/Vulcan
synced 2025-03-08 19:11:38 -05:00
77 lines
2 KiB
JavaScript
77 lines
2 KiB
JavaScript
import Posts from "meteor/nova:posts";
|
|
import Comments from "meteor/nova:comments";
|
|
import Users from 'meteor/nova:users';
|
|
import { Utils } from 'meteor/nova:core';
|
|
|
|
export const servePostsApi = (terms) => {
|
|
var posts = [];
|
|
|
|
var parameters = Posts.getParameters(terms);
|
|
|
|
const postsCursor = Posts.find(parameters.selector, parameters.options);
|
|
|
|
postsCursor.forEach(function(post) {
|
|
var url = Posts.getLink(post);
|
|
var postOutput = {
|
|
title: post.title,
|
|
headline: post.title, // for backwards compatibility
|
|
author: post.author,
|
|
date: post.postedAt,
|
|
url: url,
|
|
pageUrl: Posts.getPageUrl(post, true),
|
|
guid: post._id
|
|
};
|
|
|
|
if(post.body)
|
|
postOutput.body = post.body;
|
|
|
|
if(post.url)
|
|
postOutput.domain = Utils.getDomain(url);
|
|
|
|
if (post.thumbnailUrl) {
|
|
postOutput.thumbnailUrl = Utils.addHttp(post.thumbnailUrl);
|
|
}
|
|
|
|
var twitterName = Users.getTwitterNameById(post.userId);
|
|
if(twitterName)
|
|
postOutput.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);
|
|
});
|
|
|
|
postOutput.comments = comments;
|
|
|
|
posts.push(postOutput);
|
|
});
|
|
|
|
return JSON.stringify(posts);
|
|
};
|