| 1 |
<?php |
| 2 |
namespace Activitypub\Rest; |
| 3 |
|
| 4 |
use WP_Error; |
| 5 |
use WP_REST_Server; |
| 6 |
use WP_REST_Request; |
| 7 |
use WP_REST_Response; |
| 8 |
use Activitypub\Webfinger; |
| 9 |
use Activitypub\Activity\Activity; |
| 10 |
use Activitypub\Collection\Users as User_Collection; |
| 11 |
|
| 12 |
use function Activitypub\is_activitypub_request; |
| 13 |
|
| 14 |
/** |
| 15 |
* ActivityPub Actors REST-Class |
| 16 |
* |
| 17 |
* @author Matthias Pfefferle |
| 18 |
* |
| 19 |
* @see https://www.w3.org/TR/activitypub/#followers |
| 20 |
*/ |
| 21 |
class Actors { |
| 22 |
/** |
| 23 |
* Initialize the class, registering WordPress hooks |
| 24 |
*/ |
| 25 |
public static function init() { |
| 26 |
self::register_routes(); |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Register routes |
| 31 |
*/ |
| 32 |
public static function register_routes() { |
| 33 |
\register_rest_route( |
| 34 |
ACTIVITYPUB_REST_NAMESPACE, |
| 35 |
'/(users|actors)/(?P<user_id>[\w\-\.]+)', |
| 36 |
array( |
| 37 |
array( |
| 38 |
'methods' => WP_REST_Server::READABLE, |
| 39 |
'callback' => array( self::class, 'get' ), |
| 40 |
'args' => self::request_parameters(), |
| 41 |
'permission_callback' => '__return_true', |
| 42 |
), |
| 43 |
) |
| 44 |
); |
| 45 |
|
| 46 |
\register_rest_route( |
| 47 |
ACTIVITYPUB_REST_NAMESPACE, |
| 48 |
'/(users|actors)/(?P<user_id>[\w\-\.]+)/remote-follow', |
| 49 |
array( |
| 50 |
array( |
| 51 |
'methods' => WP_REST_Server::READABLE, |
| 52 |
'callback' => array( self::class, 'remote_follow_get' ), |
| 53 |
'permission_callback' => '__return_true', |
| 54 |
'args' => array( |
| 55 |
'resource' => array( |
| 56 |
'required' => true, |
| 57 |
'sanitize_callback' => 'sanitize_text_field', |
| 58 |
), |
| 59 |
), |
| 60 |
), |
| 61 |
) |
| 62 |
); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Handle GET request |
| 67 |
* |
| 68 |
* @param WP_REST_Request $request |
| 69 |
* |
| 70 |
* @return WP_REST_Response |
| 71 |
*/ |
| 72 |
public static function get( $request ) { |
| 73 |
$user_id = $request->get_param( 'user_id' ); |
| 74 |
$user = User_Collection::get_by_various( $user_id ); |
| 75 |
|
| 76 |
if ( is_wp_error( $user ) ) { |
| 77 |
return $user; |
| 78 |
} |
| 79 |
|
| 80 |
$link_header = sprintf( '<%1$s>; rel="alternate"; type="application/activity+json"', $user->get_id() ); |
| 81 |
|
| 82 |
// redirect to canonical URL if it is not an ActivityPub request |
| 83 |
if ( ! is_activitypub_request() ) { |
| 84 |
header( 'Link: ' . $link_header ); |
| 85 |
header( 'Location: ' . $user->get_canonical_url(), true, 301 ); |
| 86 |
exit; |
| 87 |
} |
| 88 |
|
| 89 |
/* |
| 90 |
* Action triggerd prior to the ActivityPub profile being created and sent to the client |
| 91 |
*/ |
| 92 |
\do_action( 'activitypub_rest_users_pre' ); |
| 93 |
|
| 94 |
$json = $user->to_array(); |
| 95 |
|
| 96 |
$rest_response = new WP_REST_Response( $json, 200 ); |
| 97 |
$rest_response->header( 'Content-Type', 'application/activity+json; charset=' . get_option( 'blog_charset' ) ); |
| 98 |
$rest_response->header( 'Link', $link_header ); |
| 99 |
|
| 100 |
return $rest_response; |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
/** |
| 105 |
* Endpoint for remote follow UI/Block |
| 106 |
* |
| 107 |
* @param WP_REST_Request $request The request object. |
| 108 |
* |
| 109 |
* @return void|string The URL to the remote follow page |
| 110 |
*/ |
| 111 |
public static function remote_follow_get( WP_REST_Request $request ) { |
| 112 |
$resource = $request->get_param( 'resource' ); |
| 113 |
$user_id = $request->get_param( 'user_id' ); |
| 114 |
$user = User_Collection::get_by_various( $user_id ); |
| 115 |
|
| 116 |
if ( is_wp_error( $user ) ) { |
| 117 |
return $user; |
| 118 |
} |
| 119 |
|
| 120 |
$template = Webfinger::get_remote_follow_endpoint( $resource ); |
| 121 |
|
| 122 |
if ( is_wp_error( $template ) ) { |
| 123 |
return $template; |
| 124 |
} |
| 125 |
|
| 126 |
$resource = $user->get_webfinger(); |
| 127 |
$url = str_replace( '{uri}', $resource, $template ); |
| 128 |
|
| 129 |
return new WP_REST_Response( |
| 130 |
array( 'url' => $url, 'template' => $template ), |
| 131 |
200 |
| 132 |
); |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* The supported parameters |
| 137 |
* |
| 138 |
* @return array list of parameters |
| 139 |
*/ |
| 140 |
public static function request_parameters() { |
| 141 |
$params = array(); |
| 142 |
|
| 143 |
$params['page'] = array( |
| 144 |
'type' => 'string', |
| 145 |
); |
| 146 |
|
| 147 |
$params['user_id'] = array( |
| 148 |
'required' => true, |
| 149 |
'type' => 'string', |
| 150 |
); |
| 151 |
|
| 152 |
return $params; |
| 153 |
} |
| 154 |
} |
| 155 |
|