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

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