Clean up User model

This commit is contained in:
Karina Kwiatek 2023-01-02 11:16:47 +01:00 committed by Andreas Nedbal
parent e35354a352
commit 4c158066cb
2 changed files with 32 additions and 57 deletions

View File

@ -8,6 +8,14 @@ class Report < ApplicationRecord
type.sub('Reports::', '').constantize.where(id: target_id).first
end
def append_reason(new_reason)
if reason.nil?
update(reason: new_reason)
else
update(reason: [reason || "", new_reason].join("\n"))
end
end
class << self
include CursorPaginatable

View File

@ -18,16 +18,15 @@ class User < ApplicationRecord
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :async, :registerable,
:recoverable, :rememberable, :trackable,
:validatable, :confirmable, :authentication_keys => [:login]
:validatable, :confirmable, authentication_keys: [:login]
has_one_time_password
enum otp_module: { disabled: 0, enabled: 1 }, _prefix: true
attr_accessor :otp_attempt, :otp_validation
attr_writer :login
rolify
# attr_accessor :login
has_many :questions, dependent: :destroy_async
has_many :answers, dependent: :destroy_async
has_many :comments, dependent: :destroy_async
@ -48,12 +47,12 @@ class User < ApplicationRecord
has_one :theme, dependent: :destroy
has_many :bans, class_name: "UserBan", dependent: :destroy_async
has_many :banned_users, class_name: 'UserBan',
foreign_key: 'banned_by_id',
has_many :banned_users, class_name: "UserBan",
foreign_key: "banned_by_id",
dependent: :nullify
SCREEN_NAME_REGEX = /\A[a-zA-Z0-9_]{1,16}\z/
WEBSITE_REGEX = /https?:\/\/([A-Za-z.\-]+)\/?(?:.*)/i
WEBSITE_REGEX = /https?:\/\/([A-Za-z.-]+)\/?(?:.*)/i
before_validation do
screen_name.strip!
@ -69,8 +68,7 @@ class User < ApplicationRecord
# when a user has been deleted, all reports relating to the user become invalid
before_destroy do
rep = Report.where(target_id: self.id, type: 'Reports::User')
rep.each do |r|
Report.where(target_id: id, type: "Reports::User").find_each do |r|
unless r.nil?
r.deleted = true
r.save
@ -85,18 +83,12 @@ class User < ApplicationRecord
# use the screen name as parameter for url helpers
def to_param = screen_name
def login=(login)
@login = login
end
def login
@login || self.screen_name || self.email
end
def login = @login || screen_name || email
def self.find_first_by_auth_conditions(warden_conditions)
conditions = warden_conditions.dup
if login = conditions.delete(:login)
where(conditions).where(["lower(screen_name) = :value OR lower(email) = :value", { :value => login.downcase }]).first
if (login = conditions.delete(:login))
where(conditions).where(["lower(screen_name) = :value OR lower(email) = :value", { value: login.downcase }]).first
else
where(conditions).first
end
@ -104,9 +96,7 @@ class User < ApplicationRecord
# @param list [List]
# @return [Boolean] true if +self+ is a member of +list+
def member_of?(list)
list_memberships.pluck(:list_id).include? list.id
end
def member_of?(list) = list_memberships.pluck(:list_id).include? list.id
# answers a question
# @param question [Question] the question to answer
@ -117,16 +107,12 @@ class User < ApplicationRecord
raise Errors::AnsweringSelfBlockedOther if self.blocking?(question.user)
# rubocop:enable Style/RedundantSelf
Answer.create!(content: content,
user: self,
question: question)
Answer.create!(content:, user: self, question:)
end
# has the user answered +question+ yet?
# @param question [Question]
def answered?(question)
question.answers.pluck(:user_id).include? self.id
end
def answered?(question) = question.answers.pluck(:user_id).include? id
def comment(answer, content)
# rubocop:disable Style/RedundantSelf
@ -134,49 +120,30 @@ class User < ApplicationRecord
raise Errors::CommentingOtherBlockedSelf if answer.user.blocking?(self)
# rubocop:enable Style/RedundantSelf
Comment.create!(user: self, answer: answer, content: content)
Comment.create!(user: self, answer:, content:)
end
# @return [Boolean] is the user a moderator?
def mod?
has_cached_role?(:moderator) || has_cached_role?(:administrator)
end
def mod? = has_cached_role?(:moderator) || has_cached_role?(:administrator)
def admin?
has_cached_role?(:administrator)
end
def admin? = has_cached_role?(:administrator)
# region stuff used for reporting/moderation
def report(object, reason = nil)
existing = Report.find_by(type: "Reports::#{object.class}", target_id: object.id, user_id: self.id, deleted: false)
existing = Report.find_by(type: "Reports::#{object.class}", target_id: object.id, user_id: id, deleted: false)
if existing.nil?
Report.create(type: "Reports::#{object.class}", target_id: object.id, user_id: self.id, reason: reason)
elsif not reason.nil? and reason.length > 0
if existing.reason.nil?
existing.update(reason: reason)
else
existing.update(reason: [existing.reason || "", reason].join("\n"))
end
else
existing
Report.create(type: "Reports::#{object.class}", target_id: object.id, user_id: id, reason:)
elsif !reason.nil? && reason.length.positive?
existing.append_reason(reason)
end
end
# endregion
def can_export?
unless self.export_created_at.nil?
return (Time.now > self.export_created_at.in(1.week)) && !self.export_processing
end
!self.export_processing
return (Time.zone.now > export_created_at.in(1.week)) && !export_processing unless export_created_at.nil?
!export_processing
end
def inbox_locked?
privacy_lock_inbox
end
# %w[admin moderator].each do |m|
# define_method(m) { raise "not allowed: #{m}" }
# define_method(m+??) { raise "not allowed: #{m}?"}
# define_method(m+?=) { |*a| raise "not allowed: #{m}="}
# end
def inbox_locked? = privacy_lock_inbox
end