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:
parent
485427b578
commit
d75339af76
|
@ -4,14 +4,13 @@
|
|||
# for the body and subject
|
||||
module Email
|
||||
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"
|
||||
|
||||
def initialize(to, opts = nil)
|
||||
@to = to
|
||||
@opts = opts || {}
|
||||
|
||||
@template_args = {
|
||||
site_name: SiteSetting.title,
|
||||
email_prefix: SiteSetting.email_prefix.presence || SiteSetting.title,
|
||||
|
@ -19,12 +18,23 @@ module Email
|
|||
user_preferences_url: "#{Discourse.base_url}/my/preferences",
|
||||
hostname: Discourse.current_hostname,
|
||||
}.merge!(@opts)
|
||||
|
||||
if @template_args[:url].present?
|
||||
@template_args[:header_instructions] ||= I18n.t(
|
||||
"user_notifications.header_instructions",
|
||||
@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
|
||||
@template_args[:respond_instructions] = ""
|
||||
|
@ -40,9 +50,9 @@ module Email
|
|||
string =
|
||||
(
|
||||
if allow_reply_by_email?
|
||||
+"user_notifications.reply_by_email"
|
||||
+@reply_by_email_key
|
||||
else
|
||||
+"user_notifications.visit_link_to_respond"
|
||||
+@visit_link_to_respond_key
|
||||
end
|
||||
)
|
||||
string << "_pm" if @opts[:private_reply]
|
||||
|
|
|
@ -13,6 +13,10 @@ RSpec.describe Email::MessageBuilder do
|
|||
let(:subject_modifier_block) { Proc.new { |subject, opts| "modified subject" } }
|
||||
|
||||
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
|
||||
expect(build_args[:to]).to eq(to_address)
|
||||
|
@ -50,6 +54,55 @@ RSpec.describe Email::MessageBuilder do
|
|||
)
|
||||
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
|
||||
expect(builder.build_args[:charset]).to eq("UTF-8")
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue