From eb58862777cdfc02e0574dcaf11140c2a5dbf12b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Menrath?= Date: Sat, 23 Dec 2023 09:52:03 +0100 Subject: [PATCH] Announce all posts through the application actor --- includes/class-activity-dispatcher.php | 32 ++++++++++++++++++++------ includes/collection/class-users.php | 6 +++++ includes/table/class-followers.php | 2 +- 3 files changed, 32 insertions(+), 8 deletions(-) diff --git a/includes/class-activity-dispatcher.php b/includes/class-activity-dispatcher.php index 6bb6a04..d245c9b 100644 --- a/includes/class-activity-dispatcher.php +++ b/includes/class-activity-dispatcher.php @@ -10,6 +10,7 @@ use Activitypub\Transformer\Factory; use Activitypub\Transformer\Post; use Activitypub\Transformer\Comment; +use function Activitypub\get_rest_url_by_path; use function Activitypub\is_single_user; use function Activitypub\is_user_disabled; use function Activitypub\safe_remote_post; @@ -49,8 +50,10 @@ class Activity_Dispatcher { if ( is_single_user() ) { self::send_activity( $wp_object, $type, Users::BLOG_USER_ID ); + self::send_announce( $wp_object, $type, Users::APPLICATION_USER_ID ); } else { - self::send_announce( $wp_object, $type ); + self::send_announce( $wp_object, $type, Users::BLOG_USER_ID ); + self::send_announce( $wp_object, $type, Users::APPLICATION_USER_ID ); } } @@ -88,21 +91,36 @@ class Activity_Dispatcher { * * @return void */ - public static function send_announce( $wp_object, $type ) { + public static function send_announce( $wp_object, $type, $user_id = null ) { if ( ! in_array( $type, array( 'Create', 'Update' ), true ) ) { return; } - if ( is_user_disabled( Users::BLOG_USER_ID ) ) { + $transformer = Factory::instance()->get_transformer( $wp_object ); + + if ( null !== $user_id && Users::APPLICATION_USER_ID !== $user_id ) { + $transformer->change_wp_user_id( $user_id ); + } + + if ( ! $user_id ) { + $user_id = $transformer->get_wp_user_id(); + } + + if ( is_user_disabled( $user_id ) ) { return; } - $transformer = Factory::instance()->get_transformer( $wp_object ); - $transformer->change_wp_user_id( Users::BLOG_USER_ID ); - - $user_id = $transformer->get_wp_user_id(); $activity = $transformer->to_activity( 'Announce' ); + // TODO: properly fix this for the instance-to-instance federation with Mobilizon. + // Error: + // Failed to map identity from signature (payload actor mismatch) + // key_id=http://wp.lan/wp-json/activitypub/1.0/application, actor=http://wp.lan/@blog + // Of course, the announce must be sent as the Application actor because he also signed it! + if ( Users::APPLICATION_USER_ID === $user_id ) { + $activity->set_actor( get_rest_url_by_path( 'application' ) ); + } + self::send_activity_to_inboxes( $activity, $user_id ); } diff --git a/includes/collection/class-users.php b/includes/collection/class-users.php index ad94297..8f279fb 100644 --- a/includes/collection/class-users.php +++ b/includes/collection/class-users.php @@ -7,6 +7,7 @@ use Activitypub\Model\User; use Activitypub\Model\Blog_User; use Activitypub\Model\Application_User; +use function Activitypub\get_rest_url_by_path; use function Activitypub\url_to_authorid; use function Activitypub\is_user_disabled; @@ -176,6 +177,11 @@ class Users { return self::get_by_id( self::BLOG_USER_ID ); } + // check for application actor + if ( self::normalize_url( get_rest_url_by_path( 'application' ) ) === self::normalize_url( $resource ) ) { + return self::get_by_id( self::APPLICATION_USER_ID ); + } + return new WP_Error( 'activitypub_no_user_found', \__( 'User not found', 'activitypub' ), diff --git a/includes/table/class-followers.php b/includes/table/class-followers.php index df9747b..76b1f83 100644 --- a/includes/table/class-followers.php +++ b/includes/table/class-followers.php @@ -16,7 +16,7 @@ class Followers extends WP_List_Table { public function __construct() { if ( get_current_screen()->id === 'settings_page_activitypub' ) { - $this->user_id = Users::BLOG_USER_ID; + $this->user_id = Users::APPLICATION_USER_ID; } else { $this->user_id = \get_current_user_id(); }