Adding a Followers object to the Actor
See https://github.com/dariusk/rss-to-activitypub/issues/11 for more information on why this fix is needed.
This commit is contained in:
parent
489a228b68
commit
e2ab4dd51e
3 changed files with 44 additions and 3 deletions
|
@ -15,6 +15,7 @@ function createActor(name, domain, pubkey) {
|
||||||
'type': 'Person',
|
'type': 'Person',
|
||||||
'preferredUsername': `${name}`,
|
'preferredUsername': `${name}`,
|
||||||
'inbox': `https://${domain}/api/inbox`,
|
'inbox': `https://${domain}/api/inbox`,
|
||||||
|
'followers': `https://${domain}/u/${name}/followers`,
|
||||||
|
|
||||||
'publicKey': {
|
'publicKey': {
|
||||||
'id': `https://${domain}/u/${name}#main-key`,
|
'id': `https://${domain}/u/${name}#main-key`,
|
||||||
|
|
|
@ -5,6 +5,9 @@ const express = require('express'),
|
||||||
router = express.Router();
|
router = express.Router();
|
||||||
|
|
||||||
function signAndSend(message, name, domain, req, res, targetDomain) {
|
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,'');
|
||||||
// get the private key
|
// get the private key
|
||||||
let db = req.app.get('db');
|
let db = req.app.get('db');
|
||||||
db.get('select privkey from accounts where name = $name', {$name: `${name}@${domain}`}, (err, result) => {
|
db.get('select privkey from accounts where name = $name', {$name: `${name}@${domain}`}, (err, result) => {
|
||||||
|
@ -15,14 +18,14 @@ function signAndSend(message, name, domain, req, res, targetDomain) {
|
||||||
let privkey = result.privkey;
|
let privkey = result.privkey;
|
||||||
const signer = crypto.createSign('sha256');
|
const signer = crypto.createSign('sha256');
|
||||||
let d = new Date();
|
let d = new Date();
|
||||||
let stringToSign = `(request-target): post /inbox\nhost: ${targetDomain}\ndate: ${d.toUTCString()}`;
|
let stringToSign = `(request-target): post ${inboxFragment}\nhost: ${targetDomain}\ndate: ${d.toUTCString()}`;
|
||||||
signer.update(stringToSign);
|
signer.update(stringToSign);
|
||||||
signer.end();
|
signer.end();
|
||||||
const signature = signer.sign(privkey);
|
const signature = signer.sign(privkey);
|
||||||
const signature_b64 = signature.toString('base64');
|
const signature_b64 = signature.toString('base64');
|
||||||
let header = `keyId="https://${domain}/u/${name}",headers="(request-target) host date",signature="${signature_b64}"`;
|
let header = `keyId="https://${domain}/u/${name}",headers="(request-target) host date",signature="${signature_b64}"`;
|
||||||
request({
|
request({
|
||||||
url: `https://${targetDomain}/inbox`,
|
url: inbox,
|
||||||
headers: {
|
headers: {
|
||||||
'Host': targetDomain,
|
'Host': targetDomain,
|
||||||
'Date': d.toUTCString(),
|
'Date': d.toUTCString(),
|
||||||
|
|
|
@ -10,16 +10,53 @@ router.get('/:name', function (req, res) {
|
||||||
else {
|
else {
|
||||||
let db = req.app.get('db');
|
let db = req.app.get('db');
|
||||||
let domain = req.app.get('domain');
|
let domain = req.app.get('domain');
|
||||||
|
let username = name;
|
||||||
name = `${name}@${domain}`;
|
name = `${name}@${domain}`;
|
||||||
db.get('select actor from accounts where name = $name', {$name: name}, (err, result) => {
|
db.get('select actor from accounts where name = $name', {$name: name}, (err, result) => {
|
||||||
if (result === undefined) {
|
if (result === undefined) {
|
||||||
return res.status(404).send(`No record found for ${name}.`);
|
return res.status(404).send(`No record found for ${name}.`);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
res.json(JSON.parse(result.actor));
|
let tempActor = JSON.parse(result.actor);
|
||||||
|
// Added this followers URI for Pleroma compatibility, see https://github.com/dariusk/rss-to-activitypub/issues/11#issuecomment-471390881
|
||||||
|
// New Actors should have this followers URI but in case of migration from an old version this will add it in on the fly
|
||||||
|
if (tempActor.followers === undefined) {
|
||||||
|
tempActor.followers = `https://${domain}/u/${username}/followers`;
|
||||||
|
}
|
||||||
|
res.json(tempActor);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
router.get('/:name/followers', function (req, res) {
|
||||||
|
let name = req.params.name;
|
||||||
|
if (!name) {
|
||||||
|
return res.status(400).send('Bad request.');
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
let db = req.app.get('db');
|
||||||
|
let domain = req.app.get('domain');
|
||||||
|
let result = db.prepare('select followers from accounts where name = ?').get(`${name}@${domain}`);
|
||||||
|
let followers = JSON.parse(result.followers);
|
||||||
|
if (!followers) {
|
||||||
|
followers = [];
|
||||||
|
}
|
||||||
|
let followersCollection = {
|
||||||
|
"type":"OrderedCollection",
|
||||||
|
"totalItems":followers.length,
|
||||||
|
"id":`https://${domain}/u/${name}/followers`,
|
||||||
|
"first": {
|
||||||
|
"type":"OrderedCollectionPage",
|
||||||
|
"totalItems":followers.length,
|
||||||
|
"partOf":`https://${domain}/u/${name}/followers`,
|
||||||
|
"orderedItems": followers,
|
||||||
|
"id":`https://${domain}/u/${name}/followers?page=1`
|
||||||
|
},
|
||||||
|
"@context":["https://www.w3.org/ns/activitystreams"]
|
||||||
|
};
|
||||||
|
res.json(followersCollection);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = router;
|
||||||
|
|
Loading…
Reference in a new issue