user.rb 1.13 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 8
  devise :registerable, :recoverable,
         :rememberable, :trackable, :validatable, :confirmable,
         :two_factor_authenticatable, otp_secret_encryption_key: ENV['OTP_SECRET']
9

Eugen Rochko's avatar
Eugen Rochko committed
10
  belongs_to :account, inverse_of: :user
11
  accepts_nested_attributes_for :account
12

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

Eugen Rochko's avatar
Eugen Rochko committed
17 18 19 20
  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) }
21

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

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

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