]> git.mxchange.org Git - friendica.git/commitdiff
Fix missing attached links in posts
authorMichael <heluecht@pirati.ca>
Sat, 4 Mar 2023 19:04:37 +0000 (19:04 +0000)
committerMichael <heluecht@pirati.ca>
Sat, 4 Mar 2023 19:04:37 +0000 (19:04 +0000)
src/Factory/Api/Mastodon/Status.php
src/Factory/Api/Twitter/Status.php
src/Model/Post/Media.php
src/Module/Post/Edit.php
src/Protocol/ActivityPub/Transmitter.php
src/Protocol/DFRN.php
src/Protocol/Diaspora.php
src/Protocol/Feed.php
src/Protocol/OStatus.php
src/Worker/Notifier.php

index d794247cb151c8eabd64af175f95d2347b6fc928..a1f7060ac0a81f795f7ec49441f583cba4f5991f 100644 (file)
@@ -260,9 +260,11 @@ class Status extends BaseFactory
                        }
 
                        $item['body'] = $this->contentItem->addSharedPost($item);
+                       $item['body'] = Post\Media::addHTMLAttachmentToBody($uriId, $item['body']);
 
                        if (!is_null($item['raw-body'])) {
                                $item['raw-body'] = $this->contentItem->addSharedPost($item, $item['raw-body']);
+                               $item['raw-body'] = Post\Media::addHTMLLinkToBody($uriId, $item['raw-body']);
                        }
                }
 
