2019-11-30 13:39:55 +00:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: users
|
|
|
|
#
|
2022-02-15 06:40:53 +00:00
|
|
|
# id :integer not null, primary key
|
|
|
|
# availability :integer default("online")
|
|
|
|
# confirmation_sent_at :datetime
|
|
|
|
# confirmation_token :string
|
|
|
|
# confirmed_at :datetime
|
|
|
|
# current_sign_in_at :datetime
|
|
|
|
# current_sign_in_ip :string
|
|
|
|
# custom_attributes :jsonb
|
|
|
|
# display_name :string
|
|
|
|
# email :string
|
|
|
|
# encrypted_password :string default(""), not null
|
|
|
|
# last_sign_in_at :datetime
|
|
|
|
# last_sign_in_ip :string
|
|
|
|
# message_signature :text
|
|
|
|
# name :string not null
|
|
|
|
# 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
|
|
|
|
# type :string
|
|
|
|
# ui_settings :jsonb
|
|
|
|
# uid :string default(""), not null
|
|
|
|
# unconfirmed_email :string
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
2019-11-30 13:39:55 +00:00
|
|
|
#
|
|
|
|
# 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 Avatarable
|
2019-08-14 09:48:44 +00:00
|
|
|
# Include default devise modules.
|
|
|
|
include DeviseTokenAuth::Concerns::User
|
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
|
2020-11-25 08:29:38 +00:00
|
|
|
include SsoAuthenticatable
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2019-10-20 08:47:26 +00:00
|
|
|
devise :database_authenticatable,
|
|
|
|
:registerable,
|
|
|
|
:recoverable,
|
|
|
|
:rememberable,
|
|
|
|
:trackable,
|
|
|
|
:validatable,
|
2021-06-07 11:56:08 +00:00
|
|
|
:confirmable,
|
|
|
|
:password_has_required_content
|
2019-10-20 08:47:26 +00:00
|
|
|
|
2021-10-07 07:51:46 +00:00
|
|
|
# TODO: remove in a future version once online status is moved to account users
|
|
|
|
# remove the column availability from users
|
2020-07-04 06:12:47 +00:00
|
|
|
enum availability: { online: 0, offline: 1, busy: 2 }
|
|
|
|
|
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-07-26 07:24:50 +00:00
|
|
|
|
2020-03-07 06:48:16 +00:00
|
|
|
validates :email, :name, presence: true
|
2022-02-02 22:21:17 +00:00
|
|
|
validates_length_of :name, minimum: 1, maximum: 255
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2021-11-18 05:02:29 +00:00
|
|
|
has_many :account_users, dependent: :destroy_async
|
2020-03-07 06:48:16 +00:00
|
|
|
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
|
2020-11-16 14:11:52 +00:00
|
|
|
alias_attribute :conversations, :assigned_conversations
|
2021-06-29 15:29:41 +00:00
|
|
|
has_many :csat_survey_responses, foreign_key: 'assigned_agent_id', dependent: :nullify
|
2020-11-16 14:11:52 +00:00
|
|
|
|
2021-11-18 05:02:29 +00:00
|
|
|
has_many :inbox_members, dependent: :destroy_async
|
2020-05-26 17:08:48 +00:00
|
|
|
has_many :inboxes, through: :inbox_members, source: :inbox
|
2020-06-27 16:04:53 +00:00
|
|
|
has_many :messages, as: :sender
|
2021-10-15 18:41:53 +00:00
|
|
|
has_many :invitees, through: :account_users, class_name: 'User', foreign_key: 'inviter_id', source: :inviter, dependent: :nullify
|
2020-05-01 09:23:43 +00:00
|
|
|
|
2021-12-09 05:50:14 +00:00
|
|
|
has_many :custom_filters, dependent: :destroy_async
|
|
|
|
has_many :mentions, dependent: :destroy_async
|
|
|
|
has_many :notes, dependent: :nullify
|
2021-11-18 05:02:29 +00:00
|
|
|
has_many :notification_settings, dependent: :destroy_async
|
|
|
|
has_many :notification_subscriptions, dependent: :destroy_async
|
2021-12-09 05:50:14 +00:00
|
|
|
has_many :notifications, dependent: :destroy_async
|
2021-11-18 05:02:29 +00:00
|
|
|
has_many :team_members, dependent: :destroy_async
|
2021-01-17 18:26:56 +00:00
|
|
|
has_many :teams, through: :team_members
|
2022-05-16 08:29:59 +00:00
|
|
|
has_many :portals, through: :portals_members
|
2019-08-14 09:48:44 +00:00
|
|
|
|
|
|
|
before_validation :set_password_and_uid, on: :create
|
|
|
|
|
2020-08-22 16:50:22 +00:00
|
|
|
scope :order_by_full_name, -> { order('lower(name) ASC') }
|
|
|
|
|
2019-12-24 21:33:02 +00:00
|
|
|
def send_devise_notification(notification, *args)
|
2021-06-08 17:15:01 +00:00
|
|
|
devise_mailer.with(account: Current.account).send(notification, self, *args).deliver_later
|
2019-12-24 21:33:02 +00:00
|
|
|
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-05-26 17:08:48 +00:00
|
|
|
def active_account_user
|
|
|
|
account_users.order(active_at: :desc)&.first
|
|
|
|
end
|
|
|
|
|
|
|
|
def current_account_user
|
2022-03-17 10:27:32 +00:00
|
|
|
# We want to avoid subsequent queries in case where the association is preloaded.
|
|
|
|
# using where here will trigger n+1 queries.
|
|
|
|
account_users.find { |ac_usr| ac_usr.account_id == Current.account.id } if Current.account
|
2020-03-07 06:48:16 +00:00
|
|
|
end
|
|
|
|
|
2020-07-27 16:49:26 +00:00
|
|
|
def available_name
|
2020-07-20 12:58:14 +00:00
|
|
|
self[:display_name].presence || name
|
|
|
|
end
|
|
|
|
|
2021-09-02 12:59:45 +00:00
|
|
|
# Used internally for Chatwoot in Chatwoot
|
2021-02-08 11:08:35 +00:00
|
|
|
def hmac_identifier
|
|
|
|
hmac_key = GlobalConfig.get('CHATWOOT_INBOX_HMAC_KEY')['CHATWOOT_INBOX_HMAC_KEY']
|
|
|
|
return OpenSSL::HMAC.hexdigest('sha256', hmac_key, email) if hmac_key.present?
|
|
|
|
|
|
|
|
''
|
|
|
|
end
|
|
|
|
|
2020-03-07 06:48:16 +00:00
|
|
|
def account
|
2020-05-26 17:08:48 +00:00
|
|
|
current_account_user&.account
|
|
|
|
end
|
|
|
|
|
|
|
|
def assigned_inboxes
|
2021-06-11 06:14:31 +00:00
|
|
|
administrator? ? Current.account.inboxes : inboxes.where(account_id: Current.account.id)
|
2020-03-07 06:48:16 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def administrator?
|
2020-05-26 17:08:48 +00:00
|
|
|
current_account_user&.administrator?
|
2020-03-07 06:48:16 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def agent?
|
2020-05-26 17:08:48 +00:00
|
|
|
current_account_user&.agent?
|
2020-03-07 06:48:16 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def role
|
2020-05-26 17:08:48 +00:00
|
|
|
current_account_user&.role
|
2020-03-07 06:48:16 +00:00
|
|
|
end
|
|
|
|
|
2021-10-07 07:51:46 +00:00
|
|
|
def availability_status
|
|
|
|
current_account_user&.availability_status
|
|
|
|
end
|
|
|
|
|
|
|
|
def auto_offline
|
|
|
|
current_account_user&.auto_offline
|
|
|
|
end
|
|
|
|
|
2020-03-07 06:48:16 +00:00
|
|
|
def inviter
|
2020-05-26 17:08:48 +00:00
|
|
|
current_account_user&.inviter
|
2020-03-07 06:48:16 +00:00
|
|
|
end
|
|
|
|
|
2019-08-14 09:48:44 +00:00
|
|
|
def serializable_hash(options = nil)
|
2020-09-08 05:54:08 +00:00
|
|
|
super(options).merge(confirmed: confirmed?)
|
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-07-27 16:49:26 +00:00
|
|
|
available_name: available_name,
|
2020-05-03 06:47:27 +00:00
|
|
|
avatar_url: avatar_url,
|
2020-07-04 06:12:47 +00:00
|
|
|
type: 'user',
|
2021-09-13 08:13:19 +00:00
|
|
|
availability_status: availability_status,
|
|
|
|
thumbnail: avatar_url
|
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
|
2021-12-16 14:02:49 +00:00
|
|
|
|
|
|
|
# https://github.com/lynndylanhurley/devise_token_auth/blob/6d7780ee0b9750687e7e2871b9a1c6368f2085a9/app/models/devise_token_auth/concerns/user.rb#L45
|
|
|
|
# Since this method is overriden in devise_token_auth it breaks the email reconfirmation flow.
|
|
|
|
def will_save_change_to_email?
|
|
|
|
mutations_from_database.changed?('email')
|
|
|
|
end
|
2022-03-28 14:31:23 +00:00
|
|
|
|
2022-04-04 09:04:01 +00:00
|
|
|
def notifications_meta(account_id)
|
2022-03-28 14:31:23 +00:00
|
|
|
{
|
2022-04-04 09:04:01 +00:00
|
|
|
unread_count: notifications.where(account_id: account_id, read_at: nil).count,
|
|
|
|
count: notifications.where(account_id: account_id).count
|
2022-03-28 14:31:23 +00:00
|
|
|
}
|
|
|
|
end
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|