mirror of
https://github.com/discourse/discourse.git
synced 2024-11-22 11:23:25 +08:00
FIX: create system message in user selected locale
DEV: refactor `create_from_system_user` to use `create` method
This commit is contained in:
parent
227c45107d
commit
23fdaf07f2
|
@ -12,7 +12,8 @@ class SystemMessage
|
|||
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)
|
||||
|
@ -21,50 +22,47 @@ class SystemMessage
|
|||
|
||||
def create(type, params = {})
|
||||
params = defaults.merge(params)
|
||||
from_system = params[:from_system] || false
|
||||
|
||||
title = I18n.t("system_messages.#{type}.subject_template", params)
|
||||
raw = I18n.t("system_messages.#{type}.text_body_template", params)
|
||||
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) }
|
||||
|
||||
creator = PostCreator.new(Discourse.site_contact_user,
|
||||
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(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,
|
||||
}
|
||||
|
|
|
@ -30,6 +30,24 @@ describe SystemMessage do
|
|||
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
|
||||
|
|
Loading…
Reference in New Issue
Block a user