diff --git a/src/database.js b/src/database.js index 6106c6f..af54d99 100644 --- a/src/database.js +++ b/src/database.js @@ -126,6 +126,25 @@ export function getAllChannels() { return results; } +export function getAllMessages(limit = 50) { + const stmt = db.prepare( + `SELECT * FROM messages + ORDER BY created_at DESC + LIMIT ?` + ); + + stmt.bind([limit]); + const results = []; + + while (stmt.step()) { + const row = stmt.getAsObject(); + results.push(row); + } + + stmt.free(); + return results; +} + export function getLatestMessageTime(channelId) { const stmt = db.prepare( `SELECT MAX(created_at) as latest FROM messages WHERE channel_id = ?` diff --git a/src/rss.js b/src/rss.js index dcf99ab..2cf96d5 100644 --- a/src/rss.js +++ b/src/rss.js @@ -1,14 +1,14 @@ import express from 'express'; import RSS from 'rss'; -import { getMessagesByChannel, getAllChannels } from './database.js'; +import { getMessagesByChannel, getAllChannels, getAllMessages } from './database.js'; const router = express.Router(); -function generateRSSFeed(messages, channelInfo) { +function generateRSSFeed(messages, channelInfo, feedUrlSuffix = null) { const feed = new RSS({ title: `Discord Channel - ${channelInfo}`, description: 'Messages from Discord channel', - feed_url: `${process.env.BASE_URL || 'http://localhost:3000'}/rss/${channelInfo}`, + feed_url: `${process.env.BASE_URL || 'http://localhost:3000'}/rss/${feedUrlSuffix || channelInfo}`, site_url: process.env.BASE_URL || 'http://localhost:3000', language: 'en', pubDate: new Date(), @@ -59,4 +59,21 @@ router.get('/:channelId', (req, res) => { res.type('application/rss+xml').send(xml); }); +// GET /rss-all - Combined RSS feed from all channels +router.get('/all', (req, res) => { + const limit = parseInt(req.query.limit, 10) || 50; + + const messages = getAllMessages(limit); + + if (messages.length === 0) { + return res.status(404).json({ + error: 'No messages found', + hint: 'Make sure the bot has fetched messages from channels', + }); + } + + const xml = generateRSSFeed(messages, 'All Channels', 'all'); + res.type('application/rss+xml').send(xml); +}); + export default router;