More extensibility for custom silence/suspend messages

This commit is contained in:
Robin Ward 2018-01-19 11:34:54 -05:00
parent 3cec2394b2
commit a8b46df4bd
4 changed files with 38 additions and 4 deletions

View File

@ -1,3 +1,5 @@
require_dependency 'staff_message_format'
# Responsible for logging the actions of admins and moderators.
class StaffActionLogger
@ -170,8 +172,7 @@ class StaffActionLogger
def log_user_suspend(user, reason, opts = {})
raise Discourse::InvalidParameters.new(:user) unless user
details = (reason || '').dup
details << "\n\n#{opts[:message]}" if opts[:message].present?
details = StaffMessageFormat.new(:suspend, reason, opts[:message]).format
args = params(opts).merge(
action: UserHistory.actions[:suspend_user],

View File

@ -1,3 +1,5 @@
require_dependency 'staff_message_format'
class UserSilencer
attr_reader :user_history
@ -21,8 +23,11 @@ class UserSilencer
if @user.save
message_type = @opts[:message] || :silenced_by_staff
details = (@opts[:reason] || '').dup
details << "\n\n#{@opts[:message_body]}" if @opts[:message_body].present?
details = StaffMessageFormat.new(
:silence,
@opts[:reason],
@opts[:message_body]
).format
context = "#{message_type}: '#{post.topic&.title rescue ''}' #{@opts[:reason]}"
SystemMessage.create(@user, message_type)

View File

@ -0,0 +1,5 @@
# Use this class to format messages for when an
class ModeratorMessageFormat
def initialize(reason, message = nil)
end
end

View File

@ -0,0 +1,23 @@
# This is used for formatting Suspension/Silencing messages.
# It can be extended by plugins to provide custom message formats.
class StaffMessageFormat
def initialize(type, reason, message = nil)
@type = type
@reason = reason
@message = message
after_initialize
end
# Plugins can overwrite this to munge values before formatting
def after_initialize
end
# Overwrite this to change formatting
def format
result = ""
result << @reason if @reason.present?
result << "\n\n#{@message}" if @message.present?
result
end
end