2023-05-24 16:32:00 +02:00
|
|
|
<?php
|
|
|
|
namespace Activitypub\Rest;
|
|
|
|
|
|
|
|
use WP_Error;
|
|
|
|
use WP_REST_Server;
|
|
|
|
use WP_REST_Response;
|
2023-07-03 17:59:42 +02:00
|
|
|
use \Activitypub\Activity\Activity;
|
|
|
|
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',
|
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
\do_action( 'activitypub_outbox_pre' );
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
}
|