2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-11-10 12:18:08 -05:00
|
|
|
class UserSilencer
|
2017-11-13 13:41:36 -05:00
|
|
|
attr_reader :user_history
|
|
|
|
|
2013-07-02 14:42:30 -04:00
|
|
|
def initialize(user, by_user = nil, opts = {})
|
|
|
|
@user, @by_user, @opts = user, by_user, opts
|
|
|
|
end
|
|
|
|
|
2017-11-10 12:18:08 -05:00
|
|
|
def self.silence(user, by_user = nil, opts = {})
|
|
|
|
UserSilencer.new(user, by_user, opts).silence
|
2013-07-02 14:42:30 -04:00
|
|
|
end
|
|
|
|
|
2017-11-10 12:18:08 -05:00
|
|
|
def self.unsilence(user, by_user = nil, opts = {})
|
|
|
|
UserSilencer.new(user, by_user, opts).unsilence
|
2013-07-02 14:42:30 -04:00
|
|
|
end
|
|
|
|
|
2019-02-07 13:46:05 -05:00
|
|
|
def self.was_silenced_for?(post)
|
2019-02-08 08:50:29 -05:00
|
|
|
return false if post.blank?
|
2019-02-07 13:46:05 -05:00
|
|
|
|
|
|
|
UserHistory.where(action: UserHistory.actions[:silence_user], post: post).exists?
|
|
|
|
end
|
|
|
|
|
2017-11-10 12:18:08 -05:00
|
|
|
def silence
|
2016-01-14 15:20:26 -05:00
|
|
|
hide_posts unless @opts[:keep_posts]
|
2023-02-16 04:40:11 -05:00
|
|
|
return false if @user.silenced_till.present?
|
|
|
|
@user.silenced_till = @opts[:silenced_till] || 1000.years.from_now
|
|
|
|
if @user.save
|
|
|
|
message_type = @opts[:message] || :silenced_by_staff
|
|
|
|
|
|
|
|
details = StaffMessageFormat.new(:silence, @opts[:reason], @opts[:message_body]).format
|
|
|
|
|
|
|
|
context = "#{message_type}: #{@opts[:reason]}"
|
|
|
|
|
|
|
|
if @by_user
|
|
|
|
log_params = { context: context, details: details }
|
|
|
|
log_params[:post_id] = @opts[:post_id].to_i if @opts[:post_id]
|
|
|
|
|
|
|
|
@user_history = StaffActionLogger.new(@by_user).log_silence_user(@user, log_params)
|
2013-08-02 10:55:02 -04:00
|
|
|
end
|
2023-02-16 04:40:11 -05:00
|
|
|
|
|
|
|
silence_message_params = {}
|
|
|
|
DiscourseEvent.trigger(
|
|
|
|
:user_silenced,
|
|
|
|
user: @user,
|
|
|
|
silenced_by: @by_user,
|
|
|
|
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,
|
|
|
|
silence_message_params: silence_message_params,
|
|
|
|
)
|
|
|
|
|
|
|
|
silence_message_params.merge!(post_alert_options: { skip_send_email: true })
|
|
|
|
SystemMessage.create(@user, message_type, silence_message_params)
|
|
|
|
true
|
2013-07-02 14:42:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def hide_posts
|
2016-09-12 11:58:10 -04:00
|
|
|
return unless @user.trust_level == TrustLevel[0]
|
|
|
|
|
2017-01-19 15:56:14 -05:00
|
|
|
Post
|
|
|
|
.where(user_id: @user.id)
|
|
|
|
.where("created_at > ?", 24.hours.ago)
|
|
|
|
.update_all(
|
2023-01-09 07:20:10 -05:00
|
|
|
[
|
2017-01-19 15:56:14 -05:00
|
|
|
"hidden = true, hidden_reason_id = COALESCE(hidden_reason_id, ?)",
|
|
|
|
Post.hidden_reasons[:new_user_spam_threshold_reached],
|
|
|
|
],
|
2023-01-09 07:20:10 -05:00
|
|
|
)
|
2017-01-19 15:56:14 -05:00
|
|
|
topic_ids =
|
|
|
|
Post
|
|
|
|
.where(user_id: @user.id, post_number: 1)
|
|
|
|
.where("created_at > ?", 24.hours.ago)
|
|
|
|
.pluck(:topic_id)
|
2016-03-18 07:16:37 -04:00
|
|
|
Topic.where(id: topic_ids).update_all(visible: false) unless topic_ids.empty?
|
2013-07-02 14:42:30 -04:00
|
|
|
end
|
|
|
|
|
2017-11-10 12:18:08 -05:00
|
|
|
def unsilence
|
2017-11-13 13:41:36 -05:00
|
|
|
@user.silenced_till = nil
|
2013-07-02 14:42:30 -04:00
|
|
|
if @user.save
|
2020-12-10 12:06:41 -05:00
|
|
|
DiscourseEvent.trigger(:user_unsilenced, user: @user, by_user: @by_user)
|
2017-11-10 12:18:08 -05:00
|
|
|
SystemMessage.create(@user, :unsilenced)
|
|
|
|
StaffActionLogger.new(@by_user).log_unsilence_user(@user) if @by_user
|
2013-07-02 14:42:30 -04:00
|
|
|
end
|
|
|
|
end
|
2015-04-24 13:10:43 -04:00
|
|
|
end
|