Move ban-related user methods to own module

This commit is contained in:
Karina Kwiatek 2022-06-26 17:39:52 +02:00 committed by Karina Kwiatek
parent a533f68c65
commit 53361eefdd
2 changed files with 30 additions and 28 deletions

View File

@ -5,6 +5,7 @@ class User < ApplicationRecord
include User::Relationship::Follow include User::Relationship::Follow
include User::Relationship::Block include User::Relationship::Block
include User::AnswerMethods include User::AnswerMethods
include User::BanMethods
include User::InboxMethods include User::InboxMethods
include User::QuestionMethods include User::QuestionMethods
include User::RelationshipMethods include User::RelationshipMethods
@ -226,34 +227,6 @@ class User < ApplicationRecord
end end
# endregion # endregion
def permanently_banned?
bans.current.first&.permanent? || false
end
def banned?
self.bans.current.count > 0
end
def unban
bans.current.update(
# -1s to account for flakyness with timings in tests
expires_at: DateTime.now.utc - 1.second
)
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,
banned_by: banned_by,
reason: reason
)
end
def can_export? def can_export?
unless self.export_created_at.nil? unless self.export_created_at.nil?
return (Time.now > self.export_created_at.in(1.week)) && !self.export_processing return (Time.now > self.export_created_at.in(1.week)) && !self.export_processing

View File

@ -0,0 +1,29 @@
module User::BanMethods
def permanently_banned?
bans.current.first&.permanent? || false
end
def banned?
self.bans.current.count > 0
end
def unban
bans.current.update(
# -1s to account for flakyness with timings in tests
expires_at: DateTime.now.utc - 1.second
)
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,
banned_by: banned_by,
reason: reason
)
end
end