rss-to-activitypub/routes/message.js
Darius Kazemi c22dbae3b5 Commit missing files
Fixes #21
2019-04-12 05:30:34 -07:00

45 lines
1.7 KiB
JavaScript

'use strict';
const express = require('express'),
router = express.Router(),
Parser = require('rss-parser'),
parser = new Parser();
router.get('/:guid', function (req, res) {
let guid = req.params.guid;
if (!guid) {
return res.status(400).send('Bad request.');
}
// render the raw JSON if JSON headers are passed
else if (req.headers.accept && (req.headers.accept.includes('application/activity+json') || req.headers.accept.includes('application/json') || req.headers.accept.includes('application/json+ld'))) {
let db = req.app.get('db');
let result = db.prepare('select message from messages where guid = ?').get(guid);
if (result === undefined) {
return res.status(404).send(`No record found for ${guid}.`);
}
else {
res.json(JSON.parse(result.message));
}
}
// render a human-friendly view otherwise
else {
let db = req.app.get('db');
let result = db.prepare('select message from messages where guid = ?').get(guid);
if (result === undefined) {
return res.status(404).send(`No record found for ${guid}.`);
}
else {
let message = JSON.parse(result.message);
let domain = req.app.get('domain');
let username = message.attributedTo.replace(`https://${domain}/u/`,'');
let resultFeed = db.prepare('select feed from feeds where username = ?').get(username);
if (resultFeed === undefined) {
return res.status(404).json(`Something went very wrong!`);
}
let feed = resultFeed.feed;
res.render('message', { description: message.content, feedUrl: feed, author: message.attributedTo, link: message.link});
}
}
});
module.exports = router;