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