PluginProbe
ActivityPub / 3.2.4
ActivityPub v3.2.4
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.4, at includes/rest/class-inbox.php

295 lines 7.6 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['user_id'] = array(
195 'required' => true,
196 'type' => 'string',
197 );
198
199 $params['id'] = array(
200 'required' => true,
201 'sanitize_callback' => 'esc_url_raw',
202 );
203
204 $params['actor'] = array(
205 'required' => true,
206 // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed, VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
207 'sanitize_callback' => function ( $param, $request, $key ) {
208 return object_to_uri( $param );
209 },
210 );
211
212 $params['type'] = array(
213 'required' => true,
214 );
215
216 $params['object'] = array(
217 'required' => true,
218 'validate_callback' => function ( $param, $request, $key ) {
219 return apply_filters( 'activitypub_validate_object', true, $param, $request, $key );
220 },
221 );
222
223 return $params;
224 }
225
226 /**
227 * The supported parameters
228 *
229 * @return array list of parameters
230 */
231 public static function shared_inbox_post_parameters() {
232 $params = self::user_inbox_post_parameters();
233
234 $params['to'] = array(
235 'required' => false,
236 // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed, VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
237 'sanitize_callback' => function ( $param, $request, $key ) {
238 if ( \is_string( $param ) ) {
239 $param = array( $param );
240 }
241
242 return $param;
243 },
244 );
245
246 $params['cc'] = array(
247 // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed, VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
248 'sanitize_callback' => function ( $param, $request, $key ) {
249 if ( \is_string( $param ) ) {
250 $param = array( $param );
251 }
252
253 return $param;
254 },
255 );
256
257 $params['bcc'] = array(
258 // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed, VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
259 'sanitize_callback' => function ( $param, $request, $key ) {
260 if ( \is_string( $param ) ) {
261 $param = array( $param );
262 }
263
264 return $param;
265 },
266 );
267
268 return $params;
269 }
270
271 /**
272 * Get local user recipients
273 *
274 * @param array $data
275 *
276 * @return array The list of local users
277 */
278 public static function get_recipients( $data ) {
279 $recipients = extract_recipients_from_activity( $data );
280 $users = array();
281
282 foreach ( $recipients as $recipient ) {
283 $user_id = url_to_authorid( $recipient );
284
285 $user = get_user_by( 'id', $user_id );
286
287 if ( $user ) {
288 $users[] = $user;
289 }
290 }
291
292 return $users;
293 }
294 }
295