From ea16ed281827966cc309927ac2e4bf0ff057b8ae Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Tue, 10 Nov 2026 19:13:50 +0100 Subject: [PATCH 1/9] follow moved actors per FEP-7628 --- .github/changelog/fix-move-fep-7628 | 4 + includes/handler/class-move.php | 192 +++-- .../includes/handler/class-test-move.php | 653 +++++++++++++++++- 3 files changed, 767 insertions(+), 82 deletions(-) create mode 100644 .github/changelog/fix-move-fep-7628 diff --git a/.github/changelog/fix-move-fep-7628 b/.github/changelog/fix-move-fep-7628 new file mode 100644 index 0000000000..a7aa2d8626 --- /dev/null +++ b/.github/changelog/fix-move-fep-7628 @@ -0,0 +1,4 @@ +Significance: patch +Type: fixed + +When someone you follow moves their account, you now follow the new account, and your blocks carry over. diff --git a/includes/handler/class-move.php b/includes/handler/class-move.php index f4676b76c0..30240491aa 100644 --- a/includes/handler/class-move.php +++ b/includes/handler/class-move.php @@ -7,12 +7,19 @@ namespace Activitypub\Handler; +use Activitypub\Collection\Actors; +use Activitypub\Collection\Blocked_Actors; use Activitypub\Collection\Followers; +use Activitypub\Collection\Following; +use Activitypub\Collection\Inbox; use Activitypub\Collection\Remote_Actors; +use Activitypub\Moderation; use Activitypub\Proxy; use function Activitypub\get_id_hash; use function Activitypub\get_stored_id_forms; +use function Activitypub\is_actor; +use function Activitypub\is_same_domain; use function Activitypub\object_to_uri; /** @@ -29,18 +36,27 @@ class Move { * @return void */ public static function init() { - \add_action( 'activitypub_inbox_move', array( self::class, 'handle_move' ), 10, 2 ); + \add_action( 'activitypub_inbox_move', array( self::class, 'handle_move' ), 10, 4 ); + \add_action( 'activitypub_inbox_shared_move', array( self::class, 'handle_move' ), 10, 4 ); } /** * Handle Move requests. * - * @param array $activity The JSON "Move" Activity. - * @param int|int[] $user_ids The user ID(s). + * @param array $activity The JSON "Move" Activity. + * @param int|int[] $user_ids The user ID(s). + * @param \Activitypub\Activity\Activity|null $activity_object Optional. The activity object. Default null. + * @param string|null $context Optional. The inbox context. Default null. * * @return void */ - public static function handle_move( $activity, $user_ids ) { + public static function handle_move( $activity, $user_ids, $activity_object = null, $context = null ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable + // The shared inbox invokes this once per resolved recipient and once on the shared hook; + // handle that path only on the shared hook, so one Move is handled once. + if ( Inbox::CONTEXT_SHARED_INBOX === $context && 'activitypub_inbox_shared_move' !== \current_filter() ) { + return; + } + $target_uri = self::extract_target( $activity ); $origin_uri = self::extract_origin( $activity ); @@ -48,28 +64,64 @@ public static function handle_move( $activity, $user_ids ) { return; } - $target_json = Proxy::get( $target_uri ); - $origin_json = Proxy::get( $origin_uri ); + // Only the old or the new actor may send the Move; checked before anything is fetched. + if ( ! \in_array( object_to_uri( $activity['actor'] ?? null ), array( $origin_uri, $target_uri ), true ) ) { + return; + } + + // Only a stored old actor has relationships to move, so nothing else needs a fetch. + $origin_object = Remote_Actors::get_by_uri( $origin_uri ); + if ( \is_wp_error( $origin_object ) ) { + return; + } + + // No request to our own site or to a blocked one; the confirmed id is checked again below. + if ( is_same_domain( $target_uri ) || Moderation::is_actor_blocked( $target_uri ) ) { + return; + } + + // Both are fetched fresh: a cached copy may predate the movedTo or the alsoKnownAs. + $target_json = Proxy::get( $target_uri, array( 'cached' => false ) ); + $origin_json = Proxy::get( $origin_uri, array( 'cached' => false ) ); - $verified = self::verify_move( $target_json, $origin_json ); + $verified = self::verify_move( $activity, $target_json, $origin_json ); if ( ! $verified ) { return; } - $target_object = Remote_Actors::get_by_uri( $target_uri ); - $origin_object = Remote_Actors::get_by_uri( $origin_uri ); - $result = null; - $success = false; + // The old actor must be the one we store, not one its URL redirects to. + $stored_origin = Remote_Actors::get_by_uri( $origin_json['id'] ); + if ( \is_wp_error( $stored_origin ) || $stored_origin->ID !== $origin_object->ID ) { + return; + } + + $target_id = $target_json['id']; + + // A remote actor can not move to one of ours, nor to an actor the site blocks. + if ( is_same_domain( $target_id ) || Moderation::is_actor_blocked( $target_id ) ) { + return; + } + + $target_object = Remote_Actors::get_by_uri( $target_id ); + $result = null; + $success = false; + $target_post_id = 0; + + // Our users who follow the old actor follow the new one (FEP-7628), unless they already do. + $refollow = self::get_following_user_ids( $origin_object->ID ); + if ( ! \is_wp_error( $target_object ) ) { + $refollow = \array_diff( $refollow, self::get_following_user_ids( $target_object->ID ) ); + } global $wpdb; // If the origin is followed but the target is not, update the origin to point to the target. - if ( \is_wp_error( $target_object ) && ! \is_wp_error( $origin_object ) ) { + if ( \is_wp_error( $target_object ) ) { // Written in the form an insert stores, which every guid lookup matches. - list( $guid ) = get_stored_id_forms( $target_uri ); + list( $guid ) = get_stored_id_forms( $target_id ); $data = array( 'guid' => $guid ); - $hash = get_id_hash( $target_uri ); + $hash = get_id_hash( $target_id ); if ( $hash ) { $data['post_name'] = $hash; } @@ -80,22 +132,25 @@ public static function handle_move( $activity, $user_ids ) { // Clear the cache. \wp_cache_delete( $origin_object->ID, 'posts' ); - $success = true; + $success = true; + $target_post_id = $origin_object->ID; - // Proxy::get() already self-confirmed the target, so it is safe to cache. - $result = Remote_Actors::upsert( $target_json ); + // Proxy::get() already self-confirmed the target. Update by ID: wp_update_post() keeps the guid written above. + $result = Remote_Actors::update( $origin_object->ID, $target_json ); } - // If both the target and origin are followed, merge them. - if ( ! \is_wp_error( $target_object ) && ! \is_wp_error( $origin_object ) ) { + // If both the target and origin are stored, merge them. + if ( ! \is_wp_error( $target_object ) ) { + // Followers and blocks of the origin carry over; our own follows are sent again below. $origin_users = \get_post_meta( $origin_object->ID, Followers::FOLLOWER_META_KEY, false ); $target_users = \get_post_meta( $target_object->ID, Followers::FOLLOWER_META_KEY, false ); + foreach ( \array_diff( $origin_users, $target_users ) as $user_id ) { + \add_post_meta( $target_object->ID, Followers::FOLLOWER_META_KEY, $user_id ); + } - // Get all user ids from $origin_users that are not in $target_users. - $users = \array_diff( $origin_users, $target_users ); - - foreach ( $users as $follower_user_id ) { - \add_post_meta( $target_object->ID, Followers::FOLLOWER_META_KEY, $follower_user_id ); + // Through Blocked_Actors::add(), so the block hooks run and clean up the relationships. + foreach ( \get_post_meta( $origin_object->ID, Moderation::BLOCKED_ACTORS_META_KEY, false ) as $user_id ) { + Blocked_Actors::add( (int) $user_id, $target_id ); } // Comments and Reader posts of the origin belong to the target now. @@ -123,8 +178,33 @@ public static function handle_move( $activity, $user_ids ) { } // phpcs:enable - $success = true; - $result = Remote_Actors::delete( $origin_object->ID ); + $success = true; + $target_post_id = $target_object->ID; + $result = Remote_Actors::delete( $origin_object->ID ); + } + + if ( $success ) { + // A user who blocks the new actor neither follows it nor keeps it as a follower. + foreach ( \get_post_meta( $target_post_id, Followers::FOLLOWER_META_KEY, false ) as $user_id ) { + if ( Moderation::is_actor_blocked( $target_id, (int) $user_id ) ) { + Followers::remove( $target_post_id, (int) $user_id ); + } + } + + foreach ( $refollow as $user_id ) { + // A user who can no longer follow keeps the relationship as it is, instead of losing it. + if ( \is_wp_error( Actors::get_by_id( (int) $user_id ) ) ) { + continue; + } + + // The swapped post still says the user follows; the new server does not know that yet. + \delete_post_meta( $target_post_id, Following::FOLLOWING_META_KEY, $user_id ); + \delete_post_meta( $target_post_id, Following::PENDING_META_KEY, $user_id ); + + if ( ! Moderation::is_actor_blocked( $target_id, (int) $user_id ) ) { + Following::follow( $target_post_id, (int) $user_id ); + } + } } /** @@ -164,15 +244,19 @@ private static function extract_target( $activity ) { /** * Extract the origin from the activity. * - * The ActivityStreams spec define the `origin` attribute as source - * of the activity, but Mastodon uses the `actor` attribute as source - * to move profiles. + * FEP-7628 and Mastodon send the old actor as `object` and the new one as `target`; the + * `actor` is whoever sends the Move, the old actor (push) or the new one (pull). Without a + * `target`, the `object` is the target, so the origin is the `origin` or the `actor`. * * @param array $activity The JSON "Move" Activity. * * @return string|null The origin URI or null if not found. */ private static function extract_origin( $activity ) { + if ( ! empty( $activity['target'] ) && ! empty( $activity['object'] ) ) { + return object_to_uri( $activity['object'] ); + } + if ( ! empty( $activity['origin'] ) ) { return object_to_uri( $activity['origin'] ); } @@ -187,19 +271,23 @@ private static function extract_origin( $activity ) { /** * Verify the move. * - * @param array $target_object The target object. - * @param array $origin_object The origin object. + * Both actors have to agree, each in its own document: the target lists the origin in + * `alsoKnownAs`, and the origin points at the target with `movedTo`. A `copiedTo` origin + * stays active (FEP-7628), so it is not merged away. + * + * @param array $activity The JSON "Move" Activity. + * @param array|\WP_Error $target_object The target object. + * @param array|\WP_Error $origin_object The origin object. * * @return bool True if the move is verified, false otherwise. */ - private static function verify_move( $target_object, $origin_object ) { - // Check if both objects are valid. - if ( \is_wp_error( $target_object ) || \is_wp_error( $origin_object ) ) { + private static function verify_move( $activity, $target_object, $origin_object ) { + // Check if both objects are actors. + if ( ! \is_array( $target_object ) || ! \is_array( $origin_object ) || ! is_actor( $target_object ) || ! is_actor( $origin_object ) ) { return false; } - // Check if both objects are persons. - if ( 'Person' !== $target_object['type'] || 'Person' !== $origin_object['type'] ) { + if ( ! \is_string( $target_object['id'] ?? null ) || ! \is_string( $origin_object['id'] ?? null ) ) { return false; } @@ -208,6 +296,11 @@ private static function verify_move( $target_object, $origin_object ) { return false; } + // Only one of the two actors may send the Move. + if ( ! \in_array( object_to_uri( $activity['actor'] ?? null ), array( $origin_object['id'], $target_object['id'] ), true ) ) { + return false; + } + // Normalize alsoKnownAs to a list of ids (some JSON-LD payloads use a string or objects). $also_known_as = \array_map( '\Activitypub\object_to_uri', (array) ( $target_object['alsoKnownAs'] ?? array() ) ); if ( empty( $also_known_as ) ) { @@ -223,16 +316,23 @@ private static function verify_move( $target_object, $origin_object ) { return false; } - // Check if the origin has a movedTo property. - if ( empty( $origin_object['movedTo'] ) ) { - return false; - } - - // Check if the movedTo property of the origin is the target. - if ( $origin_object['movedTo'] !== $target_object['id'] ) { - return false; - } + // Check if the origin points at the target. + return object_to_uri( $origin_object['movedTo'] ?? null ) === $target_object['id']; + } - return true; + /** + * Get the local users that follow or asked to follow a remote actor. + * + * @param int $post_id The remote actor post ID. + * + * @return string[] The user IDs. + */ + private static function get_following_user_ids( $post_id ) { + return \array_unique( + \array_merge( + \get_post_meta( $post_id, Following::FOLLOWING_META_KEY, false ), + \get_post_meta( $post_id, Following::PENDING_META_KEY, false ) + ) + ); } } diff --git a/tests/phpunit/tests/includes/handler/class-test-move.php b/tests/phpunit/tests/includes/handler/class-test-move.php index 617afa140f..ecd91ab4a8 100644 --- a/tests/phpunit/tests/includes/handler/class-test-move.php +++ b/tests/phpunit/tests/includes/handler/class-test-move.php @@ -9,9 +9,13 @@ use Activitypub\Activity\Actor; use Activitypub\Collection\Followers; +use Activitypub\Collection\Following; +use Activitypub\Collection\Inbox; +use Activitypub\Collection\Outbox; use Activitypub\Collection\Remote_Actors; use Activitypub\Collection\Remote_Posts; use Activitypub\Handler\Move; +use Activitypub\Moderation; use function Activitypub\get_id_hash; use function Activitypub\object_to_uri; @@ -642,7 +646,330 @@ public function test_handle_move_ignores_origin_webfinger() { } /** - * A move to a target id with `&` ends up on the moved actor, not on a second one. + * A Mastodon Move (old actor as `object`, new one as `target`) makes our users follow the new actor. + * + * @covers ::handle_move + */ + public function test_handle_move_follows_target_for_following_users() { + $origin = 'https://old.example/users/alice'; + $target = 'https://new.example/users/alice'; + + \get_user_by( 'id', $this->user_id )->add_cap( 'activitypub' ); + $id = Remote_Actors::upsert( $this->actor( $origin, array( 'movedTo' => $target ) ) ); + \add_post_meta( $id, Following::FOLLOWING_META_KEY, (string) $this->user_id ); + + $filter = $this->mock_remote( + array( + $origin => $this->actor( $origin, array( 'movedTo' => $target ) ), + $target => $this->actor( $target, array( 'alsoKnownAs' => array( $origin ) ) ), + ) + ); + + Move::handle_move( + array( + 'type' => 'Move', + 'actor' => $origin, + 'object' => $origin, + 'target' => $target, + ), + $this->user_id + ); + + \remove_filter( 'activitypub_pre_http_get_remote_object', $filter ); + + $moved = Remote_Actors::get_by_uri( $target ); + $this->assertSame( $id, $moved->ID, 'The stored actor must be moved in place.' ); + $this->assertNotContains( (string) $this->user_id, \get_post_meta( $id, Following::FOLLOWING_META_KEY, false ), 'The new server has not accepted yet.' ); + $this->assertContains( (string) $this->user_id, \get_post_meta( $id, Following::PENDING_META_KEY, false ) ); + $this->assertCount( 1, $this->get_follows( $this->user_id, $target ), 'A Follow must be sent to the new actor.' ); + } + + /** + * A merge keeps blocks of the old actor and follows the new one for our users. + * + * @covers ::handle_move + */ + public function test_handle_move_merge_keeps_blocks_and_follows() { + $origin = 'https://old.example/users/bob'; + $target = 'https://new.example/users/bob'; + + \get_user_by( 'id', $this->user_id )->add_cap( 'activitypub' ); + $origin_id = Remote_Actors::upsert( $this->actor( $origin, array( 'movedTo' => $target ) ) ); + $target_id = Remote_Actors::upsert( $this->actor( $target, array( 'alsoKnownAs' => array( $origin ) ) ) ); + \add_post_meta( $origin_id, Following::FOLLOWING_META_KEY, (string) $this->user_id ); + \add_post_meta( $origin_id, Moderation::BLOCKED_ACTORS_META_KEY, (string) $this->user_id_2 ); + + $filter = $this->mock_remote( + array( + $origin => $this->actor( $origin, array( 'movedTo' => $target ) ), + $target => $this->actor( $target, array( 'alsoKnownAs' => array( $origin ) ) ), + ) + ); + + Move::handle_move( + array( + 'type' => 'Move', + 'actor' => $origin, + 'object' => $origin, + 'target' => $target, + ), + $this->user_id + ); + + \remove_filter( 'activitypub_pre_http_get_remote_object', $filter ); + + $this->assertWPError( Remote_Actors::get_by_uri( $origin ), 'The old actor must be merged away.' ); + $this->assertContains( (string) $this->user_id_2, \get_post_meta( $target_id, Moderation::BLOCKED_ACTORS_META_KEY, false ), 'The block must carry over.' ); + $this->assertTrue( Moderation::is_actor_blocked( $target, $this->user_id_2 ) ); + $this->assertContains( (string) $this->user_id, \get_post_meta( $target_id, Following::PENDING_META_KEY, false ) ); + $this->assertCount( 1, $this->get_follows( $this->user_id, $target ) ); + } + + /** + * Our users do not follow a new actor they block. + * + * @covers ::handle_move + */ + public function test_handle_move_does_not_follow_blocked_target() { + $origin = 'https://old.example/users/carol'; + $target = 'https://new.example/users/carol'; + + \get_user_by( 'id', $this->user_id )->add_cap( 'activitypub' ); + Moderation::add_user_block( $this->user_id, Moderation::TYPE_DOMAIN, 'new.example' ); + $id = Remote_Actors::upsert( $this->actor( $origin, array( 'movedTo' => $target ) ) ); + \add_post_meta( $id, Following::FOLLOWING_META_KEY, (string) $this->user_id ); + + $filter = $this->mock_remote( + array( + $origin => $this->actor( $origin, array( 'movedTo' => $target ) ), + $target => $this->actor( $target, array( 'alsoKnownAs' => array( $origin ) ) ), + ) + ); + + Move::handle_move( + array( + 'type' => 'Move', + 'actor' => $origin, + 'object' => $origin, + 'target' => $target, + ), + $this->user_id + ); + + \remove_filter( 'activitypub_pre_http_get_remote_object', $filter ); + + $this->assertEmpty( \get_post_meta( $id, Following::FOLLOWING_META_KEY, false ) ); + $this->assertEmpty( \get_post_meta( $id, Following::PENDING_META_KEY, false ) ); + $this->assertCount( 0, $this->get_follows( $this->user_id, $target ) ); + } + + /** + * A Move sent by the new actor (pull mode) is accepted when the old actor points at it. + * + * @covers ::handle_move + */ + public function test_handle_move_pull_mode() { + $origin = 'https://old.example/users/dave'; + $target = 'https://new.example/users/dave'; + + $id = Remote_Actors::upsert( $this->actor( $origin ) ); + $filter = $this->mock_remote( + array( + $origin => $this->actor( $origin, array( 'movedTo' => $target ) ), + $target => $this->actor( $target, array( 'alsoKnownAs' => array( $origin ) ) ), + ) + ); + + Move::handle_move( + array( + 'type' => 'Move', + 'actor' => $target, + 'object' => $origin, + 'target' => $target, + ), + $this->user_id + ); + + \remove_filter( 'activitypub_pre_http_get_remote_object', $filter ); + + $this->assertSame( $id, Remote_Actors::get_by_uri( $target )->ID ); + } + + /** + * A `copiedTo` origin stays active, so it is not merged away. + * + * @covers ::handle_move + */ + public function test_handle_move_ignores_copied_to() { + $origin = 'https://old.example/users/gina'; + $target = 'https://new.example/users/gina'; + + $id = Remote_Actors::upsert( $this->actor( $origin ) ); + $filter = $this->mock_remote( + array( + $origin => $this->actor( $origin, array( 'copiedTo' => $target ) ), + $target => $this->actor( $target, array( 'alsoKnownAs' => array( $origin ) ) ), + ) + ); + + Move::handle_move( + array( + 'type' => 'Move', + 'actor' => $origin, + 'object' => $origin, + 'target' => $target, + ), + $this->user_id + ); + + \remove_filter( 'activitypub_pre_http_get_remote_object', $filter ); + + $this->assertSame( $id, Remote_Actors::get_by_uri( $origin )->ID ); + $this->assertWPError( Remote_Actors::get_by_uri( $target ) ); + } + + /** + * A remote actor can not move to an actor on this site. + * + * @covers ::handle_move + */ + public function test_handle_move_rejects_local_target() { + $origin = 'https://old.example/users/hank'; + $target = \home_url( '/?author=' . $this->user_id ); + + $id = Remote_Actors::upsert( $this->actor( $origin ) ); + $filter = $this->mock_remote( + array( + $origin => $this->actor( $origin, array( 'movedTo' => $target ) ), + $target => $this->actor( $target, array( 'alsoKnownAs' => array( $origin ) ) ), + ) + ); + + Move::handle_move( + array( + 'type' => 'Move', + 'actor' => $origin, + 'object' => $origin, + 'target' => $target, + ), + $this->user_id + ); + + \remove_filter( 'activitypub_pre_http_get_remote_object', $filter ); + + $this->assertSame( $id, Remote_Actors::get_by_uri( $origin )->ID ); + } + + /** + * A Move to an actor the site blocks is not applied. + * + * @covers ::handle_move + */ + public function test_handle_move_rejects_site_blocked_target() { + $origin = 'https://old.example/users/ivy'; + $target = 'https://blocked.example/users/ivy'; + + Moderation::add_site_block( Moderation::TYPE_DOMAIN, 'blocked.example' ); + $id = Remote_Actors::upsert( $this->actor( $origin ) ); + $filter = $this->mock_remote( + array( + $origin => $this->actor( $origin, array( 'movedTo' => $target ) ), + $target => $this->actor( $target, array( 'alsoKnownAs' => array( $origin ) ) ), + ) + ); + + Move::handle_move( + array( + 'type' => 'Move', + 'actor' => $origin, + 'object' => $origin, + 'target' => $target, + ), + $this->user_id + ); + + \remove_filter( 'activitypub_pre_http_get_remote_object', $filter ); + Moderation::remove_site_block( Moderation::TYPE_DOMAIN, 'blocked.example' ); + + $this->assertSame( $id, Remote_Actors::get_by_uri( $origin )->ID ); + $this->assertWPError( Remote_Actors::get_by_uri( $target ) ); + } + + /** + * A user who blocks the new actor does not keep it as a follower. + * + * @covers ::handle_move + */ + public function test_handle_move_drops_follower_blocked_by_user() { + $origin = 'https://old.example/users/jack'; + $target = 'https://new.example/users/jack'; + + Moderation::add_user_block( $this->user_id, Moderation::TYPE_DOMAIN, 'new.example' ); + $id = Remote_Actors::upsert( $this->actor( $origin ) ); + \add_post_meta( $id, Followers::FOLLOWER_META_KEY, (string) $this->user_id ); + \add_post_meta( $id, Followers::FOLLOWER_META_KEY, (string) $this->user_id_2 ); + + $filter = $this->mock_remote( + array( + $origin => $this->actor( $origin, array( 'movedTo' => $target ) ), + $target => $this->actor( $target, array( 'alsoKnownAs' => array( $origin ) ) ), + ) + ); + + Move::handle_move( + array( + 'type' => 'Move', + 'actor' => $origin, + 'object' => $origin, + 'target' => $target, + ), + $this->user_id + ); + + \remove_filter( 'activitypub_pre_http_get_remote_object', $filter ); + + $followers = \get_post_meta( $id, Followers::FOLLOWER_META_KEY, false ); + $this->assertNotContains( (string) $this->user_id, $followers ); + $this->assertContains( (string) $this->user_id_2, $followers ); + } + + /** + * The shared inbox hands one Move to every recipient; it is handled once, on the shared hook. + * + * @covers ::handle_move + */ + public function test_handle_move_runs_once_from_shared_inbox() { + $origin = 'https://old.example/users/kim'; + $activity = array( + 'type' => 'Move', + 'actor' => $origin, + 'object' => $origin, + 'target' => 'https://new.example/users/kim', + ); + + Remote_Actors::upsert( $this->actor( $origin ) ); + + $fetches = 0; + $filter = function ( $pre, $url_or_object ) use ( &$fetches, $origin ) { + ++$fetches; + return object_to_uri( $url_or_object ) === $origin ? $this->actor( $origin ) : $pre; + }; + \add_filter( 'activitypub_pre_http_get_remote_object', $filter, 10, 2 ); + + \do_action( 'activitypub_inbox_move', $activity, $this->user_id, null, Inbox::CONTEXT_SHARED_INBOX ); + \do_action( 'activitypub_inbox_move', $activity, $this->user_id_2, null, Inbox::CONTEXT_SHARED_INBOX ); + $per_recipient = $fetches; + + \do_action( 'activitypub_inbox_shared_move', $activity, array( $this->user_id, $this->user_id_2 ), null, Inbox::CONTEXT_SHARED_INBOX ); + + \remove_filter( 'activitypub_pre_http_get_remote_object', $filter ); + + $this->assertSame( 0, $per_recipient, 'The per-recipient calls must not fetch.' ); + $this->assertGreaterThan( 0, $fetches, 'The shared hook handles the Move.' ); + } + + /** + * A target id with a query string ends up on the moved post, not on a second one. * * @covers ::handle_move */ @@ -650,31 +977,231 @@ public function test_handle_move_target_with_query_string() { $origin = 'https://old.example/users/lena'; $target = 'https://new.example/?author=7&type=actor'; - $origin_object = array( - 'type' => 'Person', - 'id' => $origin, - 'url' => $origin, - 'inbox' => $origin . '/inbox', - 'movedTo' => $target, + \get_user_by( 'id', $this->user_id )->add_cap( 'activitypub' ); + $id = Remote_Actors::upsert( $this->actor( $origin ) ); + \add_post_meta( $id, Following::FOLLOWING_META_KEY, (string) $this->user_id ); + + $filter = $this->mock_remote( + array( + $origin => $this->actor( $origin, array( 'movedTo' => $target ) ), + $target => $this->actor( $target, array( 'alsoKnownAs' => array( $origin ) ) ), + ) ); - $target_object = array( - 'type' => 'Person', - 'id' => $target, - 'url' => $target, - 'inbox' => 'https://new.example/inbox', - 'alsoKnownAs' => array( $origin ), + + Move::handle_move( + array( + 'type' => 'Move', + 'actor' => $origin, + 'object' => $origin, + 'target' => $target, + ), + $this->user_id ); - $id = Remote_Actors::upsert( $origin_object ); + \remove_filter( 'activitypub_pre_http_get_remote_object', $filter ); - $filter = static function ( $pre, $url_or_object ) use ( $origin, $origin_object, $target, $target_object ) { - $url = object_to_uri( $url_or_object ); - if ( $url === $origin ) { - return $origin_object; - } - if ( $url === $target ) { - return $target_object; - } + $this->assertSame( $id, Remote_Actors::get_by_uri( $target )->ID, 'Only the moved post may hold the new id.' ); + $this->assertContains( (string) $this->user_id, \get_post_meta( $id, Following::PENDING_META_KEY, false ) ); + + $actors = \get_posts( + array( + 'post_type' => Remote_Actors::POST_TYPE, + 'post_status' => 'any', + 'fields' => 'ids', + 'numberposts' => -1, + ) + ); + $this->assertSame( array( $id ), $actors, 'No second actor may be created.' ); + } + + /** + * A new actor can not take over an old one that does not point at it. + * + * @covers ::handle_move + */ + public function test_handle_move_pull_mode_requires_origin_consent() { + $origin = 'https://old.example/users/erin'; + $target = 'https://new.example/users/erin'; + + $id = Remote_Actors::upsert( $this->actor( $origin ) ); + $filter = $this->mock_remote( + array( + $origin => $this->actor( $origin ), + $target => $this->actor( $target, array( 'alsoKnownAs' => array( $origin ) ) ), + ) + ); + + Move::handle_move( + array( + 'type' => 'Move', + 'actor' => $target, + 'object' => $origin, + 'target' => $target, + ), + $this->user_id + ); + + \remove_filter( 'activitypub_pre_http_get_remote_object', $filter ); + + $this->assertWPError( Remote_Actors::get_by_uri( $target ) ); + $this->assertSame( $id, Remote_Actors::get_by_uri( $origin )->ID ); + } + + /** + * Only the old or the new actor may send the Move. + * + * @covers ::handle_move + */ + public function test_handle_move_rejects_third_party_actor() { + $origin = 'https://old.example/users/frank'; + $target = 'https://new.example/users/frank'; + + $id = Remote_Actors::upsert( $this->actor( $origin ) ); + $filter = $this->mock_remote( + array( + $origin => $this->actor( $origin, array( 'movedTo' => $target ) ), + $target => $this->actor( $target, array( 'alsoKnownAs' => array( $origin ) ) ), + ) + ); + + Move::handle_move( + array( + 'type' => 'Move', + 'actor' => 'https://third.example/users/mallory', + 'object' => $origin, + 'target' => $target, + ), + $this->user_id + ); + + \remove_filter( 'activitypub_pre_http_get_remote_object', $filter ); + + $this->assertWPError( Remote_Actors::get_by_uri( $target ) ); + $this->assertSame( $id, Remote_Actors::get_by_uri( $origin )->ID ); + } + + /** + * Any actor type can move, not only a Person. + * + * @covers ::handle_move + */ + public function test_handle_move_accepts_group() { + $origin = 'https://old.example/@blog'; + $target = 'https://new.example/@blog'; + + $id = Remote_Actors::upsert( $this->actor( $origin, array( 'type' => 'Group' ) ) ); + $filter = $this->mock_remote( + array( + $origin => $this->actor( + $origin, + array( + 'type' => 'Group', + 'movedTo' => $target, + ) + ), + $target => $this->actor( + $target, + array( + 'type' => 'Group', + 'alsoKnownAs' => array( $origin ), + ) + ), + ) + ); + + Move::handle_move( + array( + 'type' => 'Move', + 'actor' => $origin, + 'object' => $origin, + 'target' => $target, + ), + $this->user_id + ); + + \remove_filter( 'activitypub_pre_http_get_remote_object', $filter ); + + $this->assertSame( $id, Remote_Actors::get_by_uri( $target )->ID ); + } + + /** + * A Move for an actor we do not store fetches nothing. + * + * @covers ::handle_move + */ + public function test_handle_move_skips_unknown_origin() { + $fetches = 0; + $filter = static function ( $pre ) use ( &$fetches ) { + ++$fetches; + return $pre; + }; + \add_filter( 'activitypub_pre_http_get_remote_object', $filter ); + + Move::handle_move( + array( + 'type' => 'Move', + 'actor' => 'https://old.example/users/unknown', + 'object' => 'https://old.example/users/unknown', + 'target' => 'https://new.example/users/unknown', + ), + $this->user_id + ); + + \remove_filter( 'activitypub_pre_http_get_remote_object', $filter ); + + $this->assertSame( 0, $fetches ); + } + + /** + * A user who can no longer follow keeps the relationship instead of losing it. + * + * @covers ::handle_move + */ + public function test_handle_move_keeps_relationship_of_user_who_can_not_follow() { + $origin = 'https://old.example/users/mia'; + $target = 'https://new.example/users/mia'; + + $id = Remote_Actors::upsert( $this->actor( $origin ) ); + \add_post_meta( $id, Following::FOLLOWING_META_KEY, (string) $this->user_id ); + + $filter = $this->mock_remote( + array( + $origin => $this->actor( $origin, array( 'movedTo' => $target ) ), + $target => $this->actor( $target, array( 'alsoKnownAs' => array( $origin ) ) ), + ) + ); + + Move::handle_move( + array( + 'type' => 'Move', + 'actor' => $origin, + 'object' => $origin, + 'target' => $target, + ), + $this->user_id + ); + + \remove_filter( 'activitypub_pre_http_get_remote_object', $filter ); + + $this->assertContains( (string) $this->user_id, \get_post_meta( $id, Following::FOLLOWING_META_KEY, false ) ); + $this->assertCount( 0, $this->get_follows( $this->user_id, $target ) ); + } + + /** + * A Move to a site-blocked target fetches nothing. + * + * @covers ::handle_move + */ + public function test_handle_move_does_not_fetch_blocked_target() { + $origin = 'https://old.example/users/noa'; + $target = 'https://blocked.example/users/noa'; + + Moderation::add_site_block( Moderation::TYPE_DOMAIN, 'blocked.example' ); + Remote_Actors::upsert( $this->actor( $origin ) ); + + $fetched = array(); + $filter = static function ( $pre, $url_or_object ) use ( &$fetched ) { + $fetched[] = object_to_uri( $url_or_object ); return $pre; }; \add_filter( 'activitypub_pre_http_get_remote_object', $filter, 10, 2 ); @@ -683,27 +1210,81 @@ public function test_handle_move_target_with_query_string() { array( 'type' => 'Move', 'actor' => $origin, - 'object' => $target, + 'object' => $origin, + 'target' => $target, ), $this->user_id ); \remove_filter( 'activitypub_pre_http_get_remote_object', $filter ); + Moderation::remove_site_block( Moderation::TYPE_DOMAIN, 'blocked.example' ); - $this->assertSame( $id, Remote_Actors::get_by_uri( $target )->ID, 'The moved actor must be found by its new id.' ); - $this->assertSame( - 1, - \count( - \get_posts( - array( - 'post_type' => Remote_Actors::POST_TYPE, - 'post_status' => 'any', - 'fields' => 'ids', - 'numberposts' => -1, - ) - ) + $this->assertNotContains( $target, $fetched ); + } + + /** + * Build a remote actor document. + * + * @param string $id The actor id. + * @param array $extra Extra properties. + * + * @return array The actor document. + */ + private function actor( $id, $extra = array() ) { + return \array_merge( + array( + 'type' => 'Person', + 'id' => $id, + 'url' => $id, + 'name' => 'Actor', + 'inbox' => $id . '/inbox', ), - 'No second actor may be created.' + $extra + ); + } + + /** + * Serve remote documents by URL. + * + * @param array $documents The documents, keyed by URL. + * + * @return callable The filter, to remove it again. + */ + private function mock_remote( $documents ) { + $filter = static function ( $pre, $url_or_object ) use ( $documents ) { + return $documents[ object_to_uri( $url_or_object ) ] ?? $pre; + }; + \add_filter( 'activitypub_pre_http_get_remote_object', $filter, 10, 2 ); + + return $filter; + } + + /** + * Get the Follow activities a user sent to an actor. + * + * @param int $user_id The user ID. + * @param string $actor_id The followed actor id. + * + * @return int[] The outbox post IDs. + */ + private function get_follows( $user_id, $actor_id ) { + return \get_posts( + array( + 'post_type' => Outbox::POST_TYPE, + 'post_status' => 'any', + 'author' => $user_id, + 'fields' => 'ids', + 'meta_query' => array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query + array( + 'key' => '_activitypub_object_id', + 'value' => $actor_id, + ), + array( + 'key' => '_activitypub_activity_type', + 'value' => 'Follow', + ), + ), + ) ); } } From f266f6e00af334c87e61590026d1155eecd286b1 Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Tue, 10 Nov 2026 22:20:00 +0100 Subject: [PATCH 2/9] store the target before the swap, clear follows that can not move --- includes/handler/class-move.php | 29 +++++++------ .../includes/handler/class-test-move.php | 42 +++++++++++++++++-- 2 files changed, 53 insertions(+), 18 deletions(-) diff --git a/includes/handler/class-move.php b/includes/handler/class-move.php index 30240491aa..6facfada99 100644 --- a/includes/handler/class-move.php +++ b/includes/handler/class-move.php @@ -118,6 +118,15 @@ public static function handle_move( $activity, $user_ids, $activity_object = nul // If the origin is followed but the target is not, update the origin to point to the target. if ( \is_wp_error( $target_object ) ) { + /* + * Proxy::get() already self-confirmed the target. Store it first, so a target that can + * not be stored (no inbox) leaves the origin as it was. wp_update_post() keeps the guid. + */ + $result = Remote_Actors::update( $origin_object->ID, $target_json ); + if ( \is_wp_error( $result ) ) { + return; + } + // Written in the form an insert stores, which every guid lookup matches. list( $guid ) = get_stored_id_forms( $target_id ); $data = array( 'guid' => $guid ); @@ -128,15 +137,10 @@ public static function handle_move( $activity, $user_ids, $activity_object = nul // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery $wpdb->update( $wpdb->posts, $data, array( 'ID' => (int) $origin_object->ID ) ); - - // Clear the cache. - \wp_cache_delete( $origin_object->ID, 'posts' ); + \clean_post_cache( $origin_object->ID ); $success = true; $target_post_id = $origin_object->ID; - - // Proxy::get() already self-confirmed the target. Update by ID: wp_update_post() keeps the guid written above. - $result = Remote_Actors::update( $origin_object->ID, $target_json ); } // If both the target and origin are stored, merge them. @@ -192,16 +196,11 @@ public static function handle_move( $activity, $user_ids, $activity_object = nul } foreach ( $refollow as $user_id ) { - // A user who can no longer follow keeps the relationship as it is, instead of losing it. - if ( \is_wp_error( Actors::get_by_id( (int) $user_id ) ) ) { - continue; - } - - // The swapped post still says the user follows; the new server does not know that yet. - \delete_post_meta( $target_post_id, Following::FOLLOWING_META_KEY, $user_id ); - \delete_post_meta( $target_post_id, Following::PENDING_META_KEY, $user_id ); + // After a swap the post still says the user follows; the new server does not know that yet. + Following::reject( $target_post_id, (int) $user_id ); - if ( ! Moderation::is_actor_blocked( $target_id, (int) $user_id ) ) { + // Only a user who can still follow, and does not block the new actor, follows it. + if ( ! \is_wp_error( Actors::get_by_id( (int) $user_id ) ) && ! Moderation::is_actor_blocked( $target_id, (int) $user_id ) ) { Following::follow( $target_post_id, (int) $user_id ); } } diff --git a/tests/phpunit/tests/includes/handler/class-test-move.php b/tests/phpunit/tests/includes/handler/class-test-move.php index ecd91ab4a8..1d772333df 100644 --- a/tests/phpunit/tests/includes/handler/class-test-move.php +++ b/tests/phpunit/tests/includes/handler/class-test-move.php @@ -1153,11 +1153,11 @@ public function test_handle_move_skips_unknown_origin() { } /** - * A user who can no longer follow keeps the relationship instead of losing it. + * A user who can no longer follow does not end up following the new actor without a Follow. * * @covers ::handle_move */ - public function test_handle_move_keeps_relationship_of_user_who_can_not_follow() { + public function test_handle_move_does_not_keep_following_without_follow() { $origin = 'https://old.example/users/mia'; $target = 'https://new.example/users/mia'; @@ -1183,7 +1183,7 @@ public function test_handle_move_keeps_relationship_of_user_who_can_not_follow() \remove_filter( 'activitypub_pre_http_get_remote_object', $filter ); - $this->assertContains( (string) $this->user_id, \get_post_meta( $id, Following::FOLLOWING_META_KEY, false ) ); + $this->assertNotContains( (string) $this->user_id, \get_post_meta( $id, Following::FOLLOWING_META_KEY, false ), 'The new server never got a Follow.' ); $this->assertCount( 0, $this->get_follows( $this->user_id, $target ) ); } @@ -1222,6 +1222,42 @@ public function test_handle_move_does_not_fetch_blocked_target() { $this->assertNotContains( $target, $fetched ); } + /** + * A target that can not be stored (no inbox) leaves the old actor as it was. + * + * @covers ::handle_move + */ + public function test_handle_move_keeps_origin_when_target_can_not_be_stored() { + $origin = 'https://old.example/users/ola'; + $target = 'https://new.example/users/ola'; + + $id = Remote_Actors::upsert( $this->actor( $origin ) ); + $broken = $this->actor( $target, array( 'alsoKnownAs' => array( $origin ) ) ); + unset( $broken['inbox'] ); + + $filter = $this->mock_remote( + array( + $origin => $this->actor( $origin, array( 'movedTo' => $target ) ), + $target => $broken, + ) + ); + + Move::handle_move( + array( + 'type' => 'Move', + 'actor' => $origin, + 'object' => $origin, + 'target' => $target, + ), + $this->user_id + ); + + \remove_filter( 'activitypub_pre_http_get_remote_object', $filter ); + + $this->assertSame( $id, Remote_Actors::get_by_uri( $origin )->ID, 'The old actor must stay where it was.' ); + $this->assertWPError( Remote_Actors::get_by_uri( $target ) ); + } + /** * Build a remote actor document. * From 4c2c52c72908c37e683e0a3026a5045f3bf6e639 Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Tue, 10 Nov 2026 23:18:31 +0100 Subject: [PATCH 3/9] refresh the target on merge, catch a silent update failure, simplify --- includes/handler/class-move.php | 54 ++++++-------- .../includes/handler/class-test-move.php | 70 +++++++++++++++++++ 2 files changed, 93 insertions(+), 31 deletions(-) diff --git a/includes/handler/class-move.php b/includes/handler/class-move.php index 6facfada99..7dd88b2dcc 100644 --- a/includes/handler/class-move.php +++ b/includes/handler/class-move.php @@ -7,7 +7,6 @@ namespace Activitypub\Handler; -use Activitypub\Collection\Actors; use Activitypub\Collection\Blocked_Actors; use Activitypub\Collection\Followers; use Activitypub\Collection\Following; @@ -103,10 +102,7 @@ public static function handle_move( $activity, $user_ids, $activity_object = nul return; } - $target_object = Remote_Actors::get_by_uri( $target_id ); - $result = null; - $success = false; - $target_post_id = 0; + $target_object = Remote_Actors::get_by_uri( $target_id ); // Our users who follow the old actor follow the new one (FEP-7628), unless they already do. $refollow = self::get_following_user_ids( $origin_object->ID ); @@ -116,14 +112,14 @@ public static function handle_move( $activity, $user_ids, $activity_object = nul global $wpdb; - // If the origin is followed but the target is not, update the origin to point to the target. + // If only the origin is stored, it becomes the target. if ( \is_wp_error( $target_object ) ) { /* * Proxy::get() already self-confirmed the target. Store it first, so a target that can * not be stored (no inbox) leaves the origin as it was. wp_update_post() keeps the guid. */ $result = Remote_Actors::update( $origin_object->ID, $target_json ); - if ( \is_wp_error( $result ) ) { + if ( ! $result || \is_wp_error( $result ) ) { return; } @@ -139,12 +135,12 @@ public static function handle_move( $activity, $user_ids, $activity_object = nul $wpdb->update( $wpdb->posts, $data, array( 'ID' => (int) $origin_object->ID ) ); \clean_post_cache( $origin_object->ID ); - $success = true; $target_post_id = $origin_object->ID; - } + } else { + // If both the target and origin are stored, merge them. + // The new Follows go to the inbox of the fresh copy, not to a stale stored one. + Remote_Actors::update( $target_object->ID, $target_json ); - // If both the target and origin are stored, merge them. - if ( ! \is_wp_error( $target_object ) ) { // Followers and blocks of the origin carry over; our own follows are sent again below. $origin_users = \get_post_meta( $origin_object->ID, Followers::FOLLOWER_META_KEY, false ); $target_users = \get_post_meta( $target_object->ID, Followers::FOLLOWER_META_KEY, false ); @@ -182,27 +178,24 @@ public static function handle_move( $activity, $user_ids, $activity_object = nul } // phpcs:enable - $success = true; $target_post_id = $target_object->ID; $result = Remote_Actors::delete( $origin_object->ID ); } - if ( $success ) { - // A user who blocks the new actor neither follows it nor keeps it as a follower. - foreach ( \get_post_meta( $target_post_id, Followers::FOLLOWER_META_KEY, false ) as $user_id ) { - if ( Moderation::is_actor_blocked( $target_id, (int) $user_id ) ) { - Followers::remove( $target_post_id, (int) $user_id ); - } + // A user who blocks the new actor neither follows it nor keeps it as a follower. + foreach ( \get_post_meta( $target_post_id, Followers::FOLLOWER_META_KEY, false ) as $user_id ) { + if ( Moderation::is_actor_blocked( $target_id, (int) $user_id ) ) { + Followers::remove( $target_post_id, (int) $user_id ); } + } - foreach ( $refollow as $user_id ) { - // After a swap the post still says the user follows; the new server does not know that yet. - Following::reject( $target_post_id, (int) $user_id ); + foreach ( $refollow as $user_id ) { + // After a swap the post still says the user follows; the new server does not know that yet. + Following::reject( $target_post_id, (int) $user_id ); - // Only a user who can still follow, and does not block the new actor, follows it. - if ( ! \is_wp_error( Actors::get_by_id( (int) $user_id ) ) && ! Moderation::is_actor_blocked( $target_id, (int) $user_id ) ) { - Following::follow( $target_post_id, (int) $user_id ); - } + // A user who blocks the new actor does not follow it; follow() refuses users who can no longer follow. + if ( ! Moderation::is_actor_blocked( $target_id, (int) $user_id ) ) { + Following::follow( $target_post_id, (int) $user_id ); } } @@ -211,18 +204,17 @@ public static function handle_move( $activity, $user_ids, $activity_object = nul * * @param array $activity The ActivityPub activity data. * @param int[] $user_ids The local user IDs. - * @param bool $success True on success, false otherwise. - * @param mixed $result The result of the operation (e.g., post ID, WP_Error, or status). + * @param bool $success Always true; a Move that is not handled returns before this action. + * @param mixed $result The moved actor's post ID, or the result of deleting the merged old actor. */ - \do_action( 'activitypub_handled_move', $activity, (array) $user_ids, $success, $result ); + \do_action( 'activitypub_handled_move', $activity, (array) $user_ids, true, $result ); } /** * Extract the target from the activity. * - * The ActivityStreams spec define the `target` attribute as the - * destination of the activity, but Mastodon uses the `object` - * attribute to move profiles. + * FEP-7628 and Mastodon send the new actor as `target`. Without a `target`, + * the `object` is read as the new actor, the way older senders sent it. * * @param array $activity The JSON "Move" Activity. * diff --git a/tests/phpunit/tests/includes/handler/class-test-move.php b/tests/phpunit/tests/includes/handler/class-test-move.php index 1d772333df..c25391be38 100644 --- a/tests/phpunit/tests/includes/handler/class-test-move.php +++ b/tests/phpunit/tests/includes/handler/class-test-move.php @@ -1258,6 +1258,76 @@ public function test_handle_move_keeps_origin_when_target_can_not_be_stored() { $this->assertWPError( Remote_Actors::get_by_uri( $target ) ); } + /** + * An update that fails without an error (wp_update_post() returns 0) leaves the old actor as it was. + * + * @covers ::handle_move + */ + public function test_handle_move_keeps_origin_when_update_fails() { + $origin = 'https://old.example/users/uma'; + $target = 'https://new.example/users/uma'; + + $id = Remote_Actors::upsert( $this->actor( $origin ) ); + + $filter = $this->mock_remote( + array( + $origin => $this->actor( $origin, array( 'movedTo' => $target ) ), + $target => $this->actor( $target, array( 'alsoKnownAs' => array( $origin ) ) ), + ) + ); + \add_filter( 'wp_insert_post_empty_content', '__return_true' ); + + Move::handle_move( + array( + 'type' => 'Move', + 'actor' => $origin, + 'object' => $origin, + 'target' => $target, + ), + $this->user_id + ); + + \remove_filter( 'wp_insert_post_empty_content', '__return_true' ); + \remove_filter( 'activitypub_pre_http_get_remote_object', $filter ); + + $this->assertSame( $id, Remote_Actors::get_by_uri( $origin )->ID, 'The old actor must stay where it was.' ); + $this->assertWPError( Remote_Actors::get_by_uri( $target ) ); + } + + /** + * A merge stores the fresh copy of the new actor, so the new Follows reach its current inbox. + * + * @covers ::handle_move + */ + public function test_handle_move_merge_refreshes_target() { + $origin = 'https://old.example/users/vic'; + $target = 'https://new.example/users/vic'; + + Remote_Actors::upsert( $this->actor( $origin ) ); + $target_id = Remote_Actors::upsert( $this->actor( $target, array( 'inbox' => 'https://new.example/old-inbox' ) ) ); + + $filter = $this->mock_remote( + array( + $origin => $this->actor( $origin, array( 'movedTo' => $target ) ), + $target => $this->actor( $target, array( 'alsoKnownAs' => array( $origin ) ) ), + ) + ); + + Move::handle_move( + array( + 'type' => 'Move', + 'actor' => $origin, + 'object' => $origin, + 'target' => $target, + ), + $this->user_id + ); + + \remove_filter( 'activitypub_pre_http_get_remote_object', $filter ); + + $this->assertSame( $target . '/inbox', \get_post_meta( $target_id, '_activitypub_inbox', true ) ); + } + /** * Build a remote actor document. * From ee045432619e3e9667ca91d40a268b5ef90b294b Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Wed, 11 Nov 2026 00:08:21 +0100 Subject: [PATCH 4/9] keep the old actor when the new one can not be stored on merge --- includes/handler/class-move.php | 5 ++- .../includes/handler/class-test-move.php | 40 +++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/includes/handler/class-move.php b/includes/handler/class-move.php index 7dd88b2dcc..b1720acc40 100644 --- a/includes/handler/class-move.php +++ b/includes/handler/class-move.php @@ -139,7 +139,10 @@ public static function handle_move( $activity, $user_ids, $activity_object = nul } else { // If both the target and origin are stored, merge them. // The new Follows go to the inbox of the fresh copy, not to a stale stored one. - Remote_Actors::update( $target_object->ID, $target_json ); + $result = Remote_Actors::update( $target_object->ID, $target_json ); + if ( ! $result || \is_wp_error( $result ) ) { + return; + } // Followers and blocks of the origin carry over; our own follows are sent again below. $origin_users = \get_post_meta( $origin_object->ID, Followers::FOLLOWER_META_KEY, false ); diff --git a/tests/phpunit/tests/includes/handler/class-test-move.php b/tests/phpunit/tests/includes/handler/class-test-move.php index c25391be38..c33af06f07 100644 --- a/tests/phpunit/tests/includes/handler/class-test-move.php +++ b/tests/phpunit/tests/includes/handler/class-test-move.php @@ -1328,6 +1328,46 @@ public function test_handle_move_merge_refreshes_target() { $this->assertSame( $target . '/inbox', \get_post_meta( $target_id, '_activitypub_inbox', true ) ); } + /** + * A merge into a new actor whose fresh copy can not be stored (no inbox) keeps the old actor. + * + * @covers ::handle_move + */ + public function test_handle_move_merge_keeps_origin_when_target_can_not_be_stored() { + $origin = 'https://old.example/users/wes'; + $target = 'https://new.example/users/wes'; + + $origin_id = Remote_Actors::upsert( $this->actor( $origin ) ); + Remote_Actors::upsert( $this->actor( $target ) ); + \add_post_meta( $origin_id, Following::FOLLOWING_META_KEY, (string) $this->user_id ); + + $broken = $this->actor( $target, array( 'alsoKnownAs' => array( $origin ) ) ); + unset( $broken['inbox'] ); + + $filter = $this->mock_remote( + array( + $origin => $this->actor( $origin, array( 'movedTo' => $target ) ), + $target => $broken, + ) + ); + + Move::handle_move( + array( + 'type' => 'Move', + 'actor' => $origin, + 'object' => $origin, + 'target' => $target, + ), + $this->user_id + ); + + \remove_filter( 'activitypub_pre_http_get_remote_object', $filter ); + + $this->assertSame( $origin_id, Remote_Actors::get_by_uri( $origin )->ID, 'The old actor must not be merged away.' ); + $this->assertContains( (string) $this->user_id, \get_post_meta( $origin_id, Following::FOLLOWING_META_KEY, false ) ); + $this->assertCount( 0, $this->get_follows( $this->user_id, $target ) ); + } + /** * Build a remote actor document. * From 7bd06ce687d943383d7f63c07b18d7e89b17bb2c Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Wed, 11 Nov 2026 09:02:01 +0100 Subject: [PATCH 5/9] split the move into protocol, relationships and store --- includes/class-move.php | 74 ++++++++++ includes/collection/class-remote-actors.php | 86 ++++++++++++ includes/handler/class-move.php | 128 +----------------- .../collection/class-test-remote-actors.php | 110 +++++++++++++++ 4 files changed, 276 insertions(+), 122 deletions(-) diff --git a/includes/class-move.php b/includes/class-move.php index b921699d2d..abaa4c0e03 100644 --- a/includes/class-move.php +++ b/includes/class-move.php @@ -10,6 +10,10 @@ use Activitypub\Activity\Activity; use Activitypub\Activity\Actor; use Activitypub\Collection\Actors; +use Activitypub\Collection\Blocked_Actors; +use Activitypub\Collection\Followers; +use Activitypub\Collection\Following; +use Activitypub\Collection\Remote_Actors; use Activitypub\Model\Blog; use Activitypub\Model\User; use Activitypub\Scheduler\Actor as Actor_Scheduler; @@ -215,6 +219,76 @@ public static function internally( $from, $to ) { return $outbox_id; } + /** + * Move a remote actor to its new id. + * + * Our users who follow the old actor follow the new one (FEP-7628), and their blocks carry over. + * + * @since unreleased + * + * @param \WP_Post $origin The stored old actor. + * @param array $actor The new actor, fetched and verified. + * + * @return int|\WP_Error The post ID of the new actor, or WP_Error if it can not be stored. + */ + public static function remote( $origin, $actor ) { + // Read before the move, which deletes the old actor on a merge. + $refollow = self::get_following_user_ids( $origin->ID ); + $blocked_by = \get_post_meta( $origin->ID, Moderation::BLOCKED_ACTORS_META_KEY, false ); + + $post_id = Remote_Actors::move( $origin, $actor ); + + if ( \is_wp_error( $post_id ) ) { + return $post_id; + } + + // After a swap the post keeps its follows and blocks; a merge brings them to another post. + if ( $post_id !== $origin->ID ) { + // Users who already follow the new actor keep that follow. + $refollow = \array_diff( $refollow, self::get_following_user_ids( $post_id ) ); + + // Through Blocked_Actors::add(), so the block hooks run and clean up the relationships. + foreach ( $blocked_by as $user_id ) { + Blocked_Actors::add( (int) $user_id, $actor['id'] ); + } + } + + // A user who blocks the new actor neither follows it nor keeps it as a follower. + foreach ( \get_post_meta( $post_id, Followers::FOLLOWER_META_KEY, false ) as $user_id ) { + if ( Moderation::is_actor_blocked( $actor['id'], (int) $user_id ) ) { + Followers::remove( $post_id, (int) $user_id ); + } + } + + foreach ( $refollow as $user_id ) { + // After a swap the post still says the user follows; the new server does not know that yet. + Following::reject( $post_id, (int) $user_id ); + + // A user who blocks the new actor does not follow it; follow() refuses users who can no longer follow. + if ( ! Moderation::is_actor_blocked( $actor['id'], (int) $user_id ) ) { + Following::follow( $post_id, (int) $user_id ); + } + } + + return $post_id; + } + + /** + * Get the local users that follow or asked to follow a remote actor. + * + * @param int $post_id The remote actor post ID. + * + * @return string[] The user IDs. + */ + private static function get_following_user_ids( $post_id ) { + return \array_unique( + \array_merge( + \get_post_meta( $post_id, Following::FOLLOWING_META_KEY, false ), + \get_post_meta( $post_id, Following::PENDING_META_KEY, false ) + ) + ); + } + /** * Update the alsoKnownAs property of a user. * diff --git a/includes/collection/class-remote-actors.php b/includes/collection/class-remote-actors.php index 63f27c9f05..cb8abf6d75 100644 --- a/includes/collection/class-remote-actors.php +++ b/includes/collection/class-remote-actors.php @@ -212,6 +212,92 @@ public static function delete( $post_id ) { return $result; } + /** + * Move a stored remote actor to its new id. + * + * Without a stored new actor, the old post becomes the new actor. Otherwise the followers, + * comments and Reader posts of the old actor go to the new one, and the old post is deleted. + * + * @since unreleased + * + * @param \WP_Post $origin The stored old actor. + * @param array $actor The new actor, fetched and verified. + * + * @return int|\WP_Error The post ID of the new actor, or WP_Error if it can not be stored. + */ + public static function move( $origin, $actor ) { + global $wpdb; + + $target = self::get_by_uri( $actor['id'] ); + $merge = ! \is_wp_error( $target ); + + // A merge into the old actor itself would delete it. + if ( $merge && $target->ID === $origin->ID ) { + return new \WP_Error( 'activitypub_move_same_actor', \__( 'The actor can not move to itself.', 'activitypub' ) ); + } + + // Stored first, so a new actor that can not be stored (no inbox) leaves the old one as it was. + $post_id = self::update( $merge ? $target->ID : $origin->ID, $actor ); + + if ( \is_wp_error( $post_id ) ) { + return $post_id; + } + + if ( ! $post_id ) { + return new \WP_Error( 'activitypub_actor_not_stored', \__( 'The actor could not be stored.', 'activitypub' ) ); + } + + if ( ! $merge ) { + // wp_update_post() keeps the guid. Written in the form an insert stores, which every guid lookup matches. + list( $guid ) = get_stored_id_forms( $actor['id'] ); + $data = array( 'guid' => $guid ); + $hash = get_id_hash( $actor['id'] ); + if ( $hash ) { + $data['post_name'] = $hash; + } + + // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery + $wpdb->update( $wpdb->posts, $data, array( 'ID' => (int) $origin->ID ) ); + \clean_post_cache( $origin->ID ); + + return $post_id; + } + + foreach ( \array_diff( self::get_follower_ids( $origin->ID ), self::get_follower_ids( $target->ID ) ) as $user_id ) { + \add_post_meta( $target->ID, Followers::FOLLOWER_META_KEY, $user_id ); + } + + // Comments and Reader posts of the old actor belong to the new one now. + // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.SlowDBQuery.slow_db_query_meta_key, WordPress.DB.SlowDBQuery.slow_db_query_meta_value + $where = array( + 'meta_key' => '_activitypub_remote_actor_id', + 'meta_value' => (string) $origin->ID, + ); + + // The meta type and the cache group of its queries, which core names differently. + $types = array( + 'comment' => 'comment', + 'post' => 'posts', + ); + + foreach ( $types as $type => $cache_group ) { + $table = $wpdb->{ \sprintf( '%smeta', $type ) }; + $ids = $wpdb->get_col( $wpdb->prepare( 'SELECT %i FROM %i WHERE meta_key = %s AND meta_value = %s', \sprintf( '%s_id', $type ), $table, $where['meta_key'], $where['meta_value'] ) ); + + if ( $ids ) { + $wpdb->update( $table, array( 'meta_value' => (string) $target->ID ), $where ); + \wp_cache_delete_multiple( $ids, \sprintf( '%s_meta', $type ) ); + \wp_cache_set_last_changed( $cache_group ); + } + } + // phpcs:enable + + // Clears the inbox caches of the followers, which now include the new actor. + self::delete( $origin->ID ); + + return $post_id; + } + /** * Get the IDs of the local users a remote actor follows. * diff --git a/includes/handler/class-move.php b/includes/handler/class-move.php index b1720acc40..e818986bba 100644 --- a/includes/handler/class-move.php +++ b/includes/handler/class-move.php @@ -7,16 +7,12 @@ namespace Activitypub\Handler; -use Activitypub\Collection\Blocked_Actors; -use Activitypub\Collection\Followers; -use Activitypub\Collection\Following; use Activitypub\Collection\Inbox; use Activitypub\Collection\Remote_Actors; use Activitypub\Moderation; +use Activitypub\Move as Account_Move; use Activitypub\Proxy; -use function Activitypub\get_id_hash; -use function Activitypub\get_stored_id_forms; use function Activitypub\is_actor; use function Activitypub\is_same_domain; use function Activitypub\object_to_uri; @@ -95,111 +91,15 @@ public static function handle_move( $activity, $user_ids, $activity_object = nul return; } - $target_id = $target_json['id']; - // A remote actor can not move to one of ours, nor to an actor the site blocks. - if ( is_same_domain( $target_id ) || Moderation::is_actor_blocked( $target_id ) ) { + if ( is_same_domain( $target_json['id'] ) || Moderation::is_actor_blocked( $target_json['id'] ) ) { return; } - $target_object = Remote_Actors::get_by_uri( $target_id ); - - // Our users who follow the old actor follow the new one (FEP-7628), unless they already do. - $refollow = self::get_following_user_ids( $origin_object->ID ); - if ( ! \is_wp_error( $target_object ) ) { - $refollow = \array_diff( $refollow, self::get_following_user_ids( $target_object->ID ) ); - } - - global $wpdb; - - // If only the origin is stored, it becomes the target. - if ( \is_wp_error( $target_object ) ) { - /* - * Proxy::get() already self-confirmed the target. Store it first, so a target that can - * not be stored (no inbox) leaves the origin as it was. wp_update_post() keeps the guid. - */ - $result = Remote_Actors::update( $origin_object->ID, $target_json ); - if ( ! $result || \is_wp_error( $result ) ) { - return; - } - - // Written in the form an insert stores, which every guid lookup matches. - list( $guid ) = get_stored_id_forms( $target_id ); - $data = array( 'guid' => $guid ); - $hash = get_id_hash( $target_id ); - if ( $hash ) { - $data['post_name'] = $hash; - } - - // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery - $wpdb->update( $wpdb->posts, $data, array( 'ID' => (int) $origin_object->ID ) ); - \clean_post_cache( $origin_object->ID ); - - $target_post_id = $origin_object->ID; - } else { - // If both the target and origin are stored, merge them. - // The new Follows go to the inbox of the fresh copy, not to a stale stored one. - $result = Remote_Actors::update( $target_object->ID, $target_json ); - if ( ! $result || \is_wp_error( $result ) ) { - return; - } - - // Followers and blocks of the origin carry over; our own follows are sent again below. - $origin_users = \get_post_meta( $origin_object->ID, Followers::FOLLOWER_META_KEY, false ); - $target_users = \get_post_meta( $target_object->ID, Followers::FOLLOWER_META_KEY, false ); - foreach ( \array_diff( $origin_users, $target_users ) as $user_id ) { - \add_post_meta( $target_object->ID, Followers::FOLLOWER_META_KEY, $user_id ); - } - - // Through Blocked_Actors::add(), so the block hooks run and clean up the relationships. - foreach ( \get_post_meta( $origin_object->ID, Moderation::BLOCKED_ACTORS_META_KEY, false ) as $user_id ) { - Blocked_Actors::add( (int) $user_id, $target_id ); - } - - // Comments and Reader posts of the origin belong to the target now. - // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.SlowDBQuery.slow_db_query_meta_key, WordPress.DB.SlowDBQuery.slow_db_query_meta_value - $where = array( - 'meta_key' => '_activitypub_remote_actor_id', - 'meta_value' => (string) $origin_object->ID, - ); - - // The meta type and the cache group of its queries, which core names differently. - $types = array( - 'comment' => 'comment', - 'post' => 'posts', - ); - - foreach ( $types as $type => $cache_group ) { - $table = $wpdb->{ \sprintf( '%smeta', $type ) }; - $ids = $wpdb->get_col( $wpdb->prepare( 'SELECT %i FROM %i WHERE meta_key = %s AND meta_value = %s', \sprintf( '%s_id', $type ), $table, $where['meta_key'], $where['meta_value'] ) ); - - if ( $ids ) { - $wpdb->update( $table, array( 'meta_value' => (string) $target_object->ID ), $where ); - \wp_cache_delete_multiple( $ids, \sprintf( '%s_meta', $type ) ); - \wp_cache_set_last_changed( $cache_group ); - } - } - // phpcs:enable - - $target_post_id = $target_object->ID; - $result = Remote_Actors::delete( $origin_object->ID ); - } - - // A user who blocks the new actor neither follows it nor keeps it as a follower. - foreach ( \get_post_meta( $target_post_id, Followers::FOLLOWER_META_KEY, false ) as $user_id ) { - if ( Moderation::is_actor_blocked( $target_id, (int) $user_id ) ) { - Followers::remove( $target_post_id, (int) $user_id ); - } - } - - foreach ( $refollow as $user_id ) { - // After a swap the post still says the user follows; the new server does not know that yet. - Following::reject( $target_post_id, (int) $user_id ); + $result = Account_Move::remote( $origin_object, $target_json ); - // A user who blocks the new actor does not follow it; follow() refuses users who can no longer follow. - if ( ! Moderation::is_actor_blocked( $target_id, (int) $user_id ) ) { - Following::follow( $target_post_id, (int) $user_id ); - } + if ( \is_wp_error( $result ) ) { + return; } /** @@ -208,7 +108,7 @@ public static function handle_move( $activity, $user_ids, $activity_object = nul * @param array $activity The ActivityPub activity data. * @param int[] $user_ids The local user IDs. * @param bool $success Always true; a Move that is not handled returns before this action. - * @param mixed $result The moved actor's post ID, or the result of deleting the merged old actor. + * @param int $result The post ID of the new actor. */ \do_action( 'activitypub_handled_move', $activity, (array) $user_ids, true, $result ); } @@ -313,20 +213,4 @@ private static function verify_move( $activity, $target_object, $origin_object ) // Check if the origin points at the target. return object_to_uri( $origin_object['movedTo'] ?? null ) === $target_object['id']; } - - /** - * Get the local users that follow or asked to follow a remote actor. - * - * @param int $post_id The remote actor post ID. - * - * @return string[] The user IDs. - */ - private static function get_following_user_ids( $post_id ) { - return \array_unique( - \array_merge( - \get_post_meta( $post_id, Following::FOLLOWING_META_KEY, false ), - \get_post_meta( $post_id, Following::PENDING_META_KEY, false ) - ) - ); - } } diff --git a/tests/phpunit/tests/includes/collection/class-test-remote-actors.php b/tests/phpunit/tests/includes/collection/class-test-remote-actors.php index 03aba80c23..716e825eda 100644 --- a/tests/phpunit/tests/includes/collection/class-test-remote-actors.php +++ b/tests/phpunit/tests/includes/collection/class-test-remote-actors.php @@ -2804,4 +2804,114 @@ public function test_fetch_by_acct_ignores_case( $stored, $requested ) { $this->assertSame( 0, $requests ); $this->assertSame( $stored, \get_post_meta( $post_id, '_activitypub_acct', true ), 'The stored handle must keep its case.' ); } + + /** + * Without a stored new actor, the old post becomes the new actor, found by its new id after an update. + * + * @covers ::move + */ + public function test_move_swaps_origin() { + $origin = 'https://old.example.com/users/ann'; + $target = 'https://new.example.com/users/ann?a=1&b=2'; + + $origin_id = Remote_Actors::upsert( + array( + 'id' => $origin, + 'type' => 'Person', + 'inbox' => $origin . '/inbox', + ) + ); + \add_post_meta( $origin_id, Followers::FOLLOWER_META_KEY, '1' ); + + $post_id = Remote_Actors::move( + \get_post( $origin_id ), + array( + 'id' => $target, + 'type' => 'Person', + 'inbox' => 'https://new.example.com/inbox', + ) + ); + + $this->assertSame( $origin_id, $post_id ); + $this->assertSame( $origin_id, Remote_Actors::get_by_uri( $target )->ID ); + $this->assertWPError( Remote_Actors::get_by_uri( $origin ) ); + $this->assertSame( 'https://new.example.com/inbox', \get_post_meta( $origin_id, '_activitypub_inbox', true ) ); + $this->assertSame( array( '1' ), \get_post_meta( $origin_id, Followers::FOLLOWER_META_KEY, false ), 'The followers stay on the post.' ); + + // A later update rewrites the guid; the lookup still finds it. + Remote_Actors::update( + $origin_id, + array( + 'id' => $target, + 'type' => 'Person', + 'inbox' => 'https://new.example.com/inbox', + ) + ); + $this->assertSame( $origin_id, Remote_Actors::get_by_uri( $target )->ID ); + } + + /** + * With a stored new actor, followers, comments and Reader posts of the old actor go to it, and the old one is deleted. + * + * @covers ::move + */ + public function test_move_merges_into_target() { + $origin = 'https://old.example.com/users/ben'; + $target = 'https://new.example.com/users/ben'; + + $origin_id = Remote_Actors::upsert( + array( + 'id' => $origin, + 'type' => 'Person', + 'inbox' => $origin . '/inbox', + ) + ); + $target_id = Remote_Actors::upsert( + array( + 'id' => $target, + 'type' => 'Person', + 'inbox' => $target . '/old-inbox', + ) + ); + \add_post_meta( $origin_id, Followers::FOLLOWER_META_KEY, '1' ); + $comment_id = self::factory()->comment->create( array( 'comment_meta' => array( '_activitypub_remote_actor_id' => (string) $origin_id ) ) ); + $reader_id = self::factory()->post->create( array( 'meta_input' => array( '_activitypub_remote_actor_id' => (string) $origin_id ) ) ); + + $post_id = Remote_Actors::move( + \get_post( $origin_id ), + array( + 'id' => $target, + 'type' => 'Person', + 'inbox' => $target . '/inbox', + ) + ); + + $this->assertSame( $target_id, $post_id ); + $this->assertNull( \get_post( $origin_id ), 'The old actor must be deleted.' ); + $this->assertSame( $target . '/inbox', \get_post_meta( $target_id, '_activitypub_inbox', true ), 'The new actor is stored fresh.' ); + $this->assertSame( array( '1' ), \get_post_meta( $target_id, Followers::FOLLOWER_META_KEY, false ) ); + $this->assertSame( (string) $target_id, \get_comment_meta( $comment_id, '_activitypub_remote_actor_id', true ) ); + $this->assertSame( (string) $target_id, \get_post_meta( $reader_id, '_activitypub_remote_actor_id', true ) ); + } + + /** + * A move to the old actor itself keeps it, instead of merging it into itself and deleting it. + * + * @covers ::move + */ + public function test_move_to_itself_keeps_actor() { + $id = 'https://old.example.com/users/cid'; + $actor = array( + 'id' => $id, + 'type' => 'Person', + 'inbox' => $id . '/inbox', + ); + + $post_id = Remote_Actors::upsert( $actor ); + \add_post_meta( $post_id, Followers::FOLLOWER_META_KEY, '1' ); + + $this->assertWPError( Remote_Actors::move( \get_post( $post_id ), $actor ) ); + $this->assertSame( $post_id, Remote_Actors::get_by_uri( $id )->ID ); + $this->assertSame( array( '1' ), \get_post_meta( $post_id, Followers::FOLLOWER_META_KEY, false ) ); + } } From 550a07b6f230301a8f134b41a56100778599b6b7 Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Wed, 11 Nov 2026 09:32:39 +0100 Subject: [PATCH 6/9] stop the move when the guid write fails, test Move::remote directly --- includes/collection/class-remote-actors.php | 7 +- .../tests/includes/class-test-move.php | 162 +++++++++++++++ .../collection/class-test-remote-actors.php | 43 ++++ .../includes/handler/class-test-move.php | 191 ------------------ 4 files changed, 211 insertions(+), 192 deletions(-) diff --git a/includes/collection/class-remote-actors.php b/includes/collection/class-remote-actors.php index cb8abf6d75..7d1de38c3e 100644 --- a/includes/collection/class-remote-actors.php +++ b/includes/collection/class-remote-actors.php @@ -257,9 +257,14 @@ public static function move( $origin, $actor ) { } // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery - $wpdb->update( $wpdb->posts, $data, array( 'ID' => (int) $origin->ID ) ); + $updated = $wpdb->update( $wpdb->posts, $data, array( 'ID' => (int) $origin->ID ) ); \clean_post_cache( $origin->ID ); + // Under the old guid, a Follow would go to the old actor; the next Move finds the post again. + if ( false === $updated ) { + return new \WP_Error( 'activitypub_actor_not_moved', \__( 'The actor could not be moved.', 'activitypub' ) ); + } + return $post_id; } diff --git a/tests/phpunit/tests/includes/class-test-move.php b/tests/phpunit/tests/includes/class-test-move.php index b606659e3c..dd6c3790a7 100644 --- a/tests/phpunit/tests/includes/class-test-move.php +++ b/tests/phpunit/tests/includes/class-test-move.php @@ -8,7 +8,11 @@ namespace Activitypub\Tests; use Activitypub\Collection\Actors; +use Activitypub\Collection\Followers; +use Activitypub\Collection\Following; use Activitypub\Collection\Outbox; +use Activitypub\Collection\Remote_Actors; +use Activitypub\Moderation; use Activitypub\Move; /** @@ -632,6 +636,164 @@ public function test_change_domain_with_valid_input() { \add_filter( 'option_home', '_config_wp_home' ); } + /** + * Our users who follow the old actor follow the new one, which the old post becomes. + * + * @covers ::remote + */ + public function test_remote_follows_new_actor() { + $origin = 'https://old.example/users/alice'; + $target = 'https://new.example/users/alice'; + $user_id = self::factory()->user->create(); + + \get_user_by( 'id', $user_id )->add_cap( 'activitypub' ); + $id = Remote_Actors::upsert( $this->remote_actor( $origin ) ); + \add_post_meta( $id, Following::FOLLOWING_META_KEY, (string) $user_id ); + + $this->assertSame( $id, Move::remote( \get_post( $id ), $this->remote_actor( $target ) ) ); + + $this->assertSame( $id, Remote_Actors::get_by_uri( $target )->ID, 'The stored actor must be moved in place.' ); + $this->assertNotContains( (string) $user_id, \get_post_meta( $id, Following::FOLLOWING_META_KEY, false ), 'The new server has not accepted yet.' ); + $this->assertContains( (string) $user_id, \get_post_meta( $id, Following::PENDING_META_KEY, false ) ); + $this->assertCount( 1, $this->get_follows( $user_id, $target ), 'A Follow must be sent to the new actor.' ); + } + + /** + * A merge keeps blocks of the old actor and follows the new one for our users. + * + * @covers ::remote + */ + public function test_remote_merge_keeps_blocks_and_follows() { + $origin = 'https://old.example/users/bob'; + $target = 'https://new.example/users/bob'; + $user_id = self::factory()->user->create(); + $blocker = self::factory()->user->create(); + $origin_id = Remote_Actors::upsert( $this->remote_actor( $origin ) ); + $target_id = Remote_Actors::upsert( $this->remote_actor( $target ) ); + + \get_user_by( 'id', $user_id )->add_cap( 'activitypub' ); + \add_post_meta( $origin_id, Following::FOLLOWING_META_KEY, (string) $user_id ); + \add_post_meta( $origin_id, Moderation::BLOCKED_ACTORS_META_KEY, (string) $blocker ); + + $this->assertSame( $target_id, Move::remote( \get_post( $origin_id ), $this->remote_actor( $target ) ) ); + + $this->assertWPError( Remote_Actors::get_by_uri( $origin ), 'The old actor must be merged away.' ); + $this->assertContains( (string) $blocker, \get_post_meta( $target_id, Moderation::BLOCKED_ACTORS_META_KEY, false ), 'The block must carry over.' ); + $this->assertTrue( Moderation::is_actor_blocked( $target, $blocker ) ); + $this->assertContains( (string) $user_id, \get_post_meta( $target_id, Following::PENDING_META_KEY, false ) ); + $this->assertCount( 1, $this->get_follows( $user_id, $target ) ); + } + + /** + * Our users do not follow a new actor they block. + * + * @covers ::remote + */ + public function test_remote_does_not_follow_blocked_actor() { + $origin = 'https://old.example/users/carol'; + $target = 'https://new.example/users/carol'; + $user_id = self::factory()->user->create(); + + \get_user_by( 'id', $user_id )->add_cap( 'activitypub' ); + Moderation::add_user_block( $user_id, Moderation::TYPE_DOMAIN, 'new.example' ); + $id = Remote_Actors::upsert( $this->remote_actor( $origin ) ); + \add_post_meta( $id, Following::FOLLOWING_META_KEY, (string) $user_id ); + + Move::remote( \get_post( $id ), $this->remote_actor( $target ) ); + + $this->assertEmpty( \get_post_meta( $id, Following::FOLLOWING_META_KEY, false ) ); + $this->assertEmpty( \get_post_meta( $id, Following::PENDING_META_KEY, false ) ); + $this->assertCount( 0, $this->get_follows( $user_id, $target ) ); + } + + /** + * A user who blocks the new actor does not keep it as a follower. + * + * @covers ::remote + */ + public function test_remote_drops_follower_blocked_by_user() { + $origin = 'https://old.example/users/jack'; + $target = 'https://new.example/users/jack'; + $blocker = self::factory()->user->create(); + $user_id = self::factory()->user->create(); + + Moderation::add_user_block( $blocker, Moderation::TYPE_DOMAIN, 'new.example' ); + $id = Remote_Actors::upsert( $this->remote_actor( $origin ) ); + \add_post_meta( $id, Followers::FOLLOWER_META_KEY, (string) $blocker ); + \add_post_meta( $id, Followers::FOLLOWER_META_KEY, (string) $user_id ); + + Move::remote( \get_post( $id ), $this->remote_actor( $target ) ); + + $followers = \get_post_meta( $id, Followers::FOLLOWER_META_KEY, false ); + $this->assertNotContains( (string) $blocker, $followers ); + $this->assertContains( (string) $user_id, $followers ); + } + + /** + * A user who can no longer follow does not end up following the new actor without a Follow. + * + * @covers ::remote + */ + public function test_remote_does_not_keep_following_without_follow() { + $origin = 'https://old.example/users/mia'; + $target = 'https://new.example/users/mia'; + $user_id = self::factory()->user->create(); + + $id = Remote_Actors::upsert( $this->remote_actor( $origin ) ); + \add_post_meta( $id, Following::FOLLOWING_META_KEY, (string) $user_id ); + + Move::remote( \get_post( $id ), $this->remote_actor( $target ) ); + + $this->assertNotContains( (string) $user_id, \get_post_meta( $id, Following::FOLLOWING_META_KEY, false ), 'The new server never got a Follow.' ); + $this->assertCount( 0, $this->get_follows( $user_id, $target ) ); + } + + /** + * Build a remote actor document. + * + * @param string $id The actor id. + * + * @return array The actor document. + */ + private function remote_actor( $id ) { + return array( + 'type' => 'Person', + 'id' => $id, + 'url' => $id, + 'name' => 'Actor', + 'inbox' => $id . '/inbox', + ); + } + + /** + * Get the Follow activities a user sent to an actor. + * + * @param int $user_id The user ID. + * @param string $actor_id The followed actor id. + * + * @return int[] The outbox post IDs. + */ + private function get_follows( $user_id, $actor_id ) { + return \get_posts( + array( + 'post_type' => Outbox::POST_TYPE, + 'post_status' => 'any', + 'author' => $user_id, + 'fields' => 'ids', + 'meta_query' => array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query + array( + 'key' => '_activitypub_object_id', + 'value' => $actor_id, + ), + array( + 'key' => '_activitypub_activity_type', + 'value' => 'Follow', + ), + ), + ) + ); + } + /** * The profile Updates federated for an actor. * diff --git a/tests/phpunit/tests/includes/collection/class-test-remote-actors.php b/tests/phpunit/tests/includes/collection/class-test-remote-actors.php index 716e825eda..bff85e1f84 100644 --- a/tests/phpunit/tests/includes/collection/class-test-remote-actors.php +++ b/tests/phpunit/tests/includes/collection/class-test-remote-actors.php @@ -2894,6 +2894,49 @@ public function test_move_merges_into_target() { $this->assertSame( (string) $target_id, \get_post_meta( $reader_id, '_activitypub_remote_actor_id', true ) ); } + /** + * A swap whose guid write fails returns an error, so no Follow goes out under the old guid. + * + * @covers ::move + */ + public function test_move_fails_when_guid_write_fails() { + global $wpdb; + + $origin = 'https://old.example.com/users/dan'; + $target = 'https://new.example.com/users/dan'; + + $origin_id = Remote_Actors::upsert( + array( + 'id' => $origin, + 'type' => 'Person', + 'inbox' => $origin . '/inbox', + ) + ); + + // Break only the direct guid write; wp_update_post() writes the guid among all other fields. + $break = static function ( $query ) { + return false !== \strpos( $query, 'SET `guid`' ) ? 'UPDATE broken' : $query; + }; + \add_filter( 'query', $break ); + $suppress = $wpdb->suppress_errors(); + + $result = Remote_Actors::move( + \get_post( $origin_id ), + array( + 'id' => $target, + 'type' => 'Person', + 'inbox' => $target . '/inbox', + ) + ); + + $wpdb->suppress_errors( $suppress ); + \remove_filter( 'query', $break ); + + $this->assertWPError( $result ); + $this->assertWPError( Remote_Actors::get_by_uri( $target ) ); + $this->assertSame( $origin_id, Remote_Actors::get_by_uri( $origin )->ID, 'The post keeps the old guid, so the next Move finds it.' ); + } + /** * A move to the old actor itself keeps it, instead of merging it into itself and deleting it. * diff --git a/tests/phpunit/tests/includes/handler/class-test-move.php b/tests/phpunit/tests/includes/handler/class-test-move.php index c33af06f07..5d2c160678 100644 --- a/tests/phpunit/tests/includes/handler/class-test-move.php +++ b/tests/phpunit/tests/includes/handler/class-test-move.php @@ -645,124 +645,6 @@ public function test_handle_move_ignores_origin_webfinger() { $this->assertSame( $id, Remote_Actors::get_by_uri( $origin )->ID, 'The origin must stay as it is.' ); } - /** - * A Mastodon Move (old actor as `object`, new one as `target`) makes our users follow the new actor. - * - * @covers ::handle_move - */ - public function test_handle_move_follows_target_for_following_users() { - $origin = 'https://old.example/users/alice'; - $target = 'https://new.example/users/alice'; - - \get_user_by( 'id', $this->user_id )->add_cap( 'activitypub' ); - $id = Remote_Actors::upsert( $this->actor( $origin, array( 'movedTo' => $target ) ) ); - \add_post_meta( $id, Following::FOLLOWING_META_KEY, (string) $this->user_id ); - - $filter = $this->mock_remote( - array( - $origin => $this->actor( $origin, array( 'movedTo' => $target ) ), - $target => $this->actor( $target, array( 'alsoKnownAs' => array( $origin ) ) ), - ) - ); - - Move::handle_move( - array( - 'type' => 'Move', - 'actor' => $origin, - 'object' => $origin, - 'target' => $target, - ), - $this->user_id - ); - - \remove_filter( 'activitypub_pre_http_get_remote_object', $filter ); - - $moved = Remote_Actors::get_by_uri( $target ); - $this->assertSame( $id, $moved->ID, 'The stored actor must be moved in place.' ); - $this->assertNotContains( (string) $this->user_id, \get_post_meta( $id, Following::FOLLOWING_META_KEY, false ), 'The new server has not accepted yet.' ); - $this->assertContains( (string) $this->user_id, \get_post_meta( $id, Following::PENDING_META_KEY, false ) ); - $this->assertCount( 1, $this->get_follows( $this->user_id, $target ), 'A Follow must be sent to the new actor.' ); - } - - /** - * A merge keeps blocks of the old actor and follows the new one for our users. - * - * @covers ::handle_move - */ - public function test_handle_move_merge_keeps_blocks_and_follows() { - $origin = 'https://old.example/users/bob'; - $target = 'https://new.example/users/bob'; - - \get_user_by( 'id', $this->user_id )->add_cap( 'activitypub' ); - $origin_id = Remote_Actors::upsert( $this->actor( $origin, array( 'movedTo' => $target ) ) ); - $target_id = Remote_Actors::upsert( $this->actor( $target, array( 'alsoKnownAs' => array( $origin ) ) ) ); - \add_post_meta( $origin_id, Following::FOLLOWING_META_KEY, (string) $this->user_id ); - \add_post_meta( $origin_id, Moderation::BLOCKED_ACTORS_META_KEY, (string) $this->user_id_2 ); - - $filter = $this->mock_remote( - array( - $origin => $this->actor( $origin, array( 'movedTo' => $target ) ), - $target => $this->actor( $target, array( 'alsoKnownAs' => array( $origin ) ) ), - ) - ); - - Move::handle_move( - array( - 'type' => 'Move', - 'actor' => $origin, - 'object' => $origin, - 'target' => $target, - ), - $this->user_id - ); - - \remove_filter( 'activitypub_pre_http_get_remote_object', $filter ); - - $this->assertWPError( Remote_Actors::get_by_uri( $origin ), 'The old actor must be merged away.' ); - $this->assertContains( (string) $this->user_id_2, \get_post_meta( $target_id, Moderation::BLOCKED_ACTORS_META_KEY, false ), 'The block must carry over.' ); - $this->assertTrue( Moderation::is_actor_blocked( $target, $this->user_id_2 ) ); - $this->assertContains( (string) $this->user_id, \get_post_meta( $target_id, Following::PENDING_META_KEY, false ) ); - $this->assertCount( 1, $this->get_follows( $this->user_id, $target ) ); - } - - /** - * Our users do not follow a new actor they block. - * - * @covers ::handle_move - */ - public function test_handle_move_does_not_follow_blocked_target() { - $origin = 'https://old.example/users/carol'; - $target = 'https://new.example/users/carol'; - - \get_user_by( 'id', $this->user_id )->add_cap( 'activitypub' ); - Moderation::add_user_block( $this->user_id, Moderation::TYPE_DOMAIN, 'new.example' ); - $id = Remote_Actors::upsert( $this->actor( $origin, array( 'movedTo' => $target ) ) ); - \add_post_meta( $id, Following::FOLLOWING_META_KEY, (string) $this->user_id ); - - $filter = $this->mock_remote( - array( - $origin => $this->actor( $origin, array( 'movedTo' => $target ) ), - $target => $this->actor( $target, array( 'alsoKnownAs' => array( $origin ) ) ), - ) - ); - - Move::handle_move( - array( - 'type' => 'Move', - 'actor' => $origin, - 'object' => $origin, - 'target' => $target, - ), - $this->user_id - ); - - \remove_filter( 'activitypub_pre_http_get_remote_object', $filter ); - - $this->assertEmpty( \get_post_meta( $id, Following::FOLLOWING_META_KEY, false ) ); - $this->assertEmpty( \get_post_meta( $id, Following::PENDING_META_KEY, false ) ); - $this->assertCount( 0, $this->get_follows( $this->user_id, $target ) ); - } - /** * A Move sent by the new actor (pull mode) is accepted when the old actor points at it. * @@ -895,44 +777,6 @@ public function test_handle_move_rejects_site_blocked_target() { $this->assertWPError( Remote_Actors::get_by_uri( $target ) ); } - /** - * A user who blocks the new actor does not keep it as a follower. - * - * @covers ::handle_move - */ - public function test_handle_move_drops_follower_blocked_by_user() { - $origin = 'https://old.example/users/jack'; - $target = 'https://new.example/users/jack'; - - Moderation::add_user_block( $this->user_id, Moderation::TYPE_DOMAIN, 'new.example' ); - $id = Remote_Actors::upsert( $this->actor( $origin ) ); - \add_post_meta( $id, Followers::FOLLOWER_META_KEY, (string) $this->user_id ); - \add_post_meta( $id, Followers::FOLLOWER_META_KEY, (string) $this->user_id_2 ); - - $filter = $this->mock_remote( - array( - $origin => $this->actor( $origin, array( 'movedTo' => $target ) ), - $target => $this->actor( $target, array( 'alsoKnownAs' => array( $origin ) ) ), - ) - ); - - Move::handle_move( - array( - 'type' => 'Move', - 'actor' => $origin, - 'object' => $origin, - 'target' => $target, - ), - $this->user_id - ); - - \remove_filter( 'activitypub_pre_http_get_remote_object', $filter ); - - $followers = \get_post_meta( $id, Followers::FOLLOWER_META_KEY, false ); - $this->assertNotContains( (string) $this->user_id, $followers ); - $this->assertContains( (string) $this->user_id_2, $followers ); - } - /** * The shared inbox hands one Move to every recipient; it is handled once, on the shared hook. * @@ -1152,41 +996,6 @@ public function test_handle_move_skips_unknown_origin() { $this->assertSame( 0, $fetches ); } - /** - * A user who can no longer follow does not end up following the new actor without a Follow. - * - * @covers ::handle_move - */ - public function test_handle_move_does_not_keep_following_without_follow() { - $origin = 'https://old.example/users/mia'; - $target = 'https://new.example/users/mia'; - - $id = Remote_Actors::upsert( $this->actor( $origin ) ); - \add_post_meta( $id, Following::FOLLOWING_META_KEY, (string) $this->user_id ); - - $filter = $this->mock_remote( - array( - $origin => $this->actor( $origin, array( 'movedTo' => $target ) ), - $target => $this->actor( $target, array( 'alsoKnownAs' => array( $origin ) ) ), - ) - ); - - Move::handle_move( - array( - 'type' => 'Move', - 'actor' => $origin, - 'object' => $origin, - 'target' => $target, - ), - $this->user_id - ); - - \remove_filter( 'activitypub_pre_http_get_remote_object', $filter ); - - $this->assertNotContains( (string) $this->user_id, \get_post_meta( $id, Following::FOLLOWING_META_KEY, false ), 'The new server never got a Follow.' ); - $this->assertCount( 0, $this->get_follows( $this->user_id, $target ) ); - } - /** * A Move to a site-blocked target fetches nothing. * From ef758b6118568e5a2f834abe4049b720970de457 Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Wed, 11 Nov 2026 09:43:03 +0100 Subject: [PATCH 7/9] say what Remote_Actors::move does not carry over --- includes/collection/class-remote-actors.php | 1 + 1 file changed, 1 insertion(+) diff --git a/includes/collection/class-remote-actors.php b/includes/collection/class-remote-actors.php index 7d1de38c3e..71395b2c10 100644 --- a/includes/collection/class-remote-actors.php +++ b/includes/collection/class-remote-actors.php @@ -217,6 +217,7 @@ public static function delete( $post_id ) { * * Without a stored new actor, the old post becomes the new actor. Otherwise the followers, * comments and Reader posts of the old actor go to the new one, and the old post is deleted. + * The follows and blocks of our users do not carry over; use `Move::remote()` for that. * * @since unreleased * From f61652ed5bda309a9ae1868ffa4cfc527151bcdb Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Wed, 11 Nov 2026 10:41:18 +0100 Subject: [PATCH 8/9] always merge into the stored new actor, drop the swap --- includes/class-move.php | 30 ++-- includes/collection/class-remote-actors.php | 67 ++------- .../tests/includes/class-test-move.php | 40 +++-- .../collection/class-test-remote-actors.php | 137 +----------------- .../includes/handler/class-test-move.php | 13 +- 5 files changed, 72 insertions(+), 215 deletions(-) diff --git a/includes/class-move.php b/includes/class-move.php index abaa4c0e03..8d346a3141 100644 --- a/includes/class-move.php +++ b/includes/class-move.php @@ -232,25 +232,30 @@ public static function internally( $from, $to ) { * @return int|\WP_Error The post ID of the new actor, or WP_Error if it can not be stored. */ public static function remote( $origin, $actor ) { - // Read before the move, which deletes the old actor on a merge. + // Read before the merge, which deletes the old actor. $refollow = self::get_following_user_ids( $origin->ID ); $blocked_by = \get_post_meta( $origin->ID, Moderation::BLOCKED_ACTORS_META_KEY, false ); - $post_id = Remote_Actors::move( $origin, $actor ); + // Stored first, so a new actor that can not be stored (no inbox) leaves the old one as it was. + $post_id = Remote_Actors::upsert( $actor ); if ( \is_wp_error( $post_id ) ) { return $post_id; } - // After a swap the post keeps its follows and blocks; a merge brings them to another post. - if ( $post_id !== $origin->ID ) { - // Users who already follow the new actor keep that follow. - $refollow = \array_diff( $refollow, self::get_following_user_ids( $post_id ) ); + // A merge into the old actor itself would delete it. + if ( ! $post_id || $post_id === $origin->ID ) { + return new \WP_Error( 'activitypub_actor_not_moved', \__( 'The actor could not be moved.', 'activitypub' ) ); + } - // Through Blocked_Actors::add(), so the block hooks run and clean up the relationships. - foreach ( $blocked_by as $user_id ) { - Blocked_Actors::add( (int) $user_id, $actor['id'] ); - } + // Users who already follow the new actor keep that follow. + $refollow = \array_diff( $refollow, self::get_following_user_ids( $post_id ) ); + + Remote_Actors::merge( $origin->ID, $post_id ); + + // Through Blocked_Actors::add(), so the block hooks run and clean up the relationships. + foreach ( $blocked_by as $user_id ) { + Blocked_Actors::add( (int) $user_id, $actor['id'] ); } // A user who blocks the new actor neither follows it nor keeps it as a follower. @@ -260,11 +265,8 @@ public static function remote( $origin, $actor ) { } } + // A user who blocks the new actor does not follow it; follow() refuses users who can no longer follow. foreach ( $refollow as $user_id ) { - // After a swap the post still says the user follows; the new server does not know that yet. - Following::reject( $post_id, (int) $user_id ); - - // A user who blocks the new actor does not follow it; follow() refuses users who can no longer follow. if ( ! Moderation::is_actor_blocked( $actor['id'], (int) $user_id ) ) { Following::follow( $post_id, (int) $user_id ); } diff --git a/includes/collection/class-remote-actors.php b/includes/collection/class-remote-actors.php index 71395b2c10..53d34989a9 100644 --- a/includes/collection/class-remote-actors.php +++ b/includes/collection/class-remote-actors.php @@ -213,71 +213,30 @@ public static function delete( $post_id ) { } /** - * Move a stored remote actor to its new id. + * Merge a stored remote actor into another one. * - * Without a stored new actor, the old post becomes the new actor. Otherwise the followers, - * comments and Reader posts of the old actor go to the new one, and the old post is deleted. - * The follows and blocks of our users do not carry over; use `Move::remote()` for that. + * The followers, comments and Reader posts of the old actor go to the new one, and the old post + * is deleted. The follows and blocks of our users do not carry over; use `Move::remote()` for that. * * @since unreleased * - * @param \WP_Post $origin The stored old actor. - * @param array $actor The new actor, fetched and verified. + * @param int $from The post ID of the old actor. + * @param int $to The post ID of the new actor. * - * @return int|\WP_Error The post ID of the new actor, or WP_Error if it can not be stored. + * @return void */ - public static function move( $origin, $actor ) { + public static function merge( $from, $to ) { global $wpdb; - $target = self::get_by_uri( $actor['id'] ); - $merge = ! \is_wp_error( $target ); - - // A merge into the old actor itself would delete it. - if ( $merge && $target->ID === $origin->ID ) { - return new \WP_Error( 'activitypub_move_same_actor', \__( 'The actor can not move to itself.', 'activitypub' ) ); - } - - // Stored first, so a new actor that can not be stored (no inbox) leaves the old one as it was. - $post_id = self::update( $merge ? $target->ID : $origin->ID, $actor ); - - if ( \is_wp_error( $post_id ) ) { - return $post_id; - } - - if ( ! $post_id ) { - return new \WP_Error( 'activitypub_actor_not_stored', \__( 'The actor could not be stored.', 'activitypub' ) ); - } - - if ( ! $merge ) { - // wp_update_post() keeps the guid. Written in the form an insert stores, which every guid lookup matches. - list( $guid ) = get_stored_id_forms( $actor['id'] ); - $data = array( 'guid' => $guid ); - $hash = get_id_hash( $actor['id'] ); - if ( $hash ) { - $data['post_name'] = $hash; - } - - // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery - $updated = $wpdb->update( $wpdb->posts, $data, array( 'ID' => (int) $origin->ID ) ); - \clean_post_cache( $origin->ID ); - - // Under the old guid, a Follow would go to the old actor; the next Move finds the post again. - if ( false === $updated ) { - return new \WP_Error( 'activitypub_actor_not_moved', \__( 'The actor could not be moved.', 'activitypub' ) ); - } - - return $post_id; - } - - foreach ( \array_diff( self::get_follower_ids( $origin->ID ), self::get_follower_ids( $target->ID ) ) as $user_id ) { - \add_post_meta( $target->ID, Followers::FOLLOWER_META_KEY, $user_id ); + foreach ( \array_diff( self::get_follower_ids( $from ), self::get_follower_ids( $to ) ) as $user_id ) { + \add_post_meta( $to, Followers::FOLLOWER_META_KEY, $user_id ); } // Comments and Reader posts of the old actor belong to the new one now. // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.SlowDBQuery.slow_db_query_meta_key, WordPress.DB.SlowDBQuery.slow_db_query_meta_value $where = array( 'meta_key' => '_activitypub_remote_actor_id', - 'meta_value' => (string) $origin->ID, + 'meta_value' => (string) $from, ); // The meta type and the cache group of its queries, which core names differently. @@ -291,7 +250,7 @@ public static function move( $origin, $actor ) { $ids = $wpdb->get_col( $wpdb->prepare( 'SELECT %i FROM %i WHERE meta_key = %s AND meta_value = %s', \sprintf( '%s_id', $type ), $table, $where['meta_key'], $where['meta_value'] ) ); if ( $ids ) { - $wpdb->update( $table, array( 'meta_value' => (string) $target->ID ), $where ); + $wpdb->update( $table, array( 'meta_value' => (string) $to ), $where ); \wp_cache_delete_multiple( $ids, \sprintf( '%s_meta', $type ) ); \wp_cache_set_last_changed( $cache_group ); } @@ -299,9 +258,7 @@ public static function move( $origin, $actor ) { // phpcs:enable // Clears the inbox caches of the followers, which now include the new actor. - self::delete( $origin->ID ); - - return $post_id; + self::delete( $from ); } /** diff --git a/tests/phpunit/tests/includes/class-test-move.php b/tests/phpunit/tests/includes/class-test-move.php index dd6c3790a7..d0eb585feb 100644 --- a/tests/phpunit/tests/includes/class-test-move.php +++ b/tests/phpunit/tests/includes/class-test-move.php @@ -637,7 +637,7 @@ public function test_change_domain_with_valid_input() { } /** - * Our users who follow the old actor follow the new one, which the old post becomes. + * Our users who follow the old actor follow the new one, which is stored as its own post. * * @covers ::remote */ @@ -650,11 +650,12 @@ public function test_remote_follows_new_actor() { $id = Remote_Actors::upsert( $this->remote_actor( $origin ) ); \add_post_meta( $id, Following::FOLLOWING_META_KEY, (string) $user_id ); - $this->assertSame( $id, Move::remote( \get_post( $id ), $this->remote_actor( $target ) ) ); + $post_id = Move::remote( \get_post( $id ), $this->remote_actor( $target ) ); - $this->assertSame( $id, Remote_Actors::get_by_uri( $target )->ID, 'The stored actor must be moved in place.' ); - $this->assertNotContains( (string) $user_id, \get_post_meta( $id, Following::FOLLOWING_META_KEY, false ), 'The new server has not accepted yet.' ); - $this->assertContains( (string) $user_id, \get_post_meta( $id, Following::PENDING_META_KEY, false ) ); + $this->assertSame( Remote_Actors::get_by_uri( $target )->ID, $post_id ); + $this->assertNull( \get_post( $id ), 'The old actor must be merged away.' ); + $this->assertNotContains( (string) $user_id, \get_post_meta( $post_id, Following::FOLLOWING_META_KEY, false ), 'The new server has not accepted yet.' ); + $this->assertContains( (string) $user_id, \get_post_meta( $post_id, Following::PENDING_META_KEY, false ) ); $this->assertCount( 1, $this->get_follows( $user_id, $target ), 'A Follow must be sent to the new actor.' ); } @@ -699,10 +700,10 @@ public function test_remote_does_not_follow_blocked_actor() { $id = Remote_Actors::upsert( $this->remote_actor( $origin ) ); \add_post_meta( $id, Following::FOLLOWING_META_KEY, (string) $user_id ); - Move::remote( \get_post( $id ), $this->remote_actor( $target ) ); + $post_id = Move::remote( \get_post( $id ), $this->remote_actor( $target ) ); - $this->assertEmpty( \get_post_meta( $id, Following::FOLLOWING_META_KEY, false ) ); - $this->assertEmpty( \get_post_meta( $id, Following::PENDING_META_KEY, false ) ); + $this->assertEmpty( \get_post_meta( $post_id, Following::FOLLOWING_META_KEY, false ) ); + $this->assertEmpty( \get_post_meta( $post_id, Following::PENDING_META_KEY, false ) ); $this->assertCount( 0, $this->get_follows( $user_id, $target ) ); } @@ -722,9 +723,9 @@ public function test_remote_drops_follower_blocked_by_user() { \add_post_meta( $id, Followers::FOLLOWER_META_KEY, (string) $blocker ); \add_post_meta( $id, Followers::FOLLOWER_META_KEY, (string) $user_id ); - Move::remote( \get_post( $id ), $this->remote_actor( $target ) ); + $post_id = Move::remote( \get_post( $id ), $this->remote_actor( $target ) ); - $followers = \get_post_meta( $id, Followers::FOLLOWER_META_KEY, false ); + $followers = \get_post_meta( $post_id, Followers::FOLLOWER_META_KEY, false ); $this->assertNotContains( (string) $blocker, $followers ); $this->assertContains( (string) $user_id, $followers ); } @@ -742,12 +743,27 @@ public function test_remote_does_not_keep_following_without_follow() { $id = Remote_Actors::upsert( $this->remote_actor( $origin ) ); \add_post_meta( $id, Following::FOLLOWING_META_KEY, (string) $user_id ); - Move::remote( \get_post( $id ), $this->remote_actor( $target ) ); + $post_id = Move::remote( \get_post( $id ), $this->remote_actor( $target ) ); - $this->assertNotContains( (string) $user_id, \get_post_meta( $id, Following::FOLLOWING_META_KEY, false ), 'The new server never got a Follow.' ); + $this->assertNotContains( (string) $user_id, \get_post_meta( $post_id, Following::FOLLOWING_META_KEY, false ), 'The new server never got a Follow.' ); $this->assertCount( 0, $this->get_follows( $user_id, $target ) ); } + /** + * A move to the old actor itself keeps it, instead of merging it into itself and deleting it. + * + * @covers ::remote + */ + public function test_remote_to_itself_keeps_actor() { + $origin = 'https://old.example/users/cid'; + $id = Remote_Actors::upsert( $this->remote_actor( $origin ) ); + \add_post_meta( $id, Followers::FOLLOWER_META_KEY, '1' ); + + $this->assertWPError( Move::remote( \get_post( $id ), $this->remote_actor( $origin ) ) ); + $this->assertSame( $id, Remote_Actors::get_by_uri( $origin )->ID ); + $this->assertSame( array( '1' ), \get_post_meta( $id, Followers::FOLLOWER_META_KEY, false ) ); + } + /** * Build a remote actor document. * diff --git a/tests/phpunit/tests/includes/collection/class-test-remote-actors.php b/tests/phpunit/tests/includes/collection/class-test-remote-actors.php index bff85e1f84..8d232cb910 100644 --- a/tests/phpunit/tests/includes/collection/class-test-remote-actors.php +++ b/tests/phpunit/tests/includes/collection/class-test-remote-actors.php @@ -2806,155 +2806,34 @@ public function test_fetch_by_acct_ignores_case( $stored, $requested ) { } /** - * Without a stored new actor, the old post becomes the new actor, found by its new id after an update. + * Followers, comments and Reader posts of the old actor go to the new one, and the old one is deleted. * - * @covers ::move + * @covers ::merge */ - public function test_move_swaps_origin() { - $origin = 'https://old.example.com/users/ann'; - $target = 'https://new.example.com/users/ann?a=1&b=2'; - + public function test_merge() { $origin_id = Remote_Actors::upsert( array( - 'id' => $origin, - 'type' => 'Person', - 'inbox' => $origin . '/inbox', - ) - ); - \add_post_meta( $origin_id, Followers::FOLLOWER_META_KEY, '1' ); - - $post_id = Remote_Actors::move( - \get_post( $origin_id ), - array( - 'id' => $target, - 'type' => 'Person', - 'inbox' => 'https://new.example.com/inbox', - ) - ); - - $this->assertSame( $origin_id, $post_id ); - $this->assertSame( $origin_id, Remote_Actors::get_by_uri( $target )->ID ); - $this->assertWPError( Remote_Actors::get_by_uri( $origin ) ); - $this->assertSame( 'https://new.example.com/inbox', \get_post_meta( $origin_id, '_activitypub_inbox', true ) ); - $this->assertSame( array( '1' ), \get_post_meta( $origin_id, Followers::FOLLOWER_META_KEY, false ), 'The followers stay on the post.' ); - - // A later update rewrites the guid; the lookup still finds it. - Remote_Actors::update( - $origin_id, - array( - 'id' => $target, + 'id' => 'https://old.example.com/users/ben', 'type' => 'Person', - 'inbox' => 'https://new.example.com/inbox', - ) - ); - $this->assertSame( $origin_id, Remote_Actors::get_by_uri( $target )->ID ); - } - - /** - * With a stored new actor, followers, comments and Reader posts of the old actor go to it, and the old one is deleted. - * - * @covers ::move - */ - public function test_move_merges_into_target() { - $origin = 'https://old.example.com/users/ben'; - $target = 'https://new.example.com/users/ben'; - - $origin_id = Remote_Actors::upsert( - array( - 'id' => $origin, - 'type' => 'Person', - 'inbox' => $origin . '/inbox', + 'inbox' => 'https://old.example.com/users/ben/inbox', ) ); $target_id = Remote_Actors::upsert( array( - 'id' => $target, + 'id' => 'https://new.example.com/users/ben', 'type' => 'Person', - 'inbox' => $target . '/old-inbox', + 'inbox' => 'https://new.example.com/users/ben/inbox', ) ); \add_post_meta( $origin_id, Followers::FOLLOWER_META_KEY, '1' ); $comment_id = self::factory()->comment->create( array( 'comment_meta' => array( '_activitypub_remote_actor_id' => (string) $origin_id ) ) ); $reader_id = self::factory()->post->create( array( 'meta_input' => array( '_activitypub_remote_actor_id' => (string) $origin_id ) ) ); - $post_id = Remote_Actors::move( - \get_post( $origin_id ), - array( - 'id' => $target, - 'type' => 'Person', - 'inbox' => $target . '/inbox', - ) - ); + Remote_Actors::merge( $origin_id, $target_id ); - $this->assertSame( $target_id, $post_id ); $this->assertNull( \get_post( $origin_id ), 'The old actor must be deleted.' ); - $this->assertSame( $target . '/inbox', \get_post_meta( $target_id, '_activitypub_inbox', true ), 'The new actor is stored fresh.' ); $this->assertSame( array( '1' ), \get_post_meta( $target_id, Followers::FOLLOWER_META_KEY, false ) ); $this->assertSame( (string) $target_id, \get_comment_meta( $comment_id, '_activitypub_remote_actor_id', true ) ); $this->assertSame( (string) $target_id, \get_post_meta( $reader_id, '_activitypub_remote_actor_id', true ) ); } - - /** - * A swap whose guid write fails returns an error, so no Follow goes out under the old guid. - * - * @covers ::move - */ - public function test_move_fails_when_guid_write_fails() { - global $wpdb; - - $origin = 'https://old.example.com/users/dan'; - $target = 'https://new.example.com/users/dan'; - - $origin_id = Remote_Actors::upsert( - array( - 'id' => $origin, - 'type' => 'Person', - 'inbox' => $origin . '/inbox', - ) - ); - - // Break only the direct guid write; wp_update_post() writes the guid among all other fields. - $break = static function ( $query ) { - return false !== \strpos( $query, 'SET `guid`' ) ? 'UPDATE broken' : $query; - }; - \add_filter( 'query', $break ); - $suppress = $wpdb->suppress_errors(); - - $result = Remote_Actors::move( - \get_post( $origin_id ), - array( - 'id' => $target, - 'type' => 'Person', - 'inbox' => $target . '/inbox', - ) - ); - - $wpdb->suppress_errors( $suppress ); - \remove_filter( 'query', $break ); - - $this->assertWPError( $result ); - $this->assertWPError( Remote_Actors::get_by_uri( $target ) ); - $this->assertSame( $origin_id, Remote_Actors::get_by_uri( $origin )->ID, 'The post keeps the old guid, so the next Move finds it.' ); - } - - /** - * A move to the old actor itself keeps it, instead of merging it into itself and deleting it. - * - * @covers ::move - */ - public function test_move_to_itself_keeps_actor() { - $id = 'https://old.example.com/users/cid'; - $actor = array( - 'id' => $id, - 'type' => 'Person', - 'inbox' => $id . '/inbox', - ); - - $post_id = Remote_Actors::upsert( $actor ); - \add_post_meta( $post_id, Followers::FOLLOWER_META_KEY, '1' ); - - $this->assertWPError( Remote_Actors::move( \get_post( $post_id ), $actor ) ); - $this->assertSame( $post_id, Remote_Actors::get_by_uri( $id )->ID ); - $this->assertSame( array( '1' ), \get_post_meta( $post_id, Followers::FOLLOWER_META_KEY, false ) ); - } } diff --git a/tests/phpunit/tests/includes/handler/class-test-move.php b/tests/phpunit/tests/includes/handler/class-test-move.php index 5d2c160678..738cb5b928 100644 --- a/tests/phpunit/tests/includes/handler/class-test-move.php +++ b/tests/phpunit/tests/includes/handler/class-test-move.php @@ -674,7 +674,8 @@ public function test_handle_move_pull_mode() { \remove_filter( 'activitypub_pre_http_get_remote_object', $filter ); - $this->assertSame( $id, Remote_Actors::get_by_uri( $target )->ID ); + $this->assertNotWPError( Remote_Actors::get_by_uri( $target ) ); + $this->assertNull( \get_post( $id ), 'The old actor must be merged away.' ); } /** @@ -844,8 +845,9 @@ public function test_handle_move_target_with_query_string() { \remove_filter( 'activitypub_pre_http_get_remote_object', $filter ); - $this->assertSame( $id, Remote_Actors::get_by_uri( $target )->ID, 'Only the moved post may hold the new id.' ); - $this->assertContains( (string) $this->user_id, \get_post_meta( $id, Following::PENDING_META_KEY, false ) ); + $moved = Remote_Actors::get_by_uri( $target ); + $this->assertNull( \get_post( $id ), 'The old actor must be merged away.' ); + $this->assertContains( (string) $this->user_id, \get_post_meta( $moved->ID, Following::PENDING_META_KEY, false ) ); $actors = \get_posts( array( @@ -855,7 +857,7 @@ public function test_handle_move_target_with_query_string() { 'numberposts' => -1, ) ); - $this->assertSame( array( $id ), $actors, 'No second actor may be created.' ); + $this->assertSame( array( $moved->ID ), $actors, 'Only the new actor may be left.' ); } /** @@ -965,7 +967,8 @@ public function test_handle_move_accepts_group() { \remove_filter( 'activitypub_pre_http_get_remote_object', $filter ); - $this->assertSame( $id, Remote_Actors::get_by_uri( $target )->ID ); + $this->assertNotWPError( Remote_Actors::get_by_uri( $target ) ); + $this->assertNull( \get_post( $id ), 'The old actor must be merged away.' ); } /** From 011e6e47d4afcca0986f86c10f63cd3b4441d2a9 Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Wed, 11 Nov 2026 16:04:58 +0100 Subject: [PATCH 9/9] keep the block on the old actor after a move --- includes/class-move.php | 9 +++++++++ tests/phpunit/tests/includes/class-test-move.php | 3 ++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/includes/class-move.php b/includes/class-move.php index 8d346a3141..aec4a05729 100644 --- a/includes/class-move.php +++ b/includes/class-move.php @@ -19,6 +19,7 @@ use Activitypub\Scheduler\Actor as Actor_Scheduler; use function Activitypub\is_actor; +use function Activitypub\unescape_guid; /** * ActivityPub (Account) Move Class @@ -235,6 +236,8 @@ public static function remote( $origin, $actor ) { // Read before the merge, which deletes the old actor. $refollow = self::get_following_user_ids( $origin->ID ); $blocked_by = \get_post_meta( $origin->ID, Moderation::BLOCKED_ACTORS_META_KEY, false ); + $origin_uri = unescape_guid( $origin->guid ); + $old_actor = $blocked_by ? Remote_Actors::get_actor( $origin ) : null; // Stored first, so a new actor that can not be stored (no inbox) leaves the old one as it was. $post_id = Remote_Actors::upsert( $actor ); @@ -253,9 +256,15 @@ public static function remote( $origin, $actor ) { Remote_Actors::merge( $origin->ID, $post_id ); + // The old account stays reachable, so it keeps its blocks. Stored again from our copy, without a fetch. + if ( $old_actor && ! \is_wp_error( $old_actor ) ) { + Remote_Actors::upsert( $old_actor ); + } + // Through Blocked_Actors::add(), so the block hooks run and clean up the relationships. foreach ( $blocked_by as $user_id ) { Blocked_Actors::add( (int) $user_id, $actor['id'] ); + Blocked_Actors::add( (int) $user_id, $origin_uri ); } // A user who blocks the new actor neither follows it nor keeps it as a follower. diff --git a/tests/phpunit/tests/includes/class-test-move.php b/tests/phpunit/tests/includes/class-test-move.php index d0eb585feb..6c9c107664 100644 --- a/tests/phpunit/tests/includes/class-test-move.php +++ b/tests/phpunit/tests/includes/class-test-move.php @@ -678,9 +678,10 @@ public function test_remote_merge_keeps_blocks_and_follows() { $this->assertSame( $target_id, Move::remote( \get_post( $origin_id ), $this->remote_actor( $target ) ) ); - $this->assertWPError( Remote_Actors::get_by_uri( $origin ), 'The old actor must be merged away.' ); + $this->assertNull( \get_post( $origin_id ), 'The old actor must be merged away.' ); $this->assertContains( (string) $blocker, \get_post_meta( $target_id, Moderation::BLOCKED_ACTORS_META_KEY, false ), 'The block must carry over.' ); $this->assertTrue( Moderation::is_actor_blocked( $target, $blocker ) ); + $this->assertTrue( Moderation::is_actor_blocked( $origin, $blocker ), 'The old actor must stay blocked.' ); $this->assertContains( (string) $user_id, \get_post_meta( $target_id, Following::PENDING_META_KEY, false ) ); $this->assertCount( 1, $this->get_follows( $user_id, $target ) ); }