index de01ffd07634739362a32812d38e4113255415d0..810b421cab68868f28bec257d158c0fd76e19560 100644 (file)
@@ -124,6 +124,7 @@ class Status extends BaseFactory
         */
        private function createFromArray(array $item, int $uid, bool $include_entities): \Friendica\Object\Api\Twitter\Status
        {
+               $item = Post\Media::addHTMLAttachmentToItem($item);
                $author = $this->twitterUser->createFromContactId($item['author-id'], $uid, true);
 
                if (!empty($item['causer-id']) && ($item['post-reason'] == Item::PR_ANNOUNCEMENT)) {
index 653bdba98a3879b313a8b2bdf261843a6f1d8291..c6185cf1a8230acb64e821c04a9eafd89e9f529d 100644 (file)
@@ -21,6 +21,7 @@
 
 namespace Friendica\Model\Post;
 
+use Friendica\Content\PageInfo;
 use Friendica\Content\Text\BBCode;
 use Friendica\Core\Logger;
 use Friendica\Core\Protocol;
@@ -895,6 +896,73 @@ class Media
                return $body;
        }
 
+       public static function addHTMLAttachmentToBody(int $uriid, string $body): string
+       {
+               if (preg_match("/.*(\[attachment.*?\].*?\[\/attachment\]).*/ism", $body, $match)) {
+                       return $body;
+               }
+
+               $links = self::getByURIId($uriid, [self::HTML]);
+               if (empty($links)) {
+                       return $body;
+               }
+
+               $data = [
+                       'type' => 'link',
+                       'url'  => $links[0]['url'],
+                       'title' => $links[0]['name'],
+                       'text' => $links[0]['description'],
+                       'alternative_title' => '', 
+                       'publisher_name' => $links[0]['publisher-name'], 
+                       'publisher_url' => $links[0]['publisher-url'], 
+                       'publisher_img' => $links[0]['publisher-image'], 
+                       'author_name' => $links[0]['author-name'], 
+                       'author_url' => $links[0]['author-url'], 
+                       'author_img' => $links[0]['author-image'],
+                       'images' => [[
+                               'src' => $links[0]['preview'],
+                               'height' => $links[0]['preview-height'],
+                               'width' => $links[0]['preview-width'],
+                       ]]
+               ];
+               $body .= "\n" . PageInfo::getFooterFromData($data);
+               
+               return $body;
+       }
+
+       public static function addHTMLLinkToBody(int $uriid, string $body): string
+       {
+               $links = self::getByURIId($uriid, [self::HTML]);
+               if (empty($links)) {
+                       return $body;
+               }
+
+               if (strpos($body, $links[0]['url'])) {
+                       return $body;
+               }
+
+               if (!empty($links[0]['name']) && ($links[0]['name'] != $links[0]['url'])) {
+                       return $body . "\n[url=" . $links[0]['url'] . ']' . $links[0]['name'] . "[/url]";
+               } else {
+                       return $body . "\n[url]" . $links[0]['url'] . "[/url]";
+               }
+       }
+
+       public static function addHTMLAttachmentToItem(array $item): array
+       {
+               if (($item['gravity'] == Item::GRAVITY_ACTIVITY) || empty($item['uri-id'])) {
+                       return $item;
+               }
+
+               $item['body'] = self::addHTMLAttachmentToBody($item['uri-id'], $item['body']);
+
+               if (!empty($item['raw-body'])) {
+                       $item['raw-body'] = self::addHTMLLinkToBody($item['uri-id'], $item['raw-body']);
+               }
+
+               return $item;
+       }
+
        /**
         * Get preview link for given media id
         *
index 2ca98038189701b7aee02dbdb3394bff7e586f5a..5fb339e17971b83b1b0111a8e18db1dbc108aeb1 100644 (file)
@@ -116,6 +116,7 @@ class Edit extends BaseModule
                }
 
                $item['body'] = Post\Media::addAttachmentsToBody($item['uri-id'], $item['body']);
+               $item = Post\Media::addHTMLAttachmentToItem($item);
 
                $jotplugins = '';
 
index 9123ae533f46cb54973025aba9601e07c9159a58..7da110f6716dfd5fb6a2e284e727e19d85e551b8 100644 (file)
@@ -1639,6 +1639,8 @@ class Transmitter
 
                $real_quote = false;
 
+               $item = Post\Media::addHTMLAttachmentToItem($item);
+
                $body = $item['body'];
 
                if ($type == 'Note') {
index 26d5376e4e0d40152f4fffafbdb6c9ee5ae54b34..1bb7bf3ff14dd2938339ed92c3f0d3bfb8e5aed2 100644 (file)
@@ -774,6 +774,7 @@ class DFRN
                }
 
                $body = Post\Media::addAttachmentsToBody($item['uri-id'], DI::contentItem()->addSharedPost($item));
+               $body = Post\Media::addHTMLAttachmentToBody($item['uri-id'], $body);
 
                if ($item['private'] == Item::PRIVATE) {
                        $body = Item::fixPrivatePhotos($body, $owner['uid'], $item, $cid);
index 271b71b893e1b20120a93a5bcf2da293d0a14443..91eb1cc2c412717878f48a9827a2fd5e2a84781e 100644 (file)
@@ -3326,6 +3326,7 @@ class Diaspora
 
                        $title = $item['title'];
                        $body  = Post\Media::addAttachmentsToBody($item['uri-id'], DI::contentItem()->addSharedPost($item), $attach_media);
+                       $body  = Post\Media::addHTMLLinkToBody($item['uri-id'], $body);
 
                        // Fetch the title from an attached link - if there is one
                        if (empty($item['title']) && DI::pConfig()->get($owner['uid'], 'system', 'attach_link_title')) {
@@ -3585,6 +3586,7 @@ class Diaspora
                }
 
                $body = Post\Media::addAttachmentsToBody($item['uri-id'], DI::contentItem()->addSharedPost($item));
+               $body = Post\Media::addHTMLLinkToBody($item['uri-id'], $body);
 
                // The replied to autor mention is prepended for clarity if:
                // - Item replied isn't yours
index 7d778ee582bb2836486147b418d10e291c800b83..cbffd810c1d03b444faeedac9cd9ae201b2d623c 100644 (file)
@@ -1124,6 +1124,7 @@ class Feed
                XML::addElement($doc, $entry, 'title', html_entity_decode($title, ENT_QUOTES, 'UTF-8'));
 
                $body = Post\Media::addAttachmentsToBody($item['uri-id'], DI::contentItem()->addSharedPost($item));
+               $body = Post\Media::addHTMLAttachmentToBody($item['uri-id'], $body);
 
                $body = BBCode::convertForUriId($item['uri-id'], $body, BBCode::ACTIVITYPUB);
 
index a93a0584dd234b05fb29f8ce2cc82ca3f683e481..220424a31c62569a30f285f2f52cd0470831a1d8 100644 (file)
@@ -1515,6 +1515,7 @@ class OStatus
                XML::addElement($doc, $entry, 'title', html_entity_decode($title, ENT_QUOTES, 'UTF-8'));
 
                $body = Post\Media::addAttachmentsToBody($item['uri-id'], DI::contentItem()->addSharedPost($item));
+               $body = Post\Media::addHTMLAttachmentToBody($item['uri-id'], $body);
 
                if (!empty($item['title'])) {
                        $body = '[b]' . $item['title'] . "[/b]\n\n" . $body;
index 93c6c180ab125b339a7328db8d227cb1177960ee..2e42c225fc52837dc502296581733b011e258440 100644 (file)
@@ -114,6 +114,7 @@ class Notifier
                        // find ancestors
                        $condition = ['id' => $target_id, 'visible' => true];
                        $target_item = Post::selectFirst(Item::DELIVER_FIELDLIST, $condition);
+                       $target_item = Post\Media::addHTMLAttachmentToItem($target_item);
 
                        if (!DBA::isResult($target_item) || !intval($target_item['parent'])) {
                                Logger::info('No target item', ['cmd' => $cmd, 'target' => $target_id]);