user.rb 1.21 KB
Newer Older
1 2
# frozen_string_literal: true

Eugen Rochko's avatar
Eugen Rochko committed
3
class User < ApplicationRecord
4
  include Settings::Extend
5

6 7
  devise :registerable, :recoverable,
         :rememberable, :trackable, :validatable, :confirmable,
8 9 10
         :two_factor_authenticatable, :two_factor_backupable,
         otp_secret_encryption_key: ENV['OTP_SECRET'],
         otp_number_of_backup_codes: 10
11

Eugen Rochko's avatar
Eugen Rochko committed
12
  belongs_to :account, inverse_of: :user
13
  accepts_nested_attributes_for :account
14

Eugen Rochko's avatar
Eugen Rochko committed
15
  validates :account, presence: true
Eugen Rochko's avatar
Eugen Rochko committed
16
  validates :locale, inclusion: I18n.available_locales.map(&:to_s), unless: 'locale.nil?'
17
  validates :email, email: true
18

Eugen Rochko's avatar
Eugen Rochko committed
19 20 21 22
  scope :prolific,  -> { joins('inner join statuses on statuses.account_id = users.account_id').select('users.*, count(statuses.id) as statuses_count').group('users.id').order('statuses_count desc') }
  scope :recent,    -> { order('id desc') }
  scope :admins,    -> { where(admin: true) }
  scope :confirmed, -> { where.not(confirmed_at: nil) }
23

24 25 26
  def send_devise_notification(notification, *args)
    devise_mailer.send(notification, self, *args).deliver_later
  end
27 28 29 30

  def setting_default_privacy
    settings.default_privacy || (account.locked? ? 'private' : 'public')
  end
31 32 33 34

  def setting_boost_modal
    settings.boost_modal
  end
Eugen Rochko's avatar
Eugen Rochko committed
35
end