2023-12-08 23:23:11 +00:00
|
|
|
<?php
|
2024-10-09 12:44:17 +00:00
|
|
|
/**
|
|
|
|
* Follow handler file.
|
|
|
|
*
|
|
|
|
* @package Activitypub
|
|
|
|
*/
|
|
|
|
|
2023-12-08 23:23:11 +00:00
|
|
|
namespace Activitypub\Handler;
|
|
|
|
|
|
|
|
use Activitypub\Http;
|
2024-06-27 12:10:38 +00:00
|
|
|
use Activitypub\Notification;
|
2023-12-08 23:23:11 +00:00
|
|
|
use Activitypub\Activity\Activity;
|
|
|
|
use Activitypub\Collection\Users;
|
|
|
|
use Activitypub\Collection\Followers;
|
|
|
|
|
|
|
|
/**
|
2024-10-09 12:44:17 +00:00
|
|
|
* Handle Follow requests.
|
2023-12-08 23:23:11 +00:00
|
|
|
*/
|
|
|
|
class Follow {
|
|
|
|
/**
|
2024-10-09 12:44:17 +00:00
|
|
|
* Initialize the class, registering WordPress hooks.
|
2023-12-08 23:23:11 +00:00
|
|
|
*/
|
|
|
|
public static function init() {
|
2024-02-08 12:31:25 +00:00
|
|
|
\add_action(
|
|
|
|
'activitypub_inbox_follow',
|
|
|
|
array( self::class, 'handle_follow' )
|
|
|
|
);
|
|
|
|
|
|
|
|
\add_action(
|
|
|
|
'activitypub_followers_post_follow',
|
|
|
|
array( self::class, 'send_follow_response' ),
|
|
|
|
10,
|
|
|
|
4
|
|
|
|
);
|
2023-12-08 23:23:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-10-09 12:44:17 +00:00
|
|
|
* Handle "Follow" requests.
|
2023-12-08 23:23:11 +00:00
|
|
|
*
|
2024-10-09 12:44:17 +00:00
|
|
|
* @param array $activity The activity object.
|
2023-12-08 23:23:11 +00:00
|
|
|
*/
|
2024-02-08 12:31:25 +00:00
|
|
|
public static function handle_follow( $activity ) {
|
|
|
|
$user = Users::get_by_resource( $activity['object'] );
|
|
|
|
|
|
|
|
if ( ! $user || is_wp_error( $user ) ) {
|
2024-10-09 12:44:17 +00:00
|
|
|
// If we can not find a user, we can not initiate a follow process.
|
2024-02-08 12:31:25 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$user_id = $user->get__id();
|
|
|
|
|
2024-10-09 12:44:17 +00:00
|
|
|
// Save follower.
|
2024-02-08 12:31:25 +00:00
|
|
|
$follower = Followers::add_follower(
|
|
|
|
$user_id,
|
|
|
|
$activity['actor']
|
|
|
|
);
|
2023-12-08 23:23:11 +00:00
|
|
|
|
2024-02-08 12:31:25 +00:00
|
|
|
do_action(
|
|
|
|
'activitypub_followers_post_follow',
|
|
|
|
$activity['actor'],
|
|
|
|
$activity,
|
|
|
|
$user_id,
|
|
|
|
$follower
|
|
|
|
);
|
2024-06-27 12:10:38 +00:00
|
|
|
|
2024-10-09 12:44:17 +00:00
|
|
|
// Send notification.
|
2024-06-27 12:10:38 +00:00
|
|
|
$notification = new Notification(
|
|
|
|
'follow',
|
|
|
|
$activity['actor'],
|
|
|
|
$activity,
|
|
|
|
$user_id
|
|
|
|
);
|
|
|
|
$notification->send();
|
2023-12-08 23:23:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-10-09 12:44:17 +00:00
|
|
|
* Send Accept response.
|
2023-12-08 23:23:11 +00:00
|
|
|
*
|
2024-10-09 12:44:17 +00:00
|
|
|
* @param string $actor The Actor URL.
|
|
|
|
* @param array $activity_object The Activity object.
|
|
|
|
* @param int $user_id The ID of the WordPress User.
|
|
|
|
* @param \Activitypub\Model\Follower $follower The Follower object.
|
2023-12-08 23:23:11 +00:00
|
|
|
*/
|
2024-10-09 12:44:17 +00:00
|
|
|
public static function send_follow_response( $actor, $activity_object, $user_id, $follower ) {
|
2023-12-08 23:23:11 +00:00
|
|
|
if ( \is_wp_error( $follower ) ) {
|
2024-10-09 12:44:17 +00:00
|
|
|
// Impossible to send a "Reject" because we can not get the Remote-Inbox.
|
2023-12-08 23:23:11 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-10-09 12:44:17 +00:00
|
|
|
// Only send minimal data.
|
|
|
|
$activity_object = array_intersect_key(
|
|
|
|
$activity_object,
|
2023-12-08 23:23:11 +00:00
|
|
|
array_flip(
|
|
|
|
array(
|
|
|
|
'id',
|
|
|
|
'type',
|
|
|
|
'actor',
|
|
|
|
'object',
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$user = Users::get_by_id( $user_id );
|
|
|
|
|
2024-10-09 12:44:17 +00:00
|
|
|
// Get inbox.
|
2023-12-08 23:23:11 +00:00
|
|
|
$inbox = $follower->get_shared_inbox();
|
|
|
|
|
2024-10-09 12:44:17 +00:00
|
|
|
// Send "Accept" activity.
|
2023-12-08 23:23:11 +00:00
|
|
|
$activity = new Activity();
|
|
|
|
$activity->set_type( 'Accept' );
|
2024-10-09 12:44:17 +00:00
|
|
|
$activity->set_object( $activity_object );
|
2023-12-08 23:23:11 +00:00
|
|
|
$activity->set_actor( $user->get_id() );
|
|
|
|
$activity->set_to( $actor );
|
|
|
|
$activity->set_id( $user->get_id() . '#follow-' . \preg_replace( '~^https?://~', '', $actor ) . '-' . \time() );
|
|
|
|
|
|
|
|
$activity = $activity->to_json();
|
|
|
|
|
|
|
|
Http::post( $inbox, $activity, $user_id );
|
|
|
|
}
|
|
|
|
}
|