Skip to content

Commit

Permalink
FIX: create system message in user selected locale
Browse files Browse the repository at this point in the history
DEV: refactor `create_from_system_user` to use `create` method
  • Loading branch information
arpitjalan committed May 29, 2019
1 parent 227c451 commit 23fdaf0
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 23 deletions.
44 changes: 21 additions & 23 deletions lib/system_message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ def self.create(recipient, type, params = {})
end

def self.create_from_system_user(recipient, type, params = {})
self.new(recipient).create_from_system_user(type, params)
params = params.merge(from_system: true)
self.new(recipient).create(type, params)
end

def initialize(recipient)
Expand All @@ -21,50 +22,47 @@ def initialize(recipient)

def create(type, params = {})
params = defaults.merge(params)
from_system = params[:from_system] || false

title = I18n.with_locale(@recipient.effective_locale) { I18n.t("system_messages.#{type}.subject_template", params) }
raw = I18n.with_locale(@recipient.effective_locale) { I18n.t("system_messages.#{type}.text_body_template", params) }

title = I18n.t("system_messages.#{type}.subject_template", params)
raw = I18n.t("system_messages.#{type}.text_body_template", params)
if from_system
user = Discourse.system_user
target_group_names = nil
else
user = Discourse.site_contact_user
target_group_names = Group.exists?(name: SiteSetting.site_contact_group_name) ? SiteSetting.site_contact_group_name : nil
end

creator = PostCreator.new(Discourse.site_contact_user,
creator = PostCreator.new(user,
title: title,
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,
target_group_names: target_group_names,
subtype: TopicSubtype.system_message,
skip_validations: true)

post = creator.create
post = I18n.with_locale(@recipient.effective_locale) { creator.create }

if creator.errors.present?
raise StandardError, creator.errors.full_messages.join(" ")
end

UserArchivedMessage.create!(user: Discourse.site_contact_user, topic: post.topic)
unless from_system
UserArchivedMessage.create!(user: Discourse.site_contact_user, topic: post.topic)
end

post
end

def create_from_system_user(type, params = {})
params = defaults.merge(params)

title = I18n.t("system_messages.#{type}.subject_template", params)
raw = I18n.t("system_messages.#{type}.text_body_template", params)

PostCreator.create!(Discourse.system_user,
title: title,
raw: raw,
archetype: Archetype.private_message,
target_usernames: @recipient.username,
subtype: TopicSubtype.system_message,
skip_validations: true)
end

def defaults
{
site_name: SiteSetting.title,
username: @recipient.username,
user_preferences_url: "#{Discourse.base_url}/u/#{@recipient.username_lower}/preferences",
new_user_tips: I18n.t('system_messages.usage_tips.text_body_template', base_url: Discourse.base_url),
new_user_tips: I18n.with_locale(@recipient.effective_locale) { I18n.t('system_messages.usage_tips.text_body_template', base_url: Discourse.base_url) },
site_password: "",
base_url: Discourse.base_url,
}
Expand Down
18 changes: 18 additions & 0 deletions spec/components/system_message_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,24 @@
expect(UserArchivedMessage.where(user_id: admin.id, topic_id: topic.id).length).to eq(1)
end

it 'can create a post from system user in user selected locale' do
SiteSetting.allow_user_locale = true
user_de = Fabricate(:user, locale: 'de')
system_user = Discourse.system_user

post = SystemMessage.create_from_system_user(user_de, :welcome_invite)
topic = post.topic

expect(post.valid?).to eq(true)
expect(topic).to be_private_message
expect(topic).to be_valid
expect(topic.title).to eq(I18n.with_locale(:de) { I18n.t("system_messages.welcome_invite.subject_template", site_name: SiteSetting.title) })
expect(topic.subtype).to eq(TopicSubtype.system_message)
expect(topic.allowed_users.include?(user_de)).to eq(true)
expect(topic.allowed_users.include?(system_user)).to eq(true)
expect(UserArchivedMessage.where(user_id: system_user.id, topic_id: topic.id).length).to eq(0)
end

it 'should allow site_contact_group_name' do
group = Fabricate(:group)
SiteSetting.site_contact_group_name = group.name
Expand Down

0 comments on commit 23fdaf0

Please sign in to comment.