]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/ActivityPub/Transmitter.php
Merge pull request #13610 from MrPetovan/bug/13334-calendar-public-event
[friendica.git] / src / Protocol / ActivityPub / Transmitter.php
index 6c0cbd92b9cae04813e5c912331ace7501b7bc8d..f1ced5b429b45489e139bb667b0e1c7ff2b056da 100644 (file)
@@ -195,7 +195,7 @@ class Transmitter
                }
 
                // When we hide our friends we will only show the pure number but don't allow more.
-               $show_contacts = empty($owner['hide-friends']);
+               $show_contacts = ActivityPub::isAcceptedRequester($owner['uid']) && empty($owner['hide-friends']);
 
                // Allow fetching the contact list when the requester is part of the list.
                if (($owner['page-flags'] == User::PAGE_FLAGS_PRVGROUP) && !empty($requester)) {
@@ -337,16 +337,17 @@ class Transmitter
        /**
         * Return the ActivityPub profile of the given user
         *
-        * @param int $uid User ID
+        * @param int  $uid  User ID
+        * @param bool $full If not full, only the basic information is returned
         * @return array with profile data
         * @throws HTTPException\NotFoundException
         * @throws HTTPException\InternalServerErrorException
         */
-       public static function getProfile(int $uid): array
+       public static function getProfile(int $uid, bool $full = true): array
        {
                $owner = User::getOwnerDataById($uid);
                if (!isset($owner['id'])) {
-                       DI::logger()->error('Unable to find owner data for uid', ['uid' => $uid, 'callstack' => System::callstack(20)]);
+                       DI::logger()->error('Unable to find owner data for uid', ['uid' => $uid]);
                        throw new HTTPException\NotFoundException('User not found.');
                }
 
@@ -372,16 +373,16 @@ class Transmitter
                $data['preferredUsername'] = $owner['nick'];
                $data['name'] = $owner['name'];
 
-               if (!empty($owner['country-name'] . $owner['region'] . $owner['locality'])) {
+               if (!$full && !empty($owner['country-name'] . $owner['region'] . $owner['locality'])) {
                        $data['vcard:hasAddress'] = ['@type' => 'vcard:Home', 'vcard:country-name' => $owner['country-name'],
                                'vcard:region' => $owner['region'], 'vcard:locality' => $owner['locality']];
                }
 
-               if (!empty($owner['about'])) {
+               if ($full && !empty($owner['about'])) {
                        $data['summary'] = BBCode::convertForUriId($owner['uri-id'] ?? 0, $owner['about'], BBCode::EXTERNAL);
                }
 
-               if (!empty($owner['xmpp']) || !empty($owner['matrix'])) {
+               if ($full && (!empty($owner['xmpp']) || !empty($owner['matrix']))) {
                        $data['vcard:hasInstantMessage'] = [];
 
                        if (!empty($owner['xmpp'])) {
@@ -399,7 +400,7 @@ class Transmitter
                        'owner' => $owner['url'],
                        'publicKeyPem' => $owner['pubkey']];
                $data['endpoints'] = ['sharedInbox' => DI::baseUrl() . '/inbox'];
-               if ($uid != 0) {
+               if ($full && $uid != 0) {
                        $data['icon'] = ['type' => 'Image', 'url' => User::getAvatarUrl($owner)];
 
                        $resourceid = Photo::ridFromURI($owner['photo']);
@@ -559,14 +560,13 @@ class Transmitter
         *
         * @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, bool $expand_followers, int $last_id = 0): array
+       private static function createPermissionBlockForItem(array $item, bool $blindcopy, int $last_id = 0): array
        {
                if ($last_id == 0) {
                        $last_id = $item['id'];
@@ -703,7 +703,7 @@ class Transmitter
                                $data['to'][] = $actor_profile['followers'];
                        }
                } else {
-                       $receiver_list = Item::enumeratePermissions($item, true, $expand_followers);
+                       $receiver_list = Item::enumeratePermissions($item, true, false);
 
                        foreach ($terms as $term) {
                                $cid = Contact::getIdForURL($term['url'], $item['uid']);
@@ -850,6 +850,28 @@ class Transmitter
                        unset($receivers['bcc']);
                }
 
+               if (!$blindcopy && count($receivers['audience']) == 1) {
+                       $receivers['audience'] = $receivers['audience'][0];
+               } elseif (!$receivers['audience']) {
+                       unset($receivers['audience']);
+               }
+
+               return $receivers;
+       }
+
+       /**
+        * Store the receivers for the given item
+        *
+        * @param array $item
+        * @return void
+        */
+       public static function storeReceiversForItem(array $item)
+       {
+               $receivers = self::createPermissionBlockForItem($item, true);
+               if (empty($receivers)) {
+                       return;
+               }
+
                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) {
@@ -862,6 +884,44 @@ class Transmitter
                                }
                        }
                }
+       }
+
+       /**
+        * Get a list of receivers for the provided uri-id
+        *
+        * @param array $item
+        * @param boolean $blindcopy
+        * @return void
+        */
+       public static function getReceiversForUriId(int $uri_id, bool $blindcopy)
+       {
+               $receivers = [
+                       'to'       => [],
+                       'cc'       => [],
+                       'bcc'      => [],
+                       'audience' => [],
+               ];
+
+               foreach (Tag::getByURIId($uri_id, [Tag::TO, Tag::CC, Tag::BCC, Tag::AUDIENCE]) as $receiver) {
+                       switch ($receiver['type']) {
+                               case Tag::TO:
+                                       $receivers['to'][] = $receiver['url'];
+                                       break;
+                               case Tag::CC:
+                                       $receivers['cc'][] = $receiver['url'];
+                                       break;
+                               case Tag::BCC:
+                                       $receivers['bcc'][] = $receiver['url'];
+                                       break;
+                               case Tag::AUDIENCE:
+                                       $receivers['audience'][] = $receiver['url'];
+                                       break;
+                       }
+               }
+
+               if (!$blindcopy) {
+                       unset($receivers['bcc']);
+               }
 
                if (!$blindcopy && count($receivers['audience']) == 1) {
                        $receivers['audience'] = $receivers['audience'][0];
@@ -931,11 +991,13 @@ class Transmitter
                }
 
                $condition = [
-                       'uid' => $uid,
-                       'archive' => false,
-                       'pending' => false,
-                       'blocked' => false,
-                       'network' => Protocol::FEDERATED,
+                       'uid'          => $uid,
+                       'self'         => false,
+                       'archive'      => false,
+                       'pending'      => false,
+                       'blocked'      => false,
+                       'network'      => Protocol::FEDERATED,
+                       'contact-type' => [Contact::TYPE_UNKNOWN, Contact::TYPE_PERSON, Contact::TYPE_NEWS, Contact::TYPE_ORGANISATION],
                ];
 
                if (!empty($uid)) {
@@ -979,14 +1041,13 @@ class Transmitter
         * @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(array $item, int $uid, bool $personal = false, int $last_id = 0): array
+       public static function fetchTargetInboxes(array $item, int $uid, bool $personal = false): array
        {
-               $permissions = self::createPermissionBlockForItem($item, true, true, $last_id);
+               $permissions = self::getReceiversForUriId($item['uri-id'], true);
                if (empty($permissions)) {
                        return [];
                }
@@ -1018,7 +1079,7 @@ class Transmitter
                                }
 
                                if ($item_profile && ($receiver == $item_profile['followers']) && ($uid == $profile_uid)) {
-                                       $inboxes = array_merge_recursive($inboxes, self::fetchTargetInboxesforUser($uid, $personal, self::isAPPost($last_id)));
+                                       $inboxes = array_merge_recursive($inboxes, self::fetchTargetInboxesforUser($uid, $personal, true));
                                } else {
                                        $profile = APContact::getByURL($receiver, false);
                                        if (!empty($profile)) {
@@ -1118,7 +1179,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, false));
+               $data = array_merge($data, self::createPermissionBlockForItem($mail, true));
 
                if (empty($data['to']) && !empty($data['cc'])) {
                        $data['to'] = $data['cc'];
@@ -1350,7 +1411,7 @@ class Transmitter
 
                $data['instrument'] = self::getService();
 
-               $data = array_merge($data, self::createPermissionBlockForItem($item, false, false));
+               $data = array_merge($data, self::createPermissionBlockForItem($item, false));
 
                if (in_array($data['type'], ['Create', 'Update', 'Delete'])) {
                        $data['object'] = self::createNote($item, $api_mode);
@@ -1704,7 +1765,7 @@ class Transmitter
                        $data['name'] = BBCode::toPlaintext($item['title'], false);
                }
 
-               $permission_block = self::createPermissionBlockForItem($item, false, false);
+               $permission_block = self::getReceiversForUriId($item['uri-id'], false);
 
                $real_quote = false;
 
@@ -1751,7 +1812,7 @@ class Transmitter
 
                        $body = BBCode::setMentionsToNicknames($body);
 
-                       if (!empty($item['quote-uri-id'])) {
+                       if (!empty($item['quote-uri-id']) && ($item['quote-uri-id'] != $item['uri-id'])) {
                                if (Post::exists(['uri-id' => $item['quote-uri-id'], 'network' => [Protocol::ACTIVITYPUB, Protocol::DFRN]])) {
                                        $real_quote = true;
                                        $data['quoteUrl'] = $item['quote-uri'];
@@ -1771,7 +1832,7 @@ class Transmitter
                if (!empty($language)) {
                        $richbody = BBCode::setMentionsToNicknames($item['body'] ?? '');
                        $richbody = Post\Media::removeFromEndOfBody($richbody);
-                       if (!empty($item['quote-uri-id'])) {
+                       if (!empty($item['quote-uri-id']) && ($item['quote-uri-id'] != $item['uri-id'])) {
                                if ($real_quote) {
                                        $richbody = DI::contentItem()->addShareLink($richbody, $item['quote-uri-id']);
                                } else {
@@ -1783,7 +1844,7 @@ class Transmitter
                        $data['contentMap'][$language] = BBCode::convertForUriId($item['uri-id'], $richbody, BBCode::EXTERNAL);
                }
 
-               if (!empty($item['quote-uri-id'])) {
+               if (!empty($item['quote-uri-id']) && ($item['quote-uri-id'] != $item['uri-id'])) {
                        $source = DI::contentItem()->addSharedPost($item, $item['body']);
                } else {
                        $source = $item['body'];