PluginProbe
ActivityPub / 8.1.1
ActivityPub v8.1.1
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-actors-controller.php

class-actors-controller.php in ActivityPub 8.1.1, at includes/rest/class-actors-controller.php

392 lines 10.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * ActivityPub Actors REST-Class
4 *
5 * @package Activitypub
6 */
7
8 namespace Activitypub\Rest;
9
10 use Activitypub\Collection\Actors as Actor_Collection;
11 use Activitypub\Webfinger;
12
13 /**
14 * ActivityPub Actors REST-Class.
15 *
16 * @author Matthias Pfefferle
17 *
18 * @see https://www.w3.org/TR/activitypub/#followers
19 */
20 class Actors_Controller extends \WP_REST_Controller {
21 use Verification;
22
23 /**
24 * The namespace of this controller's route.
25 *
26 * @var string
27 */
28 protected $namespace = ACTIVITYPUB_REST_NAMESPACE;
29
30 /**
31 * The base of this controller's route.
32 *
33 * @var string
34 */
35 protected $rest_base = '(?:users|actors)\/(?P<user_id>[-]?\d+)';
36
37 /**
38 * Register routes.
39 */
40 public function register_routes() {
41 \register_rest_route(
42 $this->namespace,
43 '/' . $this->rest_base,
44 array(
45 'args' => array(
46 'user_id' => array(
47 'description' => 'The ID of the actor.',
48 'type' => 'integer',
49 'required' => true,
50 'validate_callback' => array( $this, 'validate_user_id' ),
51 ),
52 ),
53 array(
54 'methods' => \WP_REST_Server::READABLE,
55 'callback' => array( $this, 'get_item' ),
56 'permission_callback' => array( $this, 'verify_signature' ),
57 ),
58 'schema' => array( $this, 'get_public_item_schema' ),
59 )
60 );
61
62 \register_rest_route(
63 $this->namespace,
64 '/' . $this->rest_base . '/remote-follow',
65 array(
66 'args' => array(
67 'user_id' => array(
68 'description' => 'The ID of the actor.',
69 'type' => 'integer',
70 'required' => true,
71 'validate_callback' => array( $this, 'validate_user_id' ),
72 ),
73 ),
74 array(
75 'methods' => \WP_REST_Server::READABLE,
76 'callback' => array( $this, 'get_remote_follow_item' ),
77 'permission_callback' => '__return_true',
78 'args' => array(
79 'resource' => array(
80 'description' => 'The resource to follow.',
81 'type' => 'string',
82 'required' => true,
83 ),
84 ),
85 ),
86 )
87 );
88 }
89
90 /**
91 * Retrieves a single actor.
92 *
93 * @param \WP_REST_Request $request Full details about the request.
94 * @return \WP_REST_Response|\WP_Error Response object on success, or WP_Error object on failure.
95 */
96 public function get_item( $request ) {
97 $user_id = $request->get_param( 'user_id' );
98 $user = Actor_Collection::get_by_id( $user_id );
99
100 /**
101 * Action triggered prior to the ActivityPub profile being created and sent to the client.
102 */
103 \do_action( 'activitypub_rest_users_pre' );
104
105 $data = $user->to_array();
106
107 $response = \rest_ensure_response( $data );
108 $response->header( 'Content-Type', 'application/activity+json; charset=' . \get_option( 'blog_charset' ) );
109 $response->header( 'Link', \sprintf( '<%1$s>; rel="alternate"; type="application/activity+json"', $user->get_id() ) );
110
111 return $response;
112 }
113
114 /**
115 * Retrieves the remote follow endpoint.
116 *
117 * @param \WP_REST_Request $request Full details about the request.
118 * @return \WP_REST_Response|\WP_Error Response object on success, or WP_Error object on failure.
119 */
120 public function get_remote_follow_item( $request ) {
121 $resource = $request->get_param( 'resource' );
122 $user_id = $request->get_param( 'user_id' );
123 $user = Actor_Collection::get_by_id( $user_id );
124
125 $template = Webfinger::get_remote_follow_endpoint( $resource );
126
127 if ( \is_wp_error( $template ) ) {
128 return $template;
129 }
130
131 $resource = $user->get_webfinger();
132 $url = \str_replace( '{uri}', $resource, $template );
133
134 return \rest_ensure_response(
135 array(
136 'url' => $url,
137 'template' => $template,
138 )
139 );
140 }
141
142 /**
143 * Retrieves the actor schema, conforming to JSON Schema.
144 *
145 * @return array Item schema data.
146 */
147 public function get_item_schema() {
148 if ( $this->schema ) {
149 return $this->add_additional_fields_schema( $this->schema );
150 }
151
152 $this->schema = array(
153 '$schema' => 'http://json-schema.org/draft-04/schema#',
154 'title' => 'actor',
155 'type' => 'object',
156 'properties' => array(
157 '@context' => array(
158 'description' => 'The JSON-LD context for the response.',
159 'type' => array( 'array', 'object' ),
160 'readonly' => true,
161 ),
162 'id' => array(
163 'description' => 'The unique identifier for the actor.',
164 'type' => 'string',
165 'format' => 'uri',
166 'readonly' => true,
167 ),
168 'type' => array(
169 'description' => 'The type of the actor.',
170 'type' => 'string',
171 'enum' => array( 'Person', 'Service', 'Organization', 'Application', 'Group' ),
172 'readonly' => true,
173 ),
174 'attachment' => array(
175 'description' => 'Additional information attached to the actor.',
176 'type' => 'array',
177 'items' => array(
178 'type' => 'object',
179 'properties' => array(
180 'type' => array(
181 'type' => 'string',
182 'enum' => array( 'PropertyValue', 'Link' ),
183 ),
184 'name' => array(
185 'type' => 'string',
186 ),
187 'value' => array(
188 'type' => 'string',
189 ),
190 'href' => array(
191 'type' => 'string',
192 'format' => 'uri',
193 ),
194 'rel' => array(
195 'type' => 'array',
196 'items' => array(
197 'type' => 'string',
198 ),
199 ),
200 ),
201 ),
202 'readonly' => true,
203 ),
204 'name' => array(
205 'description' => 'The display name of the actor.',
206 'type' => 'string',
207 'readonly' => true,
208 ),
209 'icon' => array(
210 'description' => 'The icon/avatar of the actor.',
211 'type' => 'object',
212 'properties' => array(
213 'type' => array(
214 'type' => 'string',
215 ),
216 'url' => array(
217 'type' => 'string',
218 'format' => 'uri',
219 ),
220 ),
221 'readonly' => true,
222 ),
223 'published' => array(
224 'description' => 'The date the actor was published.',
225 'type' => 'string',
226 'format' => 'date-time',
227 'readonly' => true,
228 ),
229 'summary' => array(
230 'description' => 'A summary about the actor.',
231 'type' => 'string',
232 'readonly' => true,
233 ),
234 'tag' => array(
235 'description' => 'Tags associated with the actor.',
236 'type' => 'array',
237 'items' => array(
238 'type' => 'object',
239 'properties' => array(
240 'type' => array(
241 'type' => 'string',
242 ),
243 'href' => array(
244 'type' => 'string',
245 'format' => 'uri',
246 ),
247 'name' => array(
248 'type' => 'string',
249 ),
250 ),
251 ),
252 'readonly' => true,
253 ),
254 'url' => array(
255 'description' => 'The URL to the actor\'s profile page.',
256 'type' => 'string',
257 'format' => 'uri',
258 'readonly' => true,
259 ),
260 'inbox' => array(
261 'description' => 'The inbox endpoint for the actor.',
262 'type' => 'string',
263 'format' => 'uri',
264 'readonly' => true,
265 ),
266 'outbox' => array(
267 'description' => 'The outbox endpoint for the actor.',
268 'type' => 'string',
269 'format' => 'uri',
270 'readonly' => true,
271 ),
272 'following' => array(
273 'description' => 'The following endpoint for the actor.',
274 'type' => 'string',
275 'format' => 'uri',
276 'readonly' => true,
277 ),
278 'followers' => array(
279 'description' => 'The followers endpoint for the actor.',
280 'type' => 'string',
281 'format' => 'uri',
282 'readonly' => true,
283 ),
284 'streams' => array(
285 'description' => 'The streams associated with the actor.',
286 'type' => 'array',
287 'readonly' => true,
288 ),
289 'preferredUsername' => array(
290 'description' => 'The preferred username of the actor.',
291 'type' => 'string',
292 'readonly' => true,
293 ),
294 'publicKey' => array(
295 'description' => 'The public key information for the actor.',
296 'type' => 'object',
297 'properties' => array(
298 'id' => array(
299 'type' => 'string',
300 'format' => 'uri',
301 ),
302 'owner' => array(
303 'type' => 'string',
304 'format' => 'uri',
305 ),
306 'publicKeyPem' => array(
307 'type' => 'string',
308 ),
309 ),
310 'readonly' => true,
311 ),
312 'manuallyApprovesFollowers' => array(
313 'description' => 'Whether the actor manually approves followers.',
314 'type' => 'boolean',
315 'readonly' => true,
316 ),
317 'attributionDomains' => array(
318 'description' => 'The attribution domains for the actor.',
319 'type' => 'array',
320 'items' => array(
321 'type' => 'string',
322 ),
323 'readonly' => true,
324 ),
325 'featured' => array(
326 'description' => 'The featured collection endpoint for the actor.',
327 'type' => 'string',
328 'format' => 'uri',
329 'readonly' => true,
330 ),
331 'indexable' => array(
332 'description' => 'Whether the actor is indexable.',
333 'type' => 'boolean',
334 'readonly' => true,
335 ),
336 'webfinger' => array(
337 'description' => 'The webfinger identifier for the actor.',
338 'type' => 'string',
339 'readonly' => true,
340 ),
341 'discoverable' => array(
342 'description' => 'Whether the actor is discoverable.',
343 'type' => 'boolean',
344 'readonly' => true,
345 ),
346 'generator' => array(
347 'description' => 'The generator of the object.',
348 'type' => 'object',
349 'properties' => array(
350 'type' => array(
351 'type' => 'string',
352 ),
353 'implements' => array(
354 'type' => 'array',
355 'items' => array(
356 'type' => 'object',
357 'properties' => array(
358 'href' => array(
359 'type' => 'string',
360 'format' => 'uri',
361 ),
362 'name' => array(
363 'type' => 'string',
364 ),
365 ),
366 ),
367 ),
368 ),
369 'readonly' => true,
370 ),
371 ),
372 );
373
374 return $this->add_additional_fields_schema( $this->schema );
375 }
376
377 /**
378 * Validates the user_id parameter.
379 *
380 * @param mixed $user_id The user_id parameter.
381 * @return bool|\WP_Error True if the user_id is valid, WP_Error otherwise.
382 */
383 public function validate_user_id( $user_id ) {
384 $user = Actor_Collection::get_by_id( $user_id );
385 if ( \is_wp_error( $user ) ) {
386 return $user;
387 }
388
389 return true;
390 }
391 }
392