FIX: Always use site default locale when creating notes

This commit is contained in:
David Taylor 2020-07-24 16:15:36 +01:00
parent 0ad4c3b3f8
commit ef36fc39da
No known key found for this signature in database
GPG Key ID: 46904C18B1D3F434
3 changed files with 45 additions and 8 deletions

View File

@ -229,7 +229,9 @@ after_initialize do
user = User.find_by_id(self.user_id)
created_by_user = User.find_by_id(self.created_by_id)
warning_topic = Topic.find_by_id(self.topic_id)
raw_note = I18n.t("user_notes.official_warning", username: created_by_user.username, warning_link: "[#{warning_topic.title}](#{warning_topic.url})")
raw_note = I18n.with_locale(SiteSetting.default_locale) do
I18n.t("user_notes.official_warning", username: created_by_user.username, warning_link: "[#{warning_topic.title}](#{warning_topic.url})")
end
::DiscourseUserNotes.add_note(
user,
raw_note,
@ -242,7 +244,13 @@ after_initialize do
return unless self.action == UserHistory.actions[:suspend_user]
target_user = User.find_by_id(self.target_user_id)
created_by_user = User.find_by_id(self.acting_user_id)
raw_note = I18n.t("user_notes.user_suspended", username: created_by_user.username, suspended_till: I18n.l(target_user.suspended_till, format: :date_only), reason: self.details)
raw_note = I18n.with_locale(SiteSetting.default_locale) do
I18n.t("user_notes.user_suspended",
username: created_by_user.username,
suspended_till: I18n.l(target_user.suspended_till, format: :date_only),
reason: self.details
)
end
::DiscourseUserNotes.add_note(
target_user,
raw_note,
@ -253,12 +261,14 @@ after_initialize do
end
on(:user_silenced) do |details|
raw_note = I18n.t(
"user_notes.user_silenced",
username: details[:silenced_by]&.username || '',
silenced_till: I18n.l(details[:silenced_till], format: :date_only),
reason: details[:reason]
)
raw_note = I18n.with_locale(SiteSetting.default_locale) do
I18n.t(
"user_notes.user_silenced",
username: details[:silenced_by]&.username || '',
silenced_till: I18n.l(details[:silenced_till], format: :date_only),
reason: details[:reason]
)
end
note_args = {}
if post = Post.with_deleted.where(id: details[:post_id]).first
note_args = { post_id: post.id, topic_id: post.topic_id }

View File

@ -17,6 +17,19 @@ describe UserHistory do
expect(PluginStore.get('user_notes', "notes:#{user.id}")).to be_present
end
it "should use system language" do
freeze_time
UserHistory.create!(action: UserHistory.actions[:suspend_user], target_user_id: user.id, acting_user_id: admin.id)
I18n.with_locale(:fr) do # Simulate request from french user
UserHistory.create!(action: UserHistory.actions[:suspend_user], target_user_id: user.id, acting_user_id: admin.id)
end
notes = PluginStore.get('user_notes', "notes:#{user.id}")
expect(notes[0]['raw']).to eq(notes[1]['raw'])
end
end
end
end

View File

@ -18,6 +18,20 @@ describe UserWarning do
expect(PluginStore.get('user_notes', "notes:#{user.id}")).to be_present
end
it "should use system language" do
freeze_time
warning = UserWarning.create!(topic_id: topic.id, user_id: user.id, created_by_id: admin.id)
warning.destroy!
I18n.with_locale(:fr) do # Simulate request from french user
UserWarning.create(topic_id: topic.id, user_id: user.id, created_by_id: admin.id)
end
notes = PluginStore.get('user_notes', "notes:#{user.id}")
expect(notes[0]['raw']).to eq(notes[1]['raw'])
end
end
end
end