From eca52627c150618424fb6c459393d2a9c37069c4 Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 15 Nov 2022 19:48:43 +0000 Subject: [PATCH 1/2] This fixes duplicated media in quoted posts --- src/Model/Item.php | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/src/Model/Item.php b/src/Model/Item.php index 8ab390eb0..f79e531d8 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -3006,6 +3006,7 @@ class Item $item['body'] = self::replaceVisualAttachments($attachments, $item['body'] ?? ''); $item['body'] = preg_replace("/\s*\[attachment .*?\].*?\[\/attachment\]\s*/ism", "\n", $item['body']); + $bbcode = $item['body']; self::putInCache($item); $item['body'] = $body; $s = $item["rendered-html"]; @@ -3050,13 +3051,13 @@ class Item } if (!empty($shared_attachments)) { - $s = self::addVisualAttachments($shared_attachments, $item, $s, true); + $s = self::addVisualAttachments($shared_attachments, $item, $s, true, $bbcode); $s = self::addLinkAttachment($shared_uri_id ?: $item['uri-id'], $shared_attachments, $body, $s, true, []); $s = self::addNonVisualAttachments($shared_attachments, $item, $s, true); $body = BBCode::removeSharedData($body); } - $s = self::addVisualAttachments($attachments, $item, $s, false); + $s = self::addVisualAttachments($attachments, $item, $s, false, $bbcode); $s = self::addLinkAttachment($item['uri-id'], $attachments, $body, $s, false, $shared_links); $s = self::addNonVisualAttachments($attachments, $item, $s, false); $s = self::addQuestions($item, $s); @@ -3165,12 +3166,13 @@ class Item /** * Add visual attachments to the content * - * @param array $attachments - * @param array $item + * @param array $attachments + * @param array $item * @param string $content + * @param string $body * @return string modified content */ - private static function addVisualAttachments(array $attachments, array $item, string $content, bool $shared): string + private static function addVisualAttachments(array $attachments, array $item, string $content, bool $shared, string $body): string { DI::profiler()->startRecording('rendering'); $leading = ''; @@ -3182,15 +3184,18 @@ class Item continue; } - if (!empty($attachment['preview'])) { + if ($attachment['filetype'] == 'image') { + $preview_url = Post\Media::getPreviewUrlForId($attachment['id'], ($attachment['width'] > $attachment['height']) ? Proxy::SIZE_MEDIUM : Proxy::SIZE_LARGE); + } elseif (!empty($attachment['preview'])) { $preview_url = Post\Media::getPreviewUrlForId($attachment['id'], Proxy::SIZE_LARGE); - if (self::containsLink($item['body'], $preview_url)) { - continue; - } } else { $preview_url = ''; } + if ($preview_url && (self::containsLink($item['body'], $preview_url) || self::containsLink($body, $preview_url))) { + continue; + } + if (($attachment['filetype'] == 'video')) { /// @todo Move the template to /content as well $media = Renderer::replaceMacros(Renderer::getMarkupTemplate('video_top.tpl'), [ @@ -3222,10 +3227,14 @@ class Item $trailing .= $media; } } elseif ($attachment['filetype'] == 'image') { + $src_url = Post\Media::getUrlForId($attachment['id']); + if (self::containsLink($item['body'], $src_url) || self::containsLink($body, $src_url)) { + continue; + } $media = Renderer::replaceMacros(Renderer::getMarkupTemplate('content/image.tpl'), [ '$image' => [ - 'src' => Post\Media::getUrlForId($attachment['id']), - 'preview' => Post\Media::getPreviewUrlForId($attachment['id'], ($attachment['width'] > $attachment['height']) ? Proxy::SIZE_MEDIUM : Proxy::SIZE_LARGE), + 'src' => $src_url, + 'preview' => $preview_url, 'attachment' => $attachment, ], ]); From 348156dbcf42d1aab480a81e285c92b08eaf9147 Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 15 Nov 2022 19:55:03 +0000 Subject: [PATCH 2/2] Simplify the check --- src/Model/Item.php | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/Model/Item.php b/src/Model/Item.php index f79e531d8..900bf3a2c 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -2963,7 +2963,7 @@ class Item $body = $item['body'] ?? ''; - $fields = ['uri-id', 'uri', 'body', 'title', 'author-name', 'author-link', 'author-avatar', 'guid', 'created', 'plink', 'network', 'has-media', 'quote-uri-id']; + $fields = ['uri-id', 'uri', 'body', 'title', 'author-name', 'author-link', 'author-avatar', 'guid', 'created', 'plink', 'network', 'has-media', 'quote-uri-id', 'post-type']; $shared_uri_id = 0; $shared_links = []; @@ -3006,7 +3006,6 @@ class Item $item['body'] = self::replaceVisualAttachments($attachments, $item['body'] ?? ''); $item['body'] = preg_replace("/\s*\[attachment .*?\].*?\[\/attachment\]\s*/ism", "\n", $item['body']); - $bbcode = $item['body']; self::putInCache($item); $item['body'] = $body; $s = $item["rendered-html"]; @@ -3051,13 +3050,13 @@ class Item } if (!empty($shared_attachments)) { - $s = self::addVisualAttachments($shared_attachments, $item, $s, true, $bbcode); + $s = self::addVisualAttachments($shared_attachments, $shared_item, $s, true); $s = self::addLinkAttachment($shared_uri_id ?: $item['uri-id'], $shared_attachments, $body, $s, true, []); $s = self::addNonVisualAttachments($shared_attachments, $item, $s, true); $body = BBCode::removeSharedData($body); } - $s = self::addVisualAttachments($attachments, $item, $s, false, $bbcode); + $s = self::addVisualAttachments($attachments, $item, $s, false); $s = self::addLinkAttachment($item['uri-id'], $attachments, $body, $s, false, $shared_links); $s = self::addNonVisualAttachments($attachments, $item, $s, false); $s = self::addQuestions($item, $s); @@ -3166,13 +3165,12 @@ class Item /** * Add visual attachments to the content * - * @param array $attachments - * @param array $item + * @param array $attachments + * @param array $item * @param string $content - * @param string $body * @return string modified content */ - private static function addVisualAttachments(array $attachments, array $item, string $content, bool $shared, string $body): string + private static function addVisualAttachments(array $attachments, array $item, string $content, bool $shared): string { DI::profiler()->startRecording('rendering'); $leading = ''; @@ -3192,7 +3190,7 @@ class Item $preview_url = ''; } - if ($preview_url && (self::containsLink($item['body'], $preview_url) || self::containsLink($body, $preview_url))) { + if ($preview_url && self::containsLink($item['body'], $preview_url)) { continue; } @@ -3228,7 +3226,7 @@ class Item } } elseif ($attachment['filetype'] == 'image') { $src_url = Post\Media::getUrlForId($attachment['id']); - if (self::containsLink($item['body'], $src_url) || self::containsLink($body, $src_url)) { + if (self::containsLink($item['body'], $src_url)) { continue; } $media = Renderer::replaceMacros(Renderer::getMarkupTemplate('content/image.tpl'), [