| @@ -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,9 +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 | 32 | use Language_Map; |
| 36 | 33 | |
| 37 | 34 | /** |
| 38 | 35 | * The namespace of this controller's route. |
| @@ -58,9 +55,9 @@ | ||
| 58 | 55 | array( |
| 59 | 56 | array( |
| 60 | 57 | 'methods' => \WP_REST_Server::CREATABLE, |
| 61 | 58 | 'callback' => array( $this, 'create_item' ), |
| 62 | - 'permission_callback' => array( $this, 'verify_signature' ), | |
| 59 | + 'permission_callback' => array( 'Activitypub\Rest\Server', 'verify_signature' ), | |
| 63 | 60 | 'args' => array( |
| 64 | 61 | 'id' => array( |
| 65 | 62 | 'description' => 'The unique identifier for the activity.', |
| 66 | 63 | 'type' => 'string', |
| @@ -73,16 +70,11 @@ | ||
| 73 | 70 | 'required' => true, |
| 74 | 71 | 'sanitize_callback' => '\Activitypub\object_to_uri', |
| 75 | 72 | ), |
| 76 | 73 | '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 | - }, | |
| 74 | + 'description' => 'The type of the activity.', | |
| 75 | + 'type' => 'string', | |
| 76 | + 'required' => true, | |
| 85 | 77 | ), |
| 86 | 78 | 'object' => array( |
| 87 | 79 | 'description' => 'The object of the activity.', |
| 88 | 80 | 'required' => true, |
| @@ -163,9 +155,9 @@ | ||
| 163 | 155 | * @param null $user_id The user ID. |
| 164 | 156 | * @param string $type The type of the activity. |
| 165 | 157 | * @param Activity|\WP_Error $activity The Activity object. |
| 166 | 158 | */ |
| 167 | - \do_action( 'activitypub_rest_inbox_disallowed', $data, null, $type, $activity ); | |
| 159 | + do_action( 'activitypub_rest_inbox_disallowed', $data, null, $type, $activity ); | |
| 168 | 160 | } else { |
| 169 | 161 | $recipients = $this->get_local_recipients( $data ); |
| 170 | 162 | |
| 171 | 163 | // Filter out blocked recipients. |
| @@ -378,54 +370,17 @@ | ||
| 378 | 370 | * |
| 379 | 371 | * @return array An array of user IDs who are the recipients of the activity. |
| 380 | 372 | */ |
| 381 | 373 | private function get_local_recipients( $activity ) { |
| 382 | - $user_ids = array(); | |
| 383 | - $remote_fetches = 0; | |
| 384 | - $cap_notified = false; | |
| 374 | + $user_ids = array(); | |
| 385 | 375 | |
| 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 | 376 | if ( is_activity_public( $activity ) ) { |
| 401 | - $user_ids = Following::get_follower_ids( $actor_uri ); | |
| 377 | + $user_ids = Following::get_follower_ids( $activity['actor'] ); | |
| 402 | 378 | } |
| 403 | 379 | |
| 404 | 380 | $recipients = extract_recipients_from_activity( $activity ); |
| 405 | 381 | |
| 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 | 382 | 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 | - | |
| 427 | - foreach ( $recipients as $recipient ) { | |
| 428 | 383 | // Skip public audience identifiers - they're not actual recipients to fetch. |
| 429 | 384 | if ( \in_array( $recipient, ACTIVITYPUB_PUBLIC_AUDIENCE_IDENTIFIERS, true ) ) { |
| 430 | 385 | continue; |
| 431 | 386 | } |
| @@ -430,51 +385,18 @@ | ||
| 430 | 385 | continue; |
| 431 | 386 | } |
| 432 | 387 | |
| 433 | 388 | 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 | 389 | $collection = Http::get_remote_object( $recipient ); |
| 470 | 390 | |
| 391 | + // If it is a remote actor we can skip it. | |
| 471 | 392 | if ( \is_wp_error( $collection ) ) { |
| 472 | 393 | continue; |
| 473 | 394 | } |
| 474 | 395 | |
| 475 | 396 | if ( is_collection( $collection ) ) { |
| 476 | - $user_ids = array_merge( $user_ids, Following::get_follower_ids( $actor_uri ) ); | |
| 397 | + $_user_ids = Following::get_follower_ids( $activity['actor'] ); | |
| 398 | + $user_ids = array_merge( $user_ids, $_user_ids ); | |
| 477 | 399 | continue; |
| 478 | 400 | } |
| 479 | 401 | } |
| 480 | 402 | |
| @@ -500,39 +422,6 @@ | ||
| 500 | 422 | } |
| 501 | 423 | } |
| 502 | 424 | |
| 503 | 425 | 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'] ); | |
| 537 | 426 | } |
| 538 | 427 | } |