FEATURE: Add site contact group. (#7152)

FEATURE: Add site contact group. (#7152)

diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index 4114136..c1cba64 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -1406,6 +1406,7 @@ en:
     post_menu_hidden_items: "The menu items to hide by default in the post menu unless an expansion ellipsis is clicked on."
     share_links: "Determine which items appear on the share dialog, and in what order."
     site_contact_username: "A valid staff username to send all automated messages from. If left blank the default System account will be used."
+    site_contact_group_name: "A valid group name to be invited to all automated messages."
     send_welcome_message: "Send all new users a welcome message with a quick start guide."
     send_tl1_welcome_message: "Send new trust level 1 users a welcome message."
     suppress_reply_directly_below: "Don't show the expandable reply count on a post when there is only a single reply directly below this post."
@@ -1987,6 +1988,7 @@ en:
     errors:
       invalid_email: "Invalid email address."
       invalid_username: "There's no user with that username."
+      invalid_group: "There's no group with that name."
       invalid_integer_min_max: "Value must be between %{min} and %{max}."
       invalid_integer_min: "Value must be %{min} or greater."
       invalid_integer_max: "Value cannot be higher than %{max}."
diff --git a/config/site_settings.yml b/config/site_settings.yml
index 840dfd6..24e69ec 100644
--- a/config/site_settings.yml
+++ b/config/site_settings.yml
@@ -47,6 +47,9 @@ required:
   site_contact_username:
     default: ""
     type: username
+  site_contact_group_name:
+    default: ""
+    type: group
   logo:
     default: -1
     client: true
diff --git a/lib/site_settings/type_supervisor.rb b/lib/site_settings/type_supervisor.rb
index bed7c4e..d354946 100644
--- a/lib/site_settings/type_supervisor.rb
+++ b/lib/site_settings/type_supervisor.rb
@@ -32,6 +32,7 @@ class SiteSettings::TypeSupervisor
       category: 16,
       uploaded_image_list: 17,
       upload: 18,
+      group: 19,
     )
   end
 
@@ -239,6 +240,8 @@ class SiteSettings::TypeSupervisor
       EmailSettingValidator
     when self.class.types[:username]
       UsernameSettingValidator
+    when self.class.types[:group]
+      GroupSettingValidator
     when self.class.types[:integer]
       IntegerSettingValidator
     when self.class.types[:regex]
diff --git a/lib/system_message.rb b/lib/system_message.rb
index 553f3b9..4d10c55 100644
--- a/lib/system_message.rb
+++ b/lib/system_message.rb
@@ -28,6 +28,7 @@ class SystemMessage
                        raw: raw,
                        archetype: Archetype.private_message,
                        target_usernames: @recipient.username,
+                       target_group_names: Group.exists?(name: SiteSetting.site_contact_group_name) ? SiteSetting.site_contact_group_name : nil,
                        subtype: TopicSubtype.system_message,
                        skip_validations: true)
 
diff --git a/lib/validators/group_setting_validator.rb b/lib/validators/group_setting_validator.rb
new file mode 100644
index 0000000..01ba575
--- /dev/null
+++ b/lib/validators/group_setting_validator.rb
@@ -0,0 +1,14 @@
+class GroupSettingValidator
+
+  def initialize(opts = {})
+    @opts = opts
+  end
+
+  def valid_value?(val)
+    val.blank? || Group.exists?(name: val)
+  end
+
+  def error_message
+    I18n.t('site_settings.errors.invalid_group')
+  end
+end
diff --git a/spec/components/system_message_spec.rb b/spec/components/system_message_spec.rb
index 6a77cfb..35dd84d 100644
--- a/spec/components/system_message_spec.rb
+++ b/spec/components/system_message_spec.rb
@@ -6,11 +6,14 @@ describe SystemMessage do
 
   context 'send' do
 
-    it 'should create a post correctly' do
+    let(:admin) { Fabricate(:admin) }
+    let(:user) { Fabricate(:user) }
 
-      admin = Fabricate(:admin)
-      user = Fabricate(:user)
+    before do
       SiteSetting.site_contact_username = admin.username
+    end
+
+    it 'should create a post correctly' do
       system_message = SystemMessage.new(user)
       post = system_message.create(:welcome_invite)
       topic = post.topic
@@ -24,6 +27,18 @@ describe SystemMessage do
 
       expect(UserArchivedMessage.where(user_id: admin.id, topic_id: topic.id).length).to eq(1)
     end
+
+    it 'should allow site_contact_group_name' do
+      group = Fabricate(:group)
+      SiteSetting.site_contact_group_name = group.name
+
+      post = SystemMessage.create(user, :welcome_invite)
+      expect(post.topic.allowed_groups).to contain_exactly(group)
+
+      group.update!(name: "anewname")
+      post = SystemMessage.create(user, :welcome_invite)
+      expect(post.topic.allowed_groups).to contain_exactly()
+    end
   end
 
 end
diff --git a/spec/components/validators/group_setting_validator_spec.rb b/spec/components/validators/group_setting_validator_spec.rb
new file mode 100644
index 0000000..668d44b
--- /dev/null
+++ b/spec/components/validators/group_setting_validator_spec.rb
@@ -0,0 +1,21 @@
+require 'rails_helper'
+
+describe GroupSettingValidator do
+  describe '#valid_value?' do
+    subject(:validator) { described_class.new }
+
+    it "returns true for blank values" do
+      expect(validator.valid_value?('')).to eq(true)
+      expect(validator.valid_value?(nil)).to eq(true)
+    end
+
+    it "returns true if value matches an existing group" do
+      Fabricate(:group, name: "hello")
+      expect(validator.valid_value?('hello')).to eq(true)
+    end
+
+    it "returns false if value does not match a group" do
+      expect(validator.valid_value?('notagroup')).to eq(false)
+    end
+  end
+end

GitHub sha: 76a14c47