mirror of
https://github.com/discourse/discourse.git
synced 2024-11-22 09:42:02 +08:00
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:
parent
f5051ec833
commit
d49e96c6a3
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue
Block a user