diff --git a/app/mailers/user_notifications.rb b/app/mailers/user_notifications.rb index 07b4a0a5dda..bf39562d4d7 100644 --- a/app/mailers/user_notifications.rb +++ b/app/mailers/user_notifications.rb @@ -63,15 +63,25 @@ class UserNotifications < ActionMailer::Base @post = opts[:post] return unless @post.present? + username = @notification.data_hash[:display_username] notification_type = Notification.InvertedTypes[opts[:notification].notification_type].to_s - build_email user.email, - "user_notifications.user_#{notification_type}", - topic_title: @notification.data_hash[:topic_title], - message: @post.raw, - url: @post.url, - username: @notification.data_hash[:display_username], - add_unsubscribe_link: true + + email_opts = { + topic_title: @notification.data_hash[:topic_title], + message: @post.raw, + url: @post.url, + username: username, + 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 + alias :user_invited_to_private_message :notification_template alias :user_replied :notification_template alias :user_quoted :notification_template diff --git a/lib/email_builder.rb b/lib/email_builder.rb index c2909f1f676..5f636eb2da7 100644 --- a/lib/email_builder.rb +++ b/lib/email_builder.rb @@ -14,7 +14,14 @@ module EmailBuilder body << I18n.t("unsubscribe_link", params) 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 diff --git a/lib/topic_query.rb b/lib/topic_query.rb index e2802f5cef0..8f2280ba1ca 100644 --- a/lib/topic_query.rb +++ b/lib/topic_query.rb @@ -157,9 +157,13 @@ class TopicQuery results = default_list(unordered: true, per_page: count) .where('topics.id NOT IN (?)', exclude_topic_ids) .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 end diff --git a/spec/mailers/user_notifications_spec.rb b/spec/mailers/user_notifications_spec.rb index c7431c163d8..984c21118e3 100644 --- a/spec/mailers/user_notifications_spec.rb +++ b/spec/mailers/user_notifications_spec.rb @@ -44,8 +44,10 @@ describe UserNotifications do describe '.user_mentioned' do let(:post) { Fabricate(:post, user: user) } + let(:username) { "walterwhite"} + 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 subject { UserNotifications.user_mentioned(user, notification: notification, post: notification.post) } @@ -53,6 +55,12 @@ describe UserNotifications do its(:to) { should == [user.email] } its(:subject) { should be_present } 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 } end