mirror of
https://github.com/discourse/discourse.git
synced 2025-05-24 03:36:18 +08:00
DEV: adds post_created_edited suport to chat (#28757)
This commit is contained in:
@ -138,12 +138,18 @@ en:
|
|||||||
label: Topic ID
|
label: Topic ID
|
||||||
post_created_edited:
|
post_created_edited:
|
||||||
fields:
|
fields:
|
||||||
|
original_post_only:
|
||||||
|
label: Original post only
|
||||||
|
description: Will trigger only if the post is the original post in the topic
|
||||||
action_type:
|
action_type:
|
||||||
label: Action type
|
label: Action type
|
||||||
description: "Optional, limit triggering to only created or edited events"
|
description: "Optional, limit triggering to only created or edited events"
|
||||||
valid_trust_levels:
|
valid_trust_levels:
|
||||||
label: Valid trust levels
|
label: Valid trust levels
|
||||||
description: Will trigger only if post is created by user in these trust levels, defaults to any trust level
|
description: Will trigger only if post is created by user in these trust levels, defaults to any trust level
|
||||||
|
restricted_tags:
|
||||||
|
label: Tags
|
||||||
|
description: Optional, will trigger only if the post has any of these tags
|
||||||
restricted_category:
|
restricted_category:
|
||||||
label: Category
|
label: Category
|
||||||
description: Optional, will trigger only if the post's topic is in this category
|
description: Optional, will trigger only if the post's topic is in this category
|
||||||
|
@ -12,6 +12,11 @@ module DiscourseAutomation
|
|||||||
DiscourseAutomation::Automation
|
DiscourseAutomation::Automation
|
||||||
.where(trigger: name, enabled: true)
|
.where(trigger: name, enabled: true)
|
||||||
.find_each do |automation|
|
.find_each do |automation|
|
||||||
|
original_post_only = automation.trigger_field("original_post_only")
|
||||||
|
if original_post_only["value"]
|
||||||
|
next if topic.posts_count > 1
|
||||||
|
end
|
||||||
|
|
||||||
first_post_only = automation.trigger_field("first_post_only")
|
first_post_only = automation.trigger_field("first_post_only")
|
||||||
if first_post_only["value"]
|
if first_post_only["value"]
|
||||||
next if post.user.user_stat.post_count != 1
|
next if post.user.user_stat.post_count != 1
|
||||||
@ -44,6 +49,11 @@ module DiscourseAutomation
|
|||||||
next if !category_ids.include?(restricted_category["value"])
|
next if !category_ids.include?(restricted_category["value"])
|
||||||
end
|
end
|
||||||
|
|
||||||
|
restricted_tags = automation.trigger_field("restricted_tags")
|
||||||
|
if restricted_tags["value"]
|
||||||
|
next if (restricted_tags["value"] & topic.tags.map(&:name)).empty?
|
||||||
|
end
|
||||||
|
|
||||||
restricted_group_id = automation.trigger_field("restricted_group")["value"]
|
restricted_group_id = automation.trigger_field("restricted_group")["value"]
|
||||||
if restricted_group_id.present?
|
if restricted_group_id.present?
|
||||||
next if !topic.private_message?
|
next if !topic.private_message?
|
||||||
@ -66,7 +76,15 @@ module DiscourseAutomation
|
|||||||
next if selected_action == :edited && action != :edit
|
next if selected_action == :edited && action != :edit
|
||||||
end
|
end
|
||||||
|
|
||||||
automation.trigger!("kind" => name, "action" => action, "post" => post)
|
automation.trigger!(
|
||||||
|
"kind" => name,
|
||||||
|
"action" => action,
|
||||||
|
"post" => post,
|
||||||
|
"placeholders" => {
|
||||||
|
"topic_url" => topic.relative_url,
|
||||||
|
"topic_title" => topic.title,
|
||||||
|
},
|
||||||
|
)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -13,11 +13,16 @@ DiscourseAutomation::Triggerable.add(DiscourseAutomation::Triggers::POST_CREATED
|
|||||||
],
|
],
|
||||||
}
|
}
|
||||||
field :restricted_category, component: :category
|
field :restricted_category, component: :category
|
||||||
|
field :restricted_tags, component: :tags
|
||||||
field :restricted_group, component: :group
|
field :restricted_group, component: :group
|
||||||
field :ignore_automated, component: :boolean
|
field :ignore_automated, component: :boolean
|
||||||
field :ignore_group_members, component: :boolean
|
field :ignore_group_members, component: :boolean
|
||||||
field :valid_trust_levels, component: :"trust-levels"
|
field :valid_trust_levels, component: :"trust-levels"
|
||||||
|
field :original_post_only, component: :boolean
|
||||||
field :first_post_only, component: :boolean
|
field :first_post_only, component: :boolean
|
||||||
field :first_topic_only, component: :boolean
|
field :first_topic_only, component: :boolean
|
||||||
field :skip_via_email, component: :boolean
|
field :skip_via_email, component: :boolean
|
||||||
|
|
||||||
|
placeholder :topic_url
|
||||||
|
placeholder :topic_title
|
||||||
end
|
end
|
||||||
|
@ -302,6 +302,66 @@ describe "PostCreatedEdited" do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context "with original_post_only" do
|
||||||
|
before do
|
||||||
|
automation.upsert_field!(
|
||||||
|
"original_post_only",
|
||||||
|
"boolean",
|
||||||
|
{ value: true },
|
||||||
|
target: "trigger",
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "fires the trigger only for OP" do
|
||||||
|
list = capture_contexts { PostCreator.create(user, basic_topic_params) }
|
||||||
|
|
||||||
|
expect(list.length).to eq(1)
|
||||||
|
|
||||||
|
list =
|
||||||
|
capture_contexts do
|
||||||
|
PostCreator.create(
|
||||||
|
user,
|
||||||
|
basic_topic_params.merge({ topic_id: list[0]["post"].topic_id }),
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
expect(list.length).to eq(0)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "when tags is restricted" do
|
||||||
|
fab!(:tag_1) { Fabricate(:tag) }
|
||||||
|
|
||||||
|
before do
|
||||||
|
automation.upsert_field!(
|
||||||
|
"restricted_tags",
|
||||||
|
"tags",
|
||||||
|
{ value: [tag_1.name] },
|
||||||
|
target: "trigger",
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
context "when tag is allowed" do
|
||||||
|
it "fires the trigger" do
|
||||||
|
list =
|
||||||
|
capture_contexts do
|
||||||
|
PostCreator.create(user, basic_topic_params.merge({ tags: [tag_1.name] }))
|
||||||
|
end
|
||||||
|
|
||||||
|
expect(list.length).to eq(1)
|
||||||
|
expect(list[0]["kind"]).to eq("post_created_edited")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "when tag is not allowed" do
|
||||||
|
it "fires the trigger" do
|
||||||
|
list = capture_contexts { PostCreator.create(user, basic_topic_params.merge()) }
|
||||||
|
|
||||||
|
expect(list.length).to eq(0)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context "when category is restricted" do
|
context "when category is restricted" do
|
||||||
before do
|
before do
|
||||||
automation.upsert_field!(
|
automation.upsert_field!(
|
||||||
|
@ -459,7 +459,7 @@ after_initialize do
|
|||||||
|
|
||||||
placeholder :channel_name
|
placeholder :channel_name
|
||||||
|
|
||||||
triggerables %i[recurring topic_tags_changed]
|
triggerables %i[recurring topic_tags_changed post_created_edited]
|
||||||
|
|
||||||
script do |context, fields, automation|
|
script do |context, fields, automation|
|
||||||
sender = User.find_by(username: fields.dig("sender", "value")) || Discourse.system_user
|
sender = User.find_by(username: fields.dig("sender", "value")) || Discourse.system_user
|
||||||
|
@ -387,4 +387,48 @@ describe Chat do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "when using post_edited_created trigger automation" do
|
||||||
|
describe "with the send message script" do
|
||||||
|
fab!(:automation_1) do
|
||||||
|
Fabricate(
|
||||||
|
:automation,
|
||||||
|
trigger: DiscourseAutomation::Triggers::POST_CREATED_EDITED,
|
||||||
|
script: :send_chat_message,
|
||||||
|
)
|
||||||
|
end
|
||||||
|
fab!(:user_1) { Fabricate(:admin) }
|
||||||
|
fab!(:channel_1) { Fabricate(:chat_channel) }
|
||||||
|
|
||||||
|
before do
|
||||||
|
SiteSetting.discourse_automation_enabled = true
|
||||||
|
|
||||||
|
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
|
||||||
|
PostCreator.create(
|
||||||
|
user_1,
|
||||||
|
{ title: "hello world topic", raw: "my name is fred", archetype: Archetype.default },
|
||||||
|
)
|
||||||
|
|
||||||
|
topic_1 = Topic.last
|
||||||
|
|
||||||
|
expect(channel_1.chat_messages.last.message).to eq(
|
||||||
|
"[#{topic_1.title}](#{topic_1.relative_url})",
|
||||||
|
)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
Reference in New Issue
Block a user