/* Copyright 2017 Apinf Oy
This file is covered by the EUPL license.
You may obtain a copy of the licence at
https://joinup.ec.europa.eu/community/eupl/og_page/european-union-public-licence-eupl-v11 */

// Meteor packages imports
import { Meteor } from 'meteor/meteor';

// Meteor contributed packages imports
import { RssFeed } from 'meteor/raix:rssfeed';

// Collection imports
import Apis from '/apinf_packages/apis/collection';

import Branding from '/apinf_packages/branding/collection';

// calling Rss feed publication
// first argument (apis) will build the url for the feed i.e domain-name/rss/apis
RssFeed.publish('apis', function (query) {
  // initialization of variable feed
  const feed = this;

  // RSS header title
  feed.setValue('title', feed.cdata('APIs feed'));

  // RSS header description
  const branding = Branding.findOne();
  const siteTitle = branding.siteTitle;
  feed.setValue('description', feed.cdata(`Feed for the latest Apis from ${siteTitle} site.`));

  // RSS header link
  const meteorAbsoluteUrl = Meteor.absoluteUrl().slice(0, -1);
  feed.setValue('link', meteorAbsoluteUrl);

  // lastBuildDate: About RSS feed was last built with new information.
  feed.setValue('lastBuildDate', new Date());

  // pubDate: About RSS feed publish Date
  feed.setValue('pubDate', new Date());

  /* ttl: The length of time (in minutes) RSS channel can be cached
          before refreshing from the source*/
  feed.setValue('ttl', 1);

  // The feed should only look for public APIs
  const filter = { isPublic: true };

  /*
    If the slug has been passed in query, the feed searches the specific
    API (if it are public), if not, the feed returns all public apis
  */
  if (query.slug !== undefined) {
    filter.slug = query.slug;
  }

  // Look at each entry of public Apis schema and find the latest apis in filter
  Apis.find(filter, { sort: { created_at: -1 } }).forEach((api) => {
    // append an item to our feed using the .addItem() method
    feed.addItem({
      title: api.name,
      description: `${api.description}`,
      link: `${meteorAbsoluteUrl}/apis/${api.slug}`,
      pubDate: api.created_at,
    });
  });
});
