| 1 |
<?php |
| 2 |
/** |
| 3 |
* Reject 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 "Reject" requests. |
| 19 |
*/ |
| 20 |
class Reject { |
| 21 |
/** |
| 22 |
* Initialize the class, registering WordPress hooks. |
| 23 |
*/ |
| 24 |
public static function init() { |
| 25 |
\add_action( |
| 26 |
'activitypub_inbox_reject', |
| 27 |
array( self::class, 'handle_reject' ), |
| 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 "Reject" requests. |
| 42 |
* |
| 43 |
* @param array $reject The activity-object. |
| 44 |
* @param int $user_id The id of the local blog-user. |
| 45 |
*/ |
| 46 |
public static function handle_reject( $reject, $user_id ) { |
| 47 |
// Validate that there is a preceding Activity. |
| 48 |
$outbox_post = Outbox::get_by_guid( $reject['object']['id'] ); |
| 49 |
|
| 50 |
if ( \is_wp_error( $outbox_post ) ) { |
| 51 |
return; |
| 52 |
} |
| 53 |
|
| 54 |
// We currently only support reject for Follow activities. But we will support more in the future. |
| 55 |
switch ( \get_post_meta( $outbox_post->ID, '_activitypub_activity_type', true ) ) { |
| 56 |
case 'Follow': |
| 57 |
self::reject_follow( $reject, $user_id ); |
| 58 |
break; |
| 59 |
default: |
| 60 |
break; |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Reject a "Follow" request. |
| 66 |
* |
| 67 |
* @param array $reject The activity-object. |
| 68 |
* @param int $user_id The id of the local blog-user. |
| 69 |
*/ |
| 70 |
private static function reject_follow( $reject, $user_id ) { |
| 71 |
$actor_uri = $reject['object']['actor'] ?? ''; |
| 72 |
$actor_post = Actors::get_remote_by_uri( object_to_uri( $actor_uri ) ); |
| 73 |
|
| 74 |
if ( \is_wp_error( $actor_post ) ) { |
| 75 |
return; |
| 76 |
} |
| 77 |
|
| 78 |
Following::reject( $actor_post, $user_id ); |
| 79 |
|
| 80 |
// Send notification. |
| 81 |
$notification = new Notification( |
| 82 |
'reject', |
| 83 |
$actor_post->guid, |
| 84 |
$reject, |
| 85 |
$user_id |
| 86 |
); |
| 87 |
$notification->send(); |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Validate the object. |
| 92 |
* |
| 93 |
* @param bool $valid The validation state. |
| 94 |
* @param string $param The object parameter. |
| 95 |
* @param \WP_REST_Request $request The request object. |
| 96 |
* |
| 97 |
* @return bool The validation state: true if valid, false if not. |
| 98 |
*/ |
| 99 |
public static function validate_object( $valid, $param, $request ) { |
| 100 |
$json_params = $request->get_json_params(); |
| 101 |
|
| 102 |
if ( empty( $json_params['type'] ) ) { |
| 103 |
return false; |
| 104 |
} |
| 105 |
|
| 106 |
if ( |
| 107 |
'Reject' !== $json_params['type'] || |
| 108 |
\is_wp_error( $request ) |
| 109 |
) { |
| 110 |
return $valid; |
| 111 |
} |
| 112 |
|
| 113 |
if ( empty( $json_params['actor'] ) || empty( $json_params['object'] ) ) { |
| 114 |
return false; |
| 115 |
} |
| 116 |
|
| 117 |
return $valid; |
| 118 |
} |
| 119 |
} |
| 120 |
|