diff --git a/app/mailers/user_notifications.rb b/app/mailers/user_notifications.rb index 9192ea6e7..218247690 100644 --- a/app/mailers/user_notifications.rb +++ b/app/mailers/user_notifications.rb @@ -127,7 +127,7 @@ class UserNotifications < ActionMailer::Base title: post.topic.title, post: post, username: post.user.username, - from_alias: SiteSetting.enable_email_names ? post.user.name : post.user.username, + from_alias: (SiteSetting.enable_email_names && !post.user.name.empty?) ? post.user.name : post.user.username, allow_reply_by_email: true, use_site_subject: true, add_re_to_subject: true, @@ -173,7 +173,7 @@ class UserNotifications < ActionMailer::Base user_name = @notification.data_hash[:original_username] if @post && SiteSetting.enable_email_names - user_name = User.find_by(id: @post.user_id).name + user_name = User.find_by(id: @post.user_id).name if !User.find_by(id: @post.user_id).name.empty? end notification_type = opts[:notification_type] || Notification.types[@notification.notification_type].to_s diff --git a/spec/mailers/user_notifications_spec.rb b/spec/mailers/user_notifications_spec.rb index a8ddc4246..fa03d5ac7 100644 --- a/spec/mailers/user_notifications_spec.rb +++ b/spec/mailers/user_notifications_spec.rb @@ -131,11 +131,11 @@ describe UserNotifications do end describe '.user_posted' do - let(:response_by_user) { Fabricate(:user, name: "John Doe") } + let(:response_by_user) { Fabricate(:user, name: "John Doe", username: "john") } let(:post) { Fabricate(:post) } let(:response) { Fabricate(:post, topic: post.topic, user: response_by_user)} let(:user) { Fabricate(:user) } - let(:notification) { Fabricate(:notification, user: user) } + let(:notification) { Fabricate(:notification, user: user, data: {original_username: response_by_user.username}.to_json) } it 'generates a correct email' do SiteSetting.stubs(:enable_email_names).returns(false) @@ -144,6 +144,9 @@ describe UserNotifications do # from should not include full user name if "show user full names" is disabled expect(mail[:from].display_names).to_not eql(['John Doe']) + # from should include username if "show user full names" is disabled + expect(mail[:from].display_names).to eql(['john']) + # subject should not include category name expect(mail.subject).not_to match(/Uncategorized/) @@ -160,18 +163,18 @@ describe UserNotifications do end describe '.user_private_message' do - let(:response_by_user) { Fabricate(:user, name: "John Doe") } + let(:response_by_user) { Fabricate(:user, name: "", username: "john") } let(:topic) { Fabricate(:private_message_topic) } let(:response) { Fabricate(:post, topic: topic, user: response_by_user)} let(:user) { Fabricate(:user) } - let(:notification) { Fabricate(:notification, user: user) } + let(:notification) { Fabricate(:notification, user: user, data: {original_username: response_by_user.username}.to_json) } it 'generates a correct email' do SiteSetting.stubs(:enable_email_names).returns(true) mail = UserNotifications.user_private_message(response.user, post: response, notification: notification) - # from should include full user name - expect(mail[:from].display_names).to eql(['John Doe']) + # from should include username if full user name is not provided + expect(mail[:from].display_names).to eql(['john']) # subject should include "[PM]" expect(mail.subject).to match("[PM]")