mirror of
https://github.com/discourse/discourse.git
synced 2025-05-22 22:43:33 +08:00
FIX: adds post_quote as placeholder (#29083)
The script `send_chat_message` when used with the `post_created_edited` trigger now accepts `{{post_quote}}` as placeholder for the value of `message`. This is made possible by a new method in `utils`. Usage: ```ruby placeholders["foo"] = utils.build_quote(post) ```
This commit is contained in:
@ -205,6 +205,26 @@ module DiscourseAutomation
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.build_quote(post)
|
||||||
|
return "" if post.nil? || post.raw.nil?
|
||||||
|
|
||||||
|
full_name = post.user.name
|
||||||
|
name =
|
||||||
|
if SiteSetting.display_name_on_posts && !SiteSetting.prioritize_username_in_ux
|
||||||
|
full_name || post.username
|
||||||
|
else
|
||||||
|
post.username
|
||||||
|
end
|
||||||
|
|
||||||
|
params = [name, "post:#{post.post_number}", "topic:#{post.topic_id}"]
|
||||||
|
|
||||||
|
if SiteSetting.display_name_on_posts && !SiteSetting.prioritize_username_in_ux && full_name
|
||||||
|
params.push("username:#{post.username}")
|
||||||
|
end
|
||||||
|
|
||||||
|
"[quote=#{params.join(", ")}]\n#{post.raw.strip}\n[/quote]\n\n"
|
||||||
|
end
|
||||||
|
|
||||||
def self.send_pm(
|
def self.send_pm(
|
||||||
pm,
|
pm,
|
||||||
sender: Discourse.system_user.username,
|
sender: Discourse.system_user.username,
|
||||||
|
@ -213,6 +213,82 @@ describe DiscourseAutomation::Scriptable do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe ".build_quote" do
|
||||||
|
subject(:quote) { DiscourseAutomation::Scriptable::Utils.build_quote(post) }
|
||||||
|
|
||||||
|
fab!(:user) { Fabricate(:user, name: "John Doe", username: "johndoe") }
|
||||||
|
fab!(:post) { Fabricate(:post, user: user, raw: "This is a post content", post_number: 1) }
|
||||||
|
|
||||||
|
before do
|
||||||
|
SiteSetting.display_name_on_posts = false
|
||||||
|
SiteSetting.prioritize_username_in_ux = false
|
||||||
|
end
|
||||||
|
|
||||||
|
context "when post is nil" do
|
||||||
|
let(:post) { nil } # Define post as nil in this context
|
||||||
|
|
||||||
|
it "returns an empty string" do
|
||||||
|
expect(quote).to eq("")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "when post.raw is nil" do
|
||||||
|
before { post.raw = nil }
|
||||||
|
|
||||||
|
it "returns an empty string" do
|
||||||
|
expect(quote).to eq("")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "when display_name_on_posts is true and prioritize_username_in_ux is false" do
|
||||||
|
before do
|
||||||
|
SiteSetting.display_name_on_posts = true
|
||||||
|
SiteSetting.prioritize_username_in_ux = false
|
||||||
|
end
|
||||||
|
|
||||||
|
it "returns a quote with display name" do
|
||||||
|
expect(quote).to eq(
|
||||||
|
"[quote=John Doe, post:#{post.post_number}, topic:#{post.topic_id}, username:johndoe]\nThis is a post content\n[/quote]\n\n",
|
||||||
|
)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "when display_name_on_posts is false or prioritize_username_in_ux is true" do
|
||||||
|
it "returns a quote with username" do
|
||||||
|
expect(quote).to eq(
|
||||||
|
"[quote=johndoe, post:#{post.post_number}, topic:#{post.topic_id}]\nThis is a post content\n[/quote]\n\n",
|
||||||
|
)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "when full_name is nil and display_name_on_posts is true" do
|
||||||
|
before do
|
||||||
|
user.update(name: nil)
|
||||||
|
SiteSetting.display_name_on_posts = true
|
||||||
|
SiteSetting.prioritize_username_in_ux = false
|
||||||
|
end
|
||||||
|
|
||||||
|
it "returns a quote with username" do
|
||||||
|
expect(quote).to eq(
|
||||||
|
"[quote=johndoe, post:#{post.post_number}, topic:#{post.topic_id}]\nThis is a post content\n[/quote]\n\n",
|
||||||
|
)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "when display_name_on_posts is true and prioritize_username_in_ux is true" do
|
||||||
|
before do
|
||||||
|
SiteSetting.display_name_on_posts = true
|
||||||
|
SiteSetting.prioritize_username_in_ux = true
|
||||||
|
end
|
||||||
|
|
||||||
|
it "returns a quote with username prioritized" do
|
||||||
|
expect(quote).to eq(
|
||||||
|
"[quote=johndoe, post:#{post.post_number}, topic:#{post.topic_id}]\nThis is a post content\n[/quote]\n\n",
|
||||||
|
)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
describe ".send_pm" do
|
describe ".send_pm" do
|
||||||
let(:user) { Fabricate(:user) }
|
let(:user) { Fabricate(:user) }
|
||||||
|
|
||||||
|
@ -463,6 +463,7 @@ after_initialize do
|
|||||||
field :sender, component: :user
|
field :sender, component: :user
|
||||||
|
|
||||||
placeholder :channel_name
|
placeholder :channel_name
|
||||||
|
placeholder :post_quote, triggerable: :post_created_edited
|
||||||
|
|
||||||
triggerables %i[recurring topic_tags_changed post_created_edited]
|
triggerables %i[recurring topic_tags_changed post_created_edited]
|
||||||
|
|
||||||
@ -471,6 +472,10 @@ after_initialize do
|
|||||||
channel = Chat::Channel.find_by(id: fields.dig("chat_channel_id", "value"))
|
channel = Chat::Channel.find_by(id: fields.dig("chat_channel_id", "value"))
|
||||||
placeholders = { channel_name: channel.title(sender) }.merge(context["placeholders"] || {})
|
placeholders = { channel_name: channel.title(sender) }.merge(context["placeholders"] || {})
|
||||||
|
|
||||||
|
if context["kind"] == "post_created_edited"
|
||||||
|
placeholders[:post_quote] = utils.build_quote(context["post"])
|
||||||
|
end
|
||||||
|
|
||||||
creator =
|
creator =
|
||||||
::Chat::CreateMessage.call(
|
::Chat::CreateMessage.call(
|
||||||
chat_channel_id: channel.id,
|
chat_channel_id: channel.id,
|
||||||
|
@ -412,21 +412,16 @@ describe Chat do
|
|||||||
automation_1.upsert_field!(
|
automation_1.upsert_field!(
|
||||||
"message",
|
"message",
|
||||||
"message",
|
"message",
|
||||||
{ value: "[{{topic_title}}]({{topic_url}})" },
|
{ value: "[{{topic_title}}]({{topic_url}})\n{{post_quote}}" },
|
||||||
target: "script",
|
target: "script",
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "sends the message" do
|
it "sends the message" do
|
||||||
PostCreator.create(
|
post = PostCreator.create(user_1, { title: "hello world topic", raw: "my name is fred" })
|
||||||
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(
|
expect(channel_1.chat_messages.last.message).to eq(
|
||||||
"[#{topic_1.title}](#{topic_1.relative_url})",
|
"[#{post.topic.title}](#{post.topic.relative_url})\n[quote=#{post.username}, post:#{post.post_number}, topic:#{post.topic_id}]\nmy name is fred\n[/quote]",
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Reference in New Issue
Block a user