FIX: relax automation restrictions (#32238)

Relaxed restrictions:
- Automation posts are not validated for similarity. This was causing
error when PMs were created by regular user with same content and sent
to different users.
- Don't create warning logs when PM target does not exist anymore. When
for example spammer was deleted, delayed PM is not sent, but it is
correct behaviour;
- Allow tags to be applied even if an automation user is not allowed to
tag;
- Restricted category tags should be visible in configuration UI. Still,
they will be applied only when specific topic belongs to correct
category.
This commit is contained in:
Krzysztof Kotlarek
2025-04-11 07:27:43 +08:00
committed by GitHub
parent 6fc5ce9688
commit 6e654bc596
5 changed files with 82 additions and 11 deletions

View File

@ -13,6 +13,7 @@ export default class TagsField extends BaseField {
<div class="controls">
<TagChooser
@tags={{@field.metadata.value}}
@everyTag={{true}}
@options={{hash allowAny=false disabled=@field.isDisabled}}
/>

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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