]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/ActivityPub/Transmitter.php
Fix possible fatal error when body is null
[friendica.git] / src / Protocol / ActivityPub / Transmitter.php
index 435cab3d042276252dfa597da52a75b74a5e5487..20b8f2a26221f5bc766c5a4c06a8689e5c2bece5 100644 (file)
@@ -143,7 +143,7 @@ class Transmitter
 
                return $success;
        }
-       
+
        /**
         * Collects a list of contacts of the given owner
         *
@@ -235,26 +235,28 @@ class Transmitter
         */
        public static function getOutbox($owner, $page = null, $requester = '')
        {
-               $public_contact = Contact::getIdForURL($owner['url']);
-               $condition = ['uid' => 0, 'contact-id' => $public_contact,
-                       'private' => [Item::PUBLIC, Item::UNLISTED]];
+               $condition = ['private' => [Item::PUBLIC, Item::UNLISTED]];
 
                if (!empty($requester)) {
                        $requester_id = Contact::getIdForURL($requester, $owner['uid']);
                        if (!empty($requester_id)) {
                                $permissionSets = DI::permissionSet()->selectByContactId($requester_id, $owner['uid']);
                                if (!empty($permissionSets)) {
-                                       $condition = ['uid' => $owner['uid'], 'origin' => true,
-                                               'psid' => array_merge($permissionSets->column('id'),
+                                       $condition = ['psid' => array_merge($permissionSets->column('id'),
                                                        [DI::permissionSet()->getIdFromACL($owner['uid'], '', '', '', '')])];
                                }
                        }
                }
 
                $condition = array_merge($condition,
-                       ['author-id' => $public_contact,
-                       'gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT],
-                       'deleted' => false, 'visible' => true]);
+                       ['uid'           => $owner['uid'],
+                       'author-id'      => Contact::getIdForURL($owner['url'], 0, false),
+                       'gravity'        => [GRAVITY_PARENT, GRAVITY_COMMENT],
+                       'network'        => Protocol::FEDERATED,
+                       'parent-network' => Protocol::FEDERATED,
+                       'origin'         => true,
+                       'deleted'        => false,
+                       'visible'        => true]);
 
                $count = Post::count($condition);
 
@@ -269,8 +271,6 @@ class Transmitter
                        $data['type'] = 'OrderedCollectionPage';
                        $list = [];
 
-                       $condition['parent-network'] = Protocol::NATIVE_SUPPORT;
-
                        $items = Post::select(['id'], $condition, ['limit' => [($page - 1) * 20, 20], 'order' => ['created' => true]]);
                        while ($item = Post::fetch($items)) {
                                $activity = self::createActivityFromItem($item['id'], true);
@@ -316,72 +316,48 @@ class Transmitter
         */
        public static function getProfile($uid)
        {
-               if ($uid != 0) {
-                       $condition = ['uid' => $uid, 'blocked' => false, 'account_expired' => false,
-                               'account_removed' => false, 'verified' => true];
-                       $fields = ['guid', 'nickname', 'pubkey', 'account-type', 'page-flags'];
-                       $user = DBA::selectFirst('user', $fields, $condition);
-                       if (!DBA::isResult($user)) {
-                               return [];
-                       }
-
-                       $fields = ['locality', 'region', 'country-name'];
-                       $profile = DBA::selectFirst('profile', $fields, ['uid' => $uid]);
-                       if (!DBA::isResult($profile)) {
-                               return [];
-                       }
-
-                       $fields = ['name', 'url', 'location', 'about', 'avatar', 'photo'];
-                       $contact = DBA::selectFirst('contact', $fields, ['uid' => $uid, 'self' => true]);
-                       if (!DBA::isResult($contact)) {
-                               return [];
-                       }
-               } else {
-                       $contact = User::getSystemAccount();
-                       $user = ['guid' => '', 'nickname' => $contact['nick'], 'pubkey' => $contact['pubkey'],
-                               'account-type' => $contact['contact-type'], 'page-flags' => User::PAGE_FLAGS_NORMAL];
-                       $profile = ['locality' => '', 'region' => '', 'country-name' => ''];
-               }
+               $owner = User::getOwnerDataById($uid);
 
                $data = ['@context' => ActivityPub::CONTEXT];
-               $data['id'] = $contact['url'];
+               $data['id'] = $owner['url'];
 
-               if (!empty($user['guid'])) {
-                       $data['diaspora:guid'] = $user['guid'];
+               if (!empty($owner['guid'])) {
+                       $data['diaspora:guid'] = $owner['guid'];
                }
 
-               $data['type'] = ActivityPub::ACCOUNT_TYPES[$user['account-type']];
-               
+               $data['type'] = ActivityPub::ACCOUNT_TYPES[$owner['account-type']];
+
                if ($uid != 0) {
-                       $data['following'] = DI::baseUrl() . '/following/' . $user['nickname'];
-                       $data['followers'] = DI::baseUrl() . '/followers/' . $user['nickname'];
-                       $data['inbox'] = DI::baseUrl() . '/inbox/' . $user['nickname'];
-                       $data['outbox'] = DI::baseUrl() . '/outbox/' . $user['nickname'];
+                       $data['following'] = DI::baseUrl() . '/following/' . $owner['nick'];
+                       $data['followers'] = DI::baseUrl() . '/followers/' . $owner['nick'];
+                       $data['inbox'] = DI::baseUrl() . '/inbox/' . $owner['nick'];
+                       $data['outbox'] = DI::baseUrl() . '/outbox/' . $owner['nick'];
                } else {
                        $data['inbox'] = DI::baseUrl() . '/friendica/inbox';
                }
 
-               $data['preferredUsername'] = $user['nickname'];
-               $data['name'] = $contact['name'];
+               $data['preferredUsername'] = $owner['nick'];
+               $data['name'] = $owner['name'];
 
-               if (!empty($profile['country-name'] . $profile['region'] . $profile['locality'])) {
-                       $data['vcard:hasAddress'] = ['@type' => 'vcard:Home', 'vcard:country-name' => $profile['country-name'],
-                               'vcard:region' => $profile['region'], 'vcard:locality' => $profile['locality']];
+               if (!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($contact['about'])) {
-                       $data['summary'] = BBCode::convert($contact['about'], false);
+               if (!empty($owner['about'])) {
+                       $data['summary'] = BBCode::convertForUriId($owner['uri-id'] ?? 0, $owner['about'], BBCode::EXTERNAL);
                }
 
-               $data['url'] = $contact['url'];
-               $data['manuallyApprovesFollowers'] = in_array($user['page-flags'], [User::PAGE_FLAGS_NORMAL, User::PAGE_FLAGS_PRVGROUP]);
-               $data['publicKey'] = ['id' => $contact['url'] . '#main-key',
-                       'owner' => $contact['url'],
-                       'publicKeyPem' => $user['pubkey']];
+               $data['url'] = $owner['url'];
+               $data['manuallyApprovesFollowers'] = in_array($owner['page-flags'], [User::PAGE_FLAGS_NORMAL, User::PAGE_FLAGS_PRVGROUP]);
+               $data['discoverable'] = $owner['net-publish'];
+               $data['publicKey'] = ['id' => $owner['url'] . '#main-key',
+                       'owner' => $owner['url'],
+                       'publicKeyPem' => $owner['pubkey']];
                $data['endpoints'] = ['sharedInbox' => DI::baseUrl() . '/inbox'];
-               $data['icon'] = ['type' => 'Image', 'url' => $contact['photo']];
+               $data['icon'] = ['type' => 'Image', 'url' => Contact::getAvatarUrlForId($owner['id'], '', $owner['updated'])];
 
-               $resourceid = Photo::ridFromURI($contact['photo']);
+               $resourceid = Photo::ridFromURI($owner['photo']);
                if (!empty($resourceid)) {
                        $photo = Photo::selectFirst(['type'], ["resource-id" => $resourceid]);
                        if (!empty($photo['type'])) {
@@ -389,10 +365,10 @@ class Transmitter
                        }
                }
 
-               if (!empty($contact['header'])) {
-                       $data['image'] = ['type' => 'Image', 'url' => $contact['header']];
+               if (!empty($owner['header'])) {
+                       $data['image'] = ['type' => 'Image', 'url' => Contact::getHeaderUrlForId($owner['id'], '', $owner['updated'])];
 
-                       $resourceid = Photo::ridFromURI($contact['header']);
+                       $resourceid = Photo::ridFromURI($owner['header']);
                        if (!empty($resourceid)) {
                                $photo = Photo::selectFirst(['type'], ["resource-id" => $resourceid]);
                                if (!empty($photo['type'])) {
@@ -719,10 +695,10 @@ class Transmitter
        /**
         * Check if a given contact should be delivered via AP
         *
-        * @param array $contact 
-        * @param array $networks 
-        * @return bool 
-        * @throws Exception 
+        * @param array $contact
+        * @param array $networks
+        * @return bool
+        * @throws Exception
         */
        private static function isAPContact(array $contact, array $networks)
        {
@@ -1078,7 +1054,7 @@ class Transmitter
                                if (!empty($self['uid'])) {
                                        $forum_item = Post::selectFirst(Item::DELIVER_FIELDLIST, ['uri-id' => $item['uri-id'], 'uid' => $self['uid']]);
                                        if (DBA::isResult($forum_item)) {
-                                               $item = $forum_item; 
+                                               $item = $forum_item;
                                        }
                                }
                        }
@@ -1297,10 +1273,24 @@ class Transmitter
                                }
                                $urls[] = $attachment['url'];
 
-                               $attachments[] = ['type' => 'Document',
+                               $attach = ['type' => 'Document',
                                        'mediaType' => $attachment['mimetype'],
                                        'url' => $attachment['url'],
                                        'name' => $attachment['description']];
+
+                               if (!empty($attachment['height'])) {
+                                       $attach['height'] = $attachment['height'];
+                               }
+
+                               if (!empty($attachment['width'])) {
+                                       $attach['width'] = $attachment['width'];
+                               }
+
+                               if (!empty($attachment['preview'])) {
+                                       $attach['image'] = $attachment['preview'];
+                               }
+
+                               $attachments[] = $attach;
                        }
                }
 
@@ -1315,10 +1305,24 @@ class Transmitter
                                }
                                $urls[] = $attachment['url'];
 
-                               $attachments[] = ['type' => 'Document',
+                               $attach = ['type' => 'Document',
                                        'mediaType' => $attachment['mimetype'],
                                        'url' => $attachment['url'],
                                        'name' => $attachment['description']];
+
+                               if (!empty($attachment['height'])) {
+                                       $attach['height'] = $attachment['height'];
+                               }
+
+                               if (!empty($attachment['width'])) {
+                                       $attach['width'] = $attachment['width'];
+                               }
+
+                               if (!empty($attachment['preview'])) {
+                                       $attach['image'] = $attachment['preview'];
+                               }
+
+                               $attachments[] = $attach;
                        }
                        // Currently deactivated, since it creates side effects on Mastodon and Pleroma.
                        // It will be activated, once this cleared.
@@ -1339,27 +1343,6 @@ class Transmitter
                return $attachments;
        }
 
-       /**
-        * Callback function to replace a Friendica style mention in a mention that is used on AP
-        *
-        * @param array $match Matching values for the callback
-        * @return string Replaced mention
-        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
-        */
-       private static function mentionCallback($match)
-       {
-               if (empty($match[1])) {
-                       return '';
-               }
-
-               $data = Contact::getByURL($match[1], false, ['url', 'alias', 'nick']);
-               if (empty($data['nick'])) {
-                       return $match[0];
-               }
-
-               return '[url=' . $data['url'] . ']@' . $data['nick'] . '[/url]';
-       }
-
        /**
         * Callback function to replace a Friendica style mention in a mention for a summary
         *
@@ -1460,7 +1443,7 @@ class Transmitter
        {
                $event = [];
                $event['name'] = $item['event-summary'];
-               $event['content'] = BBCode::convert($item['event-desc'], false, BBCode::ACTIVITYPUB);
+               $event['content'] = BBCode::convertForUriId($item['uri-id'], $item['event-desc'], BBCode::ACTIVITYPUB);
                $event['startTime'] = DateTimeFormat::utc($item['event-start'] . '+00:00', DateTimeFormat::ATOM);
 
                if (!$item['event-nofinish']) {
@@ -1549,7 +1532,7 @@ class Transmitter
                 * This part is currently deactivated. The automated summary seems to be more
                 * confusing than helping. But possibly we will find a better way.
                 * So the code is left here for now as a reminder
-                * 
+                *
                 * } elseif (($type == 'Article') && empty($data['summary'])) {
                 *              $regexp = "/[@!]\[url\=([^\[\]]*)\].*?\[\/url\]/ism";
                 *              $summary = preg_replace_callback($regexp, ['self', 'mentionAddrCallback'], $body);
@@ -1564,10 +1547,9 @@ class Transmitter
                if ($type == 'Event') {
                        $data = array_merge($data, self::createEvent($item));
                } else {
-                       $regexp = "/[@!]\[url\=([^\[\]]*)\].*?\[\/url\]/ism";
-                       $body = preg_replace_callback($regexp, ['self', 'mentionCallback'], $body);
+                       $body = BBCode::setMentionsToNicknames($body);
 
-                       $data['content'] = BBCode::convert($body, false, BBCode::ACTIVITYPUB);
+                       $data['content'] = BBCode::convertForUriId($item['uri-id'], $body, BBCode::ACTIVITYPUB);
                }
 
                // The regular "content" field does contain a minimized HTML. This is done since systems like
@@ -1575,11 +1557,10 @@ class Transmitter
                // The contentMap does contain the unmodified HTML.
                $language = self::getLanguage($item);
                if (!empty($language)) {
-                       $regexp = "/[@!]\[url\=([^\[\]]*)\].*?\[\/url\]/ism";
-                       $richbody = preg_replace_callback($regexp, ['self', 'mentionCallback'], $item['body']);
+                       $richbody = BBCode::setMentionsToNicknames($item['body'] ?? '');
                        $richbody = BBCode::removeAttachment($richbody);
 
-                       $data['contentMap'][$language] = BBCode::convert($richbody, false, BBCode::EXTERNAL);
+                       $data['contentMap'][$language] = BBCode::convertForUriId($item['uri-id'], $richbody, BBCode::EXTERNAL);
                }
 
                $data['source'] = ['content' => $item['body'], 'mediaType' => "text/bbcode"];