2022-06-26 08:43:34 -07:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-06-26 08:39:52 -07:00
|
|
|
module User::BanMethods
|
|
|
|
def permanently_banned?
|
|
|
|
bans.current.first&.permanent? || false
|
|
|
|
end
|
|
|
|
|
|
|
|
def banned?
|
2024-01-25 12:15:04 -08:00
|
|
|
Rails.cache.fetch("#{cache_key}/banned", expires_in: 6.hours) do
|
2023-11-27 13:30:10 -08:00
|
|
|
bans.current.count.positive?
|
|
|
|
end
|
2022-06-26 08:39:52 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
def unban
|
|
|
|
bans.current.update(
|
|
|
|
# -1s to account for flakyness with timings in tests
|
2023-11-27 13:30:10 -08:00
|
|
|
expires_at: DateTime.now.utc - 1.second,
|
2022-06-26 08:39:52 -07:00
|
|
|
)
|
2023-11-27 13:30:10 -08:00
|
|
|
Rails.cache.delete("#{cache_key}/banned")
|
2022-06-26 08:39:52 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
# Bans a user.
|
|
|
|
# @param expiry [DateTime, nil] the expiry time of the ban
|
|
|
|
# @param reason [String, nil] Reason for the ban. This is displayed to the user.
|
|
|
|
# @param banned_by [User] User who instated the ban
|
|
|
|
def ban(expiry = nil, reason = nil, banned_by = nil)
|
|
|
|
::UserBan.create!(
|
|
|
|
user: self,
|
|
|
|
expires_at: expiry,
|
2023-11-27 13:30:10 -08:00
|
|
|
banned_by:,
|
|
|
|
reason:,
|
2022-06-26 08:39:52 -07:00
|
|
|
)
|
2023-11-27 13:30:10 -08:00
|
|
|
Rails.cache.delete("#{cache_key}/banned")
|
2022-06-26 08:39:52 -07:00
|
|
|
end
|
|
|
|
end
|