@@ -33,7 +33,7 @@ public static function register_routes() {
3333 array (
3434 'methods ' => \WP_REST_Server::EDITABLE ,
3535 'callback ' => array ( '\Activitypub\Rest\Inbox ' , 'shared_inbox_post ' ),
36- 'args ' => self ::shared_inbox_request_parameters (),
36+ 'args ' => self ::shared_inbox_post_parameters (),
3737 'permission_callback ' => '__return_true ' ,
3838 ),
3939 )
@@ -46,12 +46,13 @@ public static function register_routes() {
4646 array (
4747 'methods ' => \WP_REST_Server::EDITABLE ,
4848 'callback ' => array ( '\Activitypub\Rest\Inbox ' , 'user_inbox_post ' ),
49- 'args ' => self ::user_inbox_request_parameters (),
49+ 'args ' => self ::user_inbox_post_parameters (),
5050 'permission_callback ' => '__return_true ' ,
5151 ),
5252 array (
5353 'methods ' => \WP_REST_Server::READABLE ,
5454 'callback ' => array ( '\Activitypub\Rest\Inbox ' , 'user_inbox_get ' ),
55+ 'args ' => self ::user_inbox_get_parameters (),
5556 'permission_callback ' => '__return_true ' ,
5657 ),
5758 )
@@ -195,7 +196,7 @@ public static function shared_inbox_post( $request ) {
195196 *
196197 * @return array list of parameters
197198 */
198- public static function user_inbox_request_parameters () {
199+ public static function user_inbox_get_parameters () {
199200 $ params = array ();
200201
201202 $ params ['page ' ] = array (
@@ -205,6 +206,32 @@ public static function user_inbox_request_parameters() {
205206 $ params ['user_id ' ] = array (
206207 'required ' => true ,
207208 'type ' => 'integer ' ,
209+ 'validate_callback ' => function ( $ param , $ request , $ key ) {
210+ return user_can ( $ param , 'publish_posts ' );
211+ },
212+ );
213+
214+ return $ params ;
215+ }
216+
217+ /**
218+ * The supported parameters
219+ *
220+ * @return array list of parameters
221+ */
222+ public static function user_inbox_post_parameters () {
223+ $ params = array ();
224+
225+ $ params ['page ' ] = array (
226+ 'type ' => 'integer ' ,
227+ );
228+
229+ $ params ['user_id ' ] = array (
230+ 'required ' => true ,
231+ 'type ' => 'integer ' ,
232+ 'validate_callback ' => function ( $ param , $ request , $ key ) {
233+ return user_can ( $ param , 'publish_posts ' );
234+ },
208235 );
209236
210237 $ params ['id ' ] = array (
@@ -243,7 +270,7 @@ public static function user_inbox_request_parameters() {
243270 *
244271 * @return array list of parameters
245272 */
246- public static function shared_inbox_request_parameters () {
273+ public static function shared_inbox_post_parameters () {
247274 $ params = array ();
248275
249276 $ params ['page ' ] = array (
@@ -410,6 +437,12 @@ public static function handle_create( $object, $user_id ) {
410437 return ;
411438 }
412439
440+ // check if Activity is public or not
441+ if ( ! self ::is_activity_public ( $ object ) ) {
442+ // @todo maybe send email
443+ return ;
444+ }
445+
413446 $ comment_post_id = \url_to_postid ( $ object ['object ' ]['inReplyTo ' ] );
414447
415448 // save only replys and reactions
@@ -446,21 +479,53 @@ public static function handle_create( $object, $user_id ) {
446479 \add_action ( 'check_comment_flood ' , 'check_comment_flood_db ' , 10 , 4 );
447480 }
448481
482+ /**
483+ * Extract recipient URLs from Activity object
484+ *
485+ * @param array $data
486+ *
487+ * @return array The list of user URLs
488+ */
449489 public static function extract_recipients ( $ data ) {
450- $ recipients = array ();
451- $ users = array ();
490+ $ recipient_items = array ();
452491
453492 foreach ( array ( 'to ' , 'bto ' , 'cc ' , 'bcc ' , 'audience ' ) as $ i ) {
454493 if ( array_key_exists ( $ i , $ data ) ) {
455- $ recipients = array_merge ( $ recipients , $ data [ $ i ] );
494+ $ recipient_items = array_merge ( $ recipient_items , $ data [ $ i ] );
456495 }
457496
458497 if ( array_key_exists ( $ i , $ data ['object ' ] ) ) {
459- $ recipients = array_merge ( $ recipients , $ data [ $ i ] );
498+ $ recipient_items = array_merge ( $ recipient_items , $ data [ $ i ] );
499+ }
500+ }
501+
502+ $ recipients = array ();
503+
504+ // flatten array
505+ foreach ( $ recipient_items as $ recipient ) {
506+ if ( is_array ( $ recipient ) ) {
507+ // check if recipient is an object
508+ if ( array_key_exists ( 'id ' , $ recipient ) ) {
509+ $ recipients [] = $ recipient ['id ' ];
510+ }
511+ } else {
512+ $ recipients [] = $ recipient ;
460513 }
461514 }
462515
463- $ recipients = array_unique ( $ recipients );
516+ return array_unique ( $ recipients );
517+ }
518+
519+ /**
520+ * Get local user recipients
521+ *
522+ * @param array $data
523+ *
524+ * @return array The list of local users
525+ */
526+ public static function get_recipients ( $ data ) {
527+ $ recipients = self ::extract_recipients ( $ data );
528+ $ users = array ();
464529
465530 foreach ( $ recipients as $ recipient ) {
466531 $ user_id = \Activitypub \url_to_authorid ( $ recipient );
@@ -474,4 +539,16 @@ public static function extract_recipients( $data ) {
474539
475540 return $ users ;
476541 }
542+
543+ /**
544+ * Check if passed Activity is Public
545+ *
546+ * @param array $data
547+ * @return boolean
548+ */
549+ public static function is_activity_public ( $ data ) {
550+ $ recipients = self ::extract_recipients ( $ data );
551+
552+ return in_array ( 'https://www.w3.org/ns/activitystreams#Public ' , $ recipients , true );
553+ }
477554}
0 commit comments