2023-05-24 16:32:00 +02:00
|
|
|
<?php
|
|
|
|
namespace Activitypub\Rest;
|
|
|
|
|
|
|
|
use WP_Error;
|
|
|
|
use WP_REST_Server;
|
2023-08-12 00:41:34 +02:00
|
|
|
use WP_REST_Request;
|
2023-05-24 16:32:00 +02:00
|
|
|
use WP_REST_Response;
|
2023-08-12 00:41:34 +02:00
|
|
|
use Activitypub\Webfinger;
|
2023-07-03 19:52:54 +02:00
|
|
|
use Activitypub\Activity\Activity;
|
2023-07-03 17:59:42 +02:00
|
|
|
use Activitypub\Collection\Users as User_Collection;
|
2023-05-24 16:32:00 +02:00
|
|
|
|
2023-05-30 10:22:01 +02:00
|
|
|
use function Activitypub\is_activitypub_request;
|
|
|
|
|
2023-05-24 16:32:00 +02:00
|
|
|
/**
|
|
|
|
* ActivityPub Followers REST-Class
|
|
|
|
*
|
|
|
|
* @author Matthias Pfefferle
|
|
|
|
*
|
|
|
|
* @see https://www.w3.org/TR/activitypub/#followers
|
|
|
|
*/
|
2023-05-24 17:27:46 +02:00
|
|
|
class Users {
|
2023-05-24 16:32:00 +02:00
|
|
|
/**
|
|
|
|
* Initialize the class, registering WordPress hooks
|
|
|
|
*/
|
|
|
|
public static function init() {
|
|
|
|
\add_action( 'rest_api_init', array( self::class, 'register_routes' ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register routes
|
|
|
|
*/
|
|
|
|
public static function register_routes() {
|
|
|
|
\register_rest_route(
|
|
|
|
ACTIVITYPUB_REST_NAMESPACE,
|
2023-05-26 16:08:08 +02:00
|
|
|
'/users/(?P<user_id>[\w\-\.]+)',
|
2023-05-24 16:32:00 +02:00
|
|
|
array(
|
|
|
|
array(
|
|
|
|
'methods' => WP_REST_Server::READABLE,
|
|
|
|
'callback' => array( self::class, 'get' ),
|
|
|
|
'args' => self::request_parameters(),
|
|
|
|
'permission_callback' => '__return_true',
|
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
2023-08-12 00:41:34 +02:00
|
|
|
|
|
|
|
\register_rest_route(
|
|
|
|
ACTIVITYPUB_REST_NAMESPACE,
|
|
|
|
'/users/(?P<user_id>[\w\-\.]+)/remote-follow',
|
|
|
|
array(
|
|
|
|
array(
|
|
|
|
'methods' => WP_REST_Server::READABLE,
|
|
|
|
'callback' => array( self::class, 'remote_follow_get' ),
|
|
|
|
|
|
|
|
'args' => array(
|
|
|
|
'resource' => array(
|
|
|
|
'required' => true,
|
|
|
|
'sanitize_callback' => 'sanitize_text_field',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
'permission_callback' => '__return_true',
|
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
2023-05-24 16:32:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle GET request
|
|
|
|
*
|
|
|
|
* @param WP_REST_Request $request
|
|
|
|
*
|
|
|
|
* @return WP_REST_Response
|
|
|
|
*/
|
|
|
|
public static function get( $request ) {
|
|
|
|
$user_id = $request->get_param( 'user_id' );
|
2023-07-03 11:20:44 +02:00
|
|
|
$user = User_Collection::get_by_various( $user_id );
|
2023-05-24 16:32:00 +02:00
|
|
|
|
|
|
|
if ( is_wp_error( $user ) ) {
|
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
|
2023-05-30 10:22:01 +02:00
|
|
|
// redirect to canonical URL if it is not an ActivityPub request
|
|
|
|
if ( ! is_activitypub_request() ) {
|
2023-05-30 10:29:23 +02:00
|
|
|
header( 'Location: ' . $user->get_canonical_url(), true, 301 );
|
2023-05-30 10:22:01 +02:00
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2023-05-24 16:32:00 +02:00
|
|
|
/*
|
|
|
|
* Action triggerd prior to the ActivityPub profile being created and sent to the client
|
|
|
|
*/
|
2023-07-25 10:47:59 +02:00
|
|
|
\do_action( 'activitypub_rest_users_pre' );
|
2023-05-24 16:32:00 +02:00
|
|
|
|
2023-06-28 18:02:14 +02:00
|
|
|
$user->set_context(
|
2023-07-03 11:20:44 +02:00
|
|
|
Activity::CONTEXT
|
2023-06-28 18:02:14 +02:00
|
|
|
);
|
|
|
|
|
2023-05-24 16:32:00 +02:00
|
|
|
$json = $user->to_array();
|
|
|
|
|
|
|
|
$response = new WP_REST_Response( $json, 200 );
|
|
|
|
$response->header( 'Content-Type', 'application/activity+json' );
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
2023-08-12 00:41:34 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Endpoint for remote follow UI/Block
|
|
|
|
*
|
|
|
|
* @param WP_REST_Request $request The request object.
|
|
|
|
*
|
|
|
|
* @return void|string The URL to the remote follow page
|
|
|
|
*/
|
|
|
|
public static function remote_follow_get( WP_REST_Request $request ) {
|
|
|
|
$resource = $request->get_param( 'resource' );
|
|
|
|
$user_id = $request->get_param( 'user_id' );
|
|
|
|
$user = User_Collection::get_by_various( $user_id );
|
|
|
|
|
|
|
|
if ( is_wp_error( $user ) ) {
|
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
|
|
|
|
$template = Webfinger::get_remote_follow_endpoint( $resource );
|
|
|
|
|
|
|
|
if ( is_wp_error( $template ) ) {
|
|
|
|
return $template;
|
|
|
|
}
|
|
|
|
|
|
|
|
$resource = $user->get_resource();
|
|
|
|
$url = str_replace( '{uri}', $resource, $template );
|
|
|
|
|
|
|
|
return new WP_REST_Response(
|
|
|
|
array( 'url' => $url ),
|
|
|
|
200
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-05-24 16:32:00 +02:00
|
|
|
/**
|
|
|
|
* The supported parameters
|
|
|
|
*
|
|
|
|
* @return array list of parameters
|
|
|
|
*/
|
|
|
|
public static function request_parameters() {
|
|
|
|
$params = array();
|
|
|
|
|
|
|
|
$params['page'] = array(
|
|
|
|
'type' => 'string',
|
|
|
|
);
|
|
|
|
|
|
|
|
$params['user_id'] = array(
|
|
|
|
'required' => true,
|
2023-05-30 10:22:01 +02:00
|
|
|
'type' => 'string',
|
2023-05-24 16:32:00 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
return $params;
|
|
|
|
}
|
|
|
|
}
|