From 94751480d600a44e65dd50501515d89cd7a28eb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Menrath?= Date: Wed, 27 Dec 2023 16:45:23 +0100 Subject: [PATCH] make the save of a follower return the post id instead of the object and adopt unit tests --- includes/collection/class-followers.php | 9 +-- includes/handler/class-follow.php | 6 +- includes/model/class-follow-request.php | 3 +- ...-class-activitypub-activity-dispatcher.php | 9 ++- tests/test-class-activitypub-followers.php | 55 +++++++++++-------- 5 files changed, 48 insertions(+), 34 deletions(-) diff --git a/includes/collection/class-followers.php b/includes/collection/class-followers.php index 05b653d..a55f823 100644 --- a/includes/collection/class-followers.php +++ b/includes/collection/class-followers.php @@ -23,14 +23,15 @@ class Followers { /** * Add new Follower. * - * This does not add the follow relationship. It is added when the Accept respone is sent. + * This does not add the follow relationship. + * + * The follow relationship can be added later with add_follow_relationship which is usually done when the Accept respone is sent. * - * @param int $user_id The ID of the WordPress User * @param string $actor The Actor URL * * @return array|WP_Error The Follower (WP_Post array) or an WP_Error */ - public static function add_follower( $user_id, $actor ) { + public static function add_follower( $actor ) { $meta = get_remote_metadata_by_actor( $actor ); if ( is_tombstone( $meta ) ) { @@ -55,7 +56,7 @@ class Followers { } /** - * Add the follow relationship. + * Add follow relationship between follower actor identified by internal post id and target actor identified by WordPress user id. * * @param int|string $user_id The internal id of the target WordPress user that gets followed. * @param int|string $follower_id The internal id of the follower actor. diff --git a/includes/handler/class-follow.php b/includes/handler/class-follow.php index 4d70576..a8d4475 100644 --- a/includes/handler/class-follow.php +++ b/includes/handler/class-follow.php @@ -46,19 +46,19 @@ class Follow { $user_id = $user->get__id(); // save follower - $follower = Followers::add_follower( + $follower_id = Followers::add_follower( $user_id, $activity['actor'] ); - if ( \is_wp_error( $follower ) ) { + if ( \is_wp_error( $follower_id ) ) { // it is not even possible to send a "Reject" or "Accept" because // we can not get the Remote-Inbox return; } // save follow request by this follower - $follow_request = Follow_Request::save( $follower, $user_id, $activity['id'] ); + $follow_request = Follow_Request::save( $follower_id, $user_id, $activity['id'] ); if ( ! $user->get_manually_approves_followers() ) { $follow_request->approve(); diff --git a/includes/model/class-follow-request.php b/includes/model/class-follow-request.php index 1b4f07d..310378e 100644 --- a/includes/model/class-follow-request.php +++ b/includes/model/class-follow-request.php @@ -129,8 +129,7 @@ class Follow_Request extends Base_Object { * * @return Follow_Request|WP_Error The Follow_Request or an WP_Error. */ - public static function save( $follower, $user_id, $activity_id ) { - $follower_id = $follower->get__id(); + public static function save( $follower_id, $user_id, $activity_id ) { $meta_input = array( 'activitypub_user_id' => $user_id, ); diff --git a/tests/test-class-activitypub-activity-dispatcher.php b/tests/test-class-activitypub-activity-dispatcher.php index c42d391..eb9c6ea 100644 --- a/tests/test-class-activitypub-activity-dispatcher.php +++ b/tests/test-class-activitypub-activity-dispatcher.php @@ -21,7 +21,8 @@ class Test_Activitypub_Activity_Dispatcher extends ActivityPub_TestCase_Cache_HT $followers = array( 'https://example.com/author/jon', 'https://example.org/users/username' ); foreach ( $followers as $follower ) { - \Activitypub\Collection\Followers::add_follower( 1, $follower ); + $follower_id = \Activitypub\Collection\Followers::add_follower( $follower ); + \Activitypub\Collection\Followers::add_follow_relationship( 1, $follower_id ); } $post = \wp_insert_post( @@ -100,7 +101,8 @@ class Test_Activitypub_Activity_Dispatcher extends ActivityPub_TestCase_Cache_HT $followers = array( 'https://example.com/author/jon' ); foreach ( $followers as $follower ) { - \Activitypub\Collection\Followers::add_follower( \Activitypub\Collection\Users::BLOG_USER_ID, $follower ); + $follower_id = \Activitypub\Collection\Followers::add_follower( $follower ); + \Activitypub\Collection\Followers::add_follow_relationship( \Activitypub\Collection\Users::BLOG_USER_ID, $follower_id ); } $post = \wp_insert_post( @@ -148,7 +150,8 @@ class Test_Activitypub_Activity_Dispatcher extends ActivityPub_TestCase_Cache_HT $this->assertTrue( \Activitypub\is_single_user() ); foreach ( $followers as $follower ) { - \Activitypub\Collection\Followers::add_follower( \Activitypub\Collection\Users::BLOG_USER_ID, $follower ); + $follower_id = \Activitypub\Collection\Followers::add_follower( $follower ); + \Activitypub\Collection\Followers::add_follow_relationship( \Activitypub\Collection\Users::BLOG_USER_ID, $follower_id ); } $post = \wp_insert_post( diff --git a/tests/test-class-activitypub-followers.php b/tests/test-class-activitypub-followers.php index 90323fa..3f31c33 100644 --- a/tests/test-class-activitypub-followers.php +++ b/tests/test-class-activitypub-followers.php @@ -68,7 +68,8 @@ class Test_Activitypub_Followers extends WP_UnitTestCase { add_filter( 'pre_http_request', array( $pre_http_request, 'filter' ), 10, 3 ); foreach ( $followers as $follower ) { - $response = \Activitypub\Collection\Followers::add_follower( 1, $follower ); + $follower_id = \Activitypub\Collection\Followers::add_follower( $follower ); + $response = \Activitypub\Collection\Followers::add_follow_relationship( 1, $follower_id ); } $db_followers = \Activitypub\Collection\Followers::get_followers( 1 ); @@ -91,9 +92,11 @@ class Test_Activitypub_Followers extends WP_UnitTestCase { $follower = 'https://12345.example.com'; $follower2 = 'https://user2.example.com'; - \Activitypub\Collection\Followers::add_follower( 1, $follower ); - \Activitypub\Collection\Followers::add_follower( 2, $follower ); - \Activitypub\Collection\Followers::add_follower( 2, $follower2 ); + $follwer_id = \Activitypub\Collection\Followers::add_follower( $follower ); + $follower2_id = \Activitypub\Collection\Followers::add_follower( $follower2 ); + + \Activitypub\Collection\Followers::add_follow_relationship( 1, $follwer_id ); + \Activitypub\Collection\Followers::add_follow_relationship( 2, $follower2_id ); $db_followers = \Activitypub\Collection\Followers::get_followers( 1 ); $db_followers2 = \Activitypub\Collection\Followers::get_followers( 2 ); @@ -108,13 +111,13 @@ class Test_Activitypub_Followers extends WP_UnitTestCase { $follower = 'error@example.com'; - $result = \Activitypub\Collection\Followers::add_follower( 1, $follower ); + $result = \Activitypub\Collection\Followers::add_follower( $follower ); $this->assertTrue( is_wp_error( $result ) ); $follower2 = 'https://error.example.com'; - $result = \Activitypub\Collection\Followers::add_follower( 1, $follower2 ); + $result = \Activitypub\Collection\Followers::add_follower( $follower2 ); $this->assertTrue( is_wp_error( $result ) ); @@ -131,11 +134,13 @@ class Test_Activitypub_Followers extends WP_UnitTestCase { add_filter( 'pre_http_request', array( $pre_http_request, 'filter' ), 10, 3 ); foreach ( $followers as $follower ) { - \Activitypub\Collection\Followers::add_follower( 1, $follower ); + $follower_id = \Activitypub\Collection\Followers::add_follower( $follower ); + \Activitypub\Collection\Followers::add_follow_relationship( 1, $follower_id ); } foreach ( $followers2 as $follower ) { - \Activitypub\Collection\Followers::add_follower( 2, $follower ); + $follower_id = \Activitypub\Collection\Followers::add_follower( $follower ); + \Activitypub\Collection\Followers::add_follow_relationship( 2, $follower_id ); } $follower = \Activitypub\Collection\Followers::get_follower( 1, 'https://example.com/author/jon' ); @@ -155,7 +160,7 @@ class Test_Activitypub_Followers extends WP_UnitTestCase { $this->assertEquals( 'úser2', $follower2->get_name() ); } - public function test_delete_follower() { + public function test_delete_follow_relatioship() { $followers = array( 'https://example.com/author/jon', 'https://example.org/author/doe', @@ -166,14 +171,14 @@ class Test_Activitypub_Followers extends WP_UnitTestCase { add_filter( 'pre_http_request', array( $pre_http_request, 'filter' ), 10, 3 ); foreach ( $followers as $follower ) { - \Activitypub\Collection\Followers::add_follower( 1, $follower ); - \Activitypub\Collection\Followers::add_follower( 1, $follower ); - \Activitypub\Collection\Followers::add_follower( 1, $follower ); - \Activitypub\Collection\Followers::add_follower( 2, $follower ); + $follower_id = \Activitypub\Collection\Followers::add_follower( $follower ); + \Activitypub\Collection\Followers::add_follow_relationship( 1, $follower_id ); + \Activitypub\Collection\Followers::add_follow_relationship( 2, $follower_id ); } foreach ( $followers2 as $follower2 ) { - \Activitypub\Collection\Followers::add_follower( 2, $follower2 ); + $follower_id = \Activitypub\Collection\Followers::add_follower( $follower2 ); + \Activitypub\Collection\Followers::add_follow_relationship( 2, $follower_id ); } $follower = \Activitypub\Collection\Followers::get_follower( 1, 'https://example.com/author/jon' ); @@ -204,7 +209,8 @@ class Test_Activitypub_Followers extends WP_UnitTestCase { add_filter( 'pre_http_request', array( $pre_http_request, 'filter' ), 10, 3 ); foreach ( $followers as $follower ) { - \Activitypub\Collection\Followers::add_follower( 1, $follower ); + $follower_id = \Activitypub\Collection\Followers::add_follower( $follower ); + \Activitypub\Collection\Followers::add_follow_relationship(1, $follower_id ); } $follower = \Activitypub\Collection\Followers::get_follower( 1, 'https://example.com/author/jon' ); @@ -244,7 +250,8 @@ class Test_Activitypub_Followers extends WP_UnitTestCase { add_filter( 'pre_http_request', array( $pre_http_request, 'filter' ), 10, 3 ); foreach ( $followers as $follower ) { - \Activitypub\Collection\Followers::add_follower( 1, $follower ); + $follower_id = \Activitypub\Collection\Followers::add_follower( $follower ); + \Activitypub\Collection\Followers::add_follow_relationship(1, $follower_id ); } $follower = \Activitypub\Collection\Followers::get_follower( 1, 'http://sally.example.org' ); @@ -277,12 +284,16 @@ class Test_Activitypub_Followers extends WP_UnitTestCase { $follower = 'https://12345.example.com'; - \Activitypub\Collection\Followers::add_follower( 1, $follower ); - \Activitypub\Collection\Followers::add_follower( 1, $follower ); - \Activitypub\Collection\Followers::add_follower( 1, $follower ); - \Activitypub\Collection\Followers::add_follower( 1, $follower ); - \Activitypub\Collection\Followers::add_follower( 1, $follower ); - \Activitypub\Collection\Followers::add_follower( 1, $follower ); + $follower1_id = \Activitypub\Collection\Followers::add_follower( $follower ); + $follower2_id = \Activitypub\Collection\Followers::add_follower( $follower ); + $follower3_id = \Activitypub\Collection\Followers::add_follower( $follower ); + + $this->assertEqual( $follower1_id, $follower2_id ); + $this->assertEqual( $follower1_id, $follower3_id ); + + \Activitypub\Collection\Followers::add_follow_relationship(1, $follower1_id ); + \Activitypub\Collection\Followers::add_follow_relationship(1, $follower2_id ); + \Activitypub\Collection\Followers::add_follow_relationship(1, $follower3_id ); $db_followers = \Activitypub\Collection\Followers::get_followers( 1 );