FIX: properly invalidate inline oneboxes when rebaking

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’

diff --git a/app/models/post.rb b/app/models/post.rb
index fefb43d..a9880a8 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 bae36c3..c06d9d5 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 edfd746..26f3ecc 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 aa63a7e..fef1e06 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 d99bab6..47c5abd 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: "<html><head><title>a blog</title></head></html>")
 
-      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 d74b72a..70addd3 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

GitHub sha: 7109d94e

1 Like