PluginProbe
ActivityPub / 2.5.0
ActivityPub v2.5.0
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 2.5.0, at includes/handler/class-follow.php

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