Skip to content

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

FIX: Don't apply `max_oneboxes_per_post` to chat cooks

Régis Hanol1b7fe0235a73
diff --git a/plugins/chat/app/models/chat/message.rb b/plugins/chat/app/models/chat/message.rb
index 4154a277f41..0e321c5d7c8 100644
--- a/plugins/chat/app/models/chat/message.rb
+++ b/plugins/chat/app/models/chat/message.rb
@@ -302,6 +302,7 @@ module Chat
features_override: features + DiscoursePluginRegistry.chat_markdown_features.to_a,
markdown_it_rules: rules,
force_quote_link: true,
+ onebox_limit: PrettyText::NO_ONEBOX_LIMIT,
user_id: opts[:user_id],
hashtag_context: "chat-composer",
}
diff --git a/plugins/chat/assets/javascripts/discourse/models/chat-message.js b/plugins/chat/assets/javascripts/discourse/models/chat-message.js
index 1b2b6d47a89..e9098bce427 100644
--- a/plugins/chat/assets/javascripts/discourse/models/chat-message.js
+++ b/plugins/chat/assets/javascripts/discourse/models/chat-message.js
@@ -216,6 +216,7 @@ export default class ChatMessage {
hashtagTypesInPriorityOrder:
site.hashtag_configurations?.["chat-composer"],
hashtagIcons: site.hashtag_icons,
+ oneboxLimit: site.markdown_additional_options?.chat?.onebox_limit,
};
}
diff --git a/plugins/chat/plugin.rb b/plugins/chat/plugin.rb
index b3e7e49f393..cf58b7c1ea3 100644
--- a/plugins/chat/plugin.rb
+++ b/plugins/chat/plugin.rb
@@ -679,6 +679,7 @@ after_initialize do
Site.markdown_additional_options["chat"] = {
limited_pretty_text_features: Chat::Message::MARKDOWN_FEATURES,
limited_pretty_text_markdown_rules: Chat::Message::MARKDOWN_IT_RULES,
+ onebox_limit: PrettyText::NO_ONEBOX_LIMIT,
hashtag_configurations: HashtagAutocompleteService.contexts_with_ordered_types,
}
diff --git a/plugins/chat/spec/lib/chat/message_processor_spec.rb b/plugins/chat/spec/lib/chat/message_processor_spec.rb
index 2ddbaf2148a..e808871ce69 100644
--- a/plugins/chat/spec/lib/chat/message_processor_spec.rb
+++ b/plugins/chat/spec/lib/chat/message_processor_spec.rb
@@ -37,6 +37,21 @@ RSpec.describe Chat::MessageProcessor do
# This should not raise an error even though @post is nil
expect { processor.run! }.not_to raise_error
end
+
+ it "honors max_oneboxes_per_post" do
+ SiteSetting.max_oneboxes_per_post = 1
+ urls = %w[https://example.com/1 https://example.com/2 https://example.com/3]
+ message.update!(message: urls.join("\n\n"))
+ Oneboxer.stubs(:cached_onebox).returns(nil)
+ urls.each do |url|
+ Oneboxer.stubs(:onebox).with(url, anything).returns("<aside class='onebox'>#{url}</aside>")
+ end
+
+ processor = described_class.new(message)
+ processor.run!
+
+ expect(processor.html).to have_tag("aside.onebox", count: 1)
+ end
end
describe "#process_hotlinked_image" do
diff --git a/plugins/chat/spec/models/chat/message_spec.rb b/plugins/chat/spec/models/chat/message_spec.rb
index 52743af8e07..1cda20dae2f 100644
--- a/plugins/chat/spec/models/chat/message_spec.rb
+++ b/plugins/chat/spec/models/chat/message_spec.rb
@@ -613,6 +613,16 @@ describe Chat::Message do
)
end
+ it "ignores max_oneboxes_per_post" do
+ SiteSetting.max_oneboxes_per_post = 0
+
+ cooked = described_class.cook("https://www.example.com")
+
+ expect(cooked).to eq(
+ "<p><a href=\"https://www.example.com\" class=\"onebox\" target=\"_blank\" rel=\"noopener nofollow ugc\">https://www.example.com</a></p>",
+ )
+ end
+
it "supports emoji plugin" do
cooked = described_class.cook(":grin:")
diff --git a/plugins/chat/test/javascripts/unit/models/chat-message-test.js b/plugins/chat/test/javascripts/unit/models/chat-message-test.js
index ebd5e3aa28e..bfd12ca30c1 100644
--- a/plugins/chat/test/javascripts/unit/models/chat-message-test.js
+++ b/plugins/chat/test/javascripts/unit/models/chat-message-test.js
@@ -17,6 +17,28 @@ module("Unit | Models | chat-message", function (hooks) {
assert.strictEqual(message.aiLlmName, "Model 猫");
});
+ test(".cook ignores max_oneboxes_per_post", async function (assert) {
+ this.owner.lookup("service:site-settings").max_oneboxes_per_post = 0;
+ this.owner.lookup("service:site").markdown_additional_options = {
+ chat: {
+ limited_pretty_text_features: ["onebox"],
+ limited_pretty_text_markdown_rules: ["linkify"],
+ hashtag_configurations: { "chat-composer": [] },
+ onebox_limit: "none",
+ },
+ };
+ ChatMessage.cookFunction = null;
+ const channel = new ChatFabricators(getOwner(this)).channel();
+ const message = ChatMessage.create(channel, {
+ message: "https://www.example.com",
+ });
+
+ await message.cook();
+ ChatMessage.cookFunction = null;
+
+ assert.true(message.cooked.includes('class="onebox"'));
+ });
+
test(".persisted", function (assert) {
const channel = new ChatFabricators(getOwner(this)).channel();
let message = ChatMessage.create(channel, { id: null });