Skip to content

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

DEV: send_chat_message supports topic_tags_changed (#28755)

Joffrey JAFFEUX81c5f1d75f42
diff --git a/plugins/chat/plugin.rb b/plugins/chat/plugin.rb
index 6ec051470ee..19efdd6bf3a 100644
--- a/plugins/chat/plugin.rb
+++ b/plugins/chat/plugin.rb
@@ -452,32 +452,29 @@ after_initialize do
}
end
- if defined?(DiscourseAutomation)
- add_automation_scriptable("send_chat_message") do
- field :chat_channel_id, component: :text, required: true
- field :message, component: :message, required: true, accepts_placeholders: true
- field :sender, component: :user
+ add_automation_scriptable("send_chat_message") do
+ field :chat_channel_id, component: :text, required: true
+ field :message, component: :message, required: true, accepts_placeholders: true
+ field :sender, component: :user
- placeholder :channel_name
+ placeholder :channel_name
- triggerables [:recurring]
+ triggerables %i[recurring topic_tags_changed]
- script do |context, fields, automation|
- sender = User.find_by(username: fields.dig("sender", "value")) || Discourse.system_user
- channel = Chat::Channel.find_by(id: fields.dig("chat_channel_id", "value"))
+ script do |context, fields, automation|
+ sender = User.find_by(username: fields.dig("sender", "value")) || Discourse.system_user
+ channel = Chat::Channel.find_by(id: fields.dig("chat_channel_id", "value"))
+ placeholders = { channel_name: channel.title(sender) }.merge(context["placeholders"] || {})
- placeholders = { channel_name: channel.title(sender) }.merge(context["placeholders"] || {})
-
- creator =
- ::Chat::CreateMessage.call(
- chat_channel_id: channel.id,
- guardian: sender.guardian,
- message: utils.apply_placeholders(fields.dig("message", "value"), placeholders),
- )
+ creator =
+ ::Chat::CreateMessage.call(
+ chat_channel_id: channel.id,
+ guardian: sender.guardian,
+ message: utils.apply_placeholders(fields.dig("message", "value"), placeholders),
+ )
- if creator.failure?
- Rails.logger.warn "[discourse-automation] Chat message failed to send:\n#{creator.inspect_steps.inspect}\n#{creator.inspect_steps.error}"
- end
+ if creator.failure?
+ Rails.logger.warn "[discourse-automation] Chat message failed to send:\n#{creator.inspect_steps.inspect}\n#{creator.inspect_steps.error}"
end
end
end
diff --git a/plugins/chat/spec/plugin_spec.rb b/plugins/chat/spec/plugin_spec.rb
index 51a84b2a4d1..5a50187c4a6 100644
--- a/plugins/chat/spec/plugin_spec.rb
+++ b/plugins/chat/spec/plugin_spec.rb
@@ -339,4 +339,52 @@ describe Chat do
).by(1)
end
end
+
+ describe "when using topic tags changed trigger automation" do
+ describe "with the send message script" do
+ fab!(:automation_1) do
+ Fabricate(
+ :automation,
+ trigger: DiscourseAutomation::Triggers::TOPIC_TAGS_CHANGED,
+ script: :send_chat_message,
+ )
+ end
+ fab!(:tag_1) { Fabricate(:tag) }
+ fab!(:user_1) { Fabricate(:admin) }
+ fab!(:topic_1) { Fabricate(:topic) }
+ fab!(:channel_1) { Fabricate(:chat_channel) }
+
+ before do
+ SiteSetting.discourse_automation_enabled = true
+ SiteSetting.tagging_enabled = true
+
+ automation_1.upsert_field!(
+ "watching_tags",
+ "tags",
+ { value: [tag_1.name] },
+ target: "trigger",
+ )
+ automation_1.upsert_field!(
+ "chat_channel_id",
+ "text",
+ { value: channel_1.id },
+ target: "script",
+ )
+ automation_1.upsert_field!(
+ "message",
+ "message",
+ { value: "[{{topic_title}}]({{topic_url}})" },
+ target: "script",
+ )
+ end
+
+ it "sends the message" do
+ DiscourseTagging.tag_topic_by_names(topic_1, Guardian.new(user_1), [tag_1.name])
+
+ expect(channel_1.chat_messages.last.message).to eq(
+ "[#{topic_1.title}](#{topic_1.relative_url})",
+ )
+ end
+ end
+ end
end