| @@ -1,18 +1,27 @@ | ||
| 1 | 1 | <?php |
| 2 | +/** | |
| 3 | + * Follow handler file. | |
| 4 | + * | |
| 5 | + * @package Activitypub | |
| 6 | + */ | |
| 7 | + | |
| 2 | 8 | namespace Activitypub\Handler; |
| 3 | 9 | |
| 4 | 10 | use Activitypub\Http; |
| 11 | +use Activitypub\Notification; | |
| 5 | 12 | use Activitypub\Activity\Activity; |
| 6 | -use Activitypub\Collection\Users; | |
| 13 | +use Activitypub\Collection\Actors; | |
| 7 | 14 | use Activitypub\Collection\Followers; |
| 8 | 15 | |
| 16 | +use function Activitypub\add_to_outbox; | |
| 17 | + | |
| 9 | 18 | /** |
| 10 | - * Handle Follow requests | |
| 19 | + * Handle Follow requests. | |
| 11 | 20 | */ |
| 12 | 21 | class Follow { |
| 13 | 22 | /** |
| 14 | - * Initialize the class, registering WordPress hooks | |
| 23 | + * Initialize the class, registering WordPress hooks. | |
| 15 | 24 | */ |
| 16 | 25 | public static function init() { |
| 17 | 26 | \add_action( |
| 18 | 27 | 'activitypub_inbox_follow', |
| @@ -20,9 +29,9 @@ | ||
| 20 | 29 | ); |
| 21 | 30 | |
| 22 | 31 | \add_action( |
| 23 | 32 | 'activitypub_followers_post_follow', |
| 24 | - array( self::class, 'send_follow_response' ), | |
| 33 | + array( self::class, 'queue_accept' ), | |
| 25 | 34 | 10, |
| 26 | 35 | 4 |
| 27 | 36 | ); |
| 28 | 37 | } |
| @@ -27,59 +36,65 @@ | ||
| 27 | 36 | ); |
| 28 | 37 | } |
| 29 | 38 | |
| 30 | 39 | /** |
| 31 | - * Handle "Follow" requests | |
| 40 | + * Handle "Follow" requests. | |
| 32 | 41 | * |
| 33 | - * @param array $activity The activity object | |
| 34 | - * @param int $user_id The user ID | |
| 42 | + * @param array $activity The activity object. | |
| 35 | 43 | */ |
| 36 | 44 | public static function handle_follow( $activity ) { |
| 37 | - $user = Users::get_by_resource( $activity['object'] ); | |
| 45 | + $user = Actors::get_by_resource( $activity['object'] ); | |
| 38 | 46 | |
| 39 | 47 | if ( ! $user || is_wp_error( $user ) ) { |
| 40 | - // If we can not find a user, | |
| 41 | - // we can not initiate a follow process | |
| 48 | + // If we can not find a user, we can not initiate a follow process. | |
| 42 | 49 | return; |
| 43 | 50 | } |
| 44 | 51 | |
| 45 | 52 | $user_id = $user->get__id(); |
| 46 | 53 | |
| 47 | - // save follower | |
| 54 | + // Save follower. | |
| 48 | 55 | $follower = Followers::add_follower( |
| 49 | 56 | $user_id, |
| 50 | 57 | $activity['actor'] |
| 51 | 58 | ); |
| 52 | 59 | |
| 53 | - do_action( | |
| 54 | - 'activitypub_followers_post_follow', | |
| 60 | + /** | |
| 61 | + * Fires after a new follower has been added. | |
| 62 | + * | |
| 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. | |
| 67 | + */ | |
| 68 | + do_action( 'activitypub_followers_post_follow', $activity['actor'], $activity, $user_id, $follower ); | |
| 69 | + | |
| 70 | + // Send notification. | |
| 71 | + $notification = new Notification( | |
| 72 | + 'follow', | |
| 55 | 73 | $activity['actor'], |
| 56 | 74 | $activity, |
| 57 | - $user_id, | |
| 58 | - $follower | |
| 75 | + $user_id | |
| 59 | 76 | ); |
| 77 | + $notification->send(); | |
| 60 | 78 | } |
| 61 | 79 | |
| 62 | 80 | /** |
| 63 | - * Send Accept response | |
| 81 | + * Send Accept response. | |
| 64 | 82 | * |
| 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 | |
| 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. | |
| 71 | 87 | */ |
| 72 | - public static function send_follow_response( $actor, $object, $user_id, $follower ) { | |
| 88 | + public static function queue_accept( $actor, $activity_object, $user_id, $follower ) { | |
| 73 | 89 | if ( \is_wp_error( $follower ) ) { |
| 74 | - // it is not even possible to send a "Reject" because | |
| 75 | - // we can not get the Remote-Inbox | |
| 90 | + // Impossible to send a "Reject" because we can not get the Remote-Inbox. | |
| 76 | 91 | return; |
| 77 | 92 | } |
| 78 | 93 | |
| 79 | - // only send minimal data | |
| 80 | - $object = array_intersect_key( | |
| 81 | - $object, | |
| 94 | + // Only send minimal data. | |
| 95 | + $activity_object = array_intersect_key( | |
| 96 | + $activity_object, | |
| 82 | 97 | array_flip( |
| 83 | 98 | array( |
| 84 | 99 | 'id', |
| 85 | 100 | 'type', |
| @@ -88,22 +103,10 @@ | ||
| 88 | 103 | ) |
| 89 | 104 | ) |
| 90 | 105 | ); |
| 91 | 106 | |
| 92 | - $user = Users::get_by_id( $user_id ); | |
| 107 | + // Send response only to the Follower. | |
| 108 | + $activity_object['to'] = array( $actor ); | |
| 93 | 109 | |
| 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 ); | |
| 110 | + add_to_outbox( $activity_object, 'Accept', $user_id, ACTIVITYPUB_CONTENT_VISIBILITY_PRIVATE ); | |
| 108 | 111 | } |
| 109 | 112 | } |