From a8fe587f913c52fba41154bb92640daf2fbc67eb Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Wed, 14 Jun 2023 15:02:45 +0200 Subject: [PATCH] prepare federation method --- includes/class-activity-dispatcher.php | 16 +++++++------- includes/class-scheduler.php | 29 ++++++++++++++++---------- includes/functions.php | 3 ++- includes/model/class-blog-user.php | 7 ++++++- 4 files changed, 34 insertions(+), 21 deletions(-) diff --git a/includes/class-activity-dispatcher.php b/includes/class-activity-dispatcher.php index bea6b4e..cde9785 100644 --- a/includes/class-activity-dispatcher.php +++ b/includes/class-activity-dispatcher.php @@ -26,6 +26,8 @@ class Activity_Dispatcher { \add_action( 'activitypub_send_create_activity', array( self::class, 'send_create_activity' ) ); \add_action( 'activitypub_send_update_activity', array( self::class, 'send_update_activity' ) ); \add_action( 'activitypub_send_delete_activity', array( self::class, 'send_delete_activity' ) ); + + \add_action( 'activitypub_send_activity', array( self::class, 'send_activity' ), 10, 2 ); } /** @@ -67,13 +69,8 @@ class Activity_Dispatcher { // check if a migration is needed before sending new posts Migration::maybe_migrate(); - if ( ! is_single_user_mode() ) { - // send User-Activity - self::send_user_activity( $activitypub_post, $activity_type ); - } - // send Blog-User-Activity - self::send_user_activity( $activitypub_post, $activity_type, User_Factory::BLOG_USER_ID ); + self::send_user_activity( $activitypub_post, $activity_type ); } /** @@ -92,12 +89,15 @@ class Activity_Dispatcher { $actor = $user->get_url(); } else { $user_id = $activitypub_post->get_post_author(); - $actor = $activitypub_activity->get_actor(); + $actor = null; } $activitypub_activity = new Activity( $activity_type ); $activitypub_activity->from_post( $activitypub_post ); - $activitypub_activity->set_actor( $actor ); + + if ( $actor ) { + $activitypub_activity->set_actor( $actor ); + } $follower_inboxes = Followers::get_inboxes( $user_id ); $mentioned_inboxes = Mention::get_inboxes( $activitypub_activity->get_cc() ); diff --git a/includes/class-scheduler.php b/includes/class-scheduler.php index d55fb35..a5cfbc8 100644 --- a/includes/class-scheduler.php +++ b/includes/class-scheduler.php @@ -70,22 +70,29 @@ class Scheduler { $activitypub_post = new Post( $post ); + $activity_type = false; + if ( 'publish' === $new_status && 'publish' !== $old_status ) { - \wp_schedule_single_event( - \time(), - 'activitypub_send_create_activity', - array( $activitypub_post ) - ); + $activity_type = 'Create'; } elseif ( 'publish' === $new_status ) { - \wp_schedule_single_event( - \time(), - 'activitypub_send_update_activity', - array( $activitypub_post ) - ); + $activity_type = 'Update'; } elseif ( 'trash' === $new_status ) { + $activity_type = 'Delete'; + } + + if ( $activity_type ) { \wp_schedule_single_event( \time(), - 'activitypub_send_delete_activity', + 'activitypub_send_activity', + array( $activitypub_post, $activity_type ) + ); + + \wp_schedule_single_event( + \time(), + sprintf( + 'activitypub_send_%s_activity', + \strtolower( $activity_type ) + ), array( $activitypub_post ) ); } diff --git a/includes/functions.php b/includes/functions.php index dfbdf37..595e85b 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -56,9 +56,10 @@ function get_remote_metadata_by_actor( $actor, $cached = true ) { return $actor; } + $transient_key = 'activitypub_' . $actor; + // only check the cache if needed. if ( $cached ) { - $transient_key = 'activitypub_' . $actor; $metadata = \get_transient( $transient_key ); if ( $metadata ) { diff --git a/includes/model/class-blog-user.php b/includes/model/class-blog-user.php index c92fdd3..8a04018 100644 --- a/includes/model/class-blog-user.php +++ b/includes/model/class-blog-user.php @@ -44,7 +44,12 @@ class Blog_User extends User { * @return string The User-Description. */ public function get_summary() { - return \wpautop( \wp_kses( \get_bloginfo( 'description' ), 'default' ) ); + return \wpautop( + \wp_kses( + \get_bloginfo( 'description' ), + 'default' + ) + ); } /**