| 1 |
<?php |
| 2 |
/** |
| 3 |
* Accept handler file. |
| 4 |
* |
| 5 |
* @package Activitypub |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Activitypub\Handler; |
| 9 |
|
| 10 |
use Activitypub\Notification; |
| 11 |
use Activitypub\Collection\Actors; |
| 12 |
use Activitypub\Collection\Following; |
| 13 |
use Activitypub\Collection\Outbox; |
| 14 |
|
| 15 |
use function Activitypub\object_to_uri; |
| 16 |
|
| 17 |
/** |
| 18 |
* Handle Accept requests. |
| 19 |
*/ |
| 20 |
class Accept { |
| 21 |
/** |
| 22 |
* Initialize the class, registering WordPress hooks. |
| 23 |
*/ |
| 24 |
public static function init() { |
| 25 |
\add_action( |
| 26 |
'activitypub_inbox_accept', |
| 27 |
array( self::class, 'handle_accept' ), |
| 28 |
10, |
| 29 |
2 |
| 30 |
); |
| 31 |
|
| 32 |
\add_filter( |
| 33 |
'activitypub_validate_object', |
| 34 |
array( self::class, 'validate_object' ), |
| 35 |
10, |
| 36 |
3 |
| 37 |
); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Handles "Accept" requests. |
| 42 |
* |
| 43 |
* @param array $accept The activity-object. |
| 44 |
* @param int $user_id The id of the local blog-user. |
| 45 |
*/ |
| 46 |
public static function handle_accept( $accept, $user_id ) { |
| 47 |
// Validate that there is a Follow Activity. |
| 48 |
$outbox_post = Outbox::get_by_guid( $accept['object']['id'] ); |
| 49 |
|
| 50 |
if ( |
| 51 |
\is_wp_error( $outbox_post ) || |
| 52 |
'Follow' !== \get_post_meta( $outbox_post->ID, '_activitypub_activity_type', true ) |
| 53 |
) { |
| 54 |
return; |
| 55 |
} |
| 56 |
|
| 57 |
$actor_post = Actors::get_remote_by_uri( object_to_uri( $accept['object']['object'] ) ); |
| 58 |
|
| 59 |
if ( \is_wp_error( $actor_post ) ) { |
| 60 |
return; |
| 61 |
} |
| 62 |
|
| 63 |
Following::accept( $actor_post, $user_id ); |
| 64 |
|
| 65 |
// Send notification. |
| 66 |
$notification = new Notification( |
| 67 |
'accept', |
| 68 |
$actor_post->guid, |
| 69 |
$accept, |
| 70 |
$user_id |
| 71 |
); |
| 72 |
$notification->send(); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Validate the object. |
| 77 |
* |
| 78 |
* @param bool $valid The validation state. |
| 79 |
* @param string $param The object parameter. |
| 80 |
* @param \WP_REST_Request $request The request object. |
| 81 |
* |
| 82 |
* @return bool The validation state: true if valid, false if not. |
| 83 |
*/ |
| 84 |
public static function validate_object( $valid, $param, $request ) { |
| 85 |
$json_params = $request->get_json_params(); |
| 86 |
|
| 87 |
if ( empty( $json_params['type'] ) ) { |
| 88 |
return false; |
| 89 |
} |
| 90 |
|
| 91 |
if ( |
| 92 |
'Accept' !== $json_params['type'] || |
| 93 |
\is_wp_error( $request ) |
| 94 |
) { |
| 95 |
return $valid; |
| 96 |
} |
| 97 |
|
| 98 |
$required_attributes = array( |
| 99 |
'actor', |
| 100 |
'object', |
| 101 |
); |
| 102 |
|
| 103 |
if ( ! empty( \array_diff( $required_attributes, \array_keys( $json_params ) ) ) ) { |
| 104 |
return false; |
| 105 |
} |
| 106 |
|
| 107 |
$required_object_attributes = array( |
| 108 |
'id', |
| 109 |
'type', |
| 110 |
'actor', |
| 111 |
'object', |
| 112 |
); |
| 113 |
|
| 114 |
if ( ! empty( \array_diff( $required_object_attributes, \array_keys( $json_params['object'] ) ) ) ) { |
| 115 |
return false; |
| 116 |
} |
| 117 |
|
| 118 |
return $valid; |
| 119 |
} |
| 120 |
} |
| 121 |
|