PluginProbe
ActivityPub / 1.0.3
ActivityPub v1.0.3
9.3.1 9.3.0 9.2.2 9.2.1 9.2.0 9.1.0 9.0.2 9.0.1 9.0.0 8.3.0 8.2.1 8.2.0 8.1.1 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.2.0 1.3.0 2.0.0 2.0.1 2.1.0 2.1.1 All 160 releases
activitypub / includes / rest / class-users.php

class-users.php in ActivityPub 1.0.3, at includes/rest/class-users.php

156 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 Followers REST-Class
16 *
17 * @author Matthias Pfefferle
18 *
19 * @see https://www.w3.org/TR/activitypub/#followers
20 */
21 class Users {
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/(?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/(?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
54 'args' => array(
55 'resource' => array(
56 'required' => true,
57 'sanitize_callback' => 'sanitize_text_field',
58 ),
59 ),
60 'permission_callback' => '__return_true',
61 ),
62 )
63 );
64 }
65
66 /**
67 * Handle GET request
68 *
69 * @param WP_REST_Request $request
70 *
71 * @return WP_REST_Response
72 */
73 public static function get( $request ) {
74 $user_id = $request->get_param( 'user_id' );
75 $user = User_Collection::get_by_various( $user_id );
76
77 if ( is_wp_error( $user ) ) {
78 return $user;
79 }
80
81 // redirect to canonical URL if it is not an ActivityPub request
82 if ( ! is_activitypub_request() ) {
83 header( 'Location: ' . $user->get_canonical_url(), true, 301 );
84 exit;
85 }
86
87 /*
88 * Action triggerd prior to the ActivityPub profile being created and sent to the client
89 */
90 \do_action( 'activitypub_rest_users_pre' );
91
92 $user->set_context(
93 Activity::CONTEXT
94 );
95
96 $json = $user->to_array();
97
98 $response = new WP_REST_Response( $json, 200 );
99 $response->header( 'Content-Type', 'application/activity+json' );
100
101 return $response;
102 }
103
104
105 /**
106 * Endpoint for remote follow UI/Block
107 *
108 * @param WP_REST_Request $request The request object.
109 *
110 * @return void|string The URL to the remote follow page
111 */
112 public static function remote_follow_get( WP_REST_Request $request ) {
113 $resource = $request->get_param( 'resource' );
114 $user_id = $request->get_param( 'user_id' );
115 $user = User_Collection::get_by_various( $user_id );
116
117 if ( is_wp_error( $user ) ) {
118 return $user;
119 }
120
121 $template = Webfinger::get_remote_follow_endpoint( $resource );
122
123 if ( is_wp_error( $template ) ) {
124 return $template;
125 }
126
127 $resource = $user->get_resource();
128 $url = str_replace( '{uri}', $resource, $template );
129
130 return new WP_REST_Response(
131 array( 'url' => $url ),
132 200
133 );
134 }
135
136 /**
137 * The supported parameters
138 *
139 * @return array list of parameters
140 */
141 public static function request_parameters() {
142 $params = array();
143
144 $params['page'] = array(
145 'type' => 'string',
146 );
147
148 $params['user_id'] = array(
149 'required' => true,
150 'type' => 'string',
151 );
152
153 return $params;
154 }
155 }
156