2013-07-03 02:42:30 +08:00
|
|
|
class UserBlocker
|
|
|
|
|
|
|
|
def initialize(user, by_user=nil, opts={})
|
|
|
|
@user, @by_user, @opts = user, by_user, opts
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.block(user, by_user=nil, opts={})
|
|
|
|
UserBlocker.new(user, by_user, opts).block
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.unblock(user, by_user=nil, opts={})
|
|
|
|
UserBlocker.new(user, by_user, opts).unblock
|
|
|
|
end
|
|
|
|
|
|
|
|
def block
|
2016-01-15 04:20:26 +08:00
|
|
|
hide_posts unless @opts[:keep_posts]
|
2013-08-02 22:55:02 +08:00
|
|
|
unless @user.blocked?
|
|
|
|
@user.blocked = true
|
|
|
|
if @user.save
|
|
|
|
SystemMessage.create(@user, @opts[:message] || :blocked_by_staff)
|
2016-01-15 04:05:11 +08:00
|
|
|
StaffActionLogger.new(@by_user).log_block_user(@user) if @by_user
|
2013-08-02 22:55:02 +08:00
|
|
|
end
|
|
|
|
else
|
|
|
|
false
|
2013-07-03 02:42:30 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def hide_posts
|
2013-07-03 23:17:16 +08:00
|
|
|
Post.where(user_id: @user.id).update_all(["hidden = true, hidden_reason_id = COALESCE(hidden_reason_id, ?)", Post.hidden_reasons[:new_user_spam_threshold_reached]])
|
2013-07-03 02:42:30 +08:00
|
|
|
topic_ids = Post.where('user_id = ? and post_number = ?', @user.id, 1).pluck(:topic_id)
|
2013-07-04 04:04:22 +08:00
|
|
|
Topic.where(id: topic_ids).update_all({ visible: false }) unless topic_ids.empty?
|
2013-07-03 02:42:30 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def unblock
|
|
|
|
@user.blocked = false
|
|
|
|
if @user.save
|
|
|
|
SystemMessage.create(@user, :unblocked)
|
2016-01-15 04:05:11 +08:00
|
|
|
StaffActionLogger.new(@by_user).log_unblock_user(@user) if @by_user
|
2013-07-03 02:42:30 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-04-25 01:10:43 +08:00
|
|
|
end
|