From: Michael Date: Sat, 4 Mar 2023 19:04:37 +0000 (+0000) Subject: Fix missing attached links in posts X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=b761760d6537a64b74d41bbe3a9e75a58254e947;p=friendica.git Fix missing attached links in posts --- diff --git a/src/Factory/Api/Mastodon/Status.php b/src/Factory/Api/Mastodon/Status.php index d794247cb1..a1f7060ac0 100644 --- a/src/Factory/Api/Mastodon/Status.php +++ b/src/Factory/Api/Mastodon/Status.php @@ -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']); } } diff --git a/src/Factory/Api/Twitter/Status.php b/src/Factory/Api/Twitter/Status.php index de01ffd076..810b421cab 100644 --- a/src/Factory/Api/Twitter/Status.php +++ b/src/Factory/Api/Twitter/Status.php @@ -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)) { diff --git a/src/Model/Post/Media.php b/src/Model/Post/Media.php index 653bdba98a..c6185cf1a8 100644 --- a/src/Model/Post/Media.php +++ b/src/Model/Post/Media.php @@ -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 * diff --git a/src/Module/Post/Edit.php b/src/Module/Post/Edit.php index 2ca9803818..5fb339e179 100644 --- a/src/Module/Post/Edit.php +++ b/src/Module/Post/Edit.php @@ -116,6 +116,7 @@ class Edit extends BaseModule } $item['body'] = Post\Media::addAttachmentsToBody($item['uri-id'], $item['body']); + $item = Post\Media::addHTMLAttachmentToItem($item); $jotplugins = ''; diff --git a/src/Protocol/ActivityPub/Transmitter.php b/src/Protocol/ActivityPub/Transmitter.php index 9123ae533f..7da110f671 100644 --- a/src/Protocol/ActivityPub/Transmitter.php +++ b/src/Protocol/ActivityPub/Transmitter.php @@ -1639,6 +1639,8 @@ class Transmitter $real_quote = false; + $item = Post\Media::addHTMLAttachmentToItem($item); + $body = $item['body']; if ($type == 'Note') { diff --git a/src/Protocol/DFRN.php b/src/Protocol/DFRN.php index 26d5376e4e..1bb7bf3ff1 100644 --- a/src/Protocol/DFRN.php +++ b/src/Protocol/DFRN.php @@ -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); diff --git a/src/Protocol/Diaspora.php b/src/Protocol/Diaspora.php index 271b71b893..91eb1cc2c4 100644 --- a/src/Protocol/Diaspora.php +++ b/src/Protocol/Diaspora.php @@ -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 diff --git a/src/Protocol/Feed.php b/src/Protocol/Feed.php index 7d778ee582..cbffd810c1 100644 --- a/src/Protocol/Feed.php +++ b/src/Protocol/Feed.php @@ -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); diff --git a/src/Protocol/OStatus.php b/src/Protocol/OStatus.php index a93a0584dd..220424a31c 100644 --- a/src/Protocol/OStatus.php +++ b/src/Protocol/OStatus.php @@ -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; diff --git a/src/Worker/Notifier.php b/src/Worker/Notifier.php index 93c6c180ab..2e42c225fc 100644 --- a/src/Worker/Notifier.php +++ b/src/Worker/Notifier.php @@ -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]);