| @@ -10,9 +10,8 @@ | ||
| 10 | 10 | use Activitypub\Activity\Activity; |
| 11 | 11 | use Activitypub\Collection\Actors; |
| 12 | 12 | use Activitypub\Collection\Following; |
| 13 | 13 | use Activitypub\Collection\Inbox; |
| 14 | -use Activitypub\Collection\Remote_Actors; | |
| 15 | 14 | use Activitypub\Http; |
| 16 | 15 | use Activitypub\Moderation; |
| 17 | 16 | |
| 18 | 17 | use function Activitypub\camel_to_snake_case; |
| @@ -19,9 +18,8 @@ | ||
| 19 | 18 | use function Activitypub\extract_recipients_from_activity; |
| 20 | 19 | use function Activitypub\is_activity_public; |
| 21 | 20 | use function Activitypub\is_collection; |
| 22 | 21 | use function Activitypub\is_same_domain; |
| 23 | -use function Activitypub\object_to_uri; | |
| 24 | 22 | use function Activitypub\user_can_activitypub; |
| 25 | 23 | |
| 26 | 24 | /** |
| 27 | 25 | * Inbox_Controller class. |
| @@ -30,11 +28,8 @@ | ||
| 30 | 28 | * |
| 31 | 29 | * @see https://www.w3.org/TR/activitypub/#inbox |
| 32 | 30 | */ |
| 33 | 31 | class Inbox_Controller extends \WP_REST_Controller { |
| 34 | - use Verification; | |
| 35 | - use Language_Map; | |
| 36 | - | |
| 37 | 32 | /** |
| 38 | 33 | * The namespace of this controller's route. |
| 39 | 34 | * |
| 40 | 35 | * @var string |
| @@ -58,9 +53,9 @@ | ||
| 58 | 53 | array( |
| 59 | 54 | array( |
| 60 | 55 | 'methods' => \WP_REST_Server::CREATABLE, |
| 61 | 56 | 'callback' => array( $this, 'create_item' ), |
| 62 | - 'permission_callback' => array( $this, 'verify_signature' ), | |
| 57 | + 'permission_callback' => array( 'Activitypub\Rest\Server', 'verify_signature' ), | |
| 63 | 58 | 'args' => array( |
| 64 | 59 | 'id' => array( |
| 65 | 60 | 'description' => 'The unique identifier for the activity.', |
| 66 | 61 | 'type' => 'string', |
| @@ -73,22 +68,16 @@ | ||
| 73 | 68 | 'required' => true, |
| 74 | 69 | 'sanitize_callback' => '\Activitypub\object_to_uri', |
| 75 | 70 | ), |
| 76 | 71 | 'type' => array( |
| 77 | - 'description' => 'The type of the activity.', | |
| 78 | - 'type' => 'string', | |
| 79 | - 'required' => true, | |
| 80 | - 'sanitize_callback' => 'sanitize_html_class', | |
| 81 | - 'validate_callback' => static function ( $param ) { | |
| 82 | - // Reject values that sanitize to empty so dynamic hook names always have a suffix. | |
| 83 | - return '' !== \sanitize_html_class( (string) $param ); | |
| 84 | - }, | |
| 72 | + 'description' => 'The type of the activity.', | |
| 73 | + 'type' => 'string', | |
| 74 | + 'required' => true, | |
| 85 | 75 | ), |
| 86 | 76 | 'object' => array( |
| 87 | 77 | 'description' => 'The object of the activity.', |
| 88 | 78 | 'required' => true, |
| 89 | - 'sanitize_callback' => array( $this, 'localize_language_maps' ), | |
| 90 | - 'validate_callback' => static function ( $param, $request, $key ) { | |
| 79 | + 'validate_callback' => function ( $param, $request, $key ) { | |
| 91 | 80 | /** |
| 92 | 81 | * Filter the ActivityPub object validation. |
| 93 | 82 | * |
| 94 | 83 | * @param bool $validate The validation result. |
| @@ -102,9 +91,9 @@ | ||
| 102 | 91 | 'to' => array( |
| 103 | 92 | 'description' => 'The primary recipients of the activity.', |
| 104 | 93 | 'type' => array( 'string', 'array' ), |
| 105 | 94 | 'required' => false, |
| 106 | - 'sanitize_callback' => static function ( $param ) { | |
| 95 | + 'sanitize_callback' => function ( $param ) { | |
| 107 | 96 | if ( \is_string( $param ) ) { |
| 108 | 97 | $param = array( $param ); |
| 109 | 98 | } |
| 110 | 99 | |
| @@ -113,9 +102,9 @@ | ||
| 113 | 102 | ), |
| 114 | 103 | 'cc' => array( |
| 115 | 104 | 'description' => 'The secondary recipients of the activity.', |
| 116 | 105 | 'type' => array( 'string', 'array' ), |
| 117 | - 'sanitize_callback' => static function ( $param ) { | |
| 106 | + 'sanitize_callback' => function ( $param ) { | |
| 118 | 107 | if ( \is_string( $param ) ) { |
| 119 | 108 | $param = array( $param ); |
| 120 | 109 | } |
| 121 | 110 | |
| @@ -124,9 +113,9 @@ | ||
| 124 | 113 | ), |
| 125 | 114 | 'bcc' => array( |
| 126 | 115 | 'description' => 'The private recipients of the activity.', |
| 127 | 116 | 'type' => array( 'string', 'array' ), |
| 128 | - 'sanitize_callback' => static function ( $param ) { | |
| 117 | + 'sanitize_callback' => function ( $param ) { | |
| 129 | 118 | if ( \is_string( $param ) ) { |
| 130 | 119 | $param = array( $param ); |
| 131 | 120 | } |
| 132 | 121 | |
| @@ -163,9 +152,9 @@ | ||
| 163 | 152 | * @param null $user_id The user ID. |
| 164 | 153 | * @param string $type The type of the activity. |
| 165 | 154 | * @param Activity|\WP_Error $activity The Activity object. |
| 166 | 155 | */ |
| 167 | - \do_action( 'activitypub_rest_inbox_disallowed', $data, null, $type, $activity ); | |
| 156 | + do_action( 'activitypub_rest_inbox_disallowed', $data, null, $type, $activity ); | |
| 168 | 157 | } else { |
| 169 | 158 | $recipients = $this->get_local_recipients( $data ); |
| 170 | 159 | |
| 171 | 160 | // Filter out blocked recipients. |
| @@ -378,103 +367,28 @@ | ||
| 378 | 367 | * |
| 379 | 368 | * @return array An array of user IDs who are the recipients of the activity. |
| 380 | 369 | */ |
| 381 | 370 | private function get_local_recipients( $activity ) { |
| 382 | - $user_ids = array(); | |
| 383 | - $remote_fetches = 0; | |
| 384 | - $cap_notified = false; | |
| 385 | - | |
| 386 | - /** | |
| 387 | - * Filters the maximum number of remote recipient URLs that can be | |
| 388 | - * fetched per incoming activity. | |
| 389 | - * | |
| 390 | - * @since 8.2.1 | |
| 391 | - * | |
| 392 | - * @param int $max_remote_fetches Maximum number of remote fetches. Default 10. | |
| 393 | - */ | |
| 394 | - $max_remote_fetches = (int) \apply_filters( 'activitypub_max_remote_recipient_fetches', 10 ); | |
| 395 | - | |
| 396 | - // AS2 allows actor and followers to be either an IRI string or an inline object; normalize to a URI. | |
| 397 | - $actor_uri = ! empty( $activity['actor'] ) ? object_to_uri( $activity['actor'] ) : null; | |
| 398 | - $actor_followers_url = $this->get_cached_followers_url( $actor_uri ); | |
| 399 | - | |
| 400 | - if ( is_activity_public( $activity ) ) { | |
| 401 | - $user_ids = Following::get_follower_ids( $actor_uri ); | |
| 371 | + if ( is_activity_public( $activity ) ) { // Public activity, deliver to followers of the actor. | |
| 372 | + return Following::get_follower_ids( $activity['actor'] ); | |
| 402 | 373 | } |
| 403 | 374 | |
| 404 | 375 | $recipients = extract_recipients_from_activity( $activity ); |
| 376 | + $user_ids = array(); | |
| 405 | 377 | |
| 406 | - /* | |
| 407 | - * Pre-compute which recipients are already known remote actors so the | |
| 408 | - * cached-actor short-circuit becomes an O(1) array lookup rather than | |
| 409 | - * one DB query per recipient. This bounds the DB cost of a flood of | |
| 410 | - * unknown recipient URIs to one batched SELECT (chunked) regardless | |
| 411 | - * of how many were sent. | |
| 412 | - */ | |
| 413 | - $candidate_uris = array(); | |
| 414 | 378 | foreach ( $recipients as $recipient ) { |
| 415 | - if ( | |
| 416 | - ! \is_string( $recipient ) | |
| 417 | - || \in_array( $recipient, ACTIVITYPUB_PUBLIC_AUDIENCE_IDENTIFIERS, true ) | |
| 418 | - || is_same_domain( $recipient ) | |
| 419 | - || $recipient === $actor_followers_url | |
| 420 | - ) { | |
| 421 | - continue; | |
| 422 | - } | |
| 423 | - $candidate_uris[] = $recipient; | |
| 424 | - } | |
| 425 | - $cached_uris = $candidate_uris ? Remote_Actors::get_existing_uris( $candidate_uris ) : array(); | |
| 426 | 379 | |
| 427 | - foreach ( $recipients as $recipient ) { | |
| 428 | - // Skip public audience identifiers - they're not actual recipients to fetch. | |
| 429 | - if ( \in_array( $recipient, ACTIVITYPUB_PUBLIC_AUDIENCE_IDENTIFIERS, true ) ) { | |
| 430 | - continue; | |
| 431 | - } | |
| 432 | - | |
| 433 | 380 | if ( ! is_same_domain( $recipient ) ) { |
| 434 | - // Known followers collection: resolve from local DB, no fetch needed. | |
| 435 | - if ( $recipient === $actor_followers_url ) { | |
| 436 | - $user_ids = \array_merge( $user_ids, Following::get_follower_ids( $actor_uri ) ); | |
| 437 | - continue; | |
| 438 | - } | |
| 439 | - | |
| 440 | - // Already cached as a remote actor: not a collection, so no local recipients to add. | |
| 441 | - if ( isset( $cached_uris[ $recipient ] ) ) { | |
| 442 | - continue; | |
| 443 | - } | |
| 444 | - | |
| 445 | - // Unknown URL: cap remote fetches to prevent abuse via large audience/recipient fields. | |
| 446 | - if ( $remote_fetches >= $max_remote_fetches ) { | |
| 447 | - if ( ! $cap_notified ) { | |
| 448 | - $cap_notified = true; | |
| 449 | - | |
| 450 | - /** | |
| 451 | - * Fires when an incoming activity hits the remote recipient fetch cap. | |
| 452 | - * | |
| 453 | - * Fires once per activity on the first recipient that exceeds the cap, | |
| 454 | - * not for each subsequent skipped recipient. Hook this to surface | |
| 455 | - * cap hits in your logging system of choice (Jetpack, Sentry, syslog, etc.). | |
| 456 | - * | |
| 457 | - * @since 8.2.1 | |
| 458 | - * | |
| 459 | - * @param array $activity The incoming activity data. | |
| 460 | - * @param string $recipient The recipient URI that was skipped. | |
| 461 | - * @param int $cap The configured cap. | |
| 462 | - */ | |
| 463 | - \do_action( 'activitypub_remote_recipient_fetch_cap_reached', $activity, $recipient, $max_remote_fetches ); | |
| 464 | - } | |
| 465 | - continue; | |
| 466 | - } | |
| 467 | - ++$remote_fetches; | |
| 468 | - | |
| 469 | 381 | $collection = Http::get_remote_object( $recipient ); |
| 470 | 382 | |
| 383 | + // If it is a remote actor we can skip it. | |
| 471 | 384 | if ( \is_wp_error( $collection ) ) { |
| 472 | 385 | continue; |
| 473 | 386 | } |
| 474 | 387 | |
| 475 | 388 | if ( is_collection( $collection ) ) { |
| 476 | - $user_ids = \array_merge( $user_ids, Following::get_follower_ids( $actor_uri ) ); | |
| 389 | + $_user_ids = Following::get_follower_ids( $activity['actor'] ); | |
| 390 | + $user_ids = array_merge( $user_ids, $_user_ids ); | |
| 477 | 391 | continue; |
| 478 | 392 | } |
| 479 | 393 | } |
| 480 | 394 | |
| @@ -490,49 +404,7 @@ | ||
| 490 | 404 | |
| 491 | 405 | $user_ids[] = $user_id; |
| 492 | 406 | } |
| 493 | 407 | |
| 494 | - // Check for an Actor in the Object field. | |
| 495 | - if ( empty( $user_ids ) && ! empty( $activity['object'] ) ) { | |
| 496 | - $user_id = Actors::get_id_by_resource( $activity['object'] ); | |
| 497 | - | |
| 498 | - if ( ! \is_wp_error( $user_id ) && user_can_activitypub( $user_id ) ) { | |
| 499 | - $user_ids[] = $user_id; | |
| 500 | - } | |
| 501 | - } | |
| 502 | - | |
| 503 | - return \array_unique( \array_map( 'intval', $user_ids ) ); | |
| 504 | - } | |
| 505 | - | |
| 506 | - /** | |
| 507 | - * Look up an actor's followers collection URL from the cached profile. | |
| 508 | - * | |
| 509 | - * Used to detect followers-addressed recipients without an outbound fetch. | |
| 510 | - * | |
| 511 | - * @param string|null $actor_uri Normalized actor URI. | |
| 512 | - * | |
| 513 | - * @return string|null The followers collection URL, or null if not cached/available. | |
| 514 | - */ | |
| 515 | - private function get_cached_followers_url( $actor_uri ) { | |
| 516 | - if ( empty( $actor_uri ) ) { | |
| 517 | - return null; | |
| 518 | - } | |
| 519 | - | |
| 520 | - $actor_post = Remote_Actors::get_by_uri( $actor_uri ); | |
| 521 | - if ( \is_wp_error( $actor_post ) ) { | |
| 522 | - return null; | |
| 523 | - } | |
| 524 | - | |
| 525 | - // Match Remote_Actors::get_actor()'s storage fallback: legacy actor JSON lives in postmeta when post_content is empty. | |
| 526 | - $json = $actor_post->post_content; | |
| 527 | - if ( empty( $json ) ) { | |
| 528 | - $json = \get_post_meta( $actor_post->ID, '_activitypub_actor_json', true ); | |
| 529 | - } | |
| 530 | - | |
| 531 | - $actor_data = \json_decode( $json, true ); | |
| 532 | - if ( empty( $actor_data['followers'] ) ) { | |
| 533 | - return null; | |
| 534 | - } | |
| 535 | - | |
| 536 | - return object_to_uri( $actor_data['followers'] ); | |
| 408 | + return array_unique( array_map( 'intval', $user_ids ) ); | |
| 537 | 409 | } |
| 538 | 410 | } |