From a8b46df4bd5eb6ea49df4394fa6800d8a1bee4df Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Fri, 19 Jan 2018 11:34:54 -0500 Subject: [PATCH] More extensibility for custom silence/suspend messages --- app/services/staff_action_logger.rb | 5 +++-- app/services/user_silencer.rb | 9 +++++++-- lib/moderator_message_format.rb | 5 +++++ lib/staff_message_format.rb | 23 +++++++++++++++++++++++ 4 files changed, 38 insertions(+), 4 deletions(-) create mode 100644 lib/moderator_message_format.rb create mode 100644 lib/staff_message_format.rb diff --git a/app/services/staff_action_logger.rb b/app/services/staff_action_logger.rb index 924bed26efc..eb5d87abf14 100644 --- a/app/services/staff_action_logger.rb +++ b/app/services/staff_action_logger.rb @@ -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], diff --git a/app/services/user_silencer.rb b/app/services/user_silencer.rb index 5ca1769641c..5db360637b8 100644 --- a/app/services/user_silencer.rb +++ b/app/services/user_silencer.rb @@ -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) diff --git a/lib/moderator_message_format.rb b/lib/moderator_message_format.rb new file mode 100644 index 00000000000..e499c2e2009 --- /dev/null +++ b/lib/moderator_message_format.rb @@ -0,0 +1,5 @@ +# Use this class to format messages for when an +class ModeratorMessageFormat + def initialize(reason, message = nil) + end +end diff --git a/lib/staff_message_format.rb b/lib/staff_message_format.rb new file mode 100644 index 00000000000..e6005d0fac1 --- /dev/null +++ b/lib/staff_message_format.rb @@ -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