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

380 lines 10.2 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>[\w\-\.]+)';
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 or username of the actor.',
46 'type' => 'string',
47 'required' => true,
48 'pattern' => '[\w\-\.]+',
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 or username of the actor.',
67 'type' => 'string',
68 'required' => true,
69 'pattern' => '[\w\-\.]+',
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_various( $user_id );
97
98 if ( \is_wp_error( $user ) ) {
99 return $user;
100 }
101
102 /**
103 * Action triggered prior to the ActivityPub profile being created and sent to the client.
104 */
105 \do_action( 'activitypub_rest_users_pre' );
106
107 $data = $user->to_array();
108
109 $response = \rest_ensure_response( $data );
110 $response->header( 'Content-Type', 'application/activity+json; charset=' . \get_option( 'blog_charset' ) );
111 $response->header( 'Link', \sprintf( '<%1$s>; rel="alternate"; type="application/activity+json"', $user->get_id() ) );
112
113 return $response;
114 }
115
116 /**
117 * Retrieves the remote follow endpoint.
118 *
119 * @param \WP_REST_Request $request Full details about the request.
120 * @return \WP_REST_Response|\WP_Error Response object on success, or WP_Error object on failure.
121 */
122 public function get_remote_follow_item( $request ) {
123 $resource = $request->get_param( 'resource' );
124 $user_id = $request->get_param( 'user_id' );
125 $user = Actor_Collection::get_by_various( $user_id );
126
127 if ( \is_wp_error( $user ) ) {
128 return $user;
129 }
130
131 $template = Webfinger::get_remote_follow_endpoint( $resource );
132
133 if ( \is_wp_error( $template ) ) {
134 return $template;
135 }
136
137 $resource = $user->get_webfinger();
138 $url = \str_replace( '{uri}', $resource, $template );
139
140 return \rest_ensure_response(
141 array(
142 'url' => $url,
143 'template' => $template,
144 )
145 );
146 }
147
148 /**
149 * Retrieves the actor schema, conforming to JSON Schema.
150 *
151 * @return array Item schema data.
152 */
153 public function get_item_schema() {
154 if ( $this->schema ) {
155 return $this->add_additional_fields_schema( $this->schema );
156 }
157
158 $this->schema = array(
159 '$schema' => 'http://json-schema.org/draft-04/schema#',
160 'title' => 'actor',
161 'type' => 'object',
162 'properties' => array(
163 '@context' => array(
164 'description' => 'The JSON-LD context for the response.',
165 'type' => array( 'array', 'object' ),
166 'readonly' => true,
167 ),
168 'id' => array(
169 'description' => 'The unique identifier for the actor.',
170 'type' => 'string',
171 'format' => 'uri',
172 'readonly' => true,
173 ),
174 'type' => array(
175 'description' => 'The type of the actor.',
176 'type' => 'string',
177 'enum' => array( 'Person', 'Service', 'Organization', 'Application', 'Group' ),
178 'readonly' => true,
179 ),
180 'attachment' => array(
181 'description' => 'Additional information attached to the actor.',
182 'type' => 'array',
183 'items' => array(
184 'type' => 'object',
185 'properties' => array(
186 'type' => array(
187 'type' => 'string',
188 'enum' => array( 'PropertyValue', 'Link' ),
189 ),
190 'name' => array(
191 'type' => 'string',
192 ),
193 'value' => array(
194 'type' => 'string',
195 ),
196 'href' => array(
197 'type' => 'string',
198 'format' => 'uri',
199 ),
200 'rel' => array(
201 'type' => 'array',
202 'items' => array(
203 'type' => 'string',
204 ),
205 ),
206 ),
207 ),
208 'readonly' => true,
209 ),
210 'name' => array(
211 'description' => 'The display name of the actor.',
212 'type' => 'string',
213 'readonly' => true,
214 ),
215 'icon' => array(
216 'description' => 'The icon/avatar of the actor.',
217 'type' => 'object',
218 'properties' => array(
219 'type' => array(
220 'type' => 'string',
221 ),
222 'url' => array(
223 'type' => 'string',
224 'format' => 'uri',
225 ),
226 ),
227 'readonly' => true,
228 ),
229 'published' => array(
230 'description' => 'The date the actor was published.',
231 'type' => 'string',
232 'format' => 'date-time',
233 'readonly' => true,
234 ),
235 'summary' => array(
236 'description' => 'A summary about the actor.',
237 'type' => 'string',
238 'readonly' => true,
239 ),
240 'tag' => array(
241 'description' => 'Tags associated with the actor.',
242 'type' => 'array',
243 'items' => array(
244 'type' => 'object',
245 'properties' => array(
246 'type' => array(
247 'type' => 'string',
248 ),
249 'href' => array(
250 'type' => 'string',
251 'format' => 'uri',
252 ),
253 'name' => array(
254 'type' => 'string',
255 ),
256 ),
257 ),
258 'readonly' => true,
259 ),
260 'url' => array(
261 'description' => 'The URL to the actor\'s profile page.',
262 'type' => 'string',
263 'format' => 'uri',
264 'readonly' => true,
265 ),
266 'inbox' => array(
267 'description' => 'The inbox endpoint for the actor.',
268 'type' => 'string',
269 'format' => 'uri',
270 'readonly' => true,
271 ),
272 'outbox' => array(
273 'description' => 'The outbox endpoint for the actor.',
274 'type' => 'string',
275 'format' => 'uri',
276 'readonly' => true,
277 ),
278 'following' => array(
279 'description' => 'The following endpoint for the actor.',
280 'type' => 'string',
281 'format' => 'uri',
282 'readonly' => true,
283 ),
284 'followers' => array(
285 'description' => 'The followers endpoint for the actor.',
286 'type' => 'string',
287 'format' => 'uri',
288 'readonly' => true,
289 ),
290 'streams' => array(
291 'description' => 'The streams associated with the actor.',
292 'type' => 'array',
293 'readonly' => true,
294 ),
295 'preferredUsername' => array(
296 'description' => 'The preferred username of the actor.',
297 'type' => 'string',
298 'readonly' => true,
299 ),
300 'publicKey' => array(
301 'description' => 'The public key information for the actor.',
302 'type' => 'object',
303 'properties' => array(
304 'id' => array(
305 'type' => 'string',
306 'format' => 'uri',
307 ),
308 'owner' => array(
309 'type' => 'string',
310 'format' => 'uri',
311 ),
312 'publicKeyPem' => array(
313 'type' => 'string',
314 ),
315 ),
316 'readonly' => true,
317 ),
318 'manuallyApprovesFollowers' => array(
319 'description' => 'Whether the actor manually approves followers.',
320 'type' => 'boolean',
321 'readonly' => true,
322 ),
323 'attributionDomains' => array(
324 'description' => 'The attribution domains for the actor.',
325 'type' => 'array',
326 'items' => array(
327 'type' => 'string',
328 ),
329 'readonly' => true,
330 ),
331 'featured' => array(
332 'description' => 'The featured collection endpoint for the actor.',
333 'type' => 'string',
334 'format' => 'uri',
335 'readonly' => true,
336 ),
337 'indexable' => array(
338 'description' => 'Whether the actor is indexable.',
339 'type' => 'boolean',
340 'readonly' => true,
341 ),
342 'webfinger' => array(
343 'description' => 'The webfinger identifier for the actor.',
344 'type' => 'string',
345 'readonly' => true,
346 ),
347 'discoverable' => array(
348 'description' => 'Whether the actor is discoverable.',
349 'type' => 'boolean',
350 'readonly' => true,
351 ),
352 'generator' => array(
353 'description' => 'The generator of the object.',
354 'type' => 'object',
355 'properties' => array(
356 'type' => array(
357 'type' => 'string',
358 ),
359 'implements' => array(
360 'type' => 'object',
361 'properties' => array(
362 'href' => array(
363 'type' => 'string',
364 'format' => 'uri',
365 ),
366 'name' => array(
367 'type' => 'string',
368 ),
369 ),
370 ),
371 ),
372 'readonly' => true,
373 ),
374 ),
375 );
376
377 return $this->add_additional_fields_schema( $this->schema );
378 }
379 }
380