Refactor existing ban-related methods

This commit is contained in:
Karina Kwiatek 2021-08-14 18:04:58 +02:00
parent e4241d2001
commit be0cf69368
4 changed files with 32 additions and 18 deletions

View File

@ -109,13 +109,15 @@ class Ajax::ModerationController < AjaxController
params.require :user params.require :user
params.require :ban params.require :ban
params.require :permaban params.require :permaban
params.require :duration
params.require :duration_unit
reason = params[:reason] duration = params[:duration].to_s
duration_unit = params[:duration_unit].to_s
reason = params[:reason].to_s
target = User.find_by_screen_name!(params[:user]) target = User.find_by_screen_name!(params[:user])
unban = params[:ban] == "0" unban = params[:ban] == '0'
perma = params[:permaban] == "1" perma = params[:permaban] == '1'
buntil = DateTime.strptime params[:until], "%m/%d/%Y %I:%M %p" unless unban || perma
if !unban && target.has_role?(:administrator) if !unban && target.has_role?(:administrator)
@response[:status] = :nopriv @response[:status] = :nopriv
@ -128,11 +130,11 @@ class Ajax::ModerationController < AjaxController
@response[:message] = I18n.t('messages.moderation.ban.unban') @response[:message] = I18n.t('messages.moderation.ban.unban')
@response[:success] = true @response[:success] = true
elsif perma elsif perma
target.ban nil, reason target.ban nil, nil, reason, current_user
@response[:message] = I18n.t('messages.moderation.ban.perma') @response[:message] = I18n.t('messages.moderation.ban.perma')
else else
target.ban buntil, reason target.ban duration, duration_unit, reason, current_user
@response[:message] = I18n.t('messages.moderation.ban.temp', date: buntil.to_s) @response[:message] = "User banned for #{duration} #{duration_unit}"
end end
target.save! target.save!
@ -162,7 +164,7 @@ class Ajax::ModerationController < AjaxController
@response[:checked] = status @response[:checked] = status
type = params[:type].downcase type = params[:type].downcase
target_role = {"admin" => "administrator"}.fetch(type, type).to_sym target_role = {'admin' => 'administrator'}.fetch(type, type).to_sym
if status if status
target_user.add_role target_role target_user.add_role target_role

View File

@ -223,21 +223,24 @@ class User < ApplicationRecord
end end
# endregion # endregion
# forwards fill
def banned? def banned?
self.permanently_banned? or ((not self.banned_until.nil?) and self.banned_until >= DateTime.current) self.user_bans.current.any?
end end
def unban def unban
self.update(permanently_banned: false, ban_reason: nil, banned_until: nil) self.user_bans.current.update(expires_at: DateTime.now)
end end
def ban(buntil=nil, reason=nil) # Bans a user.
if buntil == nil # @param duration [Fixnum, nil] Ban duration
self.update(permanently_banned: true, ban_reason: reason) # @param duration_unit [String, nil] Unit for the <code>duration</code> parameter. Accepted units: hours, days, weeks, months
else # @param reason [String] Reason for the ban. This is displayed to the user.
self.update(permanently_banned: false, banned_until: buntil, ban_reason: reason) # @param banned_by [User] User who instated the ban
end def ban(duration, duration_unit = 'hours', reason = nil, banned_by = nil)
raise Errors::InvalidBanDuration unless %w[hours days weeks months].include? duration_unit
expiry = duration && DateTime.now + duration.public_send(duration_unit)
self.user_bans.create(expires_at: expiry, reason: reason, banned_by: banned_by)
end end
def can_export? def can_export?

View File

@ -1,4 +1,6 @@
class UserBan < ApplicationRecord class UserBan < ApplicationRecord
belongs_to :user belongs_to :user
belongs_to :banned_by, class_name: 'User' belongs_to :banned_by, class_name: 'User'
scope :current, -> { where('expires_at IS NULL or expires_at < NOW()') }
end end

7
lib/errors.rb Normal file
View File

@ -0,0 +1,7 @@
module Errors
class Base < StandardError
end
class InvalidBanDuration < Base
end
end