Add Followers reference to Actor object

This is for Pleroma compatibility per

https://github.com/dariusk/rss-to-activitypub/issues/11#issuecomment-471390881

And fixes #11.
This commit is contained in:
Darius Kazemi 2019-03-10 21:02:12 -07:00
parent 48d1a7ba87
commit eec3b31641
2 changed files with 41 additions and 2 deletions

View File

@ -97,6 +97,7 @@ function createActor(name, domain, pubkey, displayName, imageUrl, description) {
'type': 'Person', 'type': 'Person',
'preferredUsername': `${name}`, 'preferredUsername': `${name}`,
'inbox': `https://${domain}/api/inbox`, 'inbox': `https://${domain}/api/inbox`,
'followers': `https://${domain}/u/${name}/followers`,
'name': displayName, 'name': displayName,
'publicKey': { 'publicKey': {
'id': `https://${domain}/u/${name}#main-key`, 'id': `https://${domain}/u/${name}#main-key`,

View File

@ -10,17 +10,23 @@ 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}`;
let result = db.prepare('select actor from accounts where name = ?').get(name); let result = db.prepare('select actor from accounts where name = ?').get(name);
if (result === undefined) { if (result === undefined) {
return res.status(404).json(`No record found for ${name}.`); return res.status(404).json(`No record found for ${name}.`);
} }
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'))) { 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'))) {
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);
} }
else { else {
let actor = JSON.parse(result.actor); let actor = JSON.parse(result.actor);
let username = name.replace('@'+domain,'');
let resultFeed = db.prepare('select content, feed from feeds where username = ?').get(username); let resultFeed = db.prepare('select content, feed from feeds where username = ?').get(username);
if (resultFeed === undefined) { if (resultFeed === undefined) {
return res.status(404).json(`Something went very wrong!`); return res.status(404).json(`Something went very wrong!`);
@ -41,4 +47,36 @@ router.get('/:name', function (req, res) {
} }
}); });
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);
// console.log(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);
//res.json(JSON.parse(result.actor));
}
});
module.exports = router; module.exports = router;