A platform for community discussion. Free, open, simple.
FIX: do not notify admins on suppressed categories (#20238)
Same636abeb0d92
diff --git a/app/services/post_alerter.rb b/app/services/post_alerter.rbindex db6906157d2..b6447978d7d 100644--- a/app/services/post_alerter.rb+++ b/app/services/post_alerter.rb@@ -466,8 +466,7 @@ class PostAlerterreturnend- # Make sure the user can see the post- return unless Guardian.new(user).can_see?(post)+ return if !Guardian.new(user).can_receive_post_notifications?(post)return if user.staged? && topic.category&.mailinglist_mirror?diff --git a/lib/guardian/post_guardian.rb b/lib/guardian/post_guardian.rbindex 1ecd956f1bb..8946b1bddfd 100644--- a/lib/guardian/post_guardian.rb+++ b/lib/guardian/post_guardian.rb@@ -249,6 +249,18 @@ module PostGuardian!post_action.post&.topic&.archived?end+ def can_receive_post_notifications?(post)+ return false if !authenticated?++ if is_admin? && SiteSetting.suppress_secured_categories_from_admin+ topic = post.topic+ if !topic.private_message? && topic.category.read_restricted+ return secure_category_ids.include?(topic.category_id)+ end+ end+ can_see_post?(post)+ end+def can_see_post?(post)return false if post.blank?return true if is_admin?diff --git a/spec/lib/guardian_spec.rb b/spec/lib/guardian_spec.rbindex 8bdfe06e42b..52c2885d7e0 100644--- a/spec/lib/guardian_spec.rb+++ b/spec/lib/guardian_spec.rb@@ -820,6 +820,42 @@ RSpec.describe Guardian doendend+ describe "can_receive_post_notifications?" do+ it "returns false with a nil object" do+ expect(Guardian.new.can_receive_post_notifications?(nil)).to be_falsey+ expect(Guardian.new(user).can_receive_post_notifications?(nil)).to be_falsey+ end++ it "does not allow anonymous to be notified" do+ expect(Guardian.new.can_receive_post_notifications?(post)).to be_falsey+ end++ it "allows public categories" do+ expect(Guardian.new(trust_level_0).can_receive_post_notifications?(post)).to be_truthy+ expect(Guardian.new(admin).can_receive_post_notifications?(post)).to be_truthy+ end++ it "diallows secure categories with no access" do+ secure_category = Fabricate(:category, read_restricted: true)+ post.topic.update!(category_id: secure_category.id)++ expect(Guardian.new(trust_level_0).can_receive_post_notifications?(post)).to be_falsey+ expect(Guardian.new(admin).can_receive_post_notifications?(post)).to be_truthy++ SiteSetting.suppress_secured_categories_from_admin = true++ expect(Guardian.new(admin).can_receive_post_notifications?(post)).to be_falsey++ secure_category.set_permissions(group => :write)+ secure_category.save!++ group.add(admin)+ group.save!++ expect(Guardian.new(admin).can_receive_post_notifications?(post)).to be_truthy+ end+ end+describe "can_see?" doit "returns false with a nil object" doexpect(Guardian.new.can_see?(nil)).to be_falseydiff --git a/spec/services/post_action_notifier_spec.rb b/spec/services/post_action_notifier_spec.rbindex 8ac91ea5a68..087f62416da 100644--- a/spec/services/post_action_notifier_spec.rb+++ b/spec/services/post_action_notifier_spec.rb@@ -202,25 +202,24 @@ RSpec.describe PostActionNotifier doendcontext "with private message" do+ fab!(:private_message) do+ Fabricate(:topic, archetype: Archetype.private_message, category_id: nil)+ endfab!(:user) { Fabricate(:user) }- fab!(:mention_post) { Fabricate(:post, user: user, raw: "Hello @eviltrout") }- let(:topic) do- topic = mention_post.topic- topic.update_columns archetype: Archetype.private_message, category_id: nil- topic+ fab!(:mention_post) do+ Fabricate(:post, topic: private_message, user: user, raw: "Hello @eviltrout")endit "won't notify someone who can't see the post" do- expect {- Guardian.any_instance.expects(:can_see?).with(instance_of(Post)).returns(false)- mention_post- PostAlerter.post_created(mention_post)- }.not_to change(evil_trout.notifications, :count)+ expect { PostAlerter.post_created(mention_post) }.not_to change(+ evil_trout.notifications,+ :count,+ )endit "creates like notifications" doother_user = Fabricate(:user)- topic.allowed_users << user << other_user+ private_message.allowed_users << user << other_userexpect { PostActionCreator.like(other_user, mention_post) }.to change(user.notifications,:count,diff --git a/spec/services/post_alerter_spec.rb b/spec/services/post_alerter_spec.rbindex fc4bf047952..8131cc46e3c 100644--- a/spec/services/post_alerter_spec.rb+++ b/spec/services/post_alerter_spec.rb@@ -45,6 +45,15 @@ RSpec.describe PostAlerter dofab!(:user) { Fabricate(:user) }fab!(:tl2_user) { Fabricate(:user, trust_level: TrustLevel[2]) }+ fab!(:private_category) do+ Fabricate(+ :private_category,+ group: group,+ email_in: "[email protected]",+ email_in_allow_strangers: true,+ )+ end+def create_post_with_alerts(args = {})post = Fabricate(:post, args)PostAlerter.post_created(post)@@ -609,22 +618,13 @@ RSpec.describe PostAlerter dogroup_member = Fabricate(:user)group.add(group_member)- private_category =- Fabricate(- :private_category,- group: group,- email_in: "[email protected]",- email_in_allow_strangers: true,- )-staged_user_post = create_post(user: staged_user, category: private_category)- linking =- create_post(- user: group_member,- category: private_category,- raw: "my magic topic\n##{Discourse.base_url}#{staged_user_post.url}",- )+ create_post(+ user: group_member,+ category: private_category,+ raw: "my magic topic\n##{Discourse.base_url}#{staged_user_post.url}",+ )staged_user.reloadexpect(@@ -1392,6 +1392,17 @@ RSpec.describe PostAlerter do)end+ it "does not notify admins when suppress_secured_categories_from_admin is enabled" do+ SiteSetting.suppress_secured_categories_from_admin = true++ topic = Fabricate(:topic, category: private_category)+ post = Fabricate(:post, raw: "hello @#{admin.username} how are you today?", topic: topic)++ PostAlerter.post_created(post)++ expect(admin.notifications.count).to eq(0)+ end+it "doesn't notify regular user about whispered reply" do_post = Fabricate(:post, user: user, topic: topic)@@ -1639,14 +1650,6 @@ RSpec.describe PostAlerter dogroup.add(group_member)group.add(staged_member)- private_category =- Fabricate(- :private_category,- group: group,- email_in: "[email protected]",- email_in_allow_strangers: false,- )-level = CategoryUser.notification_levels[:watching]CategoryUser.set_notification_level_for_category(group_member, level, private_category.id)CategoryUser.set_notification_level_for_category(staged_member, level, private_category.id)