PluginProbe
ActivityPub / 2.1.1
ActivityPub v2.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-inbox.php

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

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