A platform for community discussion. Free, open, simple.
FIX: Don’t email suspended users from group PM
Loïc Guitaut27f7cf18b1b4
diff --git a/app/jobs/regular/user_email.rb b/app/jobs/regular/user_email.rbindex 6bbbe6be8fb..73bdad09e92 100644--- a/app/jobs/regular/user_email.rb+++ b/app/jobs/regular/user_email.rb@@ -108,8 +108,12 @@ module Jobsreturn skip_message(SkippedEmailLog.reason_types[:user_email_anonymous_user])end- if user.suspended? && !%w[user_private_message account_suspended].include?(type.to_s)- return skip_message(SkippedEmailLog.reason_types[:user_email_user_suspended_not_pm])+ if user.suspended?+ if !type.to_s.in?(%w[user_private_message account_suspended])+ return skip_message(SkippedEmailLog.reason_types[:user_email_user_suspended_not_pm])+ elsif post.topic.group_pm?+ return skip_message(SkippedEmailLog.reason_types[:user_email_user_suspended])+ endendif type.to_s == "digest"diff --git a/app/models/topic.rb b/app/models/topic.rbindex 155736aa056..d50b52cafcc 100644--- a/app/models/topic.rb+++ b/app/models/topic.rb@@ -2032,6 +2032,10 @@ class Topic < ActiveRecord::Baseendend+ def group_pm?+ private_message? && all_allowed_users.count > 2+ end+privatedef invite_to_private_message(invited_by, target_user, guardian)diff --git a/spec/jobs/user_email_spec.rb b/spec/jobs/user_email_spec.rbindex 3a52f547be4..ca79668a9b0 100644--- a/spec/jobs/user_email_spec.rb+++ b/spec/jobs/user_email_spec.rb@@ -423,37 +423,76 @@ RSpec.describe Jobs::UserEmail doendcontext "when user is suspended" do- it "doesn't send email for a pm from a regular user" do- Jobs::UserEmail.new.execute(- type: :user_private_message,- user_id: suspended.id,- post_id: post.id,- )-- expect(ActionMailer::Base.deliveries).to eq([])- end-- it "does send an 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: suspended.id)+ context "when topic is a private message" do+ subject(:send_email) do+ described_class.new.execute(+ type: :user_private_message,+ user_id: suspended.id,+ post_id: post.id,+ notification_id: pm_notification.id,+ )+ end- pm_notification =+ let(:pm_notification) doFabricate(: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,+ topic: post.topic,+ post_number: post.post_number,+ data: { original_post_id: post.id }.to_json,)+ end+ fab!(:moderator) { Fabricate(:moderator) }+ fab!(:regular_user) { Fabricate(:user) }- Jobs::UserEmail.new.execute(- type: :user_private_message,- user_id: suspended.id,- post_id: pm_from_staff.id,- notification_id: pm_notification.id,- )+ context "when this is not a group PM" do+ let(:post) { Fabricate(:private_message_post, user: user, recipient: suspended) }++ context "when post is from a staff user" do+ let(:user) { moderator }++ it "does send an email" do+ send_email+ expect(ActionMailer::Base.deliveries.first.to).to contain_exactly(suspended.email)+ end+ end++ context "when post is from a regular user" do+ let(:user) { regular_user }++ it "doesn't send email" do+ send_email+ expect(ActionMailer::Base.deliveries).to eq([])+ end+ end+ end++ context "when this is a group PM" do+ fab!(:group) { Fabricate(:group) }+ fab!(:users) { Fabricate.times(2, :user) }++ let(:post) { Fabricate(:group_private_message_post, user: user, recipients: group) }++ before { group.users << [suspended, *users] }++ context "when post is from a staff user" do+ let(:user) { moderator }- expect(ActionMailer::Base.deliveries.first.to).to contain_exactly(suspended.email)+ it "does not send an email" do+ send_email+ expect(ActionMailer::Base.deliveries).to be_empty+ end+ end++ context "when post is from a regular user" do+ let(:user) { regular_user }++ it "does not send an email" do+ send_email+ expect(ActionMailer::Base.deliveries).to be_empty+ end+ end+ endendit "doesn't send PM from system user" dodiff --git a/spec/models/topic_spec.rb b/spec/models/topic_spec.rbindex 9f8e466a7be..2304fae1fd5 100644--- a/spec/models/topic_spec.rb+++ b/spec/models/topic_spec.rb@@ -3458,4 +3458,28 @@ RSpec.describe Topic doendendend++ describe "#group_pm?" do+ context "when topic is not a private message" do+ subject(:public_topic) { Fabricate(:topic) }++ it { is_expected.not_to be_a_group_pm }+ end++ context "when topic is a private message" do+ subject(:pm_topic) { Fabricate(:private_message_topic) }++ context "when more than two people have access" do+ let(:other_user) { Fabricate(:user) }++ before { pm_topic.allowed_users << other_user }++ it { is_expected.to be_a_group_pm }+ end++ context "when no more than two people have access" do+ it { is_expected.not_to be_a_group_pm }+ end+ end+ endend