diff --git a/app/jobs/regular/user_email.rb b/app/jobs/regular/user_email.rb index 69538e3a3e4..ab7bc548e19 100644 --- a/app/jobs/regular/user_email.rb +++ b/app/jobs/regular/user_email.rb @@ -41,6 +41,7 @@ module Jobs return if seen_recently && !user.suspended? # Load the post if present + email_args[:post] ||= Post.where(id: notification.data_hash[:original_post_id].to_i).first email_args[:post] ||= notification.post email_args[:notification] = notification diff --git a/app/mailers/user_notifications.rb b/app/mailers/user_notifications.rb index 895c80318a3..f2d5cd661bc 100644 --- a/app/mailers/user_notifications.rb +++ b/app/mailers/user_notifications.rb @@ -127,7 +127,7 @@ class UserNotifications < ActionMailer::Base return unless @notification = opts[:notification] return unless @post = opts[:post] - username = @notification.data_hash[:display_username] + username = @notification.data_hash[:original_username] notification_type = opts[:notification_type] || Notification.types[@notification.notification_type].to_s context = "" diff --git a/app/models/post_alert_observer.rb b/app/models/post_alert_observer.rb index 290fcd8f3b1..0b6b4beff86 100644 --- a/app/models/post_alert_observer.rb +++ b/app/models/post_alert_observer.rb @@ -140,6 +140,7 @@ class PostAlertObserver < ActiveRecord::Observer end original_post = post + original_username = opts[:display_username] || post.username if collapsed post = first_unread_post(user,post.topic) || post @@ -155,6 +156,8 @@ class PostAlertObserver < ActiveRecord::Observer post_number: post.post_number, post_action_id: opts[:post_action_id], data: { topic_title: post.topic.title, + original_post_id: original_post.id, + original_username: original_username, display_username: opts[:display_username] || post.user.username }.to_json) end diff --git a/spec/jobs/user_email_spec.rb b/spec/jobs/user_email_spec.rb index 7dc726d136f..ce5d9ec7645 100644 --- a/spec/jobs/user_email_spec.rb +++ b/spec/jobs/user_email_spec.rb @@ -101,7 +101,16 @@ describe Jobs::UserEmail do context 'notification' do let(:post) { Fabricate(:post, user: user) } - let!(:notification) { Fabricate(:notification, user: user, topic: post.topic, post_number: post.post_number)} + let!(:notification) { + Fabricate(:notification, + user: user, + topic: post.topic, + post_number: post.post_number, + data: { + original_post_id: post.id + }.to_json + ) + } it 'passes a notification as an argument when a notification_id is present' do Email::Sender.any_instance.expects(:send) @@ -131,11 +140,17 @@ describe Jobs::UserEmail do before do @pm_from_staff = Fabricate(:post, user: Fabricate(:moderator)) @pm_from_staff.topic.topic_allowed_users.create!(user_id: suspended.id) - @pm_notification = Fabricate(:notification, user: suspended, topic: @pm_from_staff.topic, post_number: @pm_from_staff.post_number) + @pm_notification = Fabricate(:notification, + user: suspended, + topic: @pm_from_staff.topic, + post_number: @pm_from_staff.post_number, + data: { original_post_id: @pm_from_staff.id }.to_json + ) UserNotifications.expects(:user_private_message).with(suspended, notification: @pm_notification, post: @pm_from_staff).returns(mailer) end - subject(:execute_user_email_job) { Jobs::UserEmail.new.execute(type: :user_private_message, user_id: suspended.id, notification_id: @pm_notification.id) } + subject(:execute_user_email_job) { + Jobs::UserEmail.new.execute(type: :user_private_message, user_id: suspended.id, notification_id: @pm_notification.id) } it "sends an email" do execute_user_email_job diff --git a/spec/mailers/user_notifications_spec.rb b/spec/mailers/user_notifications_spec.rb index 836ffd675d4..57462825239 100644 --- a/spec/mailers/user_notifications_spec.rb +++ b/spec/mailers/user_notifications_spec.rb @@ -123,7 +123,7 @@ describe UserNotifications do topic: post.topic, notification_type: Notification.types[notification_type], post_number: post.post_number, - data: {display_username: username}.to_json ) + data: {original_username: username}.to_json ) end describe '.user_mentioned' do diff --git a/spec/models/notification_spec.rb b/spec/models/notification_spec.rb index 0c5cf4d0d3a..652212a26b4 100644 --- a/spec/models/notification_spec.rb +++ b/spec/models/notification_spec.rb @@ -173,14 +173,14 @@ describe Notification do user_id: user.id, topic_id: 2, post_number: 1, - data: '[]', + data: {}.to_json, notification_type: Notification.types[:private_message]) other = Notification.create!(read: false, user_id: user.id, topic_id: 2, post_number: 1, - data: '[]', + data: {}.to_json, notification_type: Notification.types[:mentioned]) @@ -197,9 +197,9 @@ describe Notification do user = Fabricate(:user) (1..3).map do |i| - Notification.create!(read: false, user_id: user.id, topic_id: 2, post_number: i, data: '[]', notification_type: 1) + Notification.create!(read: false, user_id: user.id, topic_id: 2, post_number: i, data: '{}', notification_type: 1) end - Notification.create!(read: true, user_id: user.id, topic_id: 2, post_number: 4, data: '[]', notification_type: 1) + Notification.create!(read: true, user_id: user.id, topic_id: 2, post_number: 4, data: '{}', notification_type: 1) Notification.mark_posts_read(user,2,[1,2,3,4]).should == 3 end