diff --git a/app/jobs/regular/user_email.rb b/app/jobs/regular/user_email.rb index a1e235b06..49891c510 100644 --- a/app/jobs/regular/user_email.rb +++ b/app/jobs/regular/user_email.rb @@ -13,7 +13,8 @@ module Jobs # Find the user user = User.where(id: args[:user_id]).first - return if !user || user.is_banned? + return unless user + return if user.is_banned? && args[:type] != :user_private_message seen_recently = (user.last_seen_at.present? && user.last_seen_at > SiteSetting.email_time_window_mins.minutes.ago) seen_recently = false if user.email_always @@ -36,9 +37,8 @@ module Jobs notification = nil notification = Notification.where(id: args[:notification_id]).first if args[:notification_id].present? if notification.present? - # Don't email a user about a post when we've seen them recently. - return if seen_recently + return if seen_recently && !user.is_banned? # Load the post if present email_args[:post] ||= notification.post @@ -69,6 +69,7 @@ module Jobs post && (post.topic.blank? || post.user_deleted? || + (user.is_banned? && !post.user.try(:staff?)) || PostTiming.where(topic_id: post.topic_id, post_number: post.post_number, user_id: user.id).present?) end diff --git a/spec/jobs/user_email_spec.rb b/spec/jobs/user_email_spec.rb index 2b4d166f0..f149ae7a8 100644 --- a/spec/jobs/user_email_spec.rb +++ b/spec/jobs/user_email_spec.rb @@ -8,6 +8,7 @@ describe Jobs::UserEmail do end let(:user) { Fabricate(:user, last_seen_at: 11.minutes.ago ) } + let(:banned) { Fabricate(:user, last_seen_at: 10.minutes.ago, banned_at: 5.minutes.ago, banned_till: 7.days.from_now ) } let(:mailer) { Mail::Message.new(to: user.email) } it "raises an error when there is no user" do @@ -82,6 +83,19 @@ describe Jobs::UserEmail do Jobs::UserEmail.new.execute(type: :private_message, user_id: user.id, post_id: post.id) end + context 'user is banned' do + it "doesn't send email for a pm from a regular user" do + Email::Sender.any_instance.expects(:send).never + Jobs::UserEmail.new.execute(type: :private_message, user_id: banned.id, post_id: post.id) + end + + it "doesn't send email for a pm from a staff user" do + pm_from_staff = Fabricate(:post, user: Fabricate(:moderator)) + pm_from_staff.topic.topic_allowed_users.create!(user_id: banned.id) + Email::Sender.any_instance.expects(:send).never + Jobs::UserEmail.new.execute(type: :private_message, user_id: banned.id, post_id: pm_from_staff.id) + end + end end @@ -107,6 +121,32 @@ describe Jobs::UserEmail do Jobs::UserEmail.new.execute(type: :user_mentioned, user_id: user.id, notification_id: notification.id) end + context 'user is banned' do + it "doesn't send email for a pm from a regular user" do + Email::Sender.any_instance.expects(:send).never + Jobs::UserEmail.new.execute(type: :user_private_message, user_id: banned.id, notification_id: notification.id) + end + + context 'pm from staff' do + before do + @pm_from_staff = Fabricate(:post, user: Fabricate(:moderator)) + @pm_from_staff.topic.topic_allowed_users.create!(user_id: banned.id) + @pm_notification = Fabricate(:notification, user: banned, topic: @pm_from_staff.topic, post_number: @pm_from_staff.post_number) + UserNotifications.expects(:user_private_message).with(banned, 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: banned.id, notification_id: @pm_notification.id) } + + it "sends an email" do + execute_user_email_job + end + + it "sends an email even if user was last seen recently" do + banned.update_column(:last_seen_at, 1.minute.ago) + execute_user_email_job + end + end + end end end