From 6df2f94bbc360147c87ee6ec9bc13208de879d39 Mon Sep 17 00:00:00 2001 From: marstall Date: Mon, 13 May 2024 14:59:15 -0400 Subject: [PATCH] DEV add modifiers to message_builder so plugins can customize subject/body/html (#26867) --- app/mailers/user_notifications.rb | 1 + lib/email/message_builder.rb | 6 +++--- spec/lib/email/message_builder_spec.rb | 27 ++++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/app/mailers/user_notifications.rb b/app/mailers/user_notifications.rb index c3d7192add4..14cee52d9c5 100644 --- a/app/mailers/user_notifications.rb +++ b/app/mailers/user_notifications.rb @@ -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, diff --git a/lib/email/message_builder.rb b/lib/email/message_builder.rb index 0cb15ef0db0..c513f3b3973 100644 --- a/lib/email/message_builder.rb +++ b/lib/email/message_builder.rb @@ -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 diff --git a/spec/lib/email/message_builder_spec.rb b/spec/lib/email/message_builder_spec.rb index 3be2744da14..f2eb3723696 100644 --- a/spec/lib/email/message_builder_spec.rb +++ b/spec/lib/email/message_builder_spec.rb @@ -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