A platform for community discussion. Free, open, simple.
FIX: Remove dead and large images from oneboxes (#17868)
Bianca Nenciuc789c689c2e2
diff --git a/lib/cooked_post_processor.rb b/lib/cooked_post_processor.rbindex 340559a8392..2f27ecdf466 100644--- a/lib/cooked_post_processor.rb+++ b/lib/cooked_post_processor.rb@@ -378,10 +378,20 @@ class CookedPostProcessorstill_an_image = trueif 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 = falseelsif 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 = falseelsif info&.downloaded? && upload = info&.uploadimg["src"] = UrlHelper.cook_url(upload.url, secure: @with_secure_media)diff --git a/spec/lib/cooked_post_processor_spec.rb b/spec/lib/cooked_post_processor_spec.rbindex 4c633b5a547..a4515487e08 100644--- a/spec/lib/cooked_post_processor_spec.rb+++ b/spec/lib/cooked_post_processor_spec.rb@@ -1132,6 +1132,44 @@ RSpec.describe CookedPostProcessor doexpect(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" dourl = 'https://image.com/my-avatar'image_url = 'https://image.com/avatar.png'@@ -1148,6 +1186,44 @@ RSpec.describe CookedPostProcessor doexpect(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+ endenddescribe "#post_process_oneboxes removes nofollow if add_rel_nofollow_to_user_content is disabled" do