]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/ActivityPub/Transmitter.php
Ensure to transmit the audience if the parent does so
[friendica.git] / src / Protocol / ActivityPub / Transmitter.php
index 4b63463b487b763193c313dadd5522fbf2faecee..8aaa5ba09ac4016101982f9e913cf879bfeb324d 100644 (file)
@@ -330,7 +330,7 @@ class Transmitter
                return [
                        'type' => 'Service',
                        'name' =>  App::PLATFORM . " '" . App::CODENAME . "' " . App::VERSION . '-' . DB_UPDATE_VERSION,
-                       'url' => DI::baseUrl()->get()
+                       'url' => (string)DI::baseUrl()
                ];
        }
 
@@ -444,7 +444,7 @@ class Transmitter
        }
 
        /**
-        * Get a minimal actror array for the C2S API
+        * Get a minimal actor array for the C2S API
         *
         * @param integer $cid
         * @return array
@@ -492,13 +492,12 @@ class Transmitter
         * Returns an array with permissions of the thread parent of the given item array
         *
         * @param array $item
-        * @param bool  $is_forum_thread
         *
         * @return array with permissions
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         * @throws \ImagickException
         */
-       private static function fetchPermissionBlockFromThreadParent(array $item, bool $is_forum_thread): array
+       private static function fetchPermissionBlockFromThreadParent(array $item, bool $is_group_thread): array
        {
                if (empty($item['thr-parent-id'])) {
                        return [];
@@ -514,6 +513,7 @@ class Transmitter
                        'cc' => [],
                        'bto' => [],
                        'bcc' => [],
+                       'audience' => [],
                ];
 
                $parent_profile = APContact::getByURL($parent['author-link']);
@@ -525,10 +525,10 @@ class Transmitter
                        $exclude[] = $item['owner-link'];
                }
 
