import RSS from 'rss';
import Posts from "meteor/nova:posts";
import Comments from "meteor/nova:comments";
import { Utils, getSetting } from 'meteor/nova:core';
Posts.views.rss = Posts.views.new; // default to "new" view for RSS feed
const getMeta = (url) => {
const siteUrl = getSetting('siteUrl', Meteor.absoluteUrl());
return {
title: getSetting('title'),
description: getSetting('tagline'),
feed_url: siteUrl+url,
site_url: siteUrl,
image_url: siteUrl+'img/favicon.png'
};
};
export const servePostRSS = (terms, url) => {
const feed = new RSS(getMeta(url));
let parameters = Posts.getParameters(terms);
delete parameters['options']['sort']['sticky'];
const postsCursor = Posts.find(parameters.selector, parameters.options);
postsCursor.forEach((post) => {
const description = !!post.body ? post.body+'' : '';
const feedItem = {
title: post.title,
description: description + 'Discuss',
author: post.author,
date: post.postedAt,
guid: post._id,
url: (getSetting("RSSLinksPointTo", "link") === "link") ? Posts.getLink(post) : Posts.getPageUrl(post, true)
};
if (post.thumbnailUrl) {
const url = Utils.addHttp(post.thumbnailUrl);
feedItem.custom_elements = [{"imageUrl":url}, {"content": url}];
}
feed.item(feedItem);
});
return feed.xml();
};
export const serveCommentRSS = (terms, url) => {
const feed = new RSS(getMeta(url));
const commentsCursor = Comments.find({isDeleted: {$ne: true}}, {sort: {postedAt: -1}, limit: 20});
commentsCursor.forEach(function(comment) {
const post = Posts.findOne(comment.postId);
feed.item({
title: 'Comment on ' + post.title,
description: `${comment.body}Discuss`,
author: comment.author,
date: comment.postedAt,
url: Comments.getPageUrl(comment, true),
guid: comment._id
});
});
return feed.xml();
};