FIX: Category group moderators can read flagged post meta_topics (#14014)

FIX: Category group moderators can read flagged post meta_topics (#14014)

When a post is flagged with the reason of ‘Something Else’ a brief message can be added by the user which subsequently creates a meta_topic private message. The group moderators is automatically added to this topic.

If category group moderation is enabled, and the post belongs to a category with a reviewable group, that group should also be added to the meta_topic.

Note: This extends the notify_moderators logic, and will add the reviewable group to the meta_topic, regardless of the settings of that group.

diff --git a/lib/guardian.rb b/lib/guardian.rb
index 54822b9..9312442 100644
--- a/lib/guardian.rb
+++ b/lib/guardian.rb
@@ -445,7 +445,7 @@ class Guardian
     # Can't send PMs to suspended users
     (is_staff? || is_group || !target.suspended?) &&
     # Check group messageable level
-    (is_staff? || is_user || Group.messageable(@user).where(id: target.id).exists?) &&
+    (is_staff? || is_user || Group.messageable(@user).where(id: target.id).exists? || notify_moderators) &&
     # Silenced users can only send PM to staff
     (!is_silenced? || target.staff?)
   end
diff --git a/lib/post_action_creator.rb b/lib/post_action_creator.rb
index 559aaaf..e1f42ee 100644
--- a/lib/post_action_creator.rb
+++ b/lib/post_action_creator.rb
@@ -306,7 +306,11 @@ private
 
     if [:notify_moderators, :spam].include?(@post_action_name)
       create_args[:subtype] = TopicSubtype.notify_moderators
-      create_args[:target_group_names] = Group[:moderators].name
+      create_args[:target_group_names] = [Group[:moderators].name]
+
+      if SiteSetting.enable_category_group_moderation? && @post.topic&.category&.reviewable_by_group_id?
+        create_args[:target_group_names] << @post.topic.category.reviewable_by_group.name
+      end
     else
       create_args[:subtype] = TopicSubtype.notify_user
 
diff --git a/spec/models/post_action_spec.rb b/spec/models/post_action_spec.rb
index b81b735..45bf725 100644
--- a/spec/models/post_action_spec.rb
+++ b/spec/models/post_action_spec.rb
@@ -91,6 +91,28 @@ describe PostAction do
       expect(topic.message_archived?(mod)).to eq(true)
     end
 
+    context "category group moderators" do
+      fab!(:group_user) { Fabricate(:group_user) }
+      let(:group) { group_user.group }
+
+      before do
+        SiteSetting.enable_category_group_moderation = true
+        group.update!(messageable_level: Group::ALIAS_LEVELS[:nobody])
+        post.topic.category.update!(reviewable_by_group_id: group.id)
+      end
+
+      it "notifies via pm" do
+        result = PostActionCreator.notify_moderators(
+          codinghorror,
+          post,
+          "this is my special long message"
+        )
+
+        readable_by_groups = result.reviewable_score.meta_topic.topic_allowed_groups.map(&:group_id)
+        expect(readable_by_groups).to include(group.id)
+      end
+    end
+
   end
 
   describe "update_counters" do
@@ -831,16 +853,11 @@ describe PostAction do
     end
 
     it "should raise the right errors when it fails to create a post" do
-      begin
-        group = Group[:moderators]
-        messageable_level = group.messageable_level
-        group.update!(messageable_level: Group::ALIAS_LEVELS[:nobody])
+      user = Fabricate(:user)
+      UserSilencer.new(user, Discourse.system_user).silence
 
-        result = PostActionCreator.notify_moderators(Fabricate(:user), post, 'testing')
-        expect(result).to be_failed
-      ensure
-        group.update!(messageable_level: messageable_level)
-      end
+      result = PostActionCreator.notify_moderators(user, post, 'testing')
+      expect(result).to be_failed
     end
 
     it "should succeed even with low max title length" do

GitHub sha: 20a6bad87ee4a7fa14dad778120f09db49a28d54

This commit appears in #14014 which was approved by eviltrout. It was merged by jbrw.