-               $type = [Tag::TO => 'to', Tag::CC => 'cc', Tag::BTO => 'bto', Tag::BCC => 'bcc'];
-               foreach (Tag::getByURIId($item['thr-parent-id'], [Tag::TO, Tag::CC, Tag::BTO, Tag::BCC]) as $receiver) {
+               $type = [Tag::TO => 'to', Tag::CC => 'cc', Tag::BTO => 'bto', Tag::BCC => 'bcc', Tag::AUDIENCE => 'audience'];
+               foreach (Tag::getByURIId($item['thr-parent-id'], [Tag::TO, Tag::CC, Tag::BTO, Tag::BCC, Tag::AUDIENCE]) as $receiver) {
                        if (!empty($parent_profile['followers']) && $receiver['url'] == $parent_profile['followers'] && !empty($item_profile['followers'])) {
-                               if (!$is_forum_thread) {
+                               if (!$is_group_thread) {
                                        $permissions[$type[$receiver['type']]][] = $item_profile['followers'];
                                }
                        } elseif (!in_array($receiver['url'], $exclude)) {
@@ -557,22 +557,23 @@ class Transmitter
        /**
         * Creates an array of permissions from an item thread
         *
-        * @param array   $item      Item array
-        * @param boolean $blindcopy addressing via "bcc" or "cc"?
-        * @param integer $last_id   Last item id for adding receivers
+        * @param array   $item             Item array
+        * @param boolean $blindcopy        addressing via "bcc" or "cc"?
+        * @param boolean $expand_followers Expand the list of followers
+        * @param integer $last_id          Last item id for adding receivers
         *
         * @return array with permission data
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         * @throws \ImagickException
         */
-       private static function createPermissionBlockForItem(array $item, bool $blindcopy, int $last_id = 0): array
+       private static function createPermissionBlockForItem(array $item, bool $blindcopy, bool $expand_followers, int $last_id = 0): array
        {
                if ($last_id == 0) {
                        $last_id = $item['id'];
                }
 
                $always_bcc = false;
-               $is_forum   = false;
+               $is_group   = false;
                $follower   = '';
 
                // Check if we should always deliver our stuff via BCC
@@ -580,7 +581,7 @@ class Transmitter
                        $owner = User::getOwnerDataById($item['uid']);
                        if (!empty($owner)) {
                                $always_bcc = $owner['hide-friends'];
-                               $is_forum   = ($owner['account-type'] == User::ACCOUNT_TYPE_COMMUNITY) && $owner['manually-approve'];
+                               $is_group   = ($owner['account-type'] == User::ACCOUNT_TYPE_COMMUNITY) && $owner['manually-approve'];
 
                                $profile  = APContact::getByURL($owner['url'], false);
                                $follower = $profile['followers'] ?? '';
@@ -594,9 +595,48 @@ class Transmitter
                $parent = Post::selectFirst(['causer-link', 'post-reason'], ['id' => $item['parent']]);
                if (!empty($parent) && ($parent['post-reason'] == Item::PR_ANNOUNCEMENT) && !empty($parent['causer-link'])) {
                        $profile = APContact::getByURL($parent['causer-link'], false);
-                       $is_forum_thread = isset($profile['type']) && $profile['type'] == 'Group';
+                       $is_group_thread = isset($profile['type']) && $profile['type'] == 'Group';
                } else {
-                       $is_forum_thread = false;
+                       $is_group_thread = false;
+               }
+
+               $exclusive = false;
+               $mention   = false;
+               $audience  = [];
+
+               $parent_tags = Tag::getByURIId($item['parent-uri-id'], [Tag::AUDIENCE, Tag::MENTION]);
+               if (!empty($parent_tags)) {
+                       $is_group_thread = false;
+                       foreach ($parent_tags as $tag) {
+                               if ($tag['type'] != Tag::AUDIENCE) {
+                                       continue;
+                               }
+                               $profile = APContact::getByURL($tag['url'], false);
+                               if (!empty($profile) && ($profile['type'] == 'Group')) {
+                                       $audience[] = $tag['url'];
+                                       $is_group_thread = true;
+                               }
+                       }
+                       if ($is_group_thread) {
+                               foreach ($parent_tags as $tag) {
+                                       if (($tag['type'] == Tag::MENTION) && in_array($tag['url'], $audience)) {
+                                               $mention = true;
+                                       }
+                               }
+                               $exclusive = !$mention;
+                       }
+               } elseif ($is_group_thread) {
+                       foreach (Tag::getByURIId($item['parent-uri-id'], [Tag::MENTION, Tag::EXCLUSIVE_MENTION]) as $term) {
+                               $profile = APContact::getByURL($term['url'], false);
+                               if (!empty($profile) && ($profile['type'] == 'Group')) {
+                                       if ($term['type'] == Tag::EXCLUSIVE_MENTION) {
+                                               $audience[] = $term['url'];
+                                               $exclusive  = true;
+                                       } elseif ($term['type'] == Tag::MENTION) {
+                                               $mention = true;
+                                       }
+                               }
+                       }
                }
 
                if (self::isAnnounce($item) || self::isAPPost($last_id)) {
@@ -607,7 +647,7 @@ class Transmitter
                        $networks = [Protocol::ACTIVITYPUB, Protocol::OSTATUS];
                }
 
-               $data = ['to' => [], 'cc' => [], 'bcc' => []];
+               $data = ['to' => [], 'cc' => [], 'bcc' => [] , 'audience' => $audience];
 
                if ($item['gravity'] == Item::GRAVITY_PARENT) {
                        $actor_profile = APContact::getByURL($item['owner-link']);
@@ -615,23 +655,7 @@ class Transmitter
                        $actor_profile = APContact::getByURL($item['author-link']);
                }
 
-               $exclusive = false;
-               $mention   = false;
-
-               if ($is_forum_thread) {
-                       foreach (Tag::getByURIId($item['parent-uri-id'], [Tag::MENTION, Tag::EXCLUSIVE_MENTION]) as $term) {
-                               $profile = APContact::getByURL($term['url'], false);
-                               if (!empty($profile) && ($profile['type'] == 'Group')) {
-                                       if ($term['type'] == Tag::EXCLUSIVE_MENTION) {
-                                               $exclusive = true;
-                                       } elseif ($term['type'] == Tag::MENTION) {
-                                               $mention = true;
-                                       }
-                               }
-                       }
-               }
-
-               $terms = Tag::getByURIId($item['uri-id'], [Tag::MENTION, Tag::IMPLICIT_MENTION, Tag::EXCLUSIVE_MENTION]);
+               $terms = Tag::getByURIId($item['uri-id'], [Tag::MENTION, Tag::IMPLICIT_MENTION, Tag::EXCLUSIVE_MENTION, Tag::AUDIENCE]);
 
                if ($item['private'] != Item::PRIVATE) {
                        // Directly mention the original author upon a quoted reshare.
@@ -643,7 +667,9 @@ class Transmitter
                                $data['cc'][] = $announce['actor']['url'];
                        }
 
-                       $data = array_merge($data, self::fetchPermissionBlockFromThreadParent($item, $is_forum_thread));
+                       if (!$exclusive) {
+                               $data = array_merge($data, self::fetchPermissionBlockFromThreadParent($item, $is_group_thread));
+                       }
 
                        // Check if the item is completely public or unlisted
                        if ($item['private'] == Item::PUBLIC) {
@@ -655,10 +681,14 @@ class Transmitter
                        foreach ($terms as $term) {
                                $profile = APContact::getByURL($term['url'], false);
                                if (!empty($profile)) {
+                                       if (($term['type'] == Tag::AUDIENCE) && ($profile['type'] == 'Group')) {
+                                               $data['audience'][] = $profile['url'];
+                                       }
                                        if ($term['type'] == Tag::EXCLUSIVE_MENTION) {
                                                $exclusive = true;
                                                if (!empty($profile['followers']) && ($profile['type'] == 'Group')) {
-                                                       $data['cc'][] = $profile['followers'];
+                                                       $data['cc'][]       = $profile['followers'];
+                                                       $data['audience'][] = $profile['url'];
                                                }
                                        } elseif (($term['type'] == Tag::MENTION) && ($profile['type'] == 'Group')) {
                                                $mention = true;
@@ -666,8 +696,11 @@ class Transmitter
                                        $data['to'][] = $profile['url'];
                                }
                        }
+                       if (!$exclusive && ($item['private'] == Item::UNLISTED)) {
+                               $data['to'][] = $actor_profile['followers'];
+                       }
                } else {
-                       $receiver_list = Item::enumeratePermissions($item, true);
+                       $receiver_list = Item::enumeratePermissions($item, true, $expand_followers);
 
                        foreach ($terms as $term) {
                                $cid = Contact::getIdForURL($term['url'], $item['uid']);
@@ -679,10 +712,14 @@ class Transmitter
 
                                        $profile = APContact::getByURL($term['url'], false);
                                        if (!empty($profile)) {
+                                               if (($term['type'] == Tag::AUDIENCE) && ($profile['type'] == 'Group')) {
+                                                       $data['audience'][] = $profile['url'];
+                                               }
                                                if ($term['type'] == Tag::EXCLUSIVE_MENTION) {
                                                        $exclusive = true;
                                                        if (!empty($profile['followers']) && ($profile['type'] == 'Group')) {
-                                                               $data['cc'][] = $profile['followers'];
+                                                               $data['cc'][]       = $profile['followers'];
+                                                               $data['audience'][] = $profile['url'];
                                                        }
                                                } elseif (($term['type'] == Tag::MENTION) && ($profile['type'] == 'Group')) {
                                                        $mention = true;
@@ -696,10 +733,15 @@ class Transmitter
                                $exclusive = false;
                        }
 
-                       if ($is_forum && !$exclusive && !empty($follower)) {
+                       if ($is_group && !$exclusive && !empty($follower)) {
                                $data['cc'][] = $follower;
                        } elseif (!$exclusive) {
                                foreach ($receiver_list as $receiver) {
+                                       if ($receiver == -1) {
+                                               $data['to'][] = $actor_profile['followers'];
+                                               continue;
+                                       }
+
                                        $contact = DBA::selectFirst('contact', ['url', 'hidden', 'network', 'protocol', 'gsid'], ['id' => $receiver, 'network' => Protocol::FEDERATED]);
                                        if (!DBA::isResult($contact) || !self::isAPContact($contact, $networks)) {
                                                continue;
@@ -716,26 +758,31 @@ class Transmitter
                        }
                }
 
-               if (!empty($item['parent'])) {
-                       $parents = Post::select(['id', 'author-link', 'owner-link', 'gravity', 'uri'], ['parent' => $item['parent']], ['order' => ['id']]);
+               if (!empty($item['parent']) && (!$exclusive || ($item['private'] == Item::PRIVATE))) {
+                       if ($item['private'] == Item::PRIVATE) {
+                               $condition = ['parent' => $item['parent'], 'uri-id' => $item['thr-parent-id']];
+                       } else {
+                               $condition = ['parent' => $item['parent']];
+                       }
+                       $parents = Post::select(['id', 'author-link', 'owner-link', 'gravity', 'uri'], $condition, ['order' => ['id']]);
                        while ($parent = Post::fetch($parents)) {
                                if ($parent['gravity'] == Item::GRAVITY_PARENT) {
                                        $profile = APContact::getByURL($parent['owner-link'], false);
                                        if (!empty($profile)) {
                                                if ($item['gravity'] != Item::GRAVITY_PARENT) {
-                                                       // Comments to forums are directed to the forum
-                                                       // But comments to forums aren't directed to the followers collection
-                                                       // This rule is only valid when the actor isn't the forum.
-                                                       // The forum needs to transmit their content to their followers.
+                                                       // Comments to groups are directed to the group
+                                                       // But comments to groups aren't directed to the followers collection
+                                                       // This rule is only valid when the actor isn't the group.
+                                                       // The group needs to transmit their content to their followers.
                                                        if (($profile['type'] == 'Group') && ($profile['url'] != ($actor_profile['url'] ?? ''))) {
                                                                $data['to'][] = $profile['url'];
                                                        } else {
                                                                $data['cc'][] = $profile['url'];
-                                                               if (($item['private'] != Item::PRIVATE) && !empty($actor_profile['followers']) && (!$exclusive || !$is_forum_thread)) {
+                                                               if (($item['private'] != Item::PRIVATE) && !empty($actor_profile['followers']) && (!$exclusive || !$is_group_thread)) {
                                                                        $data['cc'][] = $actor_profile['followers'];
                                                                }
                                                        }
-                                               } elseif (!$exclusive && !$is_forum_thread) {
+                                               } elseif (!$exclusive && !$is_group_thread) {
                                                        // Public thread parent post always are directed to the followers.
                                                        if ($item['private'] != Item::PRIVATE) {
                                                                $data['cc'][] = $actor_profile['followers'];
@@ -761,9 +808,10 @@ class Transmitter
                        DBA::close($parents);
                }
 
-               $data['to'] = array_unique($data['to']);
-               $data['cc'] = array_unique($data['cc']);
-               $data['bcc'] = array_unique($data['bcc']);
+               $data['to']       = array_unique($data['to']);
+               $data['cc']       = array_unique($data['cc']);
+               $data['bcc']      = array_unique($data['bcc']);
+               $data['audience'] = array_unique($data['audience']);
 
                if (($key = array_search($item['author-link'], $data['to'])) !== false) {
                        unset($data['to'][$key]);
@@ -777,6 +825,10 @@ class Transmitter
                        unset($data['bcc'][$key]);
                }
 
+               if (($key = array_search($item['author-link'], $data['audience'])) !== false) {
+                       unset($data['audience'][$key]);
+               }
+
                foreach ($data['to'] as $to) {
                        if (($key = array_search($to, $data['cc'])) !== false) {
                                unset($data['cc'][$key]);
@@ -793,13 +845,13 @@ class Transmitter
                        }
                }
 
-               $receivers = ['to' => array_values($data['to']), 'cc' => array_values($data['cc']), 'bcc' => array_values($data['bcc'])];
+               $receivers = ['to' => array_values($data['to']), 'cc' => array_values($data['cc']), 'bcc' => array_values($data['bcc']), 'audience' => array_values($data['audience'])];
 
                if (!$blindcopy) {
                        unset($receivers['bcc']);
                }
 
-               foreach (['to' => Tag::TO, 'cc' => Tag::CC, 'bcc' => Tag::BCC] as $element => $type) {
+               foreach (['to' => Tag::TO, 'cc' => Tag::CC, 'bcc' => Tag::BCC, 'audience' => Tag::AUDIENCE] as $element => $type) {
                        if (!empty($receivers[$element])) {
                                foreach ($receivers[$element] as $receiver) {
                                        if ($receiver == ActivityPub::PUBLIC_COLLECTION) {
@@ -812,6 +864,12 @@ class Transmitter
                        }
                }
 
+               if (!$blindcopy && count($receivers['audience']) == 1) {
+                       $receivers['audience'] = $receivers['audience'][0];
+               } elseif (!$receivers['audience']) {
+                       unset($receivers['audience']);
+               }
+
                return $receivers;
        }
 
@@ -857,12 +915,11 @@ class Transmitter
        {
                $inboxes = [];
 
-               $isforum = false;
-
+               $isGroup = false;
                if (!empty($item['uid'])) {
                        $profile = User::getOwnerDataById($item['uid']);
                        if (!empty($profile)) {
-                               $isforum = $profile['account-type'] == User::ACCOUNT_TYPE_COMMUNITY;
+                               $isGroup = $profile['account-type'] == User::ACCOUNT_TYPE_COMMUNITY;
                        }
                }
 
@@ -892,7 +949,7 @@ class Transmitter
                                continue;
                        }
 
-                       if ($isforum && ($contact['network'] == Protocol::DFRN)) {
+                       if ($isGroup && ($contact['network'] == Protocol::DFRN)) {
                                continue;
                        }
 
@@ -930,7 +987,7 @@ class Transmitter
         */
        public static function fetchTargetInboxes(array $item, int $uid, bool $personal = false, int $last_id = 0): array
        {
-               $permissions = self::createPermissionBlockForItem($item, true, $last_id);
+               $permissions = self::createPermissionBlockForItem($item, true, true, $last_id);
                if (empty($permissions)) {
                        return [];
                }
@@ -949,7 +1006,7 @@ class Transmitter
 
                $profile_uid = User::getIdForURL($item_profile['url']);
 
-               foreach (['to', 'cc', 'bto', 'bcc'] as $element) {
+               foreach (['to', 'cc', 'bto', 'bcc', 'audience'] as $element) {
                        if (empty($permissions[$element])) {
                                continue;
                        }
@@ -973,7 +1030,7 @@ class Transmitter
                                                } else {
                                                        $target = $profile['sharedinbox'];
                                                }
-                                               if (!self::archivedInbox($target)) {
+                                               if (!self::archivedInbox($target) && !in_array($contact['id'], $inboxes[$target] ?? [])) {
                                                        $inboxes[$target][] = $contact['id'] ?? 0;
                                                }
                                        }
@@ -1062,7 +1119,7 @@ class Transmitter
                $data['actor'] = $mail['author-link'];
                $data['published'] = DateTimeFormat::utc($mail['created'] . '+00:00', DateTimeFormat::ATOM);
                $data['instrument'] = self::getService();
-               $data = array_merge($data, self::createPermissionBlockForItem($mail, true));
+               $data = array_merge($data, self::createPermissionBlockForItem($mail, true, false));
 
                if (empty($data['to']) && !empty($data['cc'])) {
                        $data['to'] = $data['cc'];
@@ -1074,12 +1131,14 @@ class Transmitter
 
                unset($data['cc']);
                unset($data['bcc']);
+               unset($data['audience']);
 
                $object['to'] = $data['to'];
                $object['tag'] = [['type' => 'Mention', 'href' => $object['to'][0], 'name' => '']];
 
                unset($object['cc']);
                unset($object['bcc']);
+               unset($object['audience']);
 
                $data['directMessage'] = true;
 
@@ -1287,7 +1346,7 @@ class Transmitter
 
                $data['instrument'] = self::getService();
 
-               $data = array_merge($data, self::createPermissionBlockForItem($item, false));
+               $data = array_merge($data, self::createPermissionBlockForItem($item, false, false));
 
                if (in_array($data['type'], ['Create', 'Update', 'Delete'])) {
                        $data['object'] = self::createNote($item, $api_mode);
@@ -1635,10 +1694,12 @@ class Transmitter
                        $data['name'] = BBCode::toPlaintext($item['title'], false);
                }
 
-               $permission_block = self::createPermissionBlockForItem($item, false);
+               $permission_block = self::createPermissionBlockForItem($item, false, false);
 
                $real_quote = false;
 
+               $item = Post\Media::addHTMLAttachmentToItem($item);
+
                $body = $item['body'];
 
                if ($type == 'Note') {
@@ -1675,7 +1736,7 @@ class Transmitter
                        if ($type == 'Page') {
                                // When we transmit "Page" posts we have to remove the attachment.
                                // The attachment contains the link that we already transmit in the "url" field.
-                               $body = preg_replace("/\s*\[attachment .*?\].*?\[\/attachment\]\s*/ism", '', $body);
+                               $body = BBCode::removeAttachment($body);
                        }
 
                        $body = BBCode::setMentionsToNicknames($body);
@@ -1707,7 +1768,7 @@ class Transmitter
                                        $richbody = DI::contentItem()->addSharedPost($item, $richbody);
                                }
                        }
-                       $richbody = BBCode::removeAttachment($richbody);
+                       $richbody = BBCode::replaceAttachment($richbody);
 
                        $data['contentMap'][$language] = BBCode::convertForUriId($item['uri-id'], $richbody, BBCode::EXTERNAL);
                }
@@ -1823,7 +1884,7 @@ class Transmitter
                $item['body'] = $announce['comment'] . "\n" . $announce['object']['plink'];
                $activity['object'] = self::createNote($item, $api_mode);
 
-               /// @todo Finally descide how to implement this in AP. This is a possible way:
+               /// @todo Finally decide how to implement this in AP. This is a possible way:
                $activity['object']['attachment'][] = self::createNote($announce['object']);
 
                $activity['object']['source']['content'] = $orig_body;
@@ -1831,7 +1892,7 @@ class Transmitter
        }
 
        /**
-        * Return announce related data if the item is an annunce
+        * Return announce related data if the item is an announce
         *
         * @param array $item
         * @return array Announcement array