FEATURE: Add site contact group. (#7152)

This commit is contained in:
Bianca Nenciu 2019-03-13 12:34:47 +02:00 committed by Régis Hanol
parent 35426b5ad6
commit 76a14c47ac
7 changed files with 62 additions and 3 deletions

View File

@ -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}."

View File

@ -47,6 +47,9 @@ required:
site_contact_username:
default: ""
type: username
site_contact_group_name:
default: ""
type: group
logo:
default: -1
client: true

View File

@ -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]

View File

@ -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)

View File

@ -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

View File

@ -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

View File

@ -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