Skip to content

Commit 83068fa

Browse files
committed
FEATURE: show full name in emails
1 parent 10094a0 commit 83068fa

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed

app/mailers/user_notifications.rb

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ def mailing_list_notify(user, post)
126126
send_notification_email(
127127
title: post.topic.title,
128128
post: post,
129-
from_alias: post.user.username,
129+
username: post.user.username,
130+
from_alias: SiteSetting.enable_names ? post.user.name : post.user.username,
130131
allow_reply_by_email: true,
131132
use_site_subject: true,
132133
add_re_to_subject: true,
@@ -170,7 +171,11 @@ def notification_email(user, opts)
170171
return unless @notification = opts[:notification]
171172
return unless @post = opts[:post]
172173

173-
username = @notification.data_hash[:original_username]
174+
user_name = @notification.data_hash[:original_username]
175+
if @post && SiteSetting.enable_names
176+
user_name = User.find_by(id: @post.user_id).name
177+
end
178+
174179
notification_type = opts[:notification_type] || Notification.types[@notification.notification_type].to_s
175180

176181
return if user.mailing_list_mode && !@post.topic.private_message? &&
@@ -185,7 +190,8 @@ def notification_email(user, opts)
185190
send_notification_email(
186191
title: title,
187192
post: @post,
188-
from_alias: username,
193+
username: @notification.data_hash[:original_username],
194+
from_alias: user_name,
189195
allow_reply_by_email: allow_reply_by_email,
190196
use_site_subject: use_site_subject,
191197
add_re_to_subject: add_re_to_subject,
@@ -202,6 +208,7 @@ def send_notification_email(opts)
202208
allow_reply_by_email = opts[:allow_reply_by_email]
203209
use_site_subject = opts[:use_site_subject]
204210
add_re_to_subject = opts[:add_re_to_subject] && post.post_number > 1
211+
username = opts[:username]
205212
from_alias = opts[:from_alias]
206213
notification_type = opts[:notification_type]
207214
user = opts[:user]
@@ -254,7 +261,7 @@ def send_notification_email(opts)
254261
post_id: post.id,
255262
topic_id: post.topic_id,
256263
context: context,
257-
username: from_alias,
264+
username: username,
258265
add_unsubscribe_link: true,
259266
allow_reply_by_email: allow_reply_by_email,
260267
use_site_subject: use_site_subject,

spec/mailers/user_notifications_spec.rb

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,21 @@
7171
end
7272

7373
describe '.user_replied' do
74+
let(:response_by_user) { Fabricate(:user, name: "John Doe") }
7475
let(:category) { Fabricate(:category, name: 'India') }
7576
let(:topic) { Fabricate(:topic, category: category) }
7677
let(:post) { Fabricate(:post, topic: topic) }
77-
let(:response) { Fabricate(:post, topic: post.topic)}
78+
let(:response) { Fabricate(:post, topic: post.topic, user: response_by_user)}
7879
let(:user) { Fabricate(:user) }
7980
let(:notification) { Fabricate(:notification, user: user) }
8081

8182
it 'generates a correct email' do
83+
SiteSetting.stubs(:enable_names).returns(true)
8284
mail = UserNotifications.user_replied(response.user, post: response, notification: notification)
8385

86+
# from should include full user name
87+
expect(mail[:from].display_names).to eql(['John Doe'])
88+
8489
# subject should include category name
8590
expect(mail.subject).to match(/India/)
8691

@@ -109,14 +114,19 @@
109114
end
110115

111116
describe '.user_posted' do
117+
let(:response_by_user) { Fabricate(:user, name: "John Doe") }
112118
let(:post) { Fabricate(:post) }
113-
let(:response) { Fabricate(:post, topic: post.topic)}
119+
let(:response) { Fabricate(:post, topic: post.topic, user: response_by_user)}
114120
let(:user) { Fabricate(:user) }
115121
let(:notification) { Fabricate(:notification, user: user) }
116122

117123
it 'generates a correct email' do
124+
SiteSetting.stubs(:enable_names).returns(false)
118125
mail = UserNotifications.user_posted(response.user, post: response, notification: notification)
119126

127+
# from should not include full user name if "show user full names" is disabled
128+
expect(mail[:from].display_names).to_not eql(['John Doe'])
129+
120130
# subject should not include category name
121131
expect(mail.subject).not_to match(/Uncategorized/)
122132

@@ -133,14 +143,19 @@
133143
end
134144

135145
describe '.user_private_message' do
146+
let(:response_by_user) { Fabricate(:user, name: "John Doe") }
136147
let(:topic) { Fabricate(:private_message_topic) }
137-
let(:response) { Fabricate(:post, topic: topic)}
148+
let(:response) { Fabricate(:post, topic: topic, user: response_by_user)}
138149
let(:user) { Fabricate(:user) }
139150
let(:notification) { Fabricate(:notification, user: user) }
140151

141152
it 'generates a correct email' do
153+
SiteSetting.stubs(:enable_names).returns(true)
142154
mail = UserNotifications.user_private_message(response.user, post: response, notification: notification)
143155

156+
# from should include full user name
157+
expect(mail[:from].display_names).to eql(['John Doe'])
158+
144159
# subject should include "[PM]"
145160
expect(mail.subject).to match("[PM]")
146161

@@ -224,7 +239,8 @@ def expects_build_with(condition)
224239
end
225240

226241
it "has a from alias" do
227-
expects_build_with(has_entry(:from_alias, "#{username}"))
242+
SiteSetting.stubs(:enable_names).returns(true)
243+
expects_build_with(has_entry(:from_alias, "#{user.name}"))
228244
end
229245

230246
it "should explain how to respond" do

0 commit comments

Comments
 (0)