mirror of
https://github.com/discourse/discourse.git
synced 2025-06-24 00:11:32 +08:00
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:

committed by
GitHub

parent
6fc5ce9688
commit
6e654bc596
@ -13,6 +13,7 @@ export default class TagsField extends BaseField {
|
|||||||
<div class="controls">
|
<div class="controls">
|
||||||
<TagChooser
|
<TagChooser
|
||||||
@tags={{@field.metadata.value}}
|
@tags={{@field.metadata.value}}
|
||||||
|
@everyTag={{true}}
|
||||||
@options={{hash allowAny=false disabled=@field.isDisabled}}
|
@options={{hash allowAny=false disabled=@field.isDisabled}}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
|
@ -254,7 +254,6 @@ module DiscourseAutomation
|
|||||||
|
|
||||||
if pm[:target_usernames].empty? && pm[:target_group_names].empty? &&
|
if pm[:target_usernames].empty? && pm[:target_group_names].empty? &&
|
||||||
pm[:target_emails].empty?
|
pm[:target_emails].empty?
|
||||||
Rails.logger.warn "[discourse-automation] Did not send PM - no target usernames, groups or emails"
|
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -300,6 +299,7 @@ module DiscourseAutomation
|
|||||||
|
|
||||||
post_created = EncryptedPostCreator.new(sender, pm).create if prefers_encrypt
|
post_created = EncryptedPostCreator.new(sender, pm).create if prefers_encrypt
|
||||||
|
|
||||||
|
pm[:acting_user] = Discourse.system_user
|
||||||
PostCreator.new(sender, pm).create! if !post_created
|
PostCreator.new(sender, pm).create! if !post_created
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -16,6 +16,11 @@ DiscourseAutomation::Scriptable.add(DiscourseAutomation::Scripts::AUTO_TAG_TOPIC
|
|||||||
|
|
||||||
tags = fields.dig("tags", "value")
|
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
|
||||||
end
|
end
|
||||||
|
@ -36,4 +36,43 @@ describe "AutoTagTopic" do
|
|||||||
expect(topic.reload.tags.pluck(:name).sort).to match_array(%w[tag1 tag2 tag3])
|
expect(topic.reload.tags.pluck(:name).sort).to match_array(%w[tag1 tag2 tag3])
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
@ -4,6 +4,7 @@ describe "SendPms" do
|
|||||||
fab!(:automation) do
|
fab!(:automation) do
|
||||||
Fabricate(:automation, script: DiscourseAutomation::Scripts::SEND_PMS, trigger: "stalled_wiki")
|
Fabricate(:automation, script: DiscourseAutomation::Scripts::SEND_PMS, trigger: "stalled_wiki")
|
||||||
end
|
end
|
||||||
|
let(:raw) { "This is a message sent to @{{receiver_username}}" }
|
||||||
|
|
||||||
before do
|
before do
|
||||||
SiteSetting.discourse_automation_enabled = true
|
SiteSetting.discourse_automation_enabled = true
|
||||||
@ -12,14 +13,7 @@ describe "SendPms" do
|
|||||||
automation.upsert_field!(
|
automation.upsert_field!(
|
||||||
"sendable_pms",
|
"sendable_pms",
|
||||||
"pms",
|
"pms",
|
||||||
{
|
{ value: [{ title: "A message from {{sender_username}}", raw: raw }] },
|
||||||
value: [
|
|
||||||
{
|
|
||||||
title: "A message from {{sender_username}}",
|
|
||||||
raw: "This is a message sent to @{{receiver_username}}",
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -54,7 +48,7 @@ describe "SendPms" do
|
|||||||
end
|
end
|
||||||
|
|
||||||
context "when run from user_added_to_group trigger" do
|
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) }
|
fab!(:tracked_group_1) { Fabricate(:group) }
|
||||||
|
|
||||||
before do
|
before do
|
||||||
@ -80,6 +74,38 @@ describe "SendPms" do
|
|||||||
)
|
)
|
||||||
}.to change { Post.count }.by(1)
|
}.to change { Post.count }.by(1)
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
context "when delayed" do
|
context "when delayed" do
|
||||||
|
Reference in New Issue
Block a user