FIX: Remove dead and large images from oneboxes (#17868)

Dead and large images are replaced with a placeholder, either a broken
chain icon or a short text. This commit no longer applies this
transformation for images inside Oneboxes, but removes them instead.
This commit is contained in:
Bianca Nenciu 2022-08-11 19:09:48 +03:00 committed by GitHub
parent 58b135d6d3
commit c789c689c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 88 additions and 2 deletions

View File

@ -378,10 +378,20 @@ class CookedPostProcessor
still_an_image = true
if info&.too_large?
add_large_image_placeholder!(img)
if img.ancestors('.onebox, .onebox-body').blank?
add_large_image_placeholder!(img)
else
img.remove
end
still_an_image = false
elsif info&.download_failed?
add_broken_image_placeholder!(img)
if img.ancestors('.onebox, .onebox-body').blank?
add_broken_image_placeholder!(img)
else
img.remove
end
still_an_image = false
elsif info&.downloaded? && upload = info&.upload
img["src"] = UrlHelper.cook_url(upload.url, secure: @with_secure_media)

View File

@ -1132,6 +1132,44 @@ RSpec.describe CookedPostProcessor do
expect(cpp.doc.to_s).to include(I18n.t("upload.placeholders.too_large_humanized", max_size: "4 MB"))
end
it "removes large images from onebox" do
url = 'https://example.com/article'
Oneboxer.stubs(:onebox).with(url, anything).returns <<~HTML
<aside class="onebox allowlistedgeneric" data-onebox-src="https://example.com/article">
<header class="source">
<img src="https://example.com/favicon.ico" class="site-icon">
<a href="https://example.com/article" target="_blank" rel="nofollow ugc noopener">Example Site</a>
</header>
<article class="onebox-body">
<img src="https://example.com/article.jpeg" class="thumbnail">
<h3><a href="https://example.com/article" target="_blank" rel="nofollow ugc noopener">Lorem Ispum</a></h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer tellus neque, malesuada ac neque ac, tempus tincidunt lectus.</p>
</article>
</aside>
HTML
post = Fabricate(:post, raw: url)
PostHotlinkedMedia.create!(url: "//example.com/favicon.ico", post: post, status: 'too_large')
PostHotlinkedMedia.create!(url: "//example.com/article.jpeg", post: post, status: 'too_large')
cpp = CookedPostProcessor.new(post, invalidate_oneboxes: true)
cpp.post_process
expect(cpp.doc).to match_html <<~HTML
<aside class="onebox allowlistedgeneric" data-onebox-src="https://example.com/article">
<header class="source">
<a href="https://example.com/article" target="_blank" rel="noopener nofollow ugc">Example Site</a>
</header>
<article class="onebox-body">
<h3><a href="https://example.com/article" target="_blank" rel="noopener nofollow ugc">Lorem Ispum</a></h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer tellus neque, malesuada ac neque ac, tempus tincidunt lectus.</p>
</article>
</aside>
HTML
end
it "replaces broken image placeholder" do
url = 'https://image.com/my-avatar'
image_url = 'https://image.com/avatar.png'
@ -1148,6 +1186,44 @@ RSpec.describe CookedPostProcessor do
expect(cpp.doc.to_s).to have_tag("span.broken-image")
expect(cpp.doc.to_s).to include(I18n.t("post.image_placeholder.broken"))
end
it "removes broken images from onebox" do
url = 'https://example.com/article'
Oneboxer.stubs(:onebox).with(url, anything).returns <<~HTML
<aside class="onebox allowlistedgeneric" data-onebox-src="https://example.com/article">
<header class="source">
<img src="https://example.com/favicon.ico" class="site-icon">
<a href="https://example.com/article" target="_blank" rel="nofollow ugc noopener">Example Site</a>
</header>
<article class="onebox-body">
<img src="https://example.com/article.jpeg" class="thumbnail">
<h3><a href="https://example.com/article" target="_blank" rel="nofollow ugc noopener">Lorem Ispum</a></h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer tellus neque, malesuada ac neque ac, tempus tincidunt lectus.</p>
</article>
</aside>
HTML
post = Fabricate(:post, raw: url)
PostHotlinkedMedia.create!(url: "//example.com/favicon.ico", post: post, status: 'download_failed')
PostHotlinkedMedia.create!(url: "//example.com/article.jpeg", post: post, status: 'download_failed')
cpp = CookedPostProcessor.new(post, invalidate_oneboxes: true)
cpp.post_process
expect(cpp.doc).to match_html <<~HTML
<aside class="onebox allowlistedgeneric" data-onebox-src="https://example.com/article">
<header class="source">
<a href="https://example.com/article" target="_blank" rel="noopener nofollow ugc">Example Site</a>
</header>
<article class="onebox-body">
<h3><a href="https://example.com/article" target="_blank" rel="noopener nofollow ugc">Lorem Ispum</a></h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer tellus neque, malesuada ac neque ac, tempus tincidunt lectus.</p>
</article>
</aside>
HTML
end
end
describe "#post_process_oneboxes removes nofollow if add_rel_nofollow_to_user_content is disabled" do