| @@ -6,15 +6,12 @@ | ||
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | namespace Activitypub\Handler; |
| 9 | 9 | |
| 10 | +use Activitypub\Collection\Followers; | |
| 10 | 11 | use Activitypub\Collection\Interactions; |
| 11 | -use Activitypub\Collection\Remote_Actors; | |
| 12 | -use Activitypub\Collection\Remote_Posts; | |
| 13 | -use Activitypub\Http; | |
| 14 | 12 | |
| 15 | -use function Activitypub\is_activity_reply; | |
| 16 | -use function Activitypub\object_to_uri; | |
| 13 | +use function Activitypub\get_remote_metadata_by_actor; | |
| 17 | 14 | |
| 18 | 15 | /** |
| 19 | 16 | * Handle Update requests. |
| 20 | 17 | */ |
| @@ -22,20 +19,21 @@ | ||
| 22 | 19 | /** |
| 23 | 20 | * Initialize the class, registering WordPress hooks. |
| 24 | 21 | */ |
| 25 | 22 | public static function init() { |
| 26 | - \add_action( 'activitypub_handled_inbox_update', array( self::class, 'handle_update' ), 10, 3 ); | |
| 23 | + \add_action( | |
| 24 | + 'activitypub_inbox_update', | |
| 25 | + array( self::class, 'handle_update' ) | |
| 26 | + ); | |
| 27 | 27 | } |
| 28 | 28 | |
| 29 | 29 | /** |
| 30 | 30 | * Handle "Update" requests. |
| 31 | 31 | * |
| 32 | - * @param array $activity The Activity object. | |
| 33 | - * @param int[] $user_ids Local recipient user IDs (followers and addressed local actors); may be empty. | |
| 34 | - * @param \Activitypub\Activity\Activity $activity_object The activity object. Default null. | |
| 32 | + * @param array $activity The Activity object. | |
| 35 | 33 | */ |
| 36 | - public static function handle_update( $activity, $user_ids, $activity_object ) { | |
| 37 | - $object_type = $activity['object']['type'] ?? ''; | |
| 34 | + public static function handle_update( $activity ) { | |
| 35 | + $object_type = isset( $activity['object']['type'] ) ? $activity['object']['type'] : ''; | |
| 38 | 36 | |
| 39 | 37 | switch ( $object_type ) { |
| 40 | 38 | /* |
| 41 | 39 | * Actor Types. |
| @@ -46,9 +44,9 @@ | ||
| 46 | 44 | case 'Group': |
| 47 | 45 | case 'Organization': |
| 48 | 46 | case 'Service': |
| 49 | 47 | case 'Application': |
| 50 | - self::update_actor( $activity, $user_ids ); | |
| 48 | + self::update_actor( $activity ); | |
| 51 | 49 | break; |
| 52 | 50 | |
| 53 | 51 | /* |
| 54 | 52 | * Object and Link Types. |
| @@ -61,9 +59,9 @@ | ||
| 61 | 59 | case 'Audio': |
| 62 | 60 | case 'Video': |
| 63 | 61 | case 'Event': |
| 64 | 62 | case 'Document': |
| 65 | - self::update_object( $activity, $user_ids, $activity_object ); | |
| 63 | + self::update_interaction( $activity ); | |
| 66 | 64 | break; |
| 67 | 65 | |
| 68 | 66 | /* |
| 69 | 67 | * Minimal Activity. |
| @@ -75,105 +73,53 @@ | ||
| 75 | 73 | } |
| 76 | 74 | } |
| 77 | 75 | |
| 78 | 76 | /** |
| 79 | - * Update an Object. | |
| 77 | + * Update an Interaction. | |
| 80 | 78 | * |
| 81 | - * @param array $activity The Activity object. | |
| 82 | - * @param int[]|null $user_ids Local recipient user IDs (followers and addressed local actors); may be empty. | |
| 83 | - * @param \Activitypub\Activity\Activity $activity_object The activity object. Default null. | |
| 79 | + * @param array $activity The Activity object. | |
| 84 | 80 | */ |
| 85 | - public static function update_object( $activity, $user_ids, $activity_object ) { | |
| 86 | - $result = new \WP_Error( 'activitypub_update_failed', 'Update failed' ); | |
| 87 | - $updated = true; | |
| 81 | + public static function update_interaction( $activity ) { | |
| 82 | + $commentdata = Interactions::update_comment( $activity ); | |
| 83 | + $reaction = null; | |
| 88 | 84 | |
| 89 | - // Check for private and/or direct messages. | |
| 90 | - if ( is_activity_reply( $activity ) ) { | |
| 91 | - $comment_data = Interactions::update_comment( $activity ); | |
| 92 | - | |
| 93 | - if ( false === $comment_data ) { | |
| 94 | - $updated = false; | |
| 95 | - } elseif ( \is_wp_error( $comment_data ) ) { | |
| 96 | - // Handled but rejected (e.g. a foreign actor): keep the failure so the | |
| 97 | - // success flag stays false and the Create fallback is not triggered. | |
| 98 | - $result = $comment_data; | |
| 99 | - } elseif ( ! empty( $comment_data['comment_ID'] ) ) { | |
| 100 | - $result = \get_comment( $comment_data['comment_ID'] ); | |
| 101 | - } | |
| 102 | - } elseif ( \get_option( 'activitypub_create_posts', false ) ) { | |
| 103 | - $result = Remote_Posts::update( $activity, $user_ids ); | |
| 104 | - | |
| 105 | - if ( \is_wp_error( $result ) && 'activitypub_post_not_found' === $result->get_error_code() ) { | |
| 106 | - $updated = false; | |
| 107 | - } | |
| 85 | + if ( ! empty( $commentdata['comment_ID'] ) ) { | |
| 86 | + $state = 1; | |
| 87 | + $reaction = \get_comment( $commentdata['comment_ID'] ); | |
| 88 | + } else { | |
| 89 | + $state = $commentdata; | |
| 108 | 90 | } |
| 109 | 91 | |
| 110 | - // There is no object to update, try to trigger create instead. | |
| 111 | - if ( ! $updated ) { | |
| 112 | - return Create::handle_create( $activity, $user_ids, $activity_object ); | |
| 113 | - } | |
| 114 | - | |
| 115 | - $success = ( $result && ! \is_wp_error( $result ) ); | |
| 116 | - | |
| 117 | 92 | /** |
| 118 | - * Fires after an ActivityPub Update activity has been handled. | |
| 93 | + * Fires after an Update activity has been handled. | |
| 119 | 94 | * |
| 120 | - * @param array $activity The ActivityPub activity data. | |
| 121 | - * @param int[]|null $user_ids The local user IDs. | |
| 122 | - * @param bool $success True on success, false otherwise. | |
| 123 | - * @param \WP_Comment|\WP_Post|\WP_Error $result The updated post, comment, or error. | |
| 95 | + * @param array $activity The complete Update activity data. | |
| 96 | + * @param null $user Always null for Update activities. | |
| 97 | + * @param int|array $state 1 if comment was updated successfully, error data otherwise. | |
| 98 | + * @param \WP_Comment|null $reaction The updated comment object if successful, null otherwise. | |
| 124 | 99 | */ |
| 125 | - \do_action( 'activitypub_handled_update', $activity, (array) $user_ids, $success, $result ); | |
| 100 | + \do_action( 'activitypub_handled_update', $activity, null, $state, $reaction ); | |
| 126 | 101 | } |
| 127 | 102 | |
| 128 | 103 | /** |
| 129 | 104 | * Update an Actor. |
| 130 | 105 | * |
| 131 | - * @param array $activity The Activity object. | |
| 132 | - * @param int[]|null $user_ids Local recipient user IDs (followers and addressed local actors); may be empty. | |
| 106 | + * @param array $activity The Activity object. | |
| 133 | 107 | */ |
| 134 | - public static function update_actor( $activity, $user_ids ) { | |
| 135 | - /* | |
| 136 | - * Prefer the actor data embedded in the activity object, as it contains | |
| 137 | - * the fresh data sent by the remote server. | |
| 138 | - */ | |
| 139 | - $actor = $activity['object'] ?? null; | |
| 108 | + public static function update_actor( $activity ) { | |
| 109 | + // Update cache. | |
| 110 | + $actor = get_remote_metadata_by_actor( $activity['actor'], false ); | |
| 140 | 111 | |
| 141 | - /* | |
| 142 | - * The object may be a string IRI instead of an embedded object, | |
| 143 | - * in which case we need to fetch the actor data remotely. | |
| 144 | - * We use Http::get_remote_object() directly instead of | |
| 145 | - * get_remote_metadata_by_actor() because the latter returns the | |
| 146 | - * stale locally cached copy via fetch_by_uri(). | |
| 147 | - */ | |
| 148 | - if ( ! \is_array( $actor ) || ! isset( $actor['id'] ) ) { | |
| 149 | - $object = Http::get_remote_object( $activity['actor'], false ); | |
| 112 | + if ( ! $actor || \is_wp_error( $actor ) || ! isset( $actor['id'] ) ) { | |
| 113 | + return; | |
| 114 | + } | |
| 150 | 115 | |
| 151 | - if ( ! \is_wp_error( $object ) && \is_array( $object ) ) { | |
| 152 | - $actor = $object; | |
| 153 | - } | |
| 154 | - } | |
| 116 | + $follower = Followers::get_follower_by_actor( $actor['id'] ); | |
| 155 | 117 | |
| 156 | - /* | |
| 157 | - * An actor may only update itself. Bind the updated object to the activity | |
| 158 | - * actor (the same constraint the Delete handler enforces) so a remote server | |
| 159 | - * cannot overwrite another host's cached actor by sending an Update whose | |
| 160 | - * object.id points at a victim actor. | |
| 161 | - */ | |
| 162 | - if ( \is_array( $actor ) && isset( $actor['id'] ) && object_to_uri( $actor ) === object_to_uri( $activity['actor'] ) ) { | |
| 163 | - $state = Remote_Actors::upsert( $actor ); | |
| 164 | - } else { | |
| 165 | - $state = new \WP_Error( 'activitypub_update_failed', \__( 'Update failed: missing, invalid, or unauthorized actor object in Update activity.', 'activitypub' ) ); | |
| 166 | - $actor = array(); | |
| 118 | + if ( ! $follower ) { | |
| 119 | + return; | |
| 167 | 120 | } |
| 168 | 121 | |
| 169 | - /** | |
| 170 | - * Fires after an ActivityPub Update activity has been handled. | |
| 171 | - * | |
| 172 | - * @param array $activity The ActivityPub activity data. | |
| 173 | - * @param int[] $user_ids The local user IDs. | |
| 174 | - * @param int|\WP_Error $state Actor post ID on success, WP_Error on failure. | |
| 175 | - * @param array $actor Remote actor meta data. | |
| 176 | - */ | |
| 177 | - \do_action( 'activitypub_handled_update', $activity, (array) $user_ids, $state, $actor ); | |
| 122 | + $follower->from_array( $actor ); | |
| 123 | + $follower->upsert(); | |
| 178 | 124 | } |
| 179 | 125 | } |