Add a better from address to the email

This commit is contained in:
Robin Ward 2013-02-27 18:30:14 -05:00
parent 7c1ae451fe
commit 421f048458
4 changed files with 40 additions and 11 deletions

View File

@ -63,15 +63,25 @@ class UserNotifications < ActionMailer::Base
@post = opts[:post] @post = opts[:post]
return unless @post.present? return unless @post.present?
username = @notification.data_hash[:display_username]
notification_type = Notification.InvertedTypes[opts[:notification].notification_type].to_s notification_type = Notification.InvertedTypes[opts[:notification].notification_type].to_s
build_email user.email,
"user_notifications.user_#{notification_type}", email_opts = {
topic_title: @notification.data_hash[:topic_title], topic_title: @notification.data_hash[:topic_title],
message: @post.raw, message: @post.raw,
url: @post.url, url: @post.url,
username: @notification.data_hash[:display_username], username: username,
add_unsubscribe_link: true add_unsubscribe_link: true
}
# If we have a display name, change the from address
if username.present?
email_opts[:from] = "\"#{username} @ #{SiteSetting.title}\" <#{SiteSetting.notification_email}>"
end
email = build_email user.email, "user_notifications.user_#{notification_type}", email_opts
end end
alias :user_invited_to_private_message :notification_template alias :user_invited_to_private_message :notification_template
alias :user_replied :notification_template alias :user_replied :notification_template
alias :user_quoted :notification_template alias :user_quoted :notification_template

View File

@ -14,7 +14,14 @@ module EmailBuilder
body << I18n.t("unsubscribe_link", params) body << I18n.t("unsubscribe_link", params)
end end
mail to: to, subject: I18n.t("#{email_key}.subject_template", params), body: body mail_args = {
to: to,
subject: I18n.t("#{email_key}.subject_template", params),
body: body
}
mail_args[:from] = params[:from] if params[:from].present?
mail(mail_args)
end end
end end

View File

@ -157,9 +157,13 @@ class TopicQuery
results = default_list(unordered: true, per_page: count) results = default_list(unordered: true, per_page: count)
.where('topics.id NOT IN (?)', exclude_topic_ids) .where('topics.id NOT IN (?)', exclude_topic_ids)
.where(closed: false, archived: false, visible: true) .where(closed: false, archived: false, visible: true)
.order('RANDOM()')
results = results.where('category_id = ?', topic.category_id) if topic.category_id.present? if topic.category_id.present?
results = results.order("CASE WHEN topics.category_id = #{topic.category_id.to_i} THEN 0 ELSE 1 END, RANDOM()")
else
results = results.order("RANDOM()")
end
results results
end end

View File

@ -44,8 +44,10 @@ describe UserNotifications do
describe '.user_mentioned' do describe '.user_mentioned' do
let(:post) { Fabricate(:post, user: user) } let(:post) { Fabricate(:post, user: user) }
let(:username) { "walterwhite"}
let(:notification) do let(:notification) do
Fabricate(:notification, user: user, topic: post.topic, post_number: post.post_number ) Fabricate(:notification, user: user, topic: post.topic, post_number: post.post_number, data: {display_username: username}.to_json )
end end
subject { UserNotifications.user_mentioned(user, notification: notification, post: notification.post) } subject { UserNotifications.user_mentioned(user, notification: notification, post: notification.post) }
@ -53,6 +55,12 @@ describe UserNotifications do
its(:to) { should == [user.email] } its(:to) { should == [user.email] }
its(:subject) { should be_present } its(:subject) { should be_present }
its(:from) { should == [SiteSetting.notification_email] } its(:from) { should == [SiteSetting.notification_email] }
it "should have the correct from address" do
subject.header['from'].to_s.should == "\"#{username} @ #{SiteSetting.title}\" <#{SiteSetting.notification_email}>"
end
its(:body) { should be_present } its(:body) { should be_present }
end end