Skip to content

Node.js based forum software built for the modern web

feat(activitypub): enable instance actor to follow remote actors

Julian Lamdeeeb3d48172
diff --git a/src/activitypub/inbox.js b/src/activitypub/inbox.js
index 2469260961..7fb308e701 100644
--- a/src/activitypub/inbox.js
+++ b/src/activitypub/inbox.js
@@ -845,7 +845,7 @@ inbox.accept = async (req) => {
const { type: localType, id } = await helpers.resolveLocalId(object.actor);
if (object.id === `${nconf.get('url')}/actor`) {
return activitypub.relays.handshake(req.body);
- } else if (!['user', 'category'].includes(localType)) {
+ } else if (!['user', 'category', 'application'].includes(localType)) {
throw new Error('[[error:invalid-data]]');
}
@@ -879,6 +879,18 @@ inbox.accept = async (req) => {
db.sortedSetAdd(`cid:${id}:following`, timestamp, actor),
db.sortedSetAdd(`followersRemote:${actor}`, timestamp, `cid|${id}`), // for notes assertion checking
]);
+ } else if (localType === 'application') {
+ // Instance actor follow acceptance
+ if (!await db.isSortedSetMember('followRequests:uid.0', actor)) {
+ if (await db.isSortedSetMember('followingRemote:0', actor)) return; // already following
+ throw new Error('[[error:invalid-data]]'); // not following, not requested, so reject to hopefully stop retries
+ }
+ const timestamp = await db.sortedSetScore('followRequests:uid.0', actor);
+ await Promise.all([
+ db.sortedSetRemove('followRequests:uid.0', actor),
+ db.sortedSetAdd('followingRemote:0', timestamp, actor),
+ db.sortedSetAdd(`followersRemote:${actor}`, timestamp, 0), // for followers backreference
+ ]);
}
activitypub.actors._followerCache.del(actor);
@@ -915,7 +927,14 @@ inbox.undo = async (req) => {
case 'Follow': {
switch (localType) {
case 'application': {
+ // Relay unfollow
await activitypub.relays.removeFollower(actor);
+ // Generic instance-actor unfollow
+ await Promise.all([
+ db.sortedSetRemove('followingRemote:0', actor),
+ db.sortedSetRemove('followRequests:uid.0', actor),
+ db.sortedSetRemove(`followersRemote:${actor}`, 0),
+ ]);
break;
}
diff --git a/src/activitypub/out.js b/src/activitypub/out.js
index 526e4ad13a..33690dffa2 100644
--- a/src/activitypub/out.js
+++ b/src/activitypub/out.js
@@ -61,9 +61,10 @@ Out.follow = enabledCheck(async (type, id, actor) => {
const timestamp = Date.now();
await db.sortedSetAdd(`followRequests:${type}.${id}`, timestamp, actor);
+ const actorUri = activitypub.helpers.resolveActor(type, id);
try {
await activitypub.send(type, id, [actor], {
- id: `${nconf.get('url')}/${type}/${id}#activity/follow/${encodeURIComponent(actor)}/${timestamp}`,
+ id: `${actorUri}#activity/follow/${encodeURIComponent(actor)}/${timestamp}`,
type: 'Follow',
to: [actor],
object: actor,
@@ -494,33 +495,35 @@ Out.undo.follow = enabledCheck(async (type, id, actor) => {
], actor);
const timestamp = timestamps[0] || timestamps[1];
+ const actorUri = activitypub.helpers.resolveActor(type, id);
const object = {
- id: `${nconf.get('url')}/${type}/${id}#activity/follow/${encodeURIComponent(actor)}/${timestamp}`,
+ id: `${actorUri}#activity/follow/${encodeURIComponent(actor)}/${timestamp}`,
type: 'Follow',
object: actor,
+ actor: actorUri,
};
- if (type === 'uid') {
- object.actor = `${nconf.get('url')}/uid/${id}`;
- } else if (type === 'cid') {
- object.actor = `${nconf.get('url')}/category/${id}`;
- }
await activitypub.send(type, id, [actor], {
- id: `${nconf.get('url')}/${type}/${id}#activity/undo:follow/${encodeURIComponent(actor)}/${timestamp}`,
+ id: `${actorUri}#activity/undo:follow/${encodeURIComponent(actor)}/${timestamp}`,
type: 'Undo',
to: [actor],
- actor: object.actor,
+ actor: actorUri,
object,
});
if (type === 'uid') {
- await Promise.all([
+ const syncPromises = [
db.sortedSetRemove(`followingRemote:${id}`, actor),
db.sortedSetRemove(`followRequests:uid.${id}`, actor),
db.sortedSetRemove(`followersRemote:${actor}`, id),
- user.syncFollowCounts(id, true, false),
- user.syncFollowCounts(actor, false, true),
- ]);
+ ];
+ if (id > 0) {
+ syncPromises.push(
+ user.syncFollowCounts(id, true, false),
+ user.syncFollowCounts(actor, false, true),
+ );
+ }
+ await Promise.all(syncPromises);
} else if (type === 'cid') {
await Promise.all([
db.sortedSetRemove(`cid:${id}:following`, actor),
diff --git a/src/controllers/activitypub/actors.js b/src/controllers/activitypub/actors.js
index 4a03ebb942..c9d9399a8b 100644
--- a/src/controllers/activitypub/actors.js
+++ b/src/controllers/activitypub/actors.js
@@ -28,6 +28,7 @@ Actors.application = async function (req, res) {
url: `${nconf.get('url')}/actor`,
inbox: `${nconf.get('url')}/inbox`,
outbox: `${nconf.get('url')}/outbox`,
+ following: `${nconf.get('url')}/actor/following`,
attributedTo: `${nconf.get('url')}/actor/admins`,
type: 'Application',
@@ -49,6 +50,29 @@ Actors.application = async function (req, res) {
});
};
+Actors.following = async function (req, res) {
+ const count = await db.sortedSetCard('followingRemote:0');
+ const collection = await activitypub.helpers.generateCollection({
+ set: 'followingRemote:0',
+ count,
+ perPage: 50,
+ page: req.query.page,
+ url: `${nconf.get('url')}/actor/following`,
+ });
+
+ if (collection.hasOwnProperty('orderedItems')) {
+ collection.orderedItems = collection.orderedItems.map((actorUri) => {
+ if (utils.isNumber(actorUri)) {
+ return `${nconf.get('url')}/uid/${actorUri}`;
+ }
+
+ return actorUri;
+ });
+ }
+
+ res.status(200).json(collection);
+};
+
Actors.user = async function (req, res) {
// Privilege-gated at the router-level
const payload = await activitypub.mocks.actors.user(req.params.uid);
diff --git a/src/routes/activitypub.js b/src/routes/activitypub.js
index b0d6cd4661..2bbc09a24a 100644
--- a/src/routes/activitypub.js
+++ b/src/routes/activitypub.js
@@ -33,6 +33,7 @@ module.exports = function (app, middleware, controllers) {
];
app.get('/actor', middlewares, helpers.tryRoute(controllers.activitypub.actors.application));
+ app.get('/actor/following', middlewares, helpers.tryRoute(controllers.activitypub.actors.following));
app.get('/actor/admins', middlewares, helpers.tryRoute(controllers.activitypub.getAdmins));
app.post('/inbox', [...middlewares, ...inboxMiddlewares], helpers.tryRoute(controllers.activitypub.postInbox));