| @@ -6,11 +6,10 @@ | ||
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | namespace Activitypub\Handler; |
| 9 | 9 | |
| 10 | +use Activitypub\Http; | |
| 10 | 11 | use Activitypub\Collection\Followers; |
| 11 | -use Activitypub\Collection\Remote_Actors; | |
| 12 | -use Activitypub\Http; | |
| 13 | 12 | |
| 14 | 13 | use function Activitypub\object_to_uri; |
| 15 | 14 | |
| 16 | 15 | /** |
| @@ -24,83 +23,100 @@ | ||
| 24 | 23 | /** |
| 25 | 24 | * Initialize the class, registering WordPress hooks. |
| 26 | 25 | */ |
| 27 | 26 | public static function init() { |
| 28 | - \add_action( 'activitypub_inbox_move', array( self::class, 'handle_move' ), 10, 2 ); | |
| 27 | + \add_action( 'activitypub_inbox_move', array( self::class, 'handle_move' ) ); | |
| 28 | + \add_filter( 'activitypub_get_outbox_activity', array( self::class, 'outbox_activity' ) ); | |
| 29 | 29 | } |
| 30 | 30 | |
| 31 | 31 | /** |
| 32 | 32 | * Handle Move requests. |
| 33 | 33 | * |
| 34 | - * @param array $activity The JSON "Move" Activity. | |
| 35 | - * @param int|int[] $user_ids The user ID(s). | |
| 34 | + * @param array $activity The JSON "Move" Activity. | |
| 36 | 35 | */ |
| 37 | - public static function handle_move( $activity, $user_ids ) { | |
| 38 | - $target_uri = self::extract_target( $activity ); | |
| 39 | - $origin_uri = self::extract_origin( $activity ); | |
| 36 | + public static function handle_move( $activity ) { | |
| 37 | + $target = self::extract_target( $activity ); | |
| 38 | + $origin = self::extract_origin( $activity ); | |
| 40 | 39 | |
| 41 | - if ( ! $target_uri || ! $origin_uri ) { | |
| 40 | + if ( ! $target || ! $origin ) { | |
| 42 | 41 | return; |
| 43 | 42 | } |
| 44 | 43 | |
| 45 | - $target_json = Http::get_remote_object( $target_uri ); | |
| 46 | - $origin_json = Http::get_remote_object( $origin_uri ); | |
| 44 | + $target_object = Http::get_remote_object( $target ); | |
| 45 | + $origin_object = Http::get_remote_object( $origin ); | |
| 47 | 46 | |
| 48 | - $verified = self::verify_move( $target_json, $origin_json ); | |
| 47 | + $verified = self::verify_move( $target_object, $origin_object ); | |
| 49 | 48 | |
| 50 | 49 | if ( ! $verified ) { |
| 51 | 50 | return; |
| 52 | 51 | } |
| 53 | 52 | |
| 54 | - $target_object = Remote_Actors::get_by_uri( $target_uri ); | |
| 55 | - $origin_object = Remote_Actors::get_by_uri( $origin_uri ); | |
| 56 | - $result = null; | |
| 57 | - $success = false; | |
| 53 | + $target_follower = Followers::get_follower_by_actor( $target ); | |
| 54 | + $origin_follower = Followers::get_follower_by_actor( $origin ); | |
| 58 | 55 | |
| 59 | - // If the origin is followed but the target is not, update the origin to point to the target. | |
| 60 | - if ( \is_wp_error( $target_object ) && ! \is_wp_error( $origin_object ) ) { | |
| 56 | + /* | |
| 57 | + * If the new target is followed, but the origin is not, | |
| 58 | + * everything is fine, so we can return. | |
| 59 | + */ | |
| 60 | + if ( $target_follower && ! $origin_follower ) { | |
| 61 | + return; | |
| 62 | + } | |
| 63 | + | |
| 64 | + /* | |
| 65 | + * If the new target is not followed, but the origin is, | |
| 66 | + * update the origin follower to the new target. | |
| 67 | + */ | |
| 68 | + if ( ! $target_follower && $origin_follower ) { | |
| 69 | + $origin_follower->from_array( $target_object ); | |
| 70 | + $origin_follower->set_id( $target ); | |
| 71 | + $origin_id = $origin_follower->upsert(); | |
| 72 | + | |
| 61 | 73 | global $wpdb; |
| 62 | 74 | // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery |
| 63 | 75 | $wpdb->update( |
| 64 | 76 | $wpdb->posts, |
| 65 | - array( 'guid' => \sanitize_url( $target_uri ) ), | |
| 66 | - array( 'ID' => \sanitize_key( $origin_object->ID ) ) | |
| 77 | + array( 'guid' => sanitize_url( $target ) ), | |
| 78 | + array( 'ID' => sanitize_key( $origin_id ) ) | |
| 67 | 79 | ); |
| 68 | 80 | |
| 69 | 81 | // Clear the cache. |
| 70 | - \wp_cache_delete( $origin_object->ID, 'posts' ); | |
| 71 | - | |
| 72 | - $success = true; | |
| 73 | - | |
| 74 | - // get_remote_object() already self-confirmed the target, so it is safe to cache. | |
| 75 | - $result = Remote_Actors::upsert( $target_json ); | |
| 82 | + wp_cache_delete( $origin_id, 'posts' ); | |
| 83 | + return; | |
| 76 | 84 | } |
| 77 | 85 | |
| 78 | - // If both the target and origin are followed, merge them. | |
| 79 | - if ( ! \is_wp_error( $target_object ) && ! \is_wp_error( $origin_object ) ) { | |
| 80 | - $origin_users = \get_post_meta( $origin_object->ID, Followers::FOLLOWER_META_KEY, false ); | |
| 81 | - $target_users = \get_post_meta( $target_object->ID, Followers::FOLLOWER_META_KEY, false ); | |
| 86 | + /* | |
| 87 | + * If the new target is followed, and the origin is followed, | |
| 88 | + * move users and delete the origin follower. | |
| 89 | + */ | |
| 90 | + if ( $target_follower && $origin_follower ) { | |
| 91 | + $origin_users = \get_post_meta( $origin_follower->get__id(), '_activitypub_user_id', false ); | |
| 92 | + $target_users = \get_post_meta( $target_follower->get__id(), '_activitypub_user_id', false ); | |
| 82 | 93 | |
| 83 | 94 | // Get all user ids from $origin_users that are not in $target_users. |
| 84 | 95 | $users = \array_diff( $origin_users, $target_users ); |
| 85 | 96 | |
| 86 | - foreach ( $users as $follower_user_id ) { | |
| 87 | - \add_post_meta( $target_object->ID, Followers::FOLLOWER_META_KEY, $follower_user_id ); | |
| 97 | + foreach ( $users as $user_id ) { | |
| 98 | + \add_post_meta( $target_follower->get__id(), '_activitypub_user_id', $user_id ); | |
| 88 | 99 | } |
| 89 | 100 | |
| 90 | - $success = true; | |
| 91 | - $result = \wp_delete_post( $origin_object->ID ); | |
| 101 | + $origin_follower->delete(); | |
| 92 | 102 | } |
| 103 | + } | |
| 93 | 104 | |
| 94 | - /** | |
| 95 | - * Fires after an ActivityPub Move activity has been handled. | |
| 96 | - * | |
| 97 | - * @param array $activity The ActivityPub activity data. | |
| 98 | - * @param int[] $user_ids The local user IDs. | |
| 99 | - * @param bool $success True on success, false otherwise. | |
| 100 | - * @param mixed $result The result of the operation (e.g., post ID, WP_Error, or status). | |
| 101 | - */ | |
| 102 | - \do_action( 'activitypub_handled_move', $activity, (array) $user_ids, $success, $result ); | |
| 105 | + /** | |
| 106 | + * Convert the object and origin to the correct format. | |
| 107 | + * | |
| 108 | + * @param \Activitypub\Activity\Activity $activity The Activity object. | |
| 109 | + * @return \Activitypub\Activity\Activity The filtered Activity object. | |
| 110 | + */ | |
| 111 | + public static function outbox_activity( $activity ) { | |
| 112 | + if ( 'Move' === $activity->get_type() ) { | |
| 113 | + $activity->set_object( object_to_uri( $activity->get_object() ) ); | |
| 114 | + $activity->set_origin( $activity->get_actor() ); | |
| 115 | + $activity->set_target( $activity->get_object() ); | |
| 116 | + } | |
| 117 | + | |
| 118 | + return $activity; | |
| 103 | 119 | } |
| 104 | 120 | |
| 105 | 121 | /** |
| 106 | 122 | * Extract the target from the activity. |
| @@ -171,25 +187,15 @@ | ||
| 171 | 187 | if ( $target_object['id'] === $origin_object['id'] ) { |
| 172 | 188 | return false; |
| 173 | 189 | } |
| 174 | 190 | |
| 175 | - // Normalize alsoKnownAs to an array (some JSON-LD payloads may use a string). | |
| 176 | - $also_known_as = (array) ( $target_object['alsoKnownAs'] ?? array() ); | |
| 177 | - if ( empty( $also_known_as ) ) { | |
| 191 | + // Check if the target has an alsoKnownAs property. | |
| 192 | + if ( empty( $target_object['also_known_as'] ) ) { | |
| 178 | 193 | return false; |
| 179 | 194 | } |
| 180 | 195 | |
| 181 | - // Collect all possible origin identifiers (id, url, webfinger). | |
| 182 | - $origin_ids = \array_filter( | |
| 183 | - array( | |
| 184 | - $origin_object['id'] ?? null, | |
| 185 | - $origin_object['url'] ?? null, | |
| 186 | - $origin_object['webfinger'] ?? null, | |
| 187 | - ) | |
| 188 | - ); | |
| 189 | - | |
| 190 | - // Check if any origin identifier is in the alsoKnownAs property of the target. | |
| 191 | - if ( ! \array_intersect( $origin_ids, $also_known_as ) ) { | |
| 196 | + // Check if the origin is in the alsoKnownAs property of the target. | |
| 197 | + if ( ! in_array( $origin_object['id'], $target_object['also_known_as'], true ) ) { | |
| 192 | 198 | return false; |
| 193 | 199 | } |
| 194 | 200 | |
| 195 | 201 | // Check if the origin has a movedTo property. |