RSS content delivery (#13)

YAB now supports RSS content delivery which can be enabled or disabled though the admin panel

Reviewed-on: #13
Co-authored-by: Armored Dragon <publicmail@armoreddragon.com>
Co-committed-by: Armored Dragon <publicmail@armoreddragon.com>
pull/2/head
Armored Dragon 2024-04-01 20:34:32 +00:00 committed by Armored Dragon
parent a403b114e8
commit 50f30f227d
4 changed files with 15 additions and 11 deletions

View File

@ -1,17 +1,14 @@
const feed_lib = require("feed").Feed; const feed_lib = require("feed").Feed;
const internal = require("./internal_api"); const core = require("./core");
// TODO: Expose ATOM Feed items // TODO: Expose ATOM Feed items
function getBaseFeed() { function getBaseFeed() {
return new feed_lib({ return new feed_lib({
title: process.env.WEBSITE_NAME, title: core.settings.WEBSITE_NAME,
description: `${process.env.S3_REGION} RSS Feed`, description: `${core.settings.WEBSITE_NAME} RSS Feed`,
id: process.env.BASE_URL, id: process.env.BASE_URL,
link: process.env.BASE_URL, link: process.env.BASE_URL,
// image: "http://example.com/image.png", favicon: `${process.env.BASE_URL}/favicon.ico`,
// favicon: "http://example.com/favicon.ico",
// copyright: "All rights reserved 2013, John Doe",
// generator: "awesome", // optional, default = 'Feed for Node.js'
feedLinks: { feedLinks: {
json: `${process.env.BASE_URL}/json`, json: `${process.env.BASE_URL}/json`,
atom: `${process.env.BASE_URL}/atom`, atom: `${process.env.BASE_URL}/atom`,
@ -20,11 +17,14 @@ function getBaseFeed() {
} }
async function getFeed({ type = "rss" }) { async function getFeed({ type = "rss" }) {
// Don't serve RSS feed if disabled by admin
if (!core.settings.CD_RSS) return;
// Get the base feed // Get the base feed
let feed = getBaseFeed(); let feed = getBaseFeed();
// Get posts // Get posts
let posts = await internal.getBlogList({}, { limit: 20 }); let posts = await core.getBlog({ limit: 20 }); // internal.getBlogList({}, { limit: 20 });
// For each post, add a formatted object to the feed // For each post, add a formatted object to the feed
posts.data.forEach((post) => { posts.data.forEach((post) => {
@ -48,7 +48,7 @@ async function getFeed({ type = "rss" }) {
}); });
// if (type === "rss") return feed.rss2(); // if (type === "rss") return feed.rss2();
if (type === "atom") return feed.atom1(); if (type === "atom") return feed.atom1();
// if (type === "json") return feed.json1(); if (type === "json") return feed.json1();
} }
module.exports = { getFeed }; module.exports = { getFeed };

View File

@ -38,7 +38,6 @@ async function postLogin(req, res) {
async function postSetting(request, response) { async function postSetting(request, response) {
const user = await core.getUser({ id: request.session.user.id }); const user = await core.getUser({ id: request.session.user.id });
// TODO: Permissions for changing settings
if (!user.success) return response.json({ success: false, message: user.message }); if (!user.success) return response.json({ success: false, message: user.message });
if (user.data.role !== "ADMIN") return response.json({ success: false, message: "User is not permitted" }); if (user.data.role !== "ADMIN") return response.json({ success: false, message: "User is not permitted" });

View File

@ -75,7 +75,10 @@ async function atom(req, res) {
res.type("application/xml"); res.type("application/xml");
res.send(await external.getFeed({ type: "atom" })); res.send(await external.getFeed({ type: "atom" }));
} }
async function jsonFeed(req, res) {
res.type("application/json");
res.send(await external.getFeed({ type: "json" }));
}
// Internal API ------------------------------ // Internal API ------------------------------
module.exports = { module.exports = {
@ -89,4 +92,5 @@ module.exports = {
blogSingle, blogSingle,
admin, admin,
atom, atom,
jsonFeed,
}; };

1
yab.js
View File

@ -46,6 +46,7 @@ app.get("/blog/new", checkAuthenticated, page_scripts.blogNew);
app.get("/blog/:blog_id", page_scripts.blogSingle); app.get("/blog/:blog_id", page_scripts.blogSingle);
app.get("/blog/:blog_id/edit", checkAuthenticated, page_scripts.blogEdit); app.get("/blog/:blog_id/edit", checkAuthenticated, page_scripts.blogEdit);
app.get("/atom", page_scripts.atom); app.get("/atom", page_scripts.atom);
app.get("/json", page_scripts.jsonFeed);
function checkAuthenticated(req, res, next) { function checkAuthenticated(req, res, next) {
if (req.session.user) return next(); if (req.session.user) return next();