]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/ActivityPub/Transmitter.php
Merge pull request #10288 from annando/mail-uri-id
[friendica.git] / src / Protocol / ActivityPub / Transmitter.php
index 8adc7c49637c6eead3602b6a76d22b672f3d3c9f..4e009d61d456622d98b8c6bd11add84850a78103 100644 (file)
@@ -861,38 +861,41 @@ class Transmitter
         * @return array
         * @throws \Exception
         */
-       public static function ItemArrayFromMail($mail_id)
+       public static function ItemArrayFromMail($mail_id, $use_title = false)
        {
                $mail = DBA::selectFirst('mail', [], ['id' => $mail_id]);
                if (!DBA::isResult($mail)) {
                        return [];
                }
 
-               $mail['uri-id'] = ItemURI::insert(['uri' => $mail['uri'], 'guid' => $mail['guid']]);
-
-               $reply = DBA::selectFirst('mail', ['uri'], ['parent-uri' => $mail['parent-uri'], 'reply' => false]);
+               $reply = DBA::selectFirst('mail', ['uri', 'uri-id', 'from-url'], ['parent-uri' => $mail['parent-uri'], 'reply' => false]);
 
                // Making the post more compatible for Mastodon by:
                // - Making it a note and not an article (no title)
                // - Moving the title into the "summary" field that is used as a "content warning"
-               $mail['body'] = '[abstract]' . $mail['title'] . "[/abstract]\n" . $mail['body'];
-               $mail['title'] = '';
-
-               $mail['author-link'] = $mail['owner-link'] = $mail['from-url'];
-               $mail['allow_cid'] = '<'.$mail['contact-id'].'>';
-               $mail['allow_gid'] = '';
-               $mail['deny_cid'] = '';
-               $mail['deny_gid'] = '';
-               $mail['private'] = true;
-               $mail['deleted'] = false;
-               $mail['edited'] = $mail['created'];
-               $mail['plink'] = $mail['uri'];
-               $mail['thr-parent'] = $reply['uri'];
-               $mail['gravity'] = ($mail['reply'] ? GRAVITY_COMMENT: GRAVITY_PARENT);
-
-               $mail['event-type'] = '';
 
-               $mail['parent'] = 0;
+               if (!$use_title) {
+                       $mail['body']         = '[abstract]' . $mail['title'] . "[/abstract]\n" . $mail['body'];
+                       $mail['title']        = '';
+               }
+
+               $mail['author-link']      = $mail['owner-link'] = $mail['from-url'];
+               $mail['owner-id']         = $mail['author-id'];
+               $mail['allow_cid']        = '<'.$mail['contact-id'].'>';
+               $mail['allow_gid']        = '';
+               $mail['deny_cid']         = '';
+               $mail['deny_gid']         = '';
+               $mail['private']          = Item::PRIVATE;
+               $mail['deleted']          = false;
+               $mail['edited']           = $mail['created'];
+               $mail['plink']            = DI::baseUrl() . '/message/' . $mail['id'];
+               $mail['parent-uri']       = $reply['uri'];
+               $mail['parent-uri-id']    = $reply['uri-id'];
+               $mail['parent-author-id'] = Contact::getIdForURL($reply['from-url'], 0, false);
+               $mail['gravity']          = ($mail['reply'] ? GRAVITY_COMMENT: GRAVITY_PARENT);
+               $mail['event-type']       = '';
+               $mail['language']         = '';
+               $mail['parent']           = 0;
 
                return $mail;
        }
@@ -1260,53 +1263,60 @@ class Transmitter
        {
                $attachments = [];
 
-               // Currently deactivated, since it creates side effects on Mastodon and Pleroma.
-               // It will be reactivated, once this cleared.
-               /*
-               $attach_data = BBCode::getAttachmentData($item['body']);
-               if (!empty($attach_data['url'])) {
-                       $attachment = ['type' => 'Page',
-                               'mediaType' => 'text/html',
-                               'url' => $attach_data['url']];
-
-                       if (!empty($attach_data['title'])) {
-                               $attachment['name'] = $attach_data['title'];
-                       }
-
-                       if (!empty($attach_data['description'])) {
-                               $attachment['summary'] = $attach_data['description'];
+               $uriids = [$item['uri-id']];
+               $shared = BBCode::fetchShareAttributes($item['body']);
+               if (!empty($shared['guid'])) {
+                       $shared_item = Post::selectFirst(['uri-id'], ['guid' => $shared['guid']]);
+                       if (!empty($shared_item['uri-id'])) {
+                               $uriids[] = $shared_item['uri-id'];
                        }
+               }
 
-                       if (!empty($attach_data['image'])) {
-                               $imgdata = Images::getInfoFromURLCached($attach_data['image']);
-                               if ($imgdata) {
-                                       $attachment['icon'] = ['type' => 'Image',
-                                               'mediaType' => $imgdata['mime'],
-                                               'width' => $imgdata[0],
-                                               'height' => $imgdata[1],
-                                               'url' => $attach_data['image']];
+               $urls = [];
+               foreach ($uriids as $uriid) {
+                       foreach (Post\Media::getByURIId($uriid, [Post\Media::DOCUMENT, Post\Media::TORRENT]) as $attachment) {
+                               if (in_array($attachment['url'], $urls)) {
+                                       continue;
                                }
-                       }
+                               $urls[] = $attachment['url'];
 
-                       $attachments[] = $attachment;
-               }
-               */
-               foreach (Post\Media::getByURIId($item['uri-id'], [Post\Media::DOCUMENT, Post\Media::TORRENT, Post\Media::UNKNOWN]) as $attachment) {
-                       $attachments[] = ['type' => 'Document',
-                               'mediaType' => $attachment['mimetype'],
-                               'url' => $attachment['url'],
-                               'name' => $attachment['description']];
+                               $attachments[] = ['type' => 'Document',
+                                       'mediaType' => $attachment['mimetype'],
+                                       'url' => $attachment['url'],
+                                       'name' => $attachment['description']];
+                       }
                }
 
                if ($type != 'Note') {
                        return $attachments;
                }
 
