PluginProbe
ActivityPub / trunk
ActivityPub vtrunk
9.3.1 9.3.0 9.2.2 9.2.1 9.2.0 9.1.0 9.0.2 9.0.1 9.0.0 8.3.0 8.2.1 8.2.0 8.1.1 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.2.0 1.3.0 2.0.0 2.0.1 2.1.0 2.1.1 All 160 releases
activitypub / includes / handler / class-reject.php

class-reject.php in ActivityPub trunk, at includes/handler/class-reject.php

126 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Reject handler file.
4 *
5 * @package Activitypub
6 */
7
8 namespace Activitypub\Handler;
9
10 use Activitypub\Collection\Following;
11 use Activitypub\Collection\Outbox;
12 use Activitypub\Collection\Remote_Actors;
13
14 use function Activitypub\object_to_uri;
15
16 /**
17 * Handle "Reject" requests.
18 */
19 class Reject {
20 /**
21 * Initialize the class, registering WordPress hooks.
22 */
23 public static function init() {
24 \add_action( 'activitypub_inbox_reject', array( self::class, 'handle_reject' ), 10, 2 );
25 \add_filter( 'activitypub_validate_object', array( self::class, 'validate_object' ), 10, 3 );
26 }
27
28 /**
29 * Handles "Reject" requests.
30 *
31 * @param array $reject The activity-object.
32 * @param int|int[] $user_ids The user ID(s).
33 */
34 public static function handle_reject( $reject, $user_ids ) {
35 // Validate that there is a preceding Activity.
36 $outbox_post = Outbox::get_by_guid( $reject['object']['id'] );
37
38 if ( \is_wp_error( $outbox_post ) ) {
39 return;
40 }
41
42 // We currently only support reject for Follow activities. But we will support more in the future.
43 switch ( \get_post_meta( $outbox_post->ID, '_activitypub_activity_type', true ) ) {
44 case 'Follow':
45 self::reject_follow( $reject, $user_ids );
46 break;
47 default:
48 break;
49 }
50 }
51
52 /**
53 * Reject a "Follow" request.
54 *
55 * @param array $reject The activity-object.
56 * @param int|int[] $user_ids The user ID(s).
57 */
58 private static function reject_follow( $reject, $user_ids ) {
59 /*
60 * For a Follow Reject, the sender must be the actor that was followed.
61 * Without this, a signed Reject from one actor could cancel a Follow that
62 * targeted another actor by referencing that pending Follow's outbox GUID.
63 */
64 $reject_actor = object_to_uri( $reject['actor'] ?? '' );
65 $followed_actor = object_to_uri( $reject['object']['object'] ?? '' );
66 if ( ! $reject_actor || ! $followed_actor || $reject_actor !== $followed_actor ) {
67 return;
68 }
69
70 $actor_post = Remote_Actors::get_by_uri( $followed_actor );
71
72 if ( \is_wp_error( $actor_post ) ) {
73 return;
74 }
75
76 $user_id = \is_array( $user_ids ) ? \reset( $user_ids ) : $user_ids;
77 $result = Following::reject( $actor_post, $user_id );
78 $success = ! \is_wp_error( $result );
79
80 /**
81 * Fires after an ActivityPub Reject activity has been handled.
82 *
83 * @param array $reject The ActivityPub activity data.
84 * @param int[] $user_ids The local user IDs.
85 * @param bool $success True on success, false otherwise.
86 * @param \WP_Post|\WP_Error $result Actor post on success, WP_Error on failure.
87 */
88 \do_action( 'activitypub_handled_reject', $reject, (array) $user_ids, $success, $result );
89 }
90
91 /**
92 * Validate the object.
93 *
94 * @param bool $valid The validation state.
95 * @param string $param The object parameter.
96 * @param \WP_REST_Request $request The request object.
97 *
98 * @return bool The validation state: true if valid, false if not.
99 */
100 public static function validate_object( $valid, $param, $request ) {
101 $activity = $request->get_json_params();
102
103 if ( empty( $activity['type'] ) ) {
104 return false;
105 }
106
107 if ( 'Reject' !== $activity['type'] ) {
108 return $valid;
109 }
110
111 if ( ! isset( $activity['actor'], $activity['object'] ) ) {
112 return false;
113 }
114
115 if ( ! \is_array( $activity['object'] ) ) {
116 return false;
117 }
118
119 if ( ! isset( $activity['object']['id'], $activity['object']['type'], $activity['object']['actor'], $activity['object']['object'] ) ) {
120 return false;
121 }
122
123 return $valid;
124 }
125 }
126