2023-11-08 10:08:40 +00:00
|
|
|
const feed_lib = require("feed").Feed;
|
2024-04-01 20:34:32 +00:00
|
|
|
const core = require("./core");
|
2023-11-08 10:08:40 +00:00
|
|
|
|
|
|
|
// TODO: Expose ATOM Feed items
|
|
|
|
function getBaseFeed() {
|
|
|
|
return new feed_lib({
|
2024-04-01 20:34:32 +00:00
|
|
|
title: core.settings.WEBSITE_NAME,
|
|
|
|
description: `${core.settings.WEBSITE_NAME} RSS Feed`,
|
2023-11-08 10:08:40 +00:00
|
|
|
id: process.env.BASE_URL,
|
|
|
|
link: process.env.BASE_URL,
|
2024-04-01 20:34:32 +00:00
|
|
|
favicon: `${process.env.BASE_URL}/favicon.ico`,
|
2023-11-08 10:08:40 +00:00
|
|
|
feedLinks: {
|
|
|
|
json: `${process.env.BASE_URL}/json`,
|
|
|
|
atom: `${process.env.BASE_URL}/atom`,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getFeed({ type = "rss" }) {
|
2024-04-01 20:34:32 +00:00
|
|
|
// Don't serve RSS feed if disabled by admin
|
|
|
|
if (!core.settings.CD_RSS) return;
|
|
|
|
|
2023-11-08 10:08:40 +00:00
|
|
|
// Get the base feed
|
|
|
|
let feed = getBaseFeed();
|
|
|
|
|
|
|
|
// Get posts
|
2024-04-01 20:34:32 +00:00
|
|
|
let posts = await core.getBlog({ limit: 20 }); // internal.getBlogList({}, { limit: 20 });
|
2023-11-08 10:08:40 +00:00
|
|
|
|
|
|
|
// For each post, add a formatted object to the feed
|
|
|
|
posts.data.forEach((post) => {
|
|
|
|
let formatted = {
|
|
|
|
title: post.title,
|
|
|
|
description: post.description,
|
|
|
|
id: post.id,
|
|
|
|
link: `${process.env.BASE_URL}/blog/${post.id}`,
|
|
|
|
content: post.content,
|
|
|
|
date: new Date(post.publish_date),
|
|
|
|
image: post.thumbnail,
|
|
|
|
author: [
|
|
|
|
{
|
|
|
|
name: post.owner.username,
|
|
|
|
link: `${process.env.BASE_URL}/author/${post.owner.username}`,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
feed.addItem(formatted);
|
|
|
|
});
|
|
|
|
// if (type === "rss") return feed.rss2();
|
|
|
|
if (type === "atom") return feed.atom1();
|
2024-04-01 20:34:32 +00:00
|
|
|
if (type === "json") return feed.json1();
|
2023-11-08 10:08:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = { getFeed };
|