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

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