rss-to-activitypub/routes/inbox.js

104 lines
3.5 KiB
JavaScript
Raw Normal View History

2018-10-15 07:18:10 +03:00
'use strict';
const express = require('express'),
crypto = require('crypto'),
request = require('request'),
fs = require('fs'),
router = express.Router();
function signAndSend(message, name, domain, req, res, targetDomain) {
// get the URI of the actor object and append 'inbox' to it
let inbox = message.object.actor+'/inbox';
let inboxFragment = inbox.replace('https://'+targetDomain,'');
2018-10-15 07:18:10 +03:00
// get the private key
let db = req.app.get('db');
let result = db.prepare('select privkey from accounts where name = ?').get(`${name}@${domain}`);
if (result === undefined) {
return res.status(404).send(`No record found for ${name}.`);
}
else {
let privkey = result.privkey;
const signer = crypto.createSign('sha256');
let d = new Date();
let stringToSign = `(request-target): post ${inboxFragment}\nhost: ${targetDomain}\ndate: ${d.toUTCString()}`;
2018-10-15 07:18:10 +03:00
signer.update(stringToSign);
signer.end();
const signature = signer.sign(privkey);
const signature_b64 = signature.toString('base64');
let header = `keyId="https://${domain}/u/${name}",headers="(request-target) host date",signature="${signature_b64}"`;
console.log('signature:',header);
console.log('message:',message);
2018-10-15 07:18:10 +03:00
request({
url: inbox,
2018-10-15 07:18:10 +03:00
headers: {
'Host': targetDomain,
'Date': d.toUTCString(),
'Signature': header
},
method: 'POST',
json: true,
body: message
}, function (error, response, body){
});
res.json('done');
}
}
function sendAcceptMessage(thebody, name, domain, req, res, targetDomain) {
const guid = crypto.randomBytes(16).toString('hex');
console.log(thebody);
let message = {
'@context': 'https://www.w3.org/ns/activitystreams',
'id': `https://${domain}/${guid}`,
'type': 'Accept',
'actor': `https://${domain}/u/${name}`,
'object': thebody,
};
signAndSend(message, name, domain, req, res, targetDomain);
}
router.post('/', function (req, res) {
// pass in a name for an account, if the account doesn't exist, create it!
let domain = req.app.get('domain');
if (req.body.actor === undefined) {
return res.status(400).send(`No actor specified.`);
}
2018-10-15 07:18:10 +03:00
const myURL = new URL(req.body.actor);
let targetDomain = myURL.hostname;
2018-11-06 19:45:33 +02:00
fs.appendFile('./inbox.log', JSON.stringify(req.body)+'\r\n', function (err) {
2018-10-15 07:18:10 +03:00
if (err) {
return console.log(err);
}
});
// TODO: add "Undo" follow event
if (typeof req.body.object === 'string' && req.body.type === 'Follow') {
let name = req.body.object.replace(`https://${domain}/u/`,'');
sendAcceptMessage(req.body, name, domain, req, res, targetDomain);
// Add the user to the DB of accounts that follow the account
let db = req.app.get('db');
// get the followers JSON for the user
let result = db.prepare('select followers from accounts where name = ?').get(`${name}@${domain}`);
if (result === undefined) {
console.log(`No record found for ${name}.`);
}
else {
// update followers
let followers = result.followers;
if (followers) {
followers = JSON.parse(followers);
2018-10-15 07:18:10 +03:00
followers.push(req.body.actor);
// unique items
followers = [...new Set(followers)];
}
else {
followers = [req.body.actor];
}
let followersText = JSON.stringify(followers);
2018-10-15 22:31:18 +03:00
console.log('adding followersText', followersText);
2018-10-15 07:18:10 +03:00
// update into DB
2018-10-15 22:31:18 +03:00
db.prepare('update accounts set followers = ? where name = ?').run(followersText, `${name}@${domain}`);
2018-10-15 07:18:10 +03:00
}
}
});
module.exports = router;