Skip to content

Node.js based forum software built for the modern web

feat(activitypub): allow plugins to handle incoming AP activities via plugin hook

Julian Lam2bf93aa9d3c1
diff --git a/src/activitypub/inbox.js b/src/activitypub/inbox.js
index 69ebc110e0..5b7c14af0c 100644
--- a/src/activitypub/inbox.js
+++ b/src/activitypub/inbox.js
@@ -895,7 +895,11 @@ inbox.undo = async (req) => {
throw new Error('[[error:activitypub.invalid-id]]');
}
- let { type: localType, id } = await helpers.resolveLocalId(object.object);
+ // The original activity's object may be embedded as a full object or a bare URL
+ const subjectId = (object.object && typeof object.object === 'object' && !Array.isArray(object.object)) ?
+ object.object.id : object.object;
+
+ let { type: localType, id } = await helpers.resolveLocalId(subjectId);
// If object is a Follow activity, check if the target is the instance actor (relay follow)
if (!localType && object?.type === 'Follow') {
@@ -961,13 +965,13 @@ inbox.undo = async (req) => {
}
await posts.unvote(id, actor);
- activitypub.feps.announce(object.object, req.body);
+ activitypub.feps.announce(subjectId, req.body);
notifications.rescind(`upvote:post:${id}:uid:${actor}`);
break;
}
case 'Announce': {
- id = id || object.object; // remote announces
+ id = id || subjectId; // remote announces
const exists = await posts.exists(id);
if (!exists) {
activitypub.helpers.log(`[activitypub/inbox/undo] Attempted to undo announce of ${id} but couldn't find it, so doing nothing.`);
diff --git a/src/controllers/activitypub/index.js b/src/controllers/activitypub/index.js
index 4f5d691774..1cd8a20f20 100644
--- a/src/controllers/activitypub/index.js
+++ b/src/controllers/activitypub/index.js
@@ -11,6 +11,7 @@ const user = require('../../user');
const groups = require('../../groups');
const privileges = require('../../privileges');
const activitypub = require('../../activitypub');
+const plugins = require('../../plugins');
const utils = require('../../utils');
const helpers = require('../helpers');
@@ -394,13 +395,26 @@ Controller.getInbox = async (req, res) => {
Controller.postInbox = async (req, res) => {
// Note: underlying methods are internal use only, hence no exposure via src/api
const method = String(req.body.type).toLowerCase();
- if (!activitypub.inbox.hasOwnProperty(method)) {
- activitypub.helpers.log(`[activitypub/inbox] Received Activity of type ${method} but unable to handle. Ignoring.`);
- return res.sendStatus(200);
- }
-
const originalBody = structuredClone(req.body);
+
try {
+ // Plugins may claim (and/or transparently rewrite) the activity before built-in handling
+ const context = await plugins.hooks.fire(`filter:activitypub.${method}`, {
+ req,
+ activity: req.body,
+ claimed: false,
+ });
+ if (context.claimed) {
+ await activitypub.analytics.receipt(context.activity);
+ return helpers.formatApiResponse(202, res);
+ }
+ req.body = context.activity;
+
+ if (!activitypub.inbox.hasOwnProperty(method)) {
+ activitypub.helpers.log(`[activitypub/inbox] Received Activity of type ${method} but unable to handle. Ignoring.`);
+ return res.sendStatus(200);
+ }
+
await activitypub.inbox[method](req);
await activitypub.analytics.receipt(req.body);
await helpers.formatApiResponse(202, res);