DEV add modifiers to message_builder so plugins can customize subject/body/html (#26867)

This commit is contained in:
marstall 2024-05-13 14:59:15 -04:00 committed by GitHub
parent 2cfcb4042e
commit 6df2f94bbc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 31 additions and 3 deletions

View File

@ -760,6 +760,7 @@ class UserNotifications < ActionMailer::Base
subject_pm: subject_pm,
participants: participants,
include_respond_instructions: !(user.suspended? || user.staged?),
notification_type: notification_type,
template: template,
use_topic_title_subject: use_topic_title_subject,
site_description: SiteSetting.site_description,

View File

@ -126,7 +126,7 @@ module Email
else
subject = @opts[:subject]
end
subject
DiscoursePluginRegistry.apply_modifier(:message_builder_subject, subject, @opts)
end
def html_part
@ -164,6 +164,7 @@ module Email
html_body: html_override.html_safe,
},
)
html = DiscoursePluginRegistry.apply_modifier(:message_builder_html_part, html, @opts)
Mail::Part.new do
content_type "text/html; charset=UTF-8"
@ -184,8 +185,7 @@ module Email
body << "\n"
body << @template_args[:unsubscribe_instructions]
end
body
DiscoursePluginRegistry.apply_modifier(:message_builder_body, body, @opts)
end
def build_args

View File

@ -10,6 +10,9 @@ RSpec.describe Email::MessageBuilder do
let(:build_args) { builder.build_args }
let(:header_args) { builder.header_args }
let(:allow_reply_header) { described_class::ALLOW_REPLY_BY_EMAIL_HEADER }
let(:subject_modifier_block) { Proc.new { |subject, opts| "modified subject" } }
let(:body_modifier_block) { Proc.new { |subject, opts| "modified body" } }
it "has the correct to address" do
expect(build_args[:to]).to eq(to_address)
@ -23,6 +26,30 @@ RSpec.describe Email::MessageBuilder do
expect(builder.body).to eq(body)
end
it "uses the message_builder subject modifier properly" do
plugin_instance = Plugin::Instance.new
plugin_instance.register_modifier(:message_builder_subject, &subject_modifier_block)
expect(builder.subject).to eq("modified subject")
ensure
DiscoursePluginRegistry.unregister_modifier(
plugin_instance,
:message_builder_subject,
&subject_modifier_block
)
end
it "uses the message_builder body modifier properly" do
plugin_instance = Plugin::Instance.new
plugin_instance.register_modifier(:message_builder_body, &body_modifier_block)
expect(builder.body).to eq("modified body")
ensure
DiscoursePluginRegistry.unregister_modifier(
plugin_instance,
:message_builder_body,
&body_modifier_block
)
end
it "has a utf-8 charset" do
expect(builder.build_args[:charset]).to eq("UTF-8")
end