Skip to content

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.rb
index 3deb70c0537..0eae8a12717 100644
--- a/app/mailers/user_notifications.rb
+++ b/app/mailers/user_notifications.rb
@@ -35,6 +35,7 @@ class UserNotifications < ActionMailer::Base
template: "user_notifications.signup_after_approval",
locale: locale,
new_user_tips: tips,
+ recipient_user: user,
)
end
@@ -50,6 +51,7 @@ class UserNotifications < ActionMailer::Base
locale: locale,
base_url: Discourse.base_url,
post_url: post_url,
+ recipient_user: user,
)
end
@@ -60,6 +62,7 @@ class UserNotifications < ActionMailer::Base
template: "user_notifications.signup_after_reject",
locale: locale,
reject_reason: opts[:reject_reason],
+ recipient_user: user,
)
end
@@ -79,6 +82,7 @@ class UserNotifications < ActionMailer::Base
browser: 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::Base
template: "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::Base
template: "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::Base
template: "user_notifications.account_silenced_forever",
locale: user_locale(user),
reason: user_history.details,
+ recipient_user: user,
)
else
silenced_till = user.silenced_till.in_time_zone(user.user_option.timezone.presence || "UTC")
@@ -174,6 +181,7 @@ class UserNotifications < ActionMailer::Base
locale: user_locale(user),
reason: user_history.details,
silenced_till: I18n.l(silenced_till, format: :long),
+ recipient_user: user,
)
end
end
@@ -206,6 +214,7 @@ class UserNotifications < ActionMailer::Base
template: "user_notifications.account_suspended_forever",
locale: user_locale(user),
reason: user_history.details,
+ recipient_user: user,
)
else
suspended_till = user.suspended_till.in_time_zone(user.user_option.timezone.presence || "UTC")
@@ -215,6 +224,7 @@ class UserNotifications < ActionMailer::Base
locale: user_locale(user),
reason: user_history.details,
suspended_till: I18n.l(suspended_till, format: :long),
+ recipient_user: user,
)
end
end
@@ -225,6 +235,7 @@ class UserNotifications < ActionMailer::Base
template: "user_notifications.account_exists",
locale: user_locale(user),
email: user.email,
+ recipient_user: user,
)
end
@@ -234,6 +245,7 @@ class UserNotifications < ActionMailer::Base
template: "user_notifications.account_second_factor_disabled",
locale: user_locale(user),
email: user.email,
+ recipient_user: user,
)
end
@@ -366,6 +378,8 @@ class UserNotifications < ActionMailer::Base
topics_for_digest.joins(:posts).where(posts: { post_number: 1 }).pluck("posts.id"),
}
+ opts[:recipient_user] = user
+
build_email(user.email, opts)
end
end
@@ -787,6 +801,7 @@ class UserNotifications < ActionMailer::Base
locale: locale,
}
+ email_opts[:recipient_user] = user
email_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::Base
private
def 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,
+ )
end
def build_summary_for(user)
diff --git a/app/models/translation_override.rb b/app/models/translation_override.rb
index fe28e6ccc52..029ff6383c5 100644
--- a/app/models/translation_override.rb
+++ b/app/models/translation_override.rb
@@ -22,6 +22,7 @@ class TranslationOverride < ActiveRecord::Base
topic_id
context
username
+ recipient_username
group_name
unsubscribe_url
subject_pm
diff --git a/lib/email/message_builder.rb b/lib/email/message_builder.rb
index c80e103324f..75a23f4088c 100644
--- a/lib/email/message_builder.rb
+++ b/lib/email/message_builder.rb
@@ -20,6 +20,10 @@ module Email
hostname: 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)
end
diff --git a/spec/lib/email/message_builder_spec.rb b/spec/lib/email/message_builder_spec.rb
index 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 do
expect(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" do
context "when include_respond_instructions is false" do
let(:private_reply) { false }
diff --git a/spec/mailers/user_notifications_spec.rb b/spec/mailers/user_notifications_spec.rb
index 7396a43fbfe..92c8fd0d02f 100644
--- a/spec/mailers/user_notifications_spec.rb
+++ b/spec/mailers/user_notifications_spec.rb
@@ -1459,6 +1459,31 @@ RSpec.describe UserNotifications do
end
end
+ 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 locale
shared_context "with notification derived from template" do
let(:user) { Fabricate(:user, locale: locale) }