| @@ -9,8 +9,10 @@ | ||
| 9 | 9 | |
| 10 | 10 | use Activitypub\Collection\Actors as Actor_Collection; |
| 11 | 11 | use Activitypub\Webfinger; |
| 12 | 12 | |
| 13 | +use function Activitypub\is_activitypub_request; | |
| 14 | + | |
| 13 | 15 | /** |
| 14 | 16 | * ActivityPub Actors REST-Class. |
| 15 | 17 | * |
| 16 | 18 | * @author Matthias Pfefferle |
| @@ -17,10 +19,8 @@ | ||
| 17 | 19 | * |
| 18 | 20 | * @see https://www.w3.org/TR/activitypub/#followers |
| 19 | 21 | */ |
| 20 | 22 | class Actors_Controller extends \WP_REST_Controller { |
| 21 | - use Verification; | |
| 22 | - | |
| 23 | 23 | /** |
| 24 | 24 | * The namespace of this controller's route. |
| 25 | 25 | * |
| 26 | 26 | * @var string |
| @@ -31,9 +31,9 @@ | ||
| 31 | 31 | * The base of this controller's route. |
| 32 | 32 | * |
| 33 | 33 | * @var string |
| 34 | 34 | */ |
| 35 | - protected $rest_base = '(?:users|actors)\/(?P<user_id>[-]?\d+)'; | |
| 35 | + protected $rest_base = '(?:users|actors)\/(?P<user_id>[\w\-\.]+)'; | |
| 36 | 36 | |
| 37 | 37 | /** |
| 38 | 38 | * Register routes. |
| 39 | 39 | */ |
| @@ -43,18 +43,18 @@ | ||
| 43 | 43 | '/' . $this->rest_base, |
| 44 | 44 | array( |
| 45 | 45 | 'args' => array( |
| 46 | 46 | 'user_id' => array( |
| 47 | - 'description' => 'The ID of the actor.', | |
| 48 | - 'type' => 'integer', | |
| 49 | - 'required' => true, | |
| 50 | - 'validate_callback' => array( $this, 'validate_user_id' ), | |
| 47 | + 'description' => 'The ID or username of the actor.', | |
| 48 | + 'type' => 'string', | |
| 49 | + 'required' => true, | |
| 50 | + 'pattern' => '[\w\-\.]+', | |
| 51 | 51 | ), |
| 52 | 52 | ), |
| 53 | 53 | array( |
| 54 | 54 | 'methods' => \WP_REST_Server::READABLE, |
| 55 | 55 | 'callback' => array( $this, 'get_item' ), |
| 56 | - 'permission_callback' => array( $this, 'verify_signature' ), | |
| 56 | + 'permission_callback' => array( 'Activitypub\Rest\Server', 'verify_signature' ), | |
| 57 | 57 | ), |
| 58 | 58 | 'schema' => array( $this, 'get_public_item_schema' ), |
| 59 | 59 | ) |
| 60 | 60 | ); |
| @@ -64,12 +64,12 @@ | ||
| 64 | 64 | '/' . $this->rest_base . '/remote-follow', |
| 65 | 65 | array( |
| 66 | 66 | 'args' => array( |
| 67 | 67 | 'user_id' => array( |
| 68 | - 'description' => 'The ID of the actor.', | |
| 69 | - 'type' => 'integer', | |
| 70 | - 'required' => true, | |
| 71 | - 'validate_callback' => array( $this, 'validate_user_id' ), | |
| 68 | + 'description' => 'The ID or username of the actor.', | |
| 69 | + 'type' => 'string', | |
| 70 | + 'required' => true, | |
| 71 | + 'pattern' => '[\w\-\.]+', | |
| 72 | 72 | ), |
| 73 | 73 | ), |
| 74 | 74 | array( |
| 75 | 75 | 'methods' => \WP_REST_Server::READABLE, |
| @@ -94,10 +94,14 @@ | ||
| 94 | 94 | * @return \WP_REST_Response|\WP_Error Response object on success, or WP_Error object on failure. |
| 95 | 95 | */ |
| 96 | 96 | public function get_item( $request ) { |
| 97 | 97 | $user_id = $request->get_param( 'user_id' ); |
| 98 | - $user = Actor_Collection::get_by_id( $user_id ); | |
| 98 | + $user = Actor_Collection::get_by_various( $user_id ); | |
| 99 | 99 | |
| 100 | + if ( \is_wp_error( $user ) ) { | |
| 101 | + return $user; | |
| 102 | + } | |
| 103 | + | |
| 100 | 104 | /** |
| 101 | 105 | * Action triggered prior to the ActivityPub profile being created and sent to the client. |
| 102 | 106 | */ |
| 103 | 107 | \do_action( 'activitypub_rest_users_pre' ); |
| @@ -119,10 +123,14 @@ | ||
| 119 | 123 | */ |
| 120 | 124 | public function get_remote_follow_item( $request ) { |
| 121 | 125 | $resource = $request->get_param( 'resource' ); |
| 122 | 126 | $user_id = $request->get_param( 'user_id' ); |
| 123 | - $user = Actor_Collection::get_by_id( $user_id ); | |
| 127 | + $user = Actor_Collection::get_by_various( $user_id ); | |
| 124 | 128 | |
| 129 | + if ( \is_wp_error( $user ) ) { | |
| 130 | + return $user; | |
| 131 | + } | |
| 132 | + | |
| 125 | 133 | $template = Webfinger::get_remote_follow_endpoint( $resource ); |
| 126 | 134 | |
| 127 | 135 | if ( \is_wp_error( $template ) ) { |
| 128 | 136 | return $template; |
| @@ -342,50 +350,10 @@ | ||
| 342 | 350 | 'description' => 'Whether the actor is discoverable.', |
| 343 | 351 | 'type' => 'boolean', |
| 344 | 352 | 'readonly' => true, |
| 345 | 353 | ), |
| 346 | - 'generator' => array( | |
| 347 | - 'description' => 'The generator of the object.', | |
| 348 | - 'type' => 'object', | |
| 349 | - 'properties' => array( | |
| 350 | - 'type' => array( | |
| 351 | - 'type' => 'string', | |
| 352 | - ), | |
| 353 | - 'implements' => array( | |
| 354 | - 'type' => 'array', | |
| 355 | - 'items' => array( | |
| 356 | - 'type' => 'object', | |
| 357 | - 'properties' => array( | |
| 358 | - 'href' => array( | |
| 359 | - 'type' => 'string', | |
| 360 | - 'format' => 'uri', | |
| 361 | - ), | |
| 362 | - 'name' => array( | |
| 363 | - 'type' => 'string', | |
| 364 | - ), | |
| 365 | - ), | |
| 366 | - ), | |
| 367 | - ), | |
| 368 | - ), | |
| 369 | - 'readonly' => true, | |
| 370 | - ), | |
| 371 | 354 | ), |
| 372 | 355 | ); |
| 373 | 356 | |
| 374 | 357 | return $this->add_additional_fields_schema( $this->schema ); |
| 375 | - } | |
| 376 | - | |
| 377 | - /** | |
| 378 | - * Validates the user_id parameter. | |
| 379 | - * | |
| 380 | - * @param mixed $user_id The user_id parameter. | |
| 381 | - * @return bool|\WP_Error True if the user_id is valid, WP_Error otherwise. | |
| 382 | - */ | |
| 383 | - public function validate_user_id( $user_id ) { | |
| 384 | - $user = Actor_Collection::get_by_id( $user_id ); | |
| 385 | - if ( \is_wp_error( $user ) ) { | |
| 386 | - return $user; | |
| 387 | - } | |
| 388 | - | |
| 389 | - return true; | |
| 390 | 358 | } |
| 391 | 359 | } |