PluginProbe
ActivityPub / 3.2.3
ActivityPub v3.2.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-inbox.php

class-inbox.php in ActivityPub 3.2.3, at includes/rest/class-inbox.php

329 lines 7.7 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_Response;
7 use Activitypub\Activity\Activity;
8 use Activitypub\Collection\Users as User_Collection;
9
10 use function Activitypub\get_context;
11 use function Activitypub\object_to_uri;
12 use function Activitypub\url_to_authorid;
13 use function Activitypub\get_rest_url_by_path;
14 use function Activitypub\get_masked_wp_version;
15 use function Activitypub\extract_recipients_from_activity;
16
17 /**
18 * ActivityPub Inbox REST-Class
19 *
20 * @author Matthias Pfefferle
21 *
22 * @see https://www.w3.org/TR/activitypub/#inbox
23 */
24 class Inbox {
25 /**
26 * Initialize the class, registering WordPress hooks
27 */
28 public static function init() {
29 self::register_routes();
30 }
31
32 /**
33 * Register routes
34 */
35 public static function register_routes() {
36 \register_rest_route(
37 ACTIVITYPUB_REST_NAMESPACE,
38 '/inbox',
39 array(
40 array(
41 'methods' => WP_REST_Server::CREATABLE,
42 'callback' => array( self::class, 'shared_inbox_post' ),
43 'args' => self::shared_inbox_post_parameters(),
44 'permission_callback' => '__return_true',
45 ),
46 )
47 );
48
49 \register_rest_route(
50 ACTIVITYPUB_REST_NAMESPACE,
51 '/(users|actors)/(?P<user_id>[\w\-\.]+)/inbox',
52 array(
53 array(
54 'methods' => WP_REST_Server::CREATABLE,
55 'callback' => array( self::class, 'user_inbox_post' ),
56 'args' => self::user_inbox_post_parameters(),
57 'permission_callback' => '__return_true',
58 ),
59 array(
60 'methods' => WP_REST_Server::READABLE,
61 'callback' => array( self::class, 'user_inbox_get' ),
62 'args' => self::user_inbox_get_parameters(),
63 'permission_callback' => '__return_true',
64 ),
65 )
66 );
67 }
68
69 /**
70 * Renders the user-inbox
71 *
72 * @param WP_REST_Request $request
73 * @return WP_REST_Response
74 */
75 public static function user_inbox_get( $request ) {
76 $user_id = $request->get_param( 'user_id' );
77 $user = User_Collection::get_by_various( $user_id );
78
79 if ( is_wp_error( $user ) ) {
80 return $user;
81 }
82
83 $page = $request->get_param( 'page', 0 );
84
85 /*
86 * Action triggerd prior to the ActivityPub profile being created and sent to the client
87 */
88 \do_action( 'activitypub_rest_inbox_pre' );
89
90 $json = new \stdClass();
91
92 $json->{'@context'} = get_context();
93 $json->id = get_rest_url_by_path( sprintf( 'actors/%d/inbox', $user->get__id() ) );
94 $json->generator = 'http://wordpress.org/?v=' . get_masked_wp_version();
95 $json->type = 'OrderedCollectionPage';
96 $json->partOf = get_rest_url_by_path( sprintf( 'actors/%d/inbox', $user->get__id() ) ); // phpcs:ignore
97 $json->totalItems = 0; // phpcs:ignore
98 $json->orderedItems = array(); // phpcs:ignore
99 $json->first = $json->partOf; // phpcs:ignore
100
101 // filter output
102 $json = \apply_filters( 'activitypub_rest_inbox_array', $json );
103
104 /*
105 * Action triggerd after the ActivityPub profile has been created and sent to the client
106 */
107 \do_action( 'activitypub_inbox_post' );
108
109 $rest_response = new WP_REST_Response( $json, 200 );
110 $rest_response->header( 'Content-Type', 'application/activity+json; charset=' . get_option( 'blog_charset' ) );
111
112 return $rest_response;
113 }
114
115 /**
116 * Handles user-inbox requests
117 *
118 * @param WP_REST_Request $request
119 *
120 * @return WP_REST_Response
121 */
122 public static function user_inbox_post( $request ) {
123 $user_id = $request->get_param( 'user_id' );
124 $user = User_Collection::get_by_various( $user_id );
125
126 if ( is_wp_error( $user ) ) {
127 return $user;
128 }
129
130 $data = $request->get_json_params();
131 $activity = Activity::init_from_array( $data );
132 $type = $request->get_param( 'type' );
133 $type = \strtolower( $type );
134
135 \do_action( 'activitypub_inbox', $data, $user->get__id(), $type, $activity );
136 \do_action( "activitypub_inbox_{$type}", $data, $user->get__id(), $activity );
137
138 $rest_response = new WP_REST_Response( array(), 202 );
139 $rest_response->header( 'Content-Type', 'application/activity+json; charset=' . get_option( 'blog_charset' ) );
140
141 return $rest_response;
142 }
143
144 /**
145 * The shared inbox
146 *
147 * @param WP_REST_Request $request
148 *
149 * @return WP_REST_Response
150 */
151 public static function shared_inbox_post( $request ) {
152 $data = $request->get_json_params();
153 $activity = Activity::init_from_array( $data );
154 $type = $request->get_param( 'type' );
155 $type = \strtolower( $type );
156
157 \do_action( 'activitypub_inbox', $data, null, $type, $activity );
158 \do_action( "activitypub_inbox_{$type}", $data, null, $activity );
159
160 $rest_response = new WP_REST_Response( array(), 202 );
161 $rest_response->header( 'Content-Type', 'application/activity+json; charset=' . get_option( 'blog_charset' ) );
162
163 return $rest_response;
164 }
165
166 /**
167 * The supported parameters
168 *
169 * @return array list of parameters
170 */
171 public static function user_inbox_get_parameters() {
172 $params = array();
173
174 $params['page'] = array(
175 'type' => 'integer',
176 );
177
178 $params['user_id'] = array(
179 'required' => true,
180 'type' => 'string',
181 );
182
183 return $params;
184 }
185
186 /**
187 * The supported parameters
188 *
189 * @return array list of parameters
190 */
191 public static function user_inbox_post_parameters() {
192 $params = array();
193
194 $params['page'] = array(
195 'type' => 'integer',
196 );
197
198 $params['user_id'] = array(
199 'required' => true,
200 'type' => 'string',
201 );
202
203 $params['id'] = array(
204 'required' => true,
205 'sanitize_callback' => 'esc_url_raw',
206 );
207
208 $params['actor'] = array(
209 'required' => true,
210 'sanitize_callback' => function ( $param, $request, $key ) {
211 return object_to_uri( $param );
212 },
213 );
214
215 $params['type'] = array(
216 'required' => true,
217 //'type' => 'enum',
218 //'enum' => array( 'Create' ),
219 //'sanitize_callback' => function ( $param, $request, $key ) {
220 // return \strtolower( $param );
221 //},
222 );
223
224 $params['object'] = array(
225 'required' => true,
226 );
227
228 return $params;
229 }
230
231 /**
232 * The supported parameters
233 *
234 * @return array list of parameters
235 */
236 public static function shared_inbox_post_parameters() {
237 $params = array();
238
239 $params['page'] = array(
240 'type' => 'integer',
241 );
242
243 $params['id'] = array(
244 'required' => true,
245 'type' => 'string',
246 'sanitize_callback' => 'esc_url_raw',
247 );
248
249 $params['actor'] = array(
250 'required' => true,
251 //'type' => array( 'object', 'string' ),
252 'sanitize_callback' => function ( $param, $request, $key ) {
253 return object_to_uri( $param );
254 },
255 );
256
257 $params['type'] = array(
258 'required' => true,
259 //'type' => 'enum',
260 //'enum' => array( 'Create' ),
261 //'sanitize_callback' => function ( $param, $request, $key ) {
262 // return \strtolower( $param );
263 //},
264 );
265
266 $params['object'] = array(
267 'required' => true,
268 //'type' => 'object',
269 );
270
271 $params['to'] = array(
272 'required' => false,
273 'sanitize_callback' => function ( $param, $request, $key ) {
274 if ( \is_string( $param ) ) {
275 $param = array( $param );
276 }
277
278 return $param;
279 },
280 );
281
282 $params['cc'] = array(
283 'sanitize_callback' => function ( $param, $request, $key ) {
284 if ( \is_string( $param ) ) {
285 $param = array( $param );
286 }
287
288 return $param;
289 },
290 );
291
292 $params['bcc'] = array(
293 'sanitize_callback' => function ( $param, $request, $key ) {
294 if ( \is_string( $param ) ) {
295 $param = array( $param );
296 }
297
298 return $param;
299 },
300 );
301
302 return $params;
303 }
304
305 /**
306 * Get local user recipients
307 *
308 * @param array $data
309 *
310 * @return array The list of local users
311 */
312 public static function get_recipients( $data ) {
313 $recipients = extract_recipients_from_activity( $data );
314 $users = array();
315
316 foreach ( $recipients as $recipient ) {
317 $user_id = url_to_authorid( $recipient );
318
319 $user = get_user_by( 'id', $user_id );
320
321 if ( $user ) {
322 $users[] = $user;
323 }
324 }
325
326 return $users;
327 }
328 }
329