discourse/app/services/user_silencer.rb

48 lines
1.5 KiB
Ruby
Raw Normal View History

2017-11-10 12:18:08 -05:00
class UserSilencer
2017-07-27 21:20:09 -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
end
2017-11-10 12:18:08 -05:00
def self.unsilence(user, by_user = nil, opts = {})
UserSilencer.new(user, by_user, opts).unsilence
end
2017-11-10 12:18:08 -05:00
def silence
hide_posts unless @opts[:keep_posts]
2017-11-10 12:18:08 -05:00
unless @user.silenced?
@user.silenced = true
if @user.save
2017-11-10 12:18:08 -05:00
message_type = @opts[:message] || :silenced_by_staff
post = SystemMessage.create(@user, message_type)
if post && @by_user
2017-11-10 12:18:08 -05:00
StaffActionLogger.new(@by_user).log_silence_user(@user, context: "#{message_type}: '#{post.topic&.title rescue ''}' #{@opts[:reason]}")
end
end
else
false
end
end
def hide_posts
return unless @user.trust_level == TrustLevel[0]
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)
Topic.where(id: topic_ids).update_all(visible: false) unless topic_ids.empty?
end
2017-11-10 12:18:08 -05:00
def unsilence
@user.silenced = false
if @user.save
2017-11-10 12:18:08 -05:00
SystemMessage.create(@user, :unsilenced)
StaffActionLogger.new(@by_user).log_unsilence_user(@user) if @by_user
end
end
end