From 268213a93c1fac0314cad57caf24790572109ea6 Mon Sep 17 00:00:00 2001 From: Joffrey JAFFEUX Date: Tue, 8 Oct 2024 21:55:11 +0900 Subject: [PATCH] FIX: adds post_quote as placeholder (#29083) The script `send_chat_message` when used with the `post_created_edited` trigger now accepts `{{post_quote}}` as placeholder for the value of `message`. This is made possible by a new method in `utils`. Usage: ```ruby placeholders["foo"] = utils.build_quote(post) ``` --- .../lib/discourse_automation/scriptable.rb | 20 +++++ .../automation/spec/lib/scriptable_spec.rb | 76 +++++++++++++++++++ plugins/chat/plugin.rb | 5 ++ plugins/chat/spec/plugin_spec.rb | 11 +-- 4 files changed, 104 insertions(+), 8 deletions(-) diff --git a/plugins/automation/lib/discourse_automation/scriptable.rb b/plugins/automation/lib/discourse_automation/scriptable.rb index 617b53b02ab..9aaf4cf2701 100644 --- a/plugins/automation/lib/discourse_automation/scriptable.rb +++ b/plugins/automation/lib/discourse_automation/scriptable.rb @@ -205,6 +205,26 @@ module DiscourseAutomation end end + def self.build_quote(post) + return "" if post.nil? || post.raw.nil? + + full_name = post.user.name + name = + if SiteSetting.display_name_on_posts && !SiteSetting.prioritize_username_in_ux + full_name || post.username + else + post.username + end + + params = [name, "post:#{post.post_number}", "topic:#{post.topic_id}"] + + if SiteSetting.display_name_on_posts && !SiteSetting.prioritize_username_in_ux && full_name + params.push("username:#{post.username}") + end + + "[quote=#{params.join(", ")}]\n#{post.raw.strip}\n[/quote]\n\n" + end + def self.send_pm( pm, sender: Discourse.system_user.username, diff --git a/plugins/automation/spec/lib/scriptable_spec.rb b/plugins/automation/spec/lib/scriptable_spec.rb index acde8d35780..5f68b9ca1dc 100644 --- a/plugins/automation/spec/lib/scriptable_spec.rb +++ b/plugins/automation/spec/lib/scriptable_spec.rb @@ -213,6 +213,82 @@ describe DiscourseAutomation::Scriptable do end end + describe ".build_quote" do + subject(:quote) { DiscourseAutomation::Scriptable::Utils.build_quote(post) } + + fab!(:user) { Fabricate(:user, name: "John Doe", username: "johndoe") } + fab!(:post) { Fabricate(:post, user: user, raw: "This is a post content", post_number: 1) } + + before do + SiteSetting.display_name_on_posts = false + SiteSetting.prioritize_username_in_ux = false + end + + context "when post is nil" do + let(:post) { nil } # Define post as nil in this context + + it "returns an empty string" do + expect(quote).to eq("") + end + end + + context "when post.raw is nil" do + before { post.raw = nil } + + it "returns an empty string" do + expect(quote).to eq("") + end + end + + context "when display_name_on_posts is true and prioritize_username_in_ux is false" do + before do + SiteSetting.display_name_on_posts = true + SiteSetting.prioritize_username_in_ux = false + end + + it "returns a quote with display name" do + expect(quote).to eq( + "[quote=John Doe, post:#{post.post_number}, topic:#{post.topic_id}, username:johndoe]\nThis is a post content\n[/quote]\n\n", + ) + end + end + + context "when display_name_on_posts is false or prioritize_username_in_ux is true" do + it "returns a quote with username" do + expect(quote).to eq( + "[quote=johndoe, post:#{post.post_number}, topic:#{post.topic_id}]\nThis is a post content\n[/quote]\n\n", + ) + end + end + + context "when full_name is nil and display_name_on_posts is true" do + before do + user.update(name: nil) + SiteSetting.display_name_on_posts = true + SiteSetting.prioritize_username_in_ux = false + end + + it "returns a quote with username" do + expect(quote).to eq( + "[quote=johndoe, post:#{post.post_number}, topic:#{post.topic_id}]\nThis is a post content\n[/quote]\n\n", + ) + end + end + + context "when display_name_on_posts is true and prioritize_username_in_ux is true" do + before do + SiteSetting.display_name_on_posts = true + SiteSetting.prioritize_username_in_ux = true + end + + it "returns a quote with username prioritized" do + expect(quote).to eq( + "[quote=johndoe, post:#{post.post_number}, topic:#{post.topic_id}]\nThis is a post content\n[/quote]\n\n", + ) + end + end + end + describe ".send_pm" do let(:user) { Fabricate(:user) } diff --git a/plugins/chat/plugin.rb b/plugins/chat/plugin.rb index f662e3011d2..827fe68c63d 100644 --- a/plugins/chat/plugin.rb +++ b/plugins/chat/plugin.rb @@ -463,6 +463,7 @@ after_initialize do field :sender, component: :user placeholder :channel_name + placeholder :post_quote, triggerable: :post_created_edited triggerables %i[recurring topic_tags_changed post_created_edited] @@ -471,6 +472,10 @@ after_initialize do channel = Chat::Channel.find_by(id: fields.dig("chat_channel_id", "value")) placeholders = { channel_name: channel.title(sender) }.merge(context["placeholders"] || {}) + if context["kind"] == "post_created_edited" + placeholders[:post_quote] = utils.build_quote(context["post"]) + end + creator = ::Chat::CreateMessage.call( chat_channel_id: channel.id, diff --git a/plugins/chat/spec/plugin_spec.rb b/plugins/chat/spec/plugin_spec.rb index 0e3842f796d..361b09f60f1 100644 --- a/plugins/chat/spec/plugin_spec.rb +++ b/plugins/chat/spec/plugin_spec.rb @@ -412,21 +412,16 @@ describe Chat do automation_1.upsert_field!( "message", "message", - { value: "[{{topic_title}}]({{topic_url}})" }, + { value: "[{{topic_title}}]({{topic_url}})\n{{post_quote}}" }, target: "script", ) end it "sends the message" do - PostCreator.create( - user_1, - { title: "hello world topic", raw: "my name is fred", archetype: Archetype.default }, - ) - - topic_1 = Topic.last + post = PostCreator.create(user_1, { title: "hello world topic", raw: "my name is fred" }) expect(channel_1.chat_messages.last.message).to eq( - "[#{topic_1.title}](#{topic_1.relative_url})", + "[#{post.topic.title}](#{post.topic.relative_url})\n[quote=#{post.username}, post:#{post.post_number}, topic:#{post.topic_id}]\nmy name is fred\n[/quote]", ) end end