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',
'preferredUsername': `${name}`,
'inbox': `https://${domain}/api/inbox`,
'followers': `https://${domain}/u/${name}/followers`,
'name': displayName,
'publicKey': {
'id': `https://${domain}/u/${name}#main-key`,

View file

@ -10,17 +10,23 @@ router.get('/:name', function (req, res) {
else {
let db = req.app.get('db');
let domain = req.app.get('domain');
let username = name;
name = `${name}@${domain}`;
let result = db.prepare('select actor from accounts where name = ?').get(name);
if (result === undefined) {
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'))) {
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 {
let actor = JSON.parse(result.actor);
let username = name.replace('@'+domain,'');
let resultFeed = db.prepare('select content, feed from feeds where username = ?').get(username);
if (resultFeed === undefined) {
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;