2019-11-30 13:39:55 +00:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: users
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
|
|
|
# confirmation_sent_at :datetime
|
|
|
|
# confirmation_token :string
|
|
|
|
# confirmed_at :datetime
|
|
|
|
# current_sign_in_at :datetime
|
|
|
|
# current_sign_in_ip :string
|
|
|
|
# email :string
|
|
|
|
# encrypted_password :string default(""), not null
|
|
|
|
# last_sign_in_at :datetime
|
|
|
|
# last_sign_in_ip :string
|
|
|
|
# name :string not null
|
|
|
|
# nickname :string
|
|
|
|
# provider :string default("email"), not null
|
|
|
|
# pubsub_token :string
|
|
|
|
# remember_created_at :datetime
|
|
|
|
# reset_password_sent_at :datetime
|
|
|
|
# reset_password_token :string
|
|
|
|
# sign_in_count :integer default(0), not null
|
|
|
|
# tokens :json
|
|
|
|
# uid :string default(""), not null
|
|
|
|
# unconfirmed_email :string
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# index_users_on_email (email)
|
|
|
|
# index_users_on_pubsub_token (pubsub_token) UNIQUE
|
|
|
|
# index_users_on_reset_password_token (reset_password_token) UNIQUE
|
|
|
|
# index_users_on_uid_and_provider (uid,provider) UNIQUE
|
|
|
|
#
|
|
|
|
|
2019-08-14 09:48:44 +00:00
|
|
|
class User < ApplicationRecord
|
2020-03-10 18:32:15 +00:00
|
|
|
include AccessTokenable
|
|
|
|
include AvailabilityStatusable
|
|
|
|
include Avatarable
|
2019-08-14 09:48:44 +00:00
|
|
|
# Include default devise modules.
|
|
|
|
include DeviseTokenAuth::Concerns::User
|
|
|
|
include Events::Types
|
2019-10-16 22:18:48 +00:00
|
|
|
include Pubsubable
|
2019-12-15 18:23:04 +00:00
|
|
|
include Rails.application.routes.url_helpers
|
2020-03-18 11:23:35 +00:00
|
|
|
include Reportable
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2019-10-20 08:47:26 +00:00
|
|
|
devise :database_authenticatable,
|
|
|
|
:registerable,
|
|
|
|
:recoverable,
|
|
|
|
:rememberable,
|
|
|
|
:trackable,
|
|
|
|
:validatable,
|
|
|
|
:confirmable
|
|
|
|
|
2019-12-01 10:12:15 +00:00
|
|
|
# The validation below has been commented out as it does not
|
|
|
|
# work because :validatable in devise overrides this.
|
|
|
|
# validates_uniqueness_of :email, scope: :account_id
|
2020-03-07 06:48:16 +00:00
|
|
|
validates :email, :name, presence: true
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2020-03-07 06:48:16 +00:00
|
|
|
has_many :account_users, dependent: :destroy
|
|
|
|
has_many :accounts, through: :account_users
|
|
|
|
accepts_nested_attributes_for :account_users
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2019-10-20 08:47:26 +00:00
|
|
|
has_many :assigned_conversations, foreign_key: 'assignee_id', class_name: 'Conversation', dependent: :nullify
|
2019-08-14 09:48:44 +00:00
|
|
|
has_many :inbox_members, dependent: :destroy
|
|
|
|
has_many :assigned_inboxes, through: :inbox_members, source: :inbox
|
|
|
|
has_many :messages
|
2020-03-07 06:48:16 +00:00
|
|
|
has_many :invitees, through: :account_users, class_name: 'User', foreign_key: 'inviter_id', dependent: :nullify
|
2020-05-01 09:23:43 +00:00
|
|
|
|
|
|
|
has_many :notifications, dependent: :destroy
|
2020-02-29 15:11:09 +00:00
|
|
|
has_many :notification_settings, dependent: :destroy
|
2020-05-01 09:23:43 +00:00
|
|
|
has_many :notification_subscriptions, dependent: :destroy
|
2019-08-14 09:48:44 +00:00
|
|
|
|
|
|
|
before_validation :set_password_and_uid, on: :create
|
|
|
|
|
2020-03-10 18:32:15 +00:00
|
|
|
after_create :notify_creation, :create_access_token
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2019-08-19 08:19:57 +00:00
|
|
|
after_destroy :notify_deletion
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2019-12-24 21:33:02 +00:00
|
|
|
def send_devise_notification(notification, *args)
|
|
|
|
devise_mailer.send(notification, self, *args).deliver_later
|
|
|
|
end
|
|
|
|
|
2019-08-14 09:48:44 +00:00
|
|
|
def set_password_and_uid
|
2019-10-20 08:47:26 +00:00
|
|
|
self.uid = email
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
2020-03-07 06:48:16 +00:00
|
|
|
def account_user
|
|
|
|
# FIXME : temporary hack to transition over to multiple accounts per user
|
|
|
|
# We should be fetching the current account user relationship here.
|
|
|
|
account_users&.first
|
|
|
|
end
|
|
|
|
|
|
|
|
def account
|
|
|
|
account_user&.account
|
|
|
|
end
|
|
|
|
|
|
|
|
def administrator?
|
|
|
|
account_user&.administrator?
|
|
|
|
end
|
|
|
|
|
|
|
|
def agent?
|
|
|
|
account_user&.agent?
|
|
|
|
end
|
|
|
|
|
|
|
|
def role
|
|
|
|
account_user&.role
|
|
|
|
end
|
|
|
|
|
|
|
|
def inviter
|
|
|
|
account_user&.inviter
|
|
|
|
end
|
|
|
|
|
2019-08-14 09:48:44 +00:00
|
|
|
def serializable_hash(options = nil)
|
2019-11-24 13:39:17 +00:00
|
|
|
serialized_user = super(options).merge(confirmed: confirmed?)
|
|
|
|
serialized_user.merge(subscription: account.try(:subscription).try(:summary)) if ENV['BILLING_ENABLED']
|
|
|
|
serialized_user
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def notify_creation
|
2019-10-20 08:47:26 +00:00
|
|
|
Rails.configuration.dispatcher.dispatch(AGENT_ADDED, Time.zone.now, account: account)
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def notify_deletion
|
2019-10-20 08:47:26 +00:00
|
|
|
Rails.configuration.dispatcher.dispatch(AGENT_REMOVED, Time.zone.now, account: account)
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def push_event_data
|
|
|
|
{
|
2020-05-05 18:40:56 +00:00
|
|
|
id: id,
|
2019-12-15 18:23:04 +00:00
|
|
|
name: name,
|
2020-05-03 06:47:27 +00:00
|
|
|
avatar_url: avatar_url,
|
|
|
|
type: 'user'
|
2019-08-14 09:48:44 +00:00
|
|
|
}
|
|
|
|
end
|
2020-02-26 04:14:24 +00:00
|
|
|
|
|
|
|
def webhook_data
|
|
|
|
{
|
|
|
|
id: id,
|
|
|
|
name: name,
|
2020-05-03 06:47:27 +00:00
|
|
|
email: email,
|
|
|
|
type: 'user'
|
2020-02-26 04:14:24 +00:00
|
|
|
}
|
|
|
|
end
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|