Skip to content

A platform for community discussion. Free, open, simple.

FIX: Use post author username as fallback in notification emails (#37659)

Régis Hanolacd09a897298
diff --git a/app/mailers/user_notifications.rb b/app/mailers/user_notifications.rb
index f5cc9b0cc82..fd0397f8ead 100644
--- a/app/mailers/user_notifications.rb
+++ b/app/mailers/user_notifications.rb
@@ -530,7 +530,8 @@ class UserNotifications < ActionMailer::Base
allow_reply_by_email = opts[:allow_reply_by_email] unless user.suspended?
original_username =
- notification_data[:original_username] || notification_data[:display_username]
+ notification_data[:original_username] || post&.user&.username ||
+ notification_data[:display_username]
if user.staged && post
original_subject =
diff --git a/spec/mailers/user_notifications_spec.rb b/spec/mailers/user_notifications_spec.rb
index efb196ce347..7396a43fbfe 100644
--- a/spec/mailers/user_notifications_spec.rb
+++ b/spec/mailers/user_notifications_spec.rb
@@ -852,6 +852,27 @@ RSpec.describe UserNotifications do
)
expect(mail.subject).to match(/Super cool topic/)
end
+
+ it "uses post author username when original_username is missing from notification data" do
+ SiteSetting.enable_names = false
+
+ notification_data = {
+ topic_title: topic.title,
+ original_post_id: response.id,
+ original_post_type: response.post_type,
+ display_username: I18n.t("embed.replies", count: 7),
+ }
+
+ mail =
+ UserNotifications.user_posted(
+ user,
+ post: response,
+ notification_type: Notification.types[:posted],
+ notification_data_hash: notification_data,
+ )
+
+ expect(mail.header["X-Discourse-Sender"].value).to eq(response_by_user.username)
+ end
end
describe ".user_private_message" do