| @@ -1,11 +1,20 @@ | ||
| 1 | 1 | <?php |
| 2 | +/** | |
| 3 | + * Update handler file. | |
| 4 | + * | |
| 5 | + * @package Activitypub | |
| 6 | + */ | |
| 7 | + | |
| 2 | 8 | namespace Activitypub\Handler; |
| 3 | 9 | |
| 4 | -use WP_Error; | |
| 5 | 10 | use Activitypub\Collection\Interactions; |
| 11 | +use Activitypub\Collection\Remote_Actors; | |
| 12 | +use Activitypub\Collection\Remote_Posts; | |
| 13 | +use Activitypub\Http; | |
| 6 | 14 | |
| 7 | -use function Activitypub\get_remote_metadata_by_actor; | |
| 15 | +use function Activitypub\is_activity_reply; | |
| 16 | +use function Activitypub\object_to_uri; | |
| 8 | 17 | |
| 9 | 18 | /** |
| 10 | 19 | * Handle Update requests. |
| 11 | 20 | */ |
| @@ -10,38 +19,43 @@ | ||
| 10 | 19 | * Handle Update requests. |
| 11 | 20 | */ |
| 12 | 21 | class Update { |
| 13 | 22 | /** |
| 14 | - * Initialize the class, registering WordPress hooks | |
| 23 | + * Initialize the class, registering WordPress hooks. | |
| 15 | 24 | */ |
| 16 | 25 | public static function init() { |
| 17 | - \add_action( | |
| 18 | - 'activitypub_inbox_update', | |
| 19 | - array( self::class, 'handle_update' ) | |
| 20 | - ); | |
| 26 | + \add_action( 'activitypub_handled_inbox_update', array( self::class, 'handle_update' ), 10, 3 ); | |
| 21 | 27 | } |
| 22 | 28 | |
| 23 | 29 | /** |
| 24 | - * Handle "Update" requests | |
| 30 | + * Handle "Update" requests. | |
| 25 | 31 | * |
| 26 | - * @param array $array The activity-object | |
| 27 | - * @param int $user_id The id of the local blog-user | |
| 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. | |
| 28 | 35 | */ |
| 29 | - public static function handle_update( $array ) { | |
| 30 | - $object_type = isset( $array['object']['type'] ) ? $array['object']['type'] : ''; | |
| 36 | + public static function handle_update( $activity, $user_ids, $activity_object ) { | |
| 37 | + $object_type = $activity['object']['type'] ?? ''; | |
| 31 | 38 | |
| 32 | 39 | switch ( $object_type ) { |
| 33 | - // Actor Types | |
| 34 | - // @see https://www.w3.org/TR/activitystreams-vocabulary/#actor-types | |
| 40 | + /* | |
| 41 | + * Actor Types. | |
| 42 | + * | |
| 43 | + * @see https://www.w3.org/TR/activitystreams-vocabulary/#actor-types | |
| 44 | + */ | |
| 35 | 45 | case 'Person': |
| 36 | 46 | case 'Group': |
| 37 | 47 | case 'Organization': |
| 38 | 48 | case 'Service': |
| 39 | 49 | case 'Application': |
| 40 | - self::update_actor( $array ); | |
| 50 | + self::update_actor( $activity, $user_ids ); | |
| 41 | 51 | break; |
| 42 | - // Object and Link Types | |
| 43 | - // @see https://www.w3.org/TR/activitystreams-vocabulary/#object-types | |
| 52 | + | |
| 53 | + /* | |
| 54 | + * Object and Link Types. | |
| 55 | + * | |
| 56 | + * @see https://www.w3.org/TR/activitystreams-vocabulary/#object-types | |
| 57 | + */ | |
| 44 | 58 | case 'Note': |
| 45 | 59 | case 'Article': |
| 46 | 60 | case 'Image': |
| 47 | 61 | case 'Audio': |
| @@ -47,12 +61,16 @@ | ||
| 47 | 61 | case 'Audio': |
| 48 | 62 | case 'Video': |
| 49 | 63 | case 'Event': |
| 50 | 64 | case 'Document': |
| 51 | - self::update_interaction( $array ); | |
| 65 | + self::update_object( $activity, $user_ids, $activity_object ); | |
| 52 | 66 | break; |
| 53 | - // Minimal Activity | |
| 54 | - // @see https://www.w3.org/TR/activitystreams-core/#example-1 | |
| 67 | + | |
| 68 | + /* | |
| 69 | + * Minimal Activity. | |
| 70 | + * | |
| 71 | + * @see https://www.w3.org/TR/activitystreams-core/#example-1 | |
| 72 | + */ | |
| 55 | 73 | default: |
| 56 | 74 | break; |
| 57 | 75 | } |
| 58 | 76 | } |
| @@ -57,39 +75,105 @@ | ||
| 57 | 75 | } |
| 58 | 76 | } |
| 59 | 77 | |
| 60 | 78 | /** |
| 61 | - * Update an Interaction | |
| 79 | + * Update an Object. | |
| 62 | 80 | * |
| 63 | - * @param array $activity The activity-object | |
| 64 | - * @param int $user_id The id of the local blog-user | |
| 65 | - * | |
| 66 | - * @return void | |
| 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. | |
| 67 | 84 | */ |
| 68 | - public static function update_interaction( $activity ) { | |
| 69 | - $commentdata = Interactions::update_comment( $activity ); | |
| 70 | - $reaction = null; | |
| 85 | + public static function update_object( $activity, $user_ids, $activity_object ) { | |
| 86 | + $result = new \WP_Error( 'activitypub_update_failed', 'Update failed' ); | |
| 87 | + $updated = true; | |
| 71 | 88 | |
| 72 | - if ( ! empty( $commentdata['comment_ID'] ) ) { | |
| 73 | - $state = 1; | |
| 74 | - $reaction = \get_comment( $commentdata['comment_ID'] ); | |
| 75 | - } else { | |
| 76 | - $state = $commentdata; | |
| 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 | + } | |
| 77 | 108 | } |
| 78 | 109 | |
| 79 | - \do_action( 'activitypub_handled_update', $activity, null, $state, $reaction ); | |
| 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 | + /** | |
| 118 | + * Fires after an ActivityPub Update activity has been handled. | |
| 119 | + * | |
| 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. | |
| 124 | + */ | |
| 125 | + \do_action( 'activitypub_handled_update', $activity, (array) $user_ids, $success, $result ); | |
| 80 | 126 | } |
| 81 | 127 | |
| 82 | 128 | /** |
| 83 | - * Update an Actor | |
| 129 | + * Update an Actor. | |
| 84 | 130 | * |
| 85 | - * @param array $activity The activity-object | |
| 86 | - * | |
| 87 | - * @return void | |
| 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. | |
| 88 | 133 | */ |
| 89 | - public static function update_actor( $activity ) { | |
| 90 | - // update cache | |
| 91 | - get_remote_metadata_by_actor( $activity['actor'], false ); | |
| 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; | |
| 92 | 140 | |
| 93 | - // @todo maybe also update all interactions | |
| 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 ); | |
| 150 | + | |
| 151 | + if ( ! \is_wp_error( $object ) && \is_array( $object ) ) { | |
| 152 | + $actor = $object; | |
| 153 | + } | |
| 154 | + } | |
| 155 | + | |
| 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(); | |
| 167 | + } | |
| 168 | + | |
| 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 ); | |
| 94 | 178 | } |
| 95 | 179 | } |