@@ -525,40 +525,60 @@ function extract_recipients_from_activity( $data ) {
525
525
$ recipient_items = array ();
526
526
527
527
foreach ( array ( 'to ' , 'bto ' , 'cc ' , 'bcc ' , 'audience ' ) as $ i ) {
528
- if ( array_key_exists ( $ i , $ data ) ) {
529
- if ( is_array ( $ data [ $ i ] ) ) {
530
- $ recipient = $ data [ $ i ];
531
- } else {
532
- $ recipient = array ( $ data [ $ i ] );
533
- }
534
- $ recipient_items = array_merge ( $ recipient_items , $ recipient );
535
- }
536
-
537
- if ( is_array ( $ data ['object ' ] ) && array_key_exists ( $ i , $ data ['object ' ] ) ) {
538
- if ( is_array ( $ data ['object ' ][ $ i ] ) ) {
539
- $ recipient = $ data ['object ' ][ $ i ];
540
- } else {
541
- $ recipient = array ( $ data ['object ' ][ $ i ] );
542
- }
543
- $ recipient_items = array_merge ( $ recipient_items , $ recipient );
544
- }
528
+ $ recipient_items = \array_merge ( $ recipient_items , extract_recipients_from_activity_property ( $ i , $ data ) );
545
529
}
546
530
531
+ return \array_unique ( $ recipient_items );
532
+ }
533
+
534
+ /**
535
+ * Extract recipient URLs from a specific property of an Activity object.
536
+ *
537
+ * @param string $property The property to extract recipients from (e.g., 'to', 'cc').
538
+ * @param array $data The Activity object as array.
539
+ *
540
+ * @return array The list of user URLs.
541
+ */
542
+ function extract_recipients_from_activity_property ( $ property , $ data ) {
547
543
$ recipients = array ();
548
544
549
- // Flatten array.
550
- foreach ( $ recipient_items as $ recipient ) {
551
- if ( is_array ( $ recipient ) ) {
552
- // Check if recipient is an object.
553
- if ( array_key_exists ( 'id ' , $ recipient ) ) {
554
- $ recipients [] = $ recipient ['id ' ];
555
- }
556
- } else {
557
- $ recipients [] = $ recipient ;
558
- }
545
+ if ( ! empty ( $ data [ $ property ] ) ) {
546
+ $ recipients = $ data [ $ property ];
547
+ } elseif ( ! empty ( $ data ['object ' ][ $ property ] ) ) {
548
+ $ recipients = $ data ['object ' ][ $ property ];
549
+ }
550
+
551
+ $ recipients = \array_map ( '\Activitypub\object_to_uri ' , (array ) $ recipients );
552
+
553
+ return \array_unique ( \array_filter ( $ recipients ) );
554
+ }
555
+
556
+ /**
557
+ * Determine the visibility of the activity based on its recipients.
558
+ *
559
+ * @param array $activity The activity data.
560
+ *
561
+ * @return string The visibility level: 'public', 'private', or 'direct'.
562
+ */
563
+ function get_activity_visibility ( $ activity ) {
564
+ // Set default visibility for specific activity types.
565
+ if ( ! empty ( $ activity ['type ' ] ) && in_array ( $ activity ['type ' ], array ( 'Accept ' , 'Delete ' , 'Follow ' , 'Reject ' , 'Undo ' ), true ) ) {
566
+ return ACTIVITYPUB_CONTENT_VISIBILITY_PRIVATE ;
567
+ }
568
+
569
+ // Check 'to' field for public visibility.
570
+ $ to = extract_recipients_from_activity_property ( 'to ' , $ activity );
571
+ if ( ! empty ( array_intersect ( $ to , ACTIVITYPUB_PUBLIC_AUDIENCE_IDENTIFIERS ) ) ) {
572
+ return ACTIVITYPUB_CONTENT_VISIBILITY_PUBLIC ;
573
+ }
574
+
575
+ // Check 'cc' field for quiet public visibility.
576
+ $ cc = extract_recipients_from_activity_property ( 'cc ' , $ activity );
577
+ if ( ! empty ( array_intersect ( $ cc , ACTIVITYPUB_PUBLIC_AUDIENCE_IDENTIFIERS ) ) ) {
578
+ return ACTIVITYPUB_CONTENT_VISIBILITY_QUIET_PUBLIC ;
559
579
}
560
580
561
- return array_unique ( $ recipients ) ;
581
+ return ACTIVITYPUB_CONTENT_VISIBILITY_PRIVATE ;
562
582
}
563
583
564
584
/**
@@ -696,7 +716,7 @@ function url_to_commentid( $url ) {
696
716
*
697
717
* @param array|string $data The ActivityPub object.
698
718
*
699
- * @return string The URI of the ActivityPub object
719
+ * @return string The URI of the ActivityPub object.
700
720
*/
701
721
function object_to_uri ( $ data ) {
702
722
// Check whether it is already simple.
0 commit comments