PluginProbe
ActivityPub / 5.3.1
ActivityPub v5.3.1
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
← All changes | includes/handler/class-follow.php +53 -92 8.2.1 → 5.3.1 View file →
@@ -6,12 +6,13 @@
6 6 */
7 7
8 8 namespace Activitypub\Handler;
9 9
10 +use Activitypub\Http;
11 +use Activitypub\Notification;
10 12 use Activitypub\Activity\Activity;
11 13 use Activitypub\Collection\Actors;
12 14 use Activitypub\Collection\Followers;
13 -use Activitypub\Collection\Remote_Actors;
14 15
15 16 use function Activitypub\add_to_outbox;
16 17
17 18 /**
@@ -21,131 +22,91 @@
21 22 /**
22 23 * Initialize the class, registering WordPress hooks.
23 24 */
24 25 public static function init() {
25 - \add_action( 'activitypub_inbox_follow', array( self::class, 'handle_follow' ), 10, 2 );
26 - \add_action( 'activitypub_handled_follow', array( self::class, 'queue_accept' ), 10, 4 );
26 + \add_action(
27 + 'activitypub_inbox_follow',
28 + array( self::class, 'handle_follow' )
29 + );
30 +
31 + \add_action(
32 + 'activitypub_followers_post_follow',
33 + array( self::class, 'queue_accept' ),
34 + 10,
35 + 4
36 + );
27 37 }
28 38
29 39 /**
30 40 * Handle "Follow" requests.
31 41 *
32 - * @param array $activity The activity object.
33 - * @param int|int[] $user_ids The user ID(s).
42 + * @param array $activity The activity object.
34 43 */
35 - public static function handle_follow( $activity, $user_ids ) {
36 - // Extract the user ID (follow requests are always for a single user).
37 - $user_id = \is_array( $user_ids ) ? \reset( $user_ids ) : $user_ids;
44 + public static function handle_follow( $activity ) {
45 + $user = Actors::get_by_resource( $activity['object'] );
38 46
39 - if ( Actors::APPLICATION_USER_ID === $user_id ) {
40 - self::queue_reject( $activity, $user_id );
47 + if ( ! $user || is_wp_error( $user ) ) {
48 + // If we can not find a user, we can not initiate a follow process.
41 49 return;
42 50 }
43 51
44 - // Check if the actor already follows the user.
45 - $already_following = false;
46 - $remote_actor = Remote_Actors::get_by_uri( $activity['actor'] );
47 - if ( ! \is_wp_error( $remote_actor ) ) {
48 - $already_following = Followers::follows( $remote_actor->ID, $user_id );
49 - }
52 + $user_id = $user->get__id();
50 53
51 - // Save follower if not already following.
52 - if ( $already_following ) {
53 - $success = false;
54 - } else {
55 - $remote_actor = Followers::add( $user_id, $activity['actor'] );
56 - $success = ! \is_wp_error( $remote_actor );
54 + // Save follower.
55 + $follower = Followers::add_follower(
56 + $user_id,
57 + $activity['actor']
58 + );
57 59
58 - if ( $success ) {
59 - $remote_actor = \get_post( $remote_actor );
60 - }
61 - }
62 -
63 60 /**
64 61 * Fires after a new follower has been added.
65 62 *
66 - * @deprecated 7.5.0 Use "activitypub_handled_follow" instead.
67 - *
68 - * @param string $actor The URL of the actor (follower) who initiated the follow.
69 - * @param array $activity The complete activity data of the follow request.
70 - * @param int $user_id The ID of the WordPress user being followed.
71 - * @param \WP_Post|\WP_Error $remote_actor The Actor object containing the new follower's data.
63 + * @param string $actor The URL of the actor (follower) who initiated the follow.
64 + * @param array $activity The complete activity data of the follow request.
65 + * @param int $user_id The ID of the WordPress user being followed.
66 + * @param \Activitypub\Model\Follower $follower The Follower object containing the new follower's data.
72 67 */
73 - \do_action_deprecated( 'activitypub_followers_post_follow', array( $activity['actor'], $activity, $user_id, $remote_actor ), '7.5.0', 'activitypub_handled_follow' );
68 + do_action( 'activitypub_followers_post_follow', $activity['actor'], $activity, $user_id, $follower );
74 69
75 - /**
76 - * Fires after a Follow activity has been handled.
77 - *
78 - * @param array $activity The ActivityPub activity data.
79 - * @param int[] $user_ids The local user IDs.
80 - * @param bool $success True on success, false otherwise.
81 - * @param \WP_Post|\WP_Error $remote_actor The remote actor/follower, or WP_Error if failed.
82 - */
83 - \do_action( 'activitypub_handled_follow', $activity, (array) $user_ids, $success, $remote_actor );
70 + // Send notification.
71 + $notification = new Notification(
72 + 'follow',
73 + $activity['actor'],
74 + $activity,
75 + $user_id
76 + );
77 + $notification->send();
84 78 }
85 79
86 80 /**
87 81 * Send Accept response.
88 82 *
89 - * @param array $activity_object The ActivityPub activity data.
90 - * @param int|int[] $user_ids The local user IDs.
91 - * @param bool $success True on success, false otherwise.
92 - * @param \WP_Post|\WP_Error $remote_actor The remote actor/follower, or WP_Error if failed.
83 + * @param string $actor The Actor URL.
84 + * @param array $activity_object The Activity object.
85 + * @param int $user_id The ID of the WordPress User.
86 + * @param \Activitypub\Model\Follower $follower The Follower object.
93 87 */
94 - public static function queue_accept( $activity_object, $user_ids, $success, $remote_actor ) {
95 - if ( \is_wp_error( $remote_actor ) ) {
88 + public static function queue_accept( $actor, $activity_object, $user_id, $follower ) {
89 + if ( \is_wp_error( $follower ) ) {
96 90 // Impossible to send a "Reject" because we can not get the Remote-Inbox.
97 91 return;
98 92 }
99 93
100 - // Extract the user ID from the array (follow requests are always for a single user).
101 - $user_id = \is_array( $user_ids ) ? \reset( $user_ids ) : $user_ids;
102 -
103 - $actor = $activity_object['actor'];
104 -
105 94 // Only send minimal data.
106 95 $activity_object = array_intersect_key(
107 96 $activity_object,
108 - array(
109 - 'id' => 1,
110 - 'type' => 1,
111 - 'actor' => 1,
112 - 'object' => 1,
97 + array_flip(
98 + array(
99 + 'id',
100 + 'type',
101 + 'actor',
102 + 'object',
103 + )
113 104 )
114 105 );
115 106
116 - $activity = new Activity();
117 - $activity->set_type( 'Accept' );
118 - $activity->set_actor( Actors::get_by_id( $user_id )->get_id() );
119 - $activity->set_object( $activity_object );
120 - $activity->set_to( array( $actor ) );
107 + // Send response only to the Follower.
108 + $activity_object['to'] = array( $actor );
121 109
122 - add_to_outbox( $activity, null, $user_id, ACTIVITYPUB_CONTENT_VISIBILITY_PRIVATE );
123 - }
124 -
125 - /**
126 - * Send Reject response.
127 - *
128 - * @param array $activity The Activity array.
129 - * @param int $user_id The ID of the WordPress User.
130 - */
131 - public static function queue_reject( $activity, $user_id ) {
132 - // Only send minimal data.
133 - $origin_activity = array_intersect_key(
134 - $activity,
135 - array(
136 - 'id' => 1,
137 - 'type' => 1,
138 - 'actor' => 1,
139 - 'object' => 1,
140 - )
141 - );
142 -
143 - $activity = new Activity();
144 - $activity->set_type( 'Reject' );
145 - $activity->set_actor( Actors::get_by_id( $user_id )->get_id() );
146 - $activity->set_object( $origin_activity );
147 - $activity->set_to( array( $origin_activity['actor'] ) );
148 -
149 - add_to_outbox( $activity, null, $user_id, ACTIVITYPUB_CONTENT_VISIBILITY_PRIVATE );
110 + add_to_outbox( $activity_object, 'Accept', $user_id, ACTIVITYPUB_CONTENT_VISIBILITY_PRIVATE );
150 111 }
151 112 }