mirror of
https://github.com/Retrospring/retrospring.git
synced 2024-11-20 14:19:53 +01:00
Refactor existing ban-related methods
This commit is contained in:
parent
e4241d2001
commit
be0cf69368
4 changed files with 32 additions and 18 deletions
|
@ -109,13 +109,15 @@ class Ajax::ModerationController < AjaxController
|
|||
params.require :user
|
||||
params.require :ban
|
||||
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])
|
||||
unban = params[:ban] == "0"
|
||||
perma = params[:permaban] == "1"
|
||||
|
||||
buntil = DateTime.strptime params[:until], "%m/%d/%Y %I:%M %p" unless unban || perma
|
||||
unban = params[:ban] == '0'
|
||||
perma = params[:permaban] == '1'
|
||||
|
||||
if !unban && target.has_role?(:administrator)
|
||||
@response[:status] = :nopriv
|
||||
|
@ -128,11 +130,11 @@ class Ajax::ModerationController < AjaxController
|
|||
@response[:message] = I18n.t('messages.moderation.ban.unban')
|
||||
@response[:success] = true
|
||||
elsif perma
|
||||
target.ban nil, reason
|
||||
target.ban nil, nil, reason, current_user
|
||||
@response[:message] = I18n.t('messages.moderation.ban.perma')
|
||||
else
|
||||
target.ban buntil, reason
|
||||
@response[:message] = I18n.t('messages.moderation.ban.temp', date: buntil.to_s)
|
||||
target.ban duration, duration_unit, reason, current_user
|
||||
@response[:message] = "User banned for #{duration} #{duration_unit}"
|
||||
end
|
||||
target.save!
|
||||
|
||||
|
@ -162,7 +164,7 @@ class Ajax::ModerationController < AjaxController
|
|||
|
||||
@response[:checked] = status
|
||||
type = params[:type].downcase
|
||||
target_role = {"admin" => "administrator"}.fetch(type, type).to_sym
|
||||
target_role = {'admin' => 'administrator'}.fetch(type, type).to_sym
|
||||
|
||||
if status
|
||||
target_user.add_role target_role
|
||||
|
|
|
@ -223,21 +223,24 @@ class User < ApplicationRecord
|
|||
end
|
||||
# endregion
|
||||
|
||||
# forwards fill
|
||||
def banned?
|
||||
self.permanently_banned? or ((not self.banned_until.nil?) and self.banned_until >= DateTime.current)
|
||||
self.user_bans.current.any?
|
||||
end
|
||||
|
||||
def unban
|
||||
self.update(permanently_banned: false, ban_reason: nil, banned_until: nil)
|
||||
self.user_bans.current.update(expires_at: DateTime.now)
|
||||
end
|
||||
|
||||
def ban(buntil=nil, reason=nil)
|
||||
if buntil == nil
|
||||
self.update(permanently_banned: true, ban_reason: reason)
|
||||
else
|
||||
self.update(permanently_banned: false, banned_until: buntil, ban_reason: reason)
|
||||
end
|
||||
# Bans a user.
|
||||
# @param duration [Fixnum, nil] Ban duration
|
||||
# @param duration_unit [String, nil] Unit for the <code>duration</code> parameter. Accepted units: hours, days, weeks, months
|
||||
# @param reason [String] Reason for the ban. This is displayed to the user.
|
||||
# @param banned_by [User] User who instated the ban
|
||||
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
|
||||
|
||||
def can_export?
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
class UserBan < ApplicationRecord
|
||||
belongs_to :user
|
||||
belongs_to :banned_by, class_name: 'User'
|
||||
|
||||
scope :current, -> { where('expires_at IS NULL or expires_at < NOW()') }
|
||||
end
|
||||
|
|
7
lib/errors.rb
Normal file
7
lib/errors.rb
Normal file
|
@ -0,0 +1,7 @@
|
|||
module Errors
|
||||
class Base < StandardError
|
||||
end
|
||||
|
||||
class InvalidBanDuration < Base
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue