2013-07-02 14:42:30 -04:00
|
|
|
class UserBlocker
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def initialize(user, by_user = nil, opts = {})
|
2013-07-02 14:42:30 -04:00
|
|
|
@user, @by_user, @opts = user, by_user, opts
|
|
|
|
end
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def self.block(user, by_user = nil, opts = {})
|
2013-07-02 14:42:30 -04:00
|
|
|
UserBlocker.new(user, by_user, opts).block
|
|
|
|
end
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def self.unblock(user, by_user = nil, opts = {})
|
2013-07-02 14:42:30 -04:00
|
|
|
UserBlocker.new(user, by_user, opts).unblock
|
|
|
|
end
|
|
|
|
|
|
|
|
def block
|
2016-01-14 15:20:26 -05:00
|
|
|
hide_posts unless @opts[:keep_posts]
|
2013-08-02 10:55:02 -04:00
|
|
|
unless @user.blocked?
|
|
|
|
@user.blocked = true
|
|
|
|
if @user.save
|
2017-01-10 16:45:36 -05:00
|
|
|
message_type = @opts[:message] || :blocked_by_staff
|
|
|
|
post = SystemMessage.create(@user, message_type)
|
|
|
|
if post && @by_user
|
2017-07-27 21:20:09 -04:00
|
|
|
StaffActionLogger.new(@by_user).log_block_user(@user, context: "#{message_type}: '#{post.topic&.title rescue ''}' #{@opts[:reason]}")
|
2017-01-10 16:45:36 -05:00
|
|
|
end
|
2013-08-02 10:55:02 -04:00
|
|
|
end
|
|
|
|
else
|
|
|
|
false
|
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(["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 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
|
|
|
|
|
|
|
|
def unblock
|
|
|
|
@user.blocked = false
|
|
|
|
if @user.save
|
|
|
|
SystemMessage.create(@user, :unblocked)
|
2016-01-14 15:05:11 -05:00
|
|
|
StaffActionLogger.new(@by_user).log_unblock_user(@user) if @by_user
|
2013-07-02 14:42:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-04-24 13:10:43 -04:00
|
|
|
end
|