A platform for community discussion. Free, open, simple.
FEATURE: Add `recipient_username` interpolation key for email templates (#38096)
Régis Hanol3e5727b76b83
diff --git a/app/mailers/user_notifications.rb b/app/mailers/user_notifications.rbindex 3deb70c0537..0eae8a12717 100644--- a/app/mailers/user_notifications.rb+++ b/app/mailers/user_notifications.rb@@ -35,6 +35,7 @@ class UserNotifications < ActionMailer::Basetemplate: "user_notifications.signup_after_approval",locale: locale,new_user_tips: tips,+ recipient_user: user,)end@@ -50,6 +51,7 @@ class UserNotifications < ActionMailer::Baselocale: locale,base_url: Discourse.base_url,post_url: post_url,+ recipient_user: user,)end@@ -60,6 +62,7 @@ class UserNotifications < ActionMailer::Basetemplate: "user_notifications.signup_after_reject",locale: locale,reject_reason: opts[:reject_reason],+ recipient_user: user,)end@@ -79,6 +82,7 @@ class UserNotifications < ActionMailer::Basebrowser: I18n.t("user_auth_tokens.browser.#{browser}"),device: I18n.t("user_auth_tokens.device.#{device}"),os: I18n.t("user_auth_tokens.os.#{os}"),+ recipient_user: user,)end@@ -88,6 +92,7 @@ class UserNotifications < ActionMailer::Basetemplate: "user_notifications.notify_old_email",locale: user_locale(user),new_email: opts[:new_email],+ recipient_user: user,)end@@ -97,6 +102,7 @@ class UserNotifications < ActionMailer::Basetemplate: "user_notifications.notify_old_email_add",locale: user_locale(user),new_email: opts[:new_email],+ recipient_user: user,)end@@ -165,6 +171,7 @@ class UserNotifications < ActionMailer::Basetemplate: "user_notifications.account_silenced_forever",locale: user_locale(user),reason: user_history.details,+ recipient_user: user,)elsesilenced_till = user.silenced_till.in_time_zone(user.user_option.timezone.presence || "UTC")@@ -174,6 +181,7 @@ class UserNotifications < ActionMailer::Baselocale: user_locale(user),reason: user_history.details,silenced_till: I18n.l(silenced_till, format: :long),+ recipient_user: user,)endend@@ -206,6 +214,7 @@ class UserNotifications < ActionMailer::Basetemplate: "user_notifications.account_suspended_forever",locale: user_locale(user),reason: user_history.details,+ recipient_user: user,)elsesuspended_till = user.suspended_till.in_time_zone(user.user_option.timezone.presence || "UTC")@@ -215,6 +224,7 @@ class UserNotifications < ActionMailer::Baselocale: user_locale(user),reason: user_history.details,suspended_till: I18n.l(suspended_till, format: :long),+ recipient_user: user,)endend@@ -225,6 +235,7 @@ class UserNotifications < ActionMailer::Basetemplate: "user_notifications.account_exists",locale: user_locale(user),email: user.email,+ recipient_user: user,)end@@ -234,6 +245,7 @@ class UserNotifications < ActionMailer::Basetemplate: "user_notifications.account_second_factor_disabled",locale: user_locale(user),email: user.email,+ recipient_user: user,)end@@ -366,6 +378,8 @@ class UserNotifications < ActionMailer::Basetopics_for_digest.joins(:posts).where(posts: { post_number: 1 }).pluck("posts.id"),}+ opts[:recipient_user] = user+build_email(user.email, opts)endend@@ -787,6 +801,7 @@ class UserNotifications < ActionMailer::Baselocale: locale,}+ email_opts[:recipient_user] = useremail_opts[:html_override] = html unless translation_override_exists# If we have a display name, change the from address@@ -855,7 +870,13 @@ class UserNotifications < ActionMailer::Baseprivatedef build_user_email_token_by_template(template, user, email_token)- build_email(user.email, template: template, locale: user_locale(user), email_token: email_token)+ build_email(+ user.email,+ template: template,+ locale: user_locale(user),+ email_token: email_token,+ recipient_user: user,+ )enddef build_summary_for(user)diff --git a/app/models/translation_override.rb b/app/models/translation_override.rbindex fe28e6ccc52..029ff6383c5 100644--- a/app/models/translation_override.rb+++ b/app/models/translation_override.rb@@ -22,6 +22,7 @@ class TranslationOverride < ActiveRecord::Basetopic_idcontextusername+ recipient_usernamegroup_nameunsubscribe_urlsubject_pmdiff --git a/lib/email/message_builder.rb b/lib/email/message_builder.rbindex c80e103324f..75a23f4088c 100644--- a/lib/email/message_builder.rb+++ b/lib/email/message_builder.rb@@ -20,6 +20,10 @@ module Emailhostname: Discourse.current_hostname,}.merge!(@opts)+ if @opts[:recipient_user].present?+ @template_args[:recipient_username] = @opts[:recipient_user].username+ end+if @opts[:template].present? && I18n.exists?("#{@opts[:template]}.preview")@template_args[:email_preview] ||= I18n.t("#{@opts[:template]}.preview", @template_args)enddiff --git a/spec/lib/email/message_builder_spec.rb b/spec/lib/email/message_builder_spec.rbindex 581cc3821c1..5f18c6b4744 100644--- a/spec/lib/email/message_builder_spec.rb+++ b/spec/lib/email/message_builder_spec.rb@@ -115,6 +115,20 @@ RSpec.describe Email::MessageBuilder doexpect(header_args["x-ms-reactions"]).to eq("disallow")end+ describe "recipient_username" do+ it "sets recipient_username when recipient_user is provided" do+ user = Fabricate(:user, username: "recipient_user")+ builder =+ Email::MessageBuilder.new(user.email, subject: "test", body: "test", recipient_user: user)+ expect(builder.template_args[:recipient_username]).to eq("recipient_user")+ end++ it "does not set recipient_username when recipient_user is not provided" do+ builder = Email::MessageBuilder.new("[email protected]", subject: "test", body: "test")+ expect(builder.template_args[:recipient_username]).to be_nil+ end+ end+describe "include_respond_instructions" docontext "when include_respond_instructions is false" dolet(:private_reply) { false }diff --git a/spec/mailers/user_notifications_spec.rb b/spec/mailers/user_notifications_spec.rbindex 7396a43fbfe..92c8fd0d02f 100644--- a/spec/mailers/user_notifications_spec.rb+++ b/spec/mailers/user_notifications_spec.rb@@ -1459,6 +1459,31 @@ RSpec.describe UserNotifications doendend+ describe "recipient_username" do+ fab!(:user)+ fab!(:topic)+ fab!(:post) { Fabricate(:post, topic: topic) }+ fab!(:response) { Fabricate(:basic_reply, topic: topic) }+ fab!(:notification) { Fabricate(:mentioned_notification, user: user, post: response) }++ it "includes recipient_username in notification emails" do+ TranslationOverride.upsert!(+ I18n.locale,+ "user_notifications.user_mentioned.text_body_template",+ "Hello %{recipient_username}, %{username} mentioned you on %{topic_title}",+ )++ mail =+ UserNotifications.user_mentioned(+ user,+ post: response,+ notification_type: notification.notification_type,+ notification_data_hash: notification.data_hash,+ )+ expect(mail.body.to_s).to include("Hello #{user.username}")+ end+ end+# notification emails derived from templates are translated into the user's localeshared_context "with notification derived from template" dolet(:user) { Fabricate(:user, locale: locale) }