diff --git a/includes/activitypub/handler/class-join.php b/includes/activitypub/handler/class-join.php new file mode 100644 index 0000000..03c5dc1 --- /dev/null +++ b/includes/activitypub/handler/class-join.php @@ -0,0 +1,137 @@ +get_actor_object()->get_id(), + $activity, + ); + } + + /** + * Send "Ignore" response. + * + * @param string $actor The actors ActivityPub ID which sends the response. + * @param array $activity_object The Activity object that gets ignored. + * @param string $to The target actor. + */ + public static function send_ignore_response( $actor, $activity_object, $to = null ) { + if ( ! $to && array_key_exists( 'actor', $activity_object ) ) { + $to = object_to_uri( $activity_object['actor'] ); + } + + if ( ! $to ) { + return; + } + + // Only send minimal data. + $activity_object = array_intersect_key( + $activity_object, + array_flip( + array( + 'id', + 'type', + 'actor', + 'object', + ) + ) + ); + + $to = Actors::get_by_resource( $to ); + $actor = Actors::get_by_resource( $actor ); + + // Get inbox. + $inbox = $to->get_shared_inbox(); + + // Send "Ignore" activity. + $activity = new Activity(); + $activity->set_type( 'Ignore' ); + $activity->set_object( $activity_object ); + $activity->set_actor( $actor->get_id() ); + $activity->set_to( $to ); + $activity->set_id( $actor->get_id() . '#ignore-' . \preg_replace( '~^https?://~', '', $activity_object['id'] ) ); + + $activity = $activity->to_json(); + + Http::post( $inbox, $activity, $actor->get__id() ); + } +} diff --git a/includes/class-setup.php b/includes/class-setup.php index 2912002..91140a1 100644 --- a/includes/class-setup.php +++ b/includes/class-setup.php @@ -15,6 +15,7 @@ namespace ActivityPub_Event_Bridge; // Exit if accessed directly. defined( 'ABSPATH' ) || exit; // @codeCoverageIgnore +use ActivityPub_Event_Bridge\ActivityPub\Handler\Join; use ActivityPub_Event_Bridge\Admin\Event_Plugin_Admin_Notices; use ActivityPub_Event_Bridge\Admin\General_Admin_Notices; use ActivityPub_Event_Bridge\Admin\Health_Check; @@ -187,6 +188,10 @@ class Setup { return; } + // Register the handler for incoming ActivityPub "Join" activities. + add_action( 'init', array( Join::class, 'init' ) ); + + // Register the custom ActivityPub transformers. add_filter( 'activitypub_transformer', array( $this, 'register_activitypub_event_transformer' ), 10, 3 ); }