DEV: let reply_by_email, visit_link_to_respond email strings be modified by plugins (#27133)

* DEV: allow reply_by_email, visit_link_to_respond strings to be modified by plugins

* DEV: separate visit_link_to_respond and reply_by_email modifiers out
This commit is contained in:
marstall
2024-05-22 15:33:06 -04:00
committed by GitHub
parent 485427b578
commit d75339af76
2 changed files with 68 additions and 5 deletions

View File

@ -4,14 +4,13 @@
# for the body and subject # for the body and subject
module Email module Email
class MessageBuilder class MessageBuilder
attr_reader :template_args attr_reader :template_args, :reply_by_email_key
ALLOW_REPLY_BY_EMAIL_HEADER = "X-Discourse-Allow-Reply-By-Email" ALLOW_REPLY_BY_EMAIL_HEADER = "X-Discourse-Allow-Reply-By-Email"
def initialize(to, opts = nil) def initialize(to, opts = nil)
@to = to @to = to
@opts = opts || {} @opts = opts || {}
@template_args = { @template_args = {
site_name: SiteSetting.title, site_name: SiteSetting.title,
email_prefix: SiteSetting.email_prefix.presence || SiteSetting.title, email_prefix: SiteSetting.email_prefix.presence || SiteSetting.title,
@ -19,12 +18,23 @@ module Email
user_preferences_url: "#{Discourse.base_url}/my/preferences", user_preferences_url: "#{Discourse.base_url}/my/preferences",
hostname: Discourse.current_hostname, hostname: Discourse.current_hostname,
}.merge!(@opts) }.merge!(@opts)
if @template_args[:url].present? if @template_args[:url].present?
@template_args[:header_instructions] ||= I18n.t( @template_args[:header_instructions] ||= I18n.t(
"user_notifications.header_instructions", "user_notifications.header_instructions",
@template_args, @template_args,
) )
@visit_link_to_respond_key =
DiscoursePluginRegistry.apply_modifier(
:message_builder_visit_link_to_respond,
"user_notifications.visit_link_to_respond",
@opts,
)
@reply_by_email_key =
DiscoursePluginRegistry.apply_modifier(
:message_builder_reply_by_email,
"user_notifications.reply_by_email",
@opts,
)
if @opts[:include_respond_instructions] == false if @opts[:include_respond_instructions] == false
@template_args[:respond_instructions] = "" @template_args[:respond_instructions] = ""
@ -40,9 +50,9 @@ module Email
string = string =
( (
if allow_reply_by_email? if allow_reply_by_email?
+"user_notifications.reply_by_email" +@reply_by_email_key
else else
+"user_notifications.visit_link_to_respond" +@visit_link_to_respond_key
end end
) )
string << "_pm" if @opts[:private_reply] string << "_pm" if @opts[:private_reply]

View File

@ -13,6 +13,10 @@ RSpec.describe Email::MessageBuilder do
let(:subject_modifier_block) { Proc.new { |subject, opts| "modified subject" } } let(:subject_modifier_block) { Proc.new { |subject, opts| "modified subject" } }
let(:body_modifier_block) { Proc.new { |subject, opts| "modified body" } } let(:body_modifier_block) { Proc.new { |subject, opts| "modified body" } }
let(:visit_link_to_respond_modifier_block) do
Proc.new { |subject, opts| "modified visit_link_to_respond" }
end
let(:reply_by_email_modifier_block) { Proc.new { |subject, opts| "modified reply_by_email" } }
it "has the correct to address" do it "has the correct to address" do
expect(build_args[:to]).to eq(to_address) expect(build_args[:to]).to eq(to_address)
@ -50,6 +54,55 @@ RSpec.describe Email::MessageBuilder do
) )
end end
it "uses the message_builder_reply_by_email modifier properly" do
plugin_instance = Plugin::Instance.new
plugin_instance.register_modifier(
:message_builder_reply_by_email,
&reply_by_email_modifier_block
)
builder2 =
Email::MessageBuilder.new(
"to@to.com",
subject: "email_subject",
body: "body",
allow_reply_by_email: true,
include_respond_instructions: true,
url: "http://localhost",
)
expect(builder2.reply_by_email_key).to equal("modified reply_by_email")
ensure
DiscoursePluginRegistry.unregister_modifier(
plugin_instance,
:message_builder_reply_by_email,
&reply_by_email_modifier_block
)
end
it "uses the message_builder_visit_link_to_respond modifier" do
plugin_instance = Plugin::Instance.new
plugin_instance.register_modifier(
:message_builder_visit_link_to_respond,
&visit_link_to_respond_modifier_block
)
builder2 =
Email::MessageBuilder.new(
"to@to.com",
subject: "email_subject",
body: "body",
include_respond_instructions: true,
url: "http://localhost",
)
expect(builder2.template_args[:respond_instructions]).to include(
"modified visit_link_to_respond",
)
ensure
DiscoursePluginRegistry.unregister_modifier(
plugin_instance,
:message_builder_visit_link_to_respond,
&visit_link_to_respond_modifier_block
)
end
it "has a utf-8 charset" do it "has a utf-8 charset" do
expect(builder.build_args[:charset]).to eq("UTF-8") expect(builder.build_args[:charset]).to eq("UTF-8")
end end