From d75339af76072a67d36764935d1fc96a34070d3c Mon Sep 17 00:00:00 2001 From: marstall Date: Wed, 22 May 2024 15:33:06 -0400 Subject: [PATCH] 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 --- lib/email/message_builder.rb | 20 +++++++--- spec/lib/email/message_builder_spec.rb | 53 ++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 5 deletions(-) diff --git a/lib/email/message_builder.rb b/lib/email/message_builder.rb index c513f3b3973..02ef75e8843 100644 --- a/lib/email/message_builder.rb +++ b/lib/email/message_builder.rb @@ -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] diff --git a/spec/lib/email/message_builder_spec.rb b/spec/lib/email/message_builder_spec.rb index f2eb3723696..3ad1fbd5300 100644 --- a/spec/lib/email/message_builder_spec.rb +++ b/spec/lib/email/message_builder_spec.rb @@ -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