-               foreach (Post\Media::getByURIId($item['uri-id'], [Post\Media::AUDIO, Post\Media::IMAGE, Post\Media::VIDEO]) as $attachment) {
-                       $attachments[] = ['type' => 'Document',
-                               'mediaType' => $attachment['mimetype'],
-                               'url' => $attachment['url'],
-                               'name' => $attachment['description']];
+               foreach ($uriids as $uriid) {
+                       foreach (Post\Media::getByURIId($uriid, [Post\Media::AUDIO, Post\Media::IMAGE, Post\Media::VIDEO]) as $attachment) {
+                               if (in_array($attachment['url'], $urls)) {
+                                       continue;
+                               }
+                               $urls[] = $attachment['url'];
+
+                               $attachments[] = ['type' => 'Document',
+                                       'mediaType' => $attachment['mimetype'],
+                                       'url' => $attachment['url'],
+                                       'name' => $attachment['description']];
+                       }
+                       // Currently deactivated, since it creates side effects on Mastodon and Pleroma.
+                       // It will be activated, once this cleared.
+                       /*
+                       foreach (Post\Media::getByURIId($uriid, [Post\Media::HTML]) as $attachment) {
+                               if (in_array($attachment['url'], $urls)) {
+                                       continue;
+                               }
+                               $urls[] = $attachment['url'];
+
+                               $attachments[] = ['type' => 'Page',
+                                       'mediaType' => $attachment['mimetype'],
+                                       'url' => $attachment['url'],
+                                       'name' => $attachment['description']];
+                       }*/
                }
 
                return $attachments;
@@ -1333,6 +1343,27 @@ class Transmitter
                return '[url=' . $data['url'] . ']@' . $data['nick'] . '[/url]';
        }
 
+       /**
+        * Callback function to replace a Friendica style mention in a mention for a summary
+        *
+        * @param array $match Matching values for the callback
+        * @return string Replaced mention
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        */
+       private static function mentionAddrCallback($match)
+       {
+               if (empty($match[1])) {
+                       return '';
+               }
+
+               $data = Contact::getByURL($match[1], false, ['addr']);
+               if (empty($data['addr'])) {
+                       return $match[0];
+               }
+
+               return '@' . $data['addr'];
+       }
+
        /**
         * Remove image elements since they are added as attachment
         *
@@ -1408,7 +1439,7 @@ class Transmitter
         * @return array with the event data
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
-       public static function createEvent($item)
+       private static function createEvent($item)
        {
                $event = [];
                $event['name'] = $item['event-summary'];
@@ -1424,6 +1455,8 @@ class Transmitter
                        $event['location'] = self::createLocation($item);
                }
 
+               $event['dfrn:adjust'] = (bool)$item['event-adjust'];
+
                return $event;
        }
 
@@ -1493,7 +1526,9 @@ class Transmitter
                if ($type == 'Note') {
                        $body = $item['raw-body'] ?? self::removePictures($body);
                } elseif (($type == 'Article') && empty($data['summary'])) {
-                       $data['summary'] = BBCode::toPlaintext(Plaintext::shorten(self::removePictures($body), 1000));
+                       $regexp = "/[@!]\[url\=([^\[\]]*)\].*?\[\/url\]/ism";
+                       $summary = preg_replace_callback($regexp, ['self', 'mentionAddrCallback'], $body);
+                       $data['summary'] = BBCode::toPlaintext(Plaintext::shorten(self::removePictures($summary), 1000));
                }
 
                if (empty($item['uid']) || !Feature::isEnabled($item['uid'], 'explicit_mentions')) {
@@ -1518,7 +1553,7 @@ class Transmitter
                        $richbody = preg_replace_callback($regexp, ['self', 'mentionCallback'], $item['body']);
                        $richbody = BBCode::removeAttachment($richbody);
 
-                       $data['contentMap'][$language] = BBCode::convert($richbody, false);
+                       $data['contentMap'][$language] = BBCode::convert($richbody, false, BBCode::EXTERNAL);
                }
 
                $data['source'] = ['content' => $item['body'], 'mediaType' => "text/bbcode"];