Skip to content

Commit bf0b51c

Browse files
committed
only save public activities
first step to #72
1 parent a27a4fc commit bf0b51c

File tree

1 file changed

+55
-5
lines changed

1 file changed

+55
-5
lines changed

includes/rest/class-inbox.php

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,12 @@ public static function handle_create( $object, $user_id ) {
437437
return;
438438
}
439439

440+
// check if Activity is public or not
441+
if ( ! self::is_activity_public( $object ) ) {
442+
// @todo maybe send email
443+
return;
444+
}
445+
440446
$comment_post_id = \url_to_postid( $object['object']['inReplyTo'] );
441447

442448
// save only replys and reactions
@@ -473,21 +479,53 @@ public static function handle_create( $object, $user_id ) {
473479
\add_action( 'check_comment_flood', 'check_comment_flood_db', 10, 4 );
474480
}
475481

482+
/**
483+
* Extract recipient URLs from Activity object
484+
*
485+
* @param array $data
486+
*
487+
* @return array The list of user URLs
488+
*/
476489
public static function extract_recipients( $data ) {
477-
$recipients = array();
478-
$users = array();
490+
$recipient_items = array();
479491

480492
foreach ( array( 'to', 'bto', 'cc', 'bcc', 'audience' ) as $i ) {
481493
if ( array_key_exists( $i, $data ) ) {
482-
$recipients = array_merge( $recipients, $data[ $i ] );
494+
$recipient_items = array_merge( $recipient_items, $data[ $i ] );
483495
}
484496

485497
if ( array_key_exists( $i, $data['object'] ) ) {
486-
$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;
487513
}
488514
}
489515

490-
$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();
491529

492530
foreach ( $recipients as $recipient ) {
493531
$user_id = \Activitypub\url_to_authorid( $recipient );
@@ -501,4 +539,16 @@ public static function extract_recipients( $data ) {
501539

502540
return $users;
503541
}
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+
}
504554
}

0 commit comments

Comments
 (0)