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