Skip to content

A platform for community discussion. Free, open, simple.

FIX: Support onebox cache invalidation in chat messages (#36529)

Régis Hanol539087671e7a
diff --git a/plugins/chat/lib/chat/message_processor.rb b/plugins/chat/lib/chat/message_processor.rb
index 077c7e14cb6..2b52849c755 100644
--- a/plugins/chat/lib/chat/message_processor.rb
+++ b/plugins/chat/lib/chat/message_processor.rb
@@ -12,7 +12,9 @@ module Chat
@size_cache = {}
@opts = opts
- cooked = Chat::Message.cook(chat_message.message, user_id: chat_message.last_editor_id)
+ cook_opts = { user_id: chat_message.last_editor_id }
+ cook_opts[:invalidate_oneboxes] = true if opts[:invalidate_oneboxes] == true
+ cooked = Chat::Message.cook(chat_message.message, **cook_opts)
@doc = Loofah.html5_fragment(cooked)
end
diff --git a/plugins/chat/spec/jobs/regular/chat/process_message_spec.rb b/plugins/chat/spec/jobs/regular/chat/process_message_spec.rb
index 392d2fd9a5f..af34db939b9 100644
--- a/plugins/chat/spec/jobs/regular/chat/process_message_spec.rb
+++ b/plugins/chat/spec/jobs/regular/chat/process_message_spec.rb
@@ -39,4 +39,24 @@ describe Jobs::Chat::ProcessMessage do
expect(link).to be_present
expect(link.url).to eq("https://discourse.org/team")
end
+
+ describe "invalidate_oneboxes" do
+ it "invalidates cached oneboxes and fetches fresh content" do
+ # Process the message once to populate the onebox cache
+ described_class.new.execute(chat_message_id: chat_message.id)
+ original_cooked = chat_message.reload.cooked
+ expect(original_cooked).to include("discourse.org")
+
+ # Update the stub to return different content
+ stub_request(:get, "https://discourse.org/team").to_return(
+ status: 200,
+ body: "<html><head><title>Updated Title</title></head></html>",
+ )
+
+ # Rebake with invalidate_oneboxes: true - this should fetch fresh content
+ described_class.new.execute(chat_message_id: chat_message.id, invalidate_oneboxes: true)
+ new_cooked = chat_message.reload.cooked
+ expect(new_cooked).to include("Updated Title")
+ end
+ end
end
diff --git a/plugins/chat/spec/lib/chat/message_processor_spec.rb b/plugins/chat/spec/lib/chat/message_processor_spec.rb
index 85c11972802..6e5cc80dd0a 100644
--- a/plugins/chat/spec/lib/chat/message_processor_spec.rb
+++ b/plugins/chat/spec/lib/chat/message_processor_spec.rb
@@ -8,6 +8,15 @@ RSpec.describe Chat::MessageProcessor do
described_class.new(message)
end
+ it "passes invalidate_oneboxes option to cook" do
+ Chat::Message.expects(:cook).with(
+ message.message,
+ user_id: message.last_editor_id,
+ invalidate_oneboxes: true,
+ )
+ described_class.new(message, invalidate_oneboxes: true)
+ end
+
describe "#run!" do
it "processes messages with hotlinked images in oneboxes without errors" do
# Create a message with an image in a onebox (common when posting URLs with images)