]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/ActivityPub/Transmitter.php
Check array key existence in ActivityPub\Transmitter::createPermissionBlockForItem
[friendica.git] / src / Protocol / ActivityPub / Transmitter.php
index 7be55898c8488ff6de86bd122831cc889cb69674..2cab827bb1afd937468687e1e542c0bf9988f897 100644 (file)
@@ -424,42 +424,34 @@ class Transmitter
        }
 
        /**
-        * Returns an array with permissions of a given item array
+        * 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 fetchPermissionBlockFromConversation($item)
+       private static function fetchPermissionBlockFromThreadParent(array $item, bool $is_forum_thread)
        {
-               if (empty($item['thr-parent'])) {
+               if (empty($item['thr-parent-id'])) {
                        return [];
                }
 
-               $condition = ['item-uri' => $item['thr-parent'], 'protocol' => Conversation::PARCEL_ACTIVITYPUB];
-               $conversation = DBA::selectFirst('conversation', ['source'], $condition);
-               if (!DBA::isResult($conversation)) {
+               $parent = Post::selectFirstPost(['author-link'], ['uri-id' => $item['thr-parent-id']]);
+               if (empty($parent)) {
                        return [];
                }
 
                $permissions = [
-                       'to' => [],
+                       'to' => [$parent['author-link']],
                        'cc' => [],
                        'bto' => [],
                        'bcc' => [],
                ];
 
-               $activity = json_decode($conversation['source'], true);
-
-               $actor = JsonLD::fetchElement($activity, 'actor', 'id');
-               if (!empty($actor)) {
-                       $permissions['to'][] = $actor;
-                       $profile = APContact::getByURL($actor);
-               } else {
-                       $profile = [];
-               }
+               $parent_profile = APContact::getByURL($parent['author-link']);
 
                $item_profile = APContact::getByURL($item['author-link']);
                $exclude[] = $item['author-link'];
@@ -468,26 +460,17 @@ class Transmitter
                        $exclude[] = $item['owner-link'];
                }
 
-               foreach (['to', 'cc', 'bto', 'bcc'] as $element) {
-                       if (empty($activity[$element])) {
-                               continue;
-                       }
-                       if (is_string($activity[$element])) {
-                               $activity[$element] = [$activity[$element]];
-                       }
-
-                       foreach ($activity[$element] as $receiver) {
-                               if (empty($receiver)) {
-                                       continue;
-                               }
-
-                               if (!empty($profile['followers']) && $receiver == $profile['followers'] && !empty($item_profile['followers'])) {
-                                       $permissions[$element][] = $item_profile['followers'];
-                               } elseif (!in_array($receiver, $exclude)) {
-                                       $permissions[$element][] = $receiver;
+               $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) {
+                       if (!empty($parent_profile['followers']) && $receiver['url'] == $parent_profile['followers'] && !empty($item_profile['followers'])) {
+                               if (!$is_forum_thread) {
+                                       $permissions[$type[$receiver['type']]][] = $item_profile['followers'];
                                }
+                       } elseif (!in_array($receiver['url'], $exclude)) {
+                               $permissions[$type[$receiver['type']]][] = $receiver['url'];
                        }
                }
+
                return $permissions;
        }
 
@@ -509,28 +492,33 @@ 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 boolean $forum_post "true" means that we are sending content to a forum
+        * @param array   $item      Item array
+        * @param boolean $blindcopy addressing via "bcc" or "cc"?
+        * @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($item, $blindcopy, $last_id = 0, $forum_post = false)
+       private static function createPermissionBlockForItem($item, $blindcopy, $last_id = 0)
        {
                if ($last_id == 0) {
                        $last_id = $item['id'];
                }
 
                $always_bcc = false;
+               $is_forum   = false;
+               $follower   = '';
 
                // Check if we should always deliver our stuff via BCC
                if (!empty($item['uid'])) {
-                       $profile = User::getOwnerDataById($item['uid']);
-                       if (!empty($profile)) {
-                               $always_bcc = $profile['hide-friends'];
+                       $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'];
+
+                               $profile  = APContact::getByURL($owner['url'], false);
+                               $follower = $profile['followers'] ?? '';
                        }
                }
 
@@ -538,6 +526,14 @@ class Transmitter
                        $always_bcc = true;
                }
 
+               $parent = Post::selectFirst(['causer-link', 'post-reason'], ['id' => $item['parent']]);
+               if (($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';
+               } else {
+                       $is_forum_thread = false;
+               }
+
                if (self::isAnnounce($item) || DI::config()->get('debug', 'total_ap_delivery') || self::isAPPost($last_id)) {
                        // Will be activated in a later step
                        $networks = Protocol::FEDERATED;
@@ -568,7 +564,7 @@ class Transmitter
                                $data['cc'][] = $announce['actor']['url'];
                        }
 
-                       $data = array_merge($data, self::fetchPermissionBlockFromConversation($item));
+                       $data = array_merge($data, self::fetchPermissionBlockFromThreadParent($item, $is_forum_thread));
 
                        // Check if the item is completely public or unlisted
                        if ($item['private'] == Item::PUBLIC) {
@@ -613,7 +609,9 @@ class Transmitter
                                }
                        }
 
-                       if (!$exclusive) {
+                       if ($is_forum && !$exclusive && !empty($follower)) {
+                               $data['cc'][] = $follower;
+                       } elseif (!$exclusive) {
                                foreach ($receiver_list as $receiver) {
                                        $contact = DBA::selectFirst('contact', ['url', 'hidden', 'network', 'protocol', 'gsid'], ['id' => $receiver, 'network' => Protocol::FEDERATED]);
                                        if (!DBA::isResult($contact) || !self::isAPContact($contact, $networks)) {
@@ -632,7 +630,7 @@ class Transmitter
                }
 
                if (!empty($item['parent'])) {
-                       $parents = Post::select(['id', 'author-link', 'owner-link', 'gravity', 'uri'], ['parent' => $item['parent']]);
+                       $parents = Post::select(['id', 'author-link', 'owner-link', 'gravity', 'uri'], ['parent' => $item['parent']], ['order' => ['id']]);
                        while ($parent = Post::fetch($parents)) {
                                if ($parent['gravity'] == GRAVITY_PARENT) {
                                        $profile = APContact::getByURL($parent['owner-link'], false);
@@ -646,15 +644,13 @@ class Transmitter
                                                                $data['to'][] = $profile['url'];
                                                        } else {
                                                                $data['cc'][] = $profile['url'];
-                                                               if (($item['private'] != Item::PRIVATE) && !empty($actor_profile['followers'])) {
+                                                               if (($item['private'] != Item::PRIVATE) && !empty($actor_profile['followers'])&& !$is_forum_thread) {
                                                                        $data['cc'][] = $actor_profile['followers'];
                                                                }
                                                        }
-                                               } elseif (!$exclusive) {
+                                               } elseif (!$exclusive && !$is_forum_thread) {
                                                        // Public thread parent post always are directed to the followers.
-                                                       // This mustn't be done by posts that are directed to forum servers via the exclusive mention.
-                                                       // But possibly in that case we could add the "followers" collection of the forum to the message.
-                                                       if (($item['private'] != Item::PRIVATE) && !$forum_post) {
+                                                       if ($item['private'] != Item::PRIVATE) {
                                                                $data['cc'][] = $actor_profile['followers'];
                                                        }
                                                }
@@ -716,6 +712,19 @@ class Transmitter
                        unset($receivers['bcc']);
                }
 
+               foreach (['to' => Tag::TO, 'cc' => Tag::CC, 'bcc' => Tag::BCC] as $element => $type) {
+                       if (!empty($receivers[$element])) {
+                               foreach ($receivers[$element] as $receiver) {
+                                       if ($receiver == ActivityPub::PUBLIC_COLLECTION) {
+                                               $name = Receiver::PUBLIC_COLLECTION;
+                                       } else {
+                                               $name = trim(parse_url($receiver, PHP_URL_PATH), '/');
+                                       }
+                                       Tag::store($item['uri-id'], $type, $name, $receiver);
+                               }
+                       }
+               }
+
                return $receivers;
        }
 
@@ -820,18 +829,17 @@ class Transmitter
        /**
         * Fetches an array of inboxes for the given item and user
         *
-        * @param array   $item       Item array
-        * @param integer $uid        User ID
-        * @param boolean $personal   fetch personal inboxes
-        * @param integer $last_id    Last item id for adding receivers
-        * @param boolean $forum_post "true" means that we are sending content to a forum
+        * @param array   $item     Item array
+        * @param integer $uid      User ID
+        * @param boolean $personal fetch personal inboxes
+        * @param integer $last_id  Last item id for adding receivers
         * @return array with inboxes
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         * @throws \ImagickException
         */
-       public static function fetchTargetInboxes($item, $uid, $personal = false, $last_id = 0, $forum_post = false)
+       public static function fetchTargetInboxes($item, $uid, $personal = false, $last_id = 0)
        {
-               $permissions = self::createPermissionBlockForItem($item, true, $last_id, $forum_post);
+               $permissions = self::createPermissionBlockForItem($item, true, $last_id);
                if (empty($permissions)) {
                        return [];
                }
@@ -914,6 +922,7 @@ class Transmitter
                        $mail['title']        = '';
                }
 
+               $mail['content-warning']  = '';
                $mail['author-link']      = $mail['owner-link'] = $mail['from-url'];
                $mail['owner-id']         = $mail['author-id'];
                $mail['allow_cid']        = '<'.$mail['contact-id'].'>';
@@ -1410,7 +1419,7 @@ class Transmitter
         */
        private static function isSensitive($uri_id)
        {
-               return DBA::exists('tag-view', ['uri-id' => $uri_id, 'name' => 'nsfw']);
+               return DBA::exists('tag-view', ['uri-id' => $uri_id, 'name' => 'nsfw', 'type' => Tag::HASHTAG]);
        }
 
        /**