PluginProbe
ActivityPub / 4.0.2
ActivityPub v4.0.2
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-follow.php

class-follow.php in ActivityPub 4.0.2, at includes/handler/class-follow.php

121 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Follow handler file.
4 *
5 * @package Activitypub
6 */
7
8 namespace Activitypub\Handler;
9
10 use Activitypub\Http;
11 use Activitypub\Notification;
12 use Activitypub\Activity\Activity;
13 use Activitypub\Collection\Users;
14 use Activitypub\Collection\Followers;
15
16 /**
17 * Handle Follow requests.
18 */
19 class Follow {
20 /**
21 * Initialize the class, registering WordPress hooks.
22 */
23 public static function init() {
24 \add_action(
25 'activitypub_inbox_follow',
26 array( self::class, 'handle_follow' )
27 );
28
29 \add_action(
30 'activitypub_followers_post_follow',
31 array( self::class, 'send_follow_response' ),
32 10,
33 4
34 );
35 }
36
37 /**
38 * Handle "Follow" requests.
39 *
40 * @param array $activity The activity object.
41 */
42 public static function handle_follow( $activity ) {
43 $user = Users::get_by_resource( $activity['object'] );
44
45 if ( ! $user || is_wp_error( $user ) ) {
46 // If we can not find a user, we can not initiate a follow process.
47 return;
48 }
49
50 $user_id = $user->get__id();
51
52 // Save follower.
53 $follower = Followers::add_follower(
54 $user_id,
55 $activity['actor']
56 );
57
58 do_action(
59 'activitypub_followers_post_follow',
60 $activity['actor'],
61 $activity,
62 $user_id,
63 $follower
64 );
65
66 // Send notification.
67 $notification = new Notification(
68 'follow',
69 $activity['actor'],
70 $activity,
71 $user_id
72 );
73 $notification->send();
74 }
75
76 /**
77 * Send Accept response.
78 *
79 * @param string $actor The Actor URL.
80 * @param array $activity_object The Activity object.
81 * @param int $user_id The ID of the WordPress User.
82 * @param \Activitypub\Model\Follower $follower The Follower object.
83 */
84 public static function send_follow_response( $actor, $activity_object, $user_id, $follower ) {
85 if ( \is_wp_error( $follower ) ) {
86 // Impossible to send a "Reject" because we can not get the Remote-Inbox.
87 return;
88 }
89
90 // Only send minimal data.
91 $activity_object = array_intersect_key(
92 $activity_object,
93 array_flip(
94 array(
95 'id',
96 'type',
97 'actor',
98 'object',
99 )
100 )
101 );
102
103 $user = Users::get_by_id( $user_id );
104
105 // Get inbox.
106 $inbox = $follower->get_shared_inbox();
107
108 // Send "Accept" activity.
109 $activity = new Activity();
110 $activity->set_type( 'Accept' );
111 $activity->set_object( $activity_object );
112 $activity->set_actor( $user->get_id() );
113 $activity->set_to( $actor );
114 $activity->set_id( $user->get_id() . '#follow-' . \preg_replace( '~^https?://~', '', $actor ) . '-' . \time() );
115
116 $activity = $activity->to_json();
117
118 Http::post( $inbox, $activity, $user_id );
119 }
120 }
121