From 88d98edbce50924b82406abbd7bf1e4a703886af Mon Sep 17 00:00:00 2001 From: me Date: Fri, 14 Mar 2025 15:29:19 +0200 Subject: [PATCH] add comments feed --- backend/main.mjs | 54 +++++++++++++++++++++++++++++++++++++++ backend/package-lock.json | 31 ++++++++++++++++++++++ backend/package.json | 1 + 3 files changed, 86 insertions(+) diff --git a/backend/main.mjs b/backend/main.mjs index 8d3bcf1..02de431 100644 --- a/backend/main.mjs +++ b/backend/main.mjs @@ -6,6 +6,7 @@ import helmet from 'helmet'; import RateLimit from 'express-rate-limit'; import morgan from 'morgan'; import cors from 'cors'; +import { Feed } from 'feed'; // Constants @@ -187,6 +188,59 @@ ORDER BY c.id res.json(comments); }); +// Feed + +const domain = process.env.DOMAIN || 'comments.alloca.space'; + +app.get('/atom/:site', (req, res) => { + const site = req.params.site; + + /* lets create an rss feed */ + var feed = new Feed({ + title: 'UCS', + description: 'תגובות עבור האתר ' + site, + id: domain + '/feed/' + site, + link: domain, + language: 'he' + }); + + const stmt = db.prepare(` +SELECT + c.id, + s.url as site, + c.path, + c.user, + c.user_website, + c.message, + c.published +FROM + (SELECT id, url from site where url = @site) s +JOIN comment c +ON c.site = s.id +ORDER BY c.published DESC +;`); + const comments = stmt.all({ site }); + + for (const comment of comments) { + feed.addItem({ + title: `New message by '${comment.user}' on ${comment.path}`, + description: comment.message, + id: `${comment.site}/${comment.path}#comment-${comment.id}`, + link: `${comment.site}/${comment.path}#comment-${comment.id}`, + date: new Date(comment.published) + }); + } + + var xml = feed.atom1(); + + res.set('Content-Type', 'text/xml'); + res.send(xml); +}); + + +// Listen + + app.listen(port, () => { console.log(`Listening on port ${port}`); }); diff --git a/backend/package-lock.json b/backend/package-lock.json index a5a9a9e..5a82f61 100644 --- a/backend/package-lock.json +++ b/backend/package-lock.json @@ -15,6 +15,7 @@ "cors": "^2.8.5", "express": "^4.21.2", "express-rate-limit": "^7.5.0", + "feed": "^4.2.2", "helmet": "^8.0.0", "morgan": "^1.10.0" }, @@ -646,6 +647,18 @@ "integrity": "sha512-C55Cr/dQWQHdwuMTF+ySNMYqydVclmKdgFHoC/8gTu5Zoe1Nrx6jgArwfZ+7jKU78VjDfhrkGAJ38ucf0lozeQ==", "license": "MIT" }, + "node_modules/feed": { + "version": "4.2.2", + "resolved": "https://registry.npmjs.org/feed/-/feed-4.2.2.tgz", + "integrity": "sha512-u5/sxGfiMfZNtJ3OvQpXcvotFpYkL0n9u9mM2vkui2nGo8b4wvDkJ8gAkYqbA8QpGyFCv3RK0Z+Iv+9veCS9bQ==", + "license": "MIT", + "dependencies": { + "xml-js": "^1.6.11" + }, + "engines": { + "node": ">=0.4.0" + } + }, "node_modules/file-uri-to-path": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/file-uri-to-path/-/file-uri-to-path-1.0.0.tgz", @@ -1452,6 +1465,12 @@ "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==", "license": "MIT" }, + "node_modules/sax": { + "version": "1.4.1", + "resolved": "https://registry.npmjs.org/sax/-/sax-1.4.1.tgz", + "integrity": "sha512-+aWOz7yVScEGoKNd4PA10LZ8sk0A/z5+nXQG5giUO5rprX9jgYsTdov9qCchZiPIZezbZH+jRut8nPodFAX4Jg==", + "license": "ISC" + }, "node_modules/semver": { "version": "7.7.1", "resolved": "https://registry.npmjs.org/semver/-/semver-7.7.1.tgz", @@ -1824,6 +1843,18 @@ "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", "integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==", "license": "ISC" + }, + "node_modules/xml-js": { + "version": "1.6.11", + "resolved": "https://registry.npmjs.org/xml-js/-/xml-js-1.6.11.tgz", + "integrity": "sha512-7rVi2KMfwfWFl+GpPg6m80IVMWXLRjO+PxTq7V2CDhoGak0wzYzFgUY2m4XJ47OGdXd8eLE8EmwfAmdjw7lC1g==", + "license": "MIT", + "dependencies": { + "sax": "^1.2.4" + }, + "bin": { + "xml-js": "bin/cli.js" + } } } } diff --git a/backend/package.json b/backend/package.json index 50febf7..32b271a 100644 --- a/backend/package.json +++ b/backend/package.json @@ -21,6 +21,7 @@ "cors": "^2.8.5", "express": "^4.21.2", "express-rate-limit": "^7.5.0", + "feed": "^4.2.2", "helmet": "^8.0.0", "morgan": "^1.10.0" },