From 1685ec7cc8725bf67d246f82a5a12b8c9ef333d4 Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Mon, 3 Jul 2023 11:56:25 +0200 Subject: [PATCH] allow sending blog-wide activities --- includes/class-activity-dispatcher.php | 95 +++++++++++-------- includes/class-scheduler.php | 6 +- ...-class-activitypub-activity-dispatcher.php | 10 +- 3 files changed, 59 insertions(+), 52 deletions(-) diff --git a/includes/class-activity-dispatcher.php b/includes/class-activity-dispatcher.php index 00ffd48..618d668 100644 --- a/includes/class-activity-dispatcher.php +++ b/includes/class-activity-dispatcher.php @@ -1,11 +1,14 @@ from_post( $activitypub_post ); + if ( is_user_disabled( $wp_post->post_author ) ) { + return; + } - $user_id = $activitypub_post->get_user_id(); + $post = new Post( $wp_post ); + + $activitypub_activity = new Activity( $activity_type ); + $activitypub_activity->from_post( $post ); + + $user_id = $wp_post->post_author; + $follower_inboxes = Followers::get_inboxes( $user_id ); + $mentioned_inboxes = Mention::get_inboxes( $activitypub_activity->get_cc() ); + + $inboxes = array_merge( $follower_inboxes, $mentioned_inboxes ); + $inboxes = array_unique( $inboxes ); + + foreach ( $inboxes as $inbox ) { + $activity = $activitypub_activity->to_json(); + + safe_remote_post( $inbox, $activity, $user_id ); + } + } + + /** + * Send Activities to followers and mentioned users. + * + * @param WP_Post $wp_post The ActivityPub Post. + * @param string $activity_type The Activity-Type. + * + * @return void + */ + public static function send_blog_activity( WP_Post $wp_post, $activity_type ) { + // check if a migration is needed before sending new posts + Migration::maybe_migrate(); + + if ( is_user_disabled( User_Factory::BLOG_USER_ID ) ) { + return; + } + + $post = new Post( $wp_post, User_Factory::BLOG_USER_ID ); + + $activitypub_activity = new Activity( $activity_type ); + $activitypub_activity->from_post( $post ); + + $user_id = User_Factory::BLOG_USER_ID; $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 db2d28c..fa8ccc2 100644 --- a/includes/class-scheduler.php +++ b/includes/class-scheduler.php @@ -83,12 +83,10 @@ class Scheduler { return; } - $activitypub_post = new Post( $post, User_Factory::BLOG_USER_ID ); - \wp_schedule_single_event( \time(), 'activitypub_send_activity', - array( $activitypub_post, $type ) + array( $post, $type ) ); \wp_schedule_single_event( @@ -97,7 +95,7 @@ class Scheduler { 'activitypub_send_%s_activity', \strtolower( $type ) ), - array( $activitypub_post ) + array( $post ) ); } diff --git a/tests/test-class-activitypub-activity-dispatcher.php b/tests/test-class-activitypub-activity-dispatcher.php index 61f8c2b..cb76c17 100644 --- a/tests/test-class-activitypub-activity-dispatcher.php +++ b/tests/test-class-activitypub-activity-dispatcher.php @@ -34,10 +34,9 @@ class Test_Activitypub_Activity_Dispatcher extends ActivityPub_TestCase_Cache_HT $pre_http_request = new MockAction(); add_filter( 'pre_http_request', array( $pre_http_request, 'filter' ), 10, 3 ); - $activitypub_post = new \Activitypub\Model\Post( $post ); - \Activitypub\Activity_Dispatcher::send_create_activity( $activitypub_post ); + $post = get_post( $post ); - $this->assertNotEmpty( $activitypub_post->get_content() ); + \Activitypub\Activity_Dispatcher::send_user_activity( $post, 'Create' ); $this->assertSame( 2, $pre_http_request->get_call_count() ); $all_args = $pre_http_request->get_args(); @@ -77,10 +76,9 @@ class Test_Activitypub_Activity_Dispatcher extends ActivityPub_TestCase_Cache_HT $pre_http_request = new MockAction(); add_filter( 'pre_http_request', array( $pre_http_request, 'filter' ), 10, 3 ); - $activitypub_post = new \Activitypub\Model\Post( $post ); - \Activitypub\Activity_Dispatcher::send_create_activity( $activitypub_post ); + $post = get_post( $post ); - $this->assertNotEmpty( $activitypub_post->get_content() ); + \Activitypub\Activity_Dispatcher::send_user_activity( $post, 'Create' ); $this->assertSame( 1, $pre_http_request->get_call_count() ); $all_args = $pre_http_request->get_args();