| @@ -1,8 +1,14 @@ | ||
| 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; | |
| 10 | +use Activitypub\Collection\Followers; | |
| 5 | 11 | use Activitypub\Collection\Interactions; |
| 6 | 12 | |
| 7 | 13 | use function Activitypub\get_remote_metadata_by_actor; |
| 8 | 14 | |
| @@ -10,9 +16,9 @@ | ||
| 10 | 16 | * Handle Update requests. |
| 11 | 17 | */ |
| 12 | 18 | class Update { |
| 13 | 19 | /** |
| 14 | - * Initialize the class, registering WordPress hooks | |
| 20 | + * Initialize the class, registering WordPress hooks. | |
| 15 | 21 | */ |
| 16 | 22 | public static function init() { |
| 17 | 23 | \add_action( |
| 18 | 24 | 'activitypub_inbox_update', |
| @@ -20,28 +26,34 @@ | ||
| 20 | 26 | ); |
| 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. | |
| 28 | 33 | */ |
| 29 | - public static function handle_update( $array ) { | |
| 30 | - $object_type = isset( $array['object']['type'] ) ? $array['object']['type'] : ''; | |
| 34 | + public static function handle_update( $activity ) { | |
| 35 | + $object_type = isset( $activity['object']['type'] ) ? $activity['object']['type'] : ''; | |
| 31 | 36 | |
| 32 | 37 | switch ( $object_type ) { |
| 33 | - // Actor Types | |
| 34 | - // @see https://www.w3.org/TR/activitystreams-vocabulary/#actor-types | |
| 38 | + /* | |
| 39 | + * Actor Types. | |
| 40 | + * | |
| 41 | + * @see https://www.w3.org/TR/activitystreams-vocabulary/#actor-types | |
| 42 | + */ | |
| 35 | 43 | case 'Person': |
| 36 | 44 | case 'Group': |
| 37 | 45 | case 'Organization': |
| 38 | 46 | case 'Service': |
| 39 | 47 | case 'Application': |
| 40 | - self::update_actor( $array ); | |
| 48 | + self::update_actor( $activity ); | |
| 41 | 49 | break; |
| 42 | - // Object and Link Types | |
| 43 | - // @see https://www.w3.org/TR/activitystreams-vocabulary/#object-types | |
| 50 | + | |
| 51 | + /* | |
| 52 | + * Object and Link Types. | |
| 53 | + * | |
| 54 | + * @see https://www.w3.org/TR/activitystreams-vocabulary/#object-types | |
| 55 | + */ | |
| 44 | 56 | case 'Note': |
| 45 | 57 | case 'Article': |
| 46 | 58 | case 'Image': |
| 47 | 59 | case 'Audio': |
| @@ -47,12 +59,16 @@ | ||
| 47 | 59 | case 'Audio': |
| 48 | 60 | case 'Video': |
| 49 | 61 | case 'Event': |
| 50 | 62 | case 'Document': |
| 51 | - self::update_interaction( $array ); | |
| 63 | + self::update_interaction( $activity ); | |
| 52 | 64 | break; |
| 53 | - // Minimal Activity | |
| 54 | - // @see https://www.w3.org/TR/activitystreams-core/#example-1 | |
| 65 | + | |
| 66 | + /* | |
| 67 | + * Minimal Activity. | |
| 68 | + * | |
| 69 | + * @see https://www.w3.org/TR/activitystreams-core/#example-1 | |
| 70 | + */ | |
| 55 | 71 | default: |
| 56 | 72 | break; |
| 57 | 73 | } |
| 58 | 74 | } |
| @@ -57,14 +73,11 @@ | ||
| 57 | 73 | } |
| 58 | 74 | } |
| 59 | 75 | |
| 60 | 76 | /** |
| 61 | - * Update an Interaction | |
| 77 | + * Update an Interaction. | |
| 62 | 78 | * |
| 63 | - * @param array $activity The activity-object | |
| 64 | - * @param int $user_id The id of the local blog-user | |
| 65 | - * | |
| 66 | - * @return void | |
| 79 | + * @param array $activity The Activity object. | |
| 67 | 80 | */ |
| 68 | 81 | public static function update_interaction( $activity ) { |
| 69 | 82 | $commentdata = Interactions::update_comment( $activity ); |
| 70 | 83 | $reaction = null; |
| @@ -75,21 +88,38 @@ | ||
| 75 | 88 | } else { |
| 76 | 89 | $state = $commentdata; |
| 77 | 90 | } |
| 78 | 91 | |
| 92 | + /** | |
| 93 | + * Fires after an Update activity has been handled. | |
| 94 | + * | |
| 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. | |
| 99 | + */ | |
| 79 | 100 | \do_action( 'activitypub_handled_update', $activity, null, $state, $reaction ); |
| 80 | 101 | } |
| 81 | 102 | |
| 82 | 103 | /** |
| 83 | - * Update an Actor | |
| 104 | + * Update an Actor. | |
| 84 | 105 | * |
| 85 | - * @param array $activity The activity-object | |
| 86 | - * | |
| 87 | - * @return void | |
| 106 | + * @param array $activity The Activity object. | |
| 88 | 107 | */ |
| 89 | 108 | public static function update_actor( $activity ) { |
| 90 | - // update cache | |
| 91 | - get_remote_metadata_by_actor( $activity['actor'], false ); | |
| 109 | + // Update cache. | |
| 110 | + $actor = get_remote_metadata_by_actor( $activity['actor'], false ); | |
| 92 | 111 | |
| 93 | - // @todo maybe also update all interactions | |
| 112 | + if ( ! $actor || \is_wp_error( $actor ) || ! isset( $actor['id'] ) ) { | |
| 113 | + return; | |
| 114 | + } | |
| 115 | + | |
| 116 | + $follower = Followers::get_follower_by_actor( $actor['id'] ); | |
| 117 | + | |
| 118 | + if ( ! $follower ) { | |
| 119 | + return; | |
| 120 | + } | |
| 121 | + | |
| 122 | + $follower->from_array( $actor ); | |
| 123 | + $follower->upsert(); | |
| 94 | 124 | } |
| 95 | 125 | } |