PluginProbe
ActivityPub / 7.7.0
ActivityPub v7.7.0
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 7.7.0, at includes/rest/class-actors-controller.php

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