2018-01-19 10:34:54 -06:00
|
|
|
require_dependency 'staff_message_format'
|
|
|
|
|
2017-11-10 11:18:08 -06:00
|
|
|
class UserSilencer
|
2013-07-02 13:42:30 -05:00
|
|
|
|
2017-11-13 12:41:36 -06:00
|
|
|
attr_reader :user_history
|
|
|
|
|
2017-07-27 20:20:09 -05:00
|
|
|
def initialize(user, by_user = nil, opts = {})
|
2013-07-02 13:42:30 -05:00
|
|
|
@user, @by_user, @opts = user, by_user, opts
|
|
|
|
end
|
|
|
|
|
2017-11-10 11:18:08 -06:00
|
|
|
def self.silence(user, by_user = nil, opts = {})
|
|
|
|
UserSilencer.new(user, by_user, opts).silence
|
2013-07-02 13:42:30 -05:00
|
|
|
end
|
|
|
|
|
2017-11-10 11:18:08 -06:00
|
|
|
def self.unsilence(user, by_user = nil, opts = {})
|
|
|
|
UserSilencer.new(user, by_user, opts).unsilence
|
2013-07-02 13:42:30 -05:00
|
|
|
end
|
|
|
|
|
2017-11-10 11:18:08 -06:00
|
|
|
def silence
|
2016-01-14 14:20:26 -06:00
|
|
|
hide_posts unless @opts[:keep_posts]
|
2017-11-13 12:41:36 -06:00
|
|
|
unless @user.silenced_till.present?
|
|
|
|
@user.silenced_till = @opts[:silenced_till] || 1000.years.from_now
|
2013-08-02 09:55:02 -05:00
|
|
|
if @user.save
|
2017-11-10 11:18:08 -06:00
|
|
|
message_type = @opts[:message] || :silenced_by_staff
|
2017-11-13 12:41:36 -06:00
|
|
|
|
2018-01-19 10:34:54 -06:00
|
|
|
details = StaffMessageFormat.new(
|
|
|
|
:silence,
|
|
|
|
@opts[:reason],
|
|
|
|
@opts[:message_body]
|
|
|
|
).format
|
2017-11-20 11:33:02 -06:00
|
|
|
|
|
|
|
context = "#{message_type}: '#{post.topic&.title rescue ''}' #{@opts[:reason]}"
|
|
|
|
SystemMessage.create(@user, message_type)
|
2017-11-13 12:41:36 -06:00
|
|
|
|
|
|
|
if @by_user
|
2018-02-13 14:33:15 -06:00
|
|
|
log_params = { context: context, details: details }
|
|
|
|
log_params[:post_id] = @opts[:post_id].to_i if @opts[:post_id]
|
|
|
|
|
2017-11-13 12:41:36 -06:00
|
|
|
@user_history = StaffActionLogger.new(@by_user).log_silence_user(
|
|
|
|
@user,
|
2018-02-13 14:33:15 -06:00
|
|
|
log_params
|
2017-11-13 12:41:36 -06:00
|
|
|
)
|
2017-01-10 15:45:36 -06:00
|
|
|
end
|
2018-01-11 11:56:28 -06:00
|
|
|
|
|
|
|
DiscourseEvent.trigger(
|
|
|
|
:user_silenced,
|
|
|
|
user: @user,
|
2018-03-29 15:43:52 -05:00
|
|
|
silenced_by: @by_user,
|
2018-01-11 11:56:28 -06:00
|
|
|
reason: @opts[:reason],
|
|
|
|
message: @opts[:message_body],
|
|
|
|
user_history: @user_history,
|
|
|
|
post_id: @opts[:post_id],
|
|
|
|
silenced_till: @user.silenced_till,
|
|
|
|
silenced_at: DateTime.now
|
|
|
|
)
|
2017-11-13 12:41:36 -06:00
|
|
|
return true
|
2013-08-02 09:55:02 -05:00
|
|
|
end
|
|
|
|
else
|
|
|
|
false
|
2013-07-02 13:42:30 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def hide_posts
|
2016-09-12 10:58:10 -05:00
|
|
|
return unless @user.trust_level == TrustLevel[0]
|
|
|
|
|
2017-01-19 14:56:14 -06:00
|
|
|
Post.where(user_id: @user.id).where("created_at > ?", 24.hours.ago).update_all(["hidden = true, hidden_reason_id = COALESCE(hidden_reason_id, ?)", Post.hidden_reasons[:new_user_spam_threshold_reached]])
|
|
|
|
topic_ids = Post.where(user_id: @user.id, post_number: 1).where("created_at > ?", 24.hours.ago).pluck(:topic_id)
|
2016-03-18 06:16:37 -05:00
|
|
|
Topic.where(id: topic_ids).update_all(visible: false) unless topic_ids.empty?
|
2013-07-02 13:42:30 -05:00
|
|
|
end
|
|
|
|
|
2017-11-10 11:18:08 -06:00
|
|
|
def unsilence
|
2017-11-13 12:41:36 -06:00
|
|
|
@user.silenced_till = nil
|
2013-07-02 13:42:30 -05:00
|
|
|
if @user.save
|
2017-11-10 11:18:08 -06:00
|
|
|
SystemMessage.create(@user, :unsilenced)
|
|
|
|
StaffActionLogger.new(@by_user).log_unsilence_user(@user) if @by_user
|
2013-07-02 13:42:30 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-04-24 12:10:43 -05:00
|
|
|
end
|