Skip to content

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

FIX: relax automation restrictions (#32238)

Krzysztof Kotlarek6e654bc59672
diff --git a/plugins/automation/admin/assets/javascripts/admin/components/fields/da-tags-field.gjs b/plugins/automation/admin/assets/javascripts/admin/components/fields/da-tags-field.gjs
index 55e149156b8..3ffe7bc5ee6 100644
--- a/plugins/automation/admin/assets/javascripts/admin/components/fields/da-tags-field.gjs
+++ b/plugins/automation/admin/assets/javascripts/admin/components/fields/da-tags-field.gjs
@@ -13,6 +13,7 @@ export default class TagsField extends BaseField {
<div class="controls">
<TagChooser
@tags={{@field.metadata.value}}
+ @everyTag={{true}}
@options={{hash allowAny=false [email protected]}}
/>
diff --git a/plugins/automation/lib/discourse_automation/scriptable.rb b/plugins/automation/lib/discourse_automation/scriptable.rb
index cf34202f312..e25b5cc2301 100644
--- a/plugins/automation/lib/discourse_automation/scriptable.rb
+++ b/plugins/automation/lib/discourse_automation/scriptable.rb
@@ -254,7 +254,6 @@ module DiscourseAutomation
if pm[:target_usernames].empty? && pm[:target_group_names].empty? &&
pm[:target_emails].empty?
- Rails.logger.warn "[discourse-automation] Did not send PM - no target usernames, groups or emails"
return
end
@@ -300,6 +299,7 @@ module DiscourseAutomation
post_created = EncryptedPostCreator.new(sender, pm).create if prefers_encrypt
+ pm[:acting_user] = Discourse.system_user
PostCreator.new(sender, pm).create! if !post_created
end
end
diff --git a/plugins/automation/lib/discourse_automation/scripts/auto_tag_topic.rb b/plugins/automation/lib/discourse_automation/scripts/auto_tag_topic.rb
index d216033b455..65a63f75905 100644
--- a/plugins/automation/lib/discourse_automation/scripts/auto_tag_topic.rb
+++ b/plugins/automation/lib/discourse_automation/scripts/auto_tag_topic.rb
@@ -16,6 +16,11 @@ DiscourseAutomation::Scriptable.add(DiscourseAutomation::Scripts::AUTO_TAG_TOPIC
tags = fields.dig("tags", "value")
- DiscourseTagging.tag_topic_by_names(topic, Guardian.new(post.user), tags, append: true)
+ DiscourseTagging.tag_topic_by_names(
+ topic,
+ Guardian.new(Discourse.system_user),
+ tags,
+ append: true,
+ )
end
end
diff --git a/plugins/automation/spec/scripts/auto_tag_topic_spec.rb b/plugins/automation/spec/scripts/auto_tag_topic_spec.rb
index b342e3f4528..10086aa8ff5 100644
--- a/plugins/automation/spec/scripts/auto_tag_topic_spec.rb
+++ b/plugins/automation/spec/scripts/auto_tag_topic_spec.rb
@@ -36,4 +36,43 @@ describe "AutoTagTopic" do
expect(topic.reload.tags.pluck(:name).sort).to match_array(%w[tag1 tag2 tag3])
end
end
+
+ context "with restricted tags" do
+ fab!(:restricted_tag) { Fabricate(:tag, name: "restricted") }
+ before { automation.upsert_field!("tags", "tags", { value: ["restricted"] }) }
+
+ context "when group restricted tags" do
+ fab!(:tag_group) do
+ Fabricate(:tag_group, permissions: { "staff" => 1 }, tag_names: ["restricted"])
+ end
+
+ it "works" do
+ post = create_post(topic: topic)
+ automation.trigger!("post" => post)
+ expect(topic.reload.tags.pluck(:name).sort).to match_array(["restricted"])
+ end
+ end
+
+ context "when category restricted tags" do
+ fab!(:category)
+ fab!(:restricted_category) { Fabricate(:category) }
+ fab!(:category_tag) do
+ CategoryTag.create!(category: restricted_category, tag: restricted_tag)
+ end
+
+ it "works" do
+ topic.update!(category: restricted_category)
+ post = create_post(topic: topic)
+ automation.trigger!("post" => post)
+ expect(topic.reload.tags.pluck(:name).sort).to match_array(["restricted"])
+ end
+
+ it "does not work when incorrect category" do
+ topic.update!(category: category)
+ post = create_post(topic: topic)
+ automation.trigger!("post" => post)
+ expect(topic.reload.tags.pluck(:name).sort).to match_array([])
+ end
+ end
+ end
end
diff --git a/plugins/automation/spec/scripts/send_pms_spec.rb b/plugins/automation/spec/scripts/send_pms_spec.rb
index a1628bee358..df5fc89f3e8 100644
--- a/plugins/automation/spec/scripts/send_pms_spec.rb
+++ b/plugins/automation/spec/scripts/send_pms_spec.rb
@@ -4,6 +4,7 @@ describe "SendPms" do
fab!(:automation) do
Fabricate(:automation, script: DiscourseAutomation::Scripts::SEND_PMS, trigger: "stalled_wiki")
end
+ let(:raw) { "This is a message sent to @{{receiver_username}}" }
before do
SiteSetting.discourse_automation_enabled = true
@@ -12,14 +13,7 @@ describe "SendPms" do
automation.upsert_field!(
"sendable_pms",
"pms",
- {
- value: [
- {
- title: "A message from {{sender_username}}",
- raw: "This is a message sent to @{{receiver_username}}",
- },
- ],
- },
+ { value: [{ title: "A message from {{sender_username}}", raw: raw }] },
)
end
@@ -54,7 +48,7 @@ describe "SendPms" do
end
context "when run from user_added_to_group trigger" do
- fab!(:user_1) { Fabricate(:user) }
+ fab!(:user_1) { Fabricate(:user, refresh_auto_groups: true) }
fab!(:tracked_group_1) { Fabricate(:group) }
before do
@@ -80,6 +74,38 @@ describe "SendPms" do
)
}.to change { Post.count }.by(1)
end
+
+ context "when sent from regular user" do
+ fab!(:user_2) { Fabricate(:user) }
+ fab!(:user_3) { Fabricate(:user) }
+ let(:raw) { "This is a general message sent" }
+
+ before do
+ SiteSetting.unique_posts_mins = 1
+ automation.upsert_field!("sender", "user", { value: user_1.username })
+ end
+
+ it "creates expected PMs without validating similarity" do
+ expect {
+ tracked_group_1.add(user_2)
+
+ post = Post.last
+ expect(post.topic.title).to eq("A message from #{user_1.username}")
+ expect(post.raw).to eq("This is a general message sent")
+ expect(post.topic.topic_allowed_users.exists?(user_id: user_1.id)).to eq(true)
+ expect(post.topic.topic_allowed_users.exists?(user_id: user_2.id)).to eq(true)
+ }.to change { Post.count }.by(1)
+ expect {
+ tracked_group_1.add(user_3)
+
+ post = Post.last
+ expect(post.topic.title).to eq("A message from #{user_1.username}")
+ expect(post.raw).to eq("This is a general message sent")
+ expect(post.topic.topic_allowed_users.exists?(user_id: user_1.id)).to eq(true)
+ expect(post.topic.topic_allowed_users.exists?(user_id: user_3.id)).to eq(true)
+ }.to change { Post.count }.by(1)
+ end
+ end
end
context "when delayed" do