FEATURE: add `trigger_with_pms` option to automation

This commit is contained in:
Gabriel Grubba 2024-10-08 09:22:33 -03:00
parent e906290311
commit 74e5cc6695
No known key found for this signature in database
GPG Key ID: 5FE41764F62D556C
3 changed files with 24 additions and 9 deletions

View File

@ -290,3 +290,5 @@ en:
description: "You can find the channel name in the Chat Integration settings"
provider:
label: Provider
trigger_with_pms:
label: Trigger with PMs

View File

@ -97,13 +97,16 @@ after_initialize do
},
required: true
field :channel_name, component: :text, required: true
field :trigger_with_pms, component: :boolean
version 1
triggerables %i[topic_tags_changed]
script do |context, fields, automation|
next if context["topic"].private_message?
if context["topic"].private_message?
next unless fields.dig("trigger_with_pms", "value")
end
# DiscourseTagging.tag_topic_by_names runs on topic creation and on tags change
# we only want to send a message when tags change

View File

@ -87,7 +87,7 @@ RSpec.describe "Triggering notifications" do
expect(validated_provider.sent_messages.length).to eq(0)
end
it "should not trigger notifications for PMs" do
it "should not trigger notifications for PMs by default" do
automation.upsert_field!(
"watching_categories",
"categories",
@ -95,15 +95,25 @@ RSpec.describe "Triggering notifications" do
target: "trigger",
)
topic =
TopicCreator.create(
admin,
Guardian.new(admin),
valid_attrs.merge(archetype: Archetype.private_message, target_usernames: admin.username),
)
pm = Fabricate(:private_message_topic)
DiscourseTagging.tag_topic_by_names(pm, Guardian.new(admin), [tag.name])
DiscourseTagging.tag_topic_by_names(topic, Guardian.new(admin), [tag.name])
expect(validated_provider.sent_messages.length).to eq(0)
end
it "should trigger notifications for PMs when trigger_with_pms is set" do
automation.upsert_field!(
"watching_categories",
"categories",
{ "value" => [nil] },
target: "trigger",
)
automation.upsert_field!("trigger_with_pms", "boolean", { "value" => true }, target: "script")
pm = Fabricate(:private_message_topic)
DiscourseTagging.tag_topic_by_names(pm, Guardian.new(admin), [tag.name])
expect(validated_provider.sent_messages.length).to eq(1)
end
end
end