DEV: add plugin hooks for silence message parameters (#10538)

DEV: add plugin hooks for silence message parameters

Allows plugins to add, and update extra silence message params for custom
i18n vars

Allows plugins to override system messages via `message_title` and
`message_raw` parameters. We can later expose these params where necessary via event
hooks. Expose the parameter for the on user_silenced trigger.
This commit is contained in:
Jeff Wong 2020-09-01 14:25:24 -10:00 committed by GitHub
parent f5051ec833
commit d49e96c6a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 46 additions and 7 deletions

View File

@ -36,7 +36,6 @@ class UserSilencer
).format
context = "#{message_type}: '#{post.topic&.title rescue ''}' #{@opts[:reason]}"
SystemMessage.create(@user, message_type)
if @by_user
log_params = { context: context, details: details }
@ -48,6 +47,7 @@ class UserSilencer
)
end
silence_message_params = {}
DiscourseEvent.trigger(
:user_silenced,
user: @user,
@ -57,8 +57,11 @@ class UserSilencer
user_history: @user_history,
post_id: @opts[:post_id],
silenced_till: @user.silenced_till,
silenced_at: DateTime.now
silenced_at: DateTime.now,
silence_message_params: silence_message_params
)
SystemMessage.create(@user, message_type, silence_message_params)
true
end
else

View File

@ -21,8 +21,8 @@ class SystemMessage
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 = params[:message_title] || I18n.with_locale(@recipient.effective_locale) { I18n.t("system_messages.#{type}.subject_template", params) }
raw = params[:message_raw] || I18n.with_locale(@recipient.effective_locale) { I18n.t("system_messages.#{type}.text_body_template", params) }
if from_system
user = Discourse.system_user

View File

@ -55,6 +55,19 @@ describe SystemMessage do
).count).to eq(0)
end
it 'allows message_title and message_raw ops to override content' do
user = Fabricate(:user)
system_user = Discourse.system_user
post = SystemMessage.create_from_system_user(user, :welcome_invite, { message_title: "override title", message_raw: "override body" })
topic = post.topic
expect(topic.private_message?).to eq(true)
expect(topic.title).to eq("override title")
expect(topic.subtype).to eq(TopicSubtype.system_message)
expect(post.raw).to eq("override body")
end
it 'should allow site_contact_group_name' do
group = Fabricate(:group)
SiteSetting.site_contact_group_name = group.name

View File

@ -26,7 +26,7 @@ describe UserSilencer do
context 'given a staff user argument' do
it 'sends the correct message to the silenced user' do
SystemMessage.unstub(:create)
SystemMessage.expects(:create).with(user, :silenced_by_staff).returns(true)
SystemMessage.expects(:create).with(user, :silenced_by_staff, {}).returns(true)
UserSilencer.silence(user, Fabricate.build(:admin))
end
end
@ -34,7 +34,7 @@ describe UserSilencer do
context 'not given a staff user argument' do
it 'sends a default message to the user' do
SystemMessage.unstub(:create)
SystemMessage.expects(:create).with(user, :silenced_by_staff).returns(true)
SystemMessage.expects(:create).with(user, :silenced_by_staff, {}).returns(true)
UserSilencer.silence(user, Fabricate.build(:admin))
end
end
@ -42,7 +42,7 @@ describe UserSilencer do
context 'given a message option' do
it 'sends that message to the user' do
SystemMessage.unstub(:create)
SystemMessage.expects(:create).with(user, :the_custom_message).returns(true)
SystemMessage.expects(:create).with(user, :the_custom_message, {}).returns(true)
UserSilencer.silence(user, Fabricate.build(:admin), message: :the_custom_message)
end
end
@ -68,6 +68,29 @@ describe UserSilencer do
}.to change { UserHistory.count }.by(1)
expect(UserHistory.last.context).to be_present
end
context 'with a plugin hook' do
before do
@override_silence_message = -> (opts) do
opts[:silence_message_params][:message_title] = "override title"
opts[:silence_message_params][:message_raw] = "override raw"
end
DiscourseEvent.on(:user_silenced, &@override_silence_message)
end
after do
DiscourseEvent.off(:user_silenced, &@override_silence_message)
end
it 'allows the message to be overridden' do
SystemMessage.unstub(:create)
UserSilencer.silence(user, Fabricate.build(:admin))
post = Discourse.system_user.posts.last
expect(post.topic.title).to eq("override title")
expect(post.raw).to eq("override raw")
end
end
end
describe 'unsilence' do