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 type.sub('Reports::', '').constantize.where(id: target_id).first
end 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 class << self
include CursorPaginatable include CursorPaginatable

View File

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