2023-11-30 11:43:48 +01:00
|
|
|
<?php
|
|
|
|
namespace Activitypub\Handler;
|
|
|
|
|
|
|
|
use Activitypub\Http;
|
|
|
|
use Activitypub\Activity\Activity;
|
|
|
|
use Activitypub\Collection\Users;
|
|
|
|
use Activitypub\Collection\Followers;
|
2023-12-25 22:35:04 +01:00
|
|
|
use Activitypub\Model\Follow_Request;
|
2023-11-30 11:43:48 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle Follow requests
|
|
|
|
*/
|
|
|
|
class Follow {
|
|
|
|
/**
|
|
|
|
* Initialize the class, registering WordPress hooks
|
|
|
|
*/
|
|
|
|
public static function init() {
|
2023-12-22 17:45:27 +01:00
|
|
|
\add_action(
|
|
|
|
'activitypub_inbox_follow',
|
|
|
|
array( self::class, 'handle_follow' )
|
|
|
|
);
|
|
|
|
|
|
|
|
\add_action(
|
2023-12-25 22:35:04 +01:00
|
|
|
'activitypub_send_follow_response',
|
2023-12-22 17:45:27 +01:00
|
|
|
array( self::class, 'send_follow_response' ),
|
|
|
|
10,
|
|
|
|
4
|
|
|
|
);
|
2023-11-30 11:43:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle "Follow" requests
|
|
|
|
*
|
|
|
|
* @param array $activity The activity object
|
|
|
|
* @param int $user_id The user ID
|
|
|
|
*/
|
2023-12-22 17:45:27 +01:00
|
|
|
public static function handle_follow( $activity ) {
|
|
|
|
$user = Users::get_by_resource( $activity['object'] );
|
|
|
|
|
|
|
|
if ( ! $user || is_wp_error( $user ) ) {
|
|
|
|
// If we can not find a user,
|
|
|
|
// we can not initiate a follow process
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$user_id = $user->get__id();
|
|
|
|
|
2023-11-30 11:43:48 +01:00
|
|
|
// save follower
|
2023-12-27 16:45:23 +01:00
|
|
|
$follower_id = Followers::add_follower(
|
2023-12-22 17:45:27 +01:00
|
|
|
$user_id,
|
|
|
|
$activity['actor']
|
|
|
|
);
|
2023-11-30 11:43:48 +01:00
|
|
|
|
2023-12-27 16:45:23 +01:00
|
|
|
if ( \is_wp_error( $follower_id ) ) {
|
2023-12-25 22:35:04 +01:00
|
|
|
// it is not even possible to send a "Reject" or "Accept" because
|
|
|
|
// we can not get the Remote-Inbox
|
|
|
|
return;
|
|
|
|
}
|
2023-12-26 13:50:54 +01:00
|
|
|
|
2023-12-25 22:35:04 +01:00
|
|
|
// save follow request by this follower
|
2023-12-27 16:45:23 +01:00
|
|
|
$follow_request = Follow_Request::save( $follower_id, $user_id, $activity['id'] );
|
2023-12-25 22:35:04 +01:00
|
|
|
|
|
|
|
if ( ! $user->get_manually_approves_followers() ) {
|
2023-12-26 10:51:43 +01:00
|
|
|
$follow_request->approve();
|
2023-12-25 22:35:04 +01:00
|
|
|
}
|
2023-11-30 11:43:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-12-25 22:35:04 +01:00
|
|
|
* Send Follow response
|
2023-11-30 11:43:48 +01:00
|
|
|
*
|
2023-12-25 22:35:04 +01:00
|
|
|
* @param Activitypub\Model\User $user The Target Users ActivityPub object
|
|
|
|
* @param Activitypub\Model\Follower $follower The Followers ActivityPub object
|
|
|
|
* @param array|object $object The ActivityPub follow object
|
|
|
|
* @param string $type The reponse object type: 'Accept' or 'Reject'
|
2023-11-30 11:43:48 +01:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2023-12-27 15:39:53 +01:00
|
|
|
public static function send_follow_response( $user, $inbox, $object, $type ) {
|
2023-12-25 22:35:04 +01:00
|
|
|
// send activity
|
2023-11-30 11:43:48 +01:00
|
|
|
$activity = new Activity();
|
2023-12-25 22:35:04 +01:00
|
|
|
$activity->set_type( $type );
|
2023-11-30 11:43:48 +01:00
|
|
|
$activity->set_object( $object );
|
|
|
|
$activity->set_actor( $user->get_id() );
|
2023-12-27 15:39:53 +01:00
|
|
|
$activity->set_to( $object['actor'] );
|
|
|
|
$activity->set_id( $user->get_id() . '#accept-' . \preg_replace( '~^https?://~', '', $object['actor'] ) . '-' . \time() );
|
2023-11-30 11:43:48 +01:00
|
|
|
|
|
|
|
$activity = $activity->to_json();
|
|
|
|
|
2023-12-27 15:39:53 +01:00
|
|
|
Http::post( $inbox, $activity, $user->get__id() );
|
2023-11-30 11:43:48 +01:00
|
|
|
}
|
|
|
|
}
|