diff --git a/lib/cooked_post_processor.rb b/lib/cooked_post_processor.rb index 340559a8392..2f27ecdf466 100644 --- a/lib/cooked_post_processor.rb +++ b/lib/cooked_post_processor.rb @@ -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) diff --git a/spec/lib/cooked_post_processor_spec.rb b/spec/lib/cooked_post_processor_spec.rb index 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 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 + + 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 + + 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 + + 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 + + HTML + end end describe "#post_process_oneboxes removes nofollow if add_rel_nofollow_to_user_content is disabled" do