From 7109d94ee77a9a1ba908fa8115b1bd9acf2631ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Wed, 24 Jun 2020 11:54:54 +0200 Subject: [PATCH] FIX: properly invalidate inline oneboxes when rebaking When rebaking a post we were invalidating _regular_ oneboxes but not inline oneboxes. DEV: also renamed 'InlineOneboxer.purge' to 'InlineOneboxer.invalidate' to keep the API consistent with 'Oneboxer.invalidate' --- app/models/post.rb | 12 +++++------- app/models/post_analyzer.rb | 5 ++++- lib/inline_oneboxer.rb | 12 +++--------- spec/components/cooked_post_processor_spec.rb | 4 ++-- spec/components/inline_oneboxer_spec.rb | 6 +++--- spec/models/post_analyzer_spec.rb | 1 + 6 files changed, 18 insertions(+), 22 deletions(-) diff --git a/app/models/post.rb b/app/models/post.rb index fefb43d787c..a9880a8d26d 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -752,18 +752,16 @@ class Post < ActiveRecord::Base # Enqueue post processing for this post def trigger_post_process(bypass_bump: false, priority: :normal, new_post: false, skip_pull_hotlinked_images: false) args = { - post_id: id, bypass_bump: bypass_bump, + cooking_options: self.cooking_options, new_post: new_post, + post_id: id, skip_pull_hotlinked_images: skip_pull_hotlinked_images, } - args[:image_sizes] = image_sizes if image_sizes.present? - args[:invalidate_oneboxes] = true if invalidate_oneboxes.present? - args[:cooking_options] = self.cooking_options - if priority && priority != :normal - args[:queue] = priority.to_s - end + args[:image_sizes] = image_sizes if self.image_sizes.present? + args[:invalidate_oneboxes] = true if self.invalidate_oneboxes.present? + args[:queue] = priority.to_s if priority && priority != :normal Jobs.enqueue(:process_post, args) DiscourseEvent.trigger(:after_trigger_post_process, self) diff --git a/app/models/post_analyzer.rb b/app/models/post_analyzer.rb index bae36c39f86..c06d9d5f532 100644 --- a/app/models/post_analyzer.rb +++ b/app/models/post_analyzer.rb @@ -33,7 +33,10 @@ class PostAnalyzer result = Oneboxer.apply(cooked) do |url| @onebox_urls << url - Oneboxer.invalidate(url) if opts[:invalidate_oneboxes] + if opts[:invalidate_oneboxes] + Oneboxer.invalidate(url) + InlineOneboxer.invalidate(url) + end onebox = Oneboxer.cached_onebox(url) @found_oneboxes = true if onebox.present? onebox diff --git a/lib/inline_oneboxer.rb b/lib/inline_oneboxer.rb index edfd7465626..26f3ecc9ba6 100644 --- a/lib/inline_oneboxer.rb +++ b/lib/inline_oneboxer.rb @@ -13,7 +13,7 @@ class InlineOneboxer @urls.map { |url| InlineOneboxer.lookup(url, @opts) }.compact end - def self.purge(url) + def self.invalidate(url) Discourse.cache.delete(cache_key(url)) end @@ -65,14 +65,8 @@ class InlineOneboxer private def self.onebox_for(url, title, opts) - onebox = { - url: url, - title: title && Emoji.gsub_emoji_to_unicode(title) - } - unless opts[:skip_cache] - Discourse.cache.write(cache_key(url), onebox, expires_in: 1.day) - end - + onebox = { url: url, title: title && Emoji.gsub_emoji_to_unicode(title) } + Discourse.cache.write(cache_key(url), onebox, expires_in: 1.day) if !opts[:skip_cache] onebox end diff --git a/spec/components/cooked_post_processor_spec.rb b/spec/components/cooked_post_processor_spec.rb index aa63a7e4c8b..fef1e06aa3c 100644 --- a/spec/components/cooked_post_processor_spec.rb +++ b/spec/components/cooked_post_processor_spec.rb @@ -86,7 +86,7 @@ describe CookedPostProcessor do end after do - InlineOneboxer.purge(url) + InlineOneboxer.invalidate(url) Oneboxer.invalidate(url) end @@ -210,7 +210,7 @@ describe CookedPostProcessor do end after do - urls.each { |url| InlineOneboxer.purge(url) } + urls.each { |url| InlineOneboxer.invalidate(url) } end it 'should convert the right links to inline oneboxes' do diff --git a/spec/components/inline_oneboxer_spec.rb b/spec/components/inline_oneboxer_spec.rb index d99bab6d425..47c5abd6bb5 100644 --- a/spec/components/inline_oneboxer_spec.rb +++ b/spec/components/inline_oneboxer_spec.rb @@ -26,7 +26,7 @@ describe InlineOneboxer do fab!(:topic) { Fabricate(:topic) } before do - InlineOneboxer.purge(topic.url) + InlineOneboxer.invalidate(topic.url) end it "puts an entry in the cache" do @@ -34,7 +34,7 @@ describe InlineOneboxer do url = "https://example.com/random-url" stub_request(:get, url).to_return(status: 200, body: "a blog") - InlineOneboxer.purge(url) + InlineOneboxer.invalidate(url) expect(InlineOneboxer.cache_lookup(url)).to be_blank result = InlineOneboxer.lookup(url) @@ -49,7 +49,7 @@ describe InlineOneboxer do SiteSetting.enable_inline_onebox_on_all_domains = true url = "https://example.com/random-url" - InlineOneboxer.purge(url) + InlineOneboxer.invalidate(url) expect(InlineOneboxer.cache_lookup(url)).to be_blank result = InlineOneboxer.lookup(url) diff --git a/spec/models/post_analyzer_spec.rb b/spec/models/post_analyzer_spec.rb index d74b72a4e1c..70addd30cd2 100644 --- a/spec/models/post_analyzer_spec.rb +++ b/spec/models/post_analyzer_spec.rb @@ -31,6 +31,7 @@ describe PostAnalyzer do it 'invalidates the oneboxes for urls in the post' do Oneboxer.expects(:invalidate).with url + InlineOneboxer.expects(:invalidate).with url post_analyzer.cook(raw, options) end end