2019-11-30 13:39:55 +00:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: contacts
|
|
|
|
#
|
2020-02-09 10:17:48 +00:00
|
|
|
# id :integer not null, primary key
|
|
|
|
# additional_attributes :jsonb
|
2020-08-21 14:00:27 +00:00
|
|
|
# custom_attributes :jsonb
|
2020-02-09 10:17:48 +00:00
|
|
|
# email :string
|
2020-04-03 07:34:58 +00:00
|
|
|
# identifier :string
|
2020-02-09 10:17:48 +00:00
|
|
|
# name :string
|
|
|
|
# phone_number :string
|
|
|
|
# pubsub_token :string
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
|
|
|
# account_id :integer not null
|
2019-11-30 13:39:55 +00:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
2020-04-03 07:34:58 +00:00
|
|
|
# index_contacts_on_account_id (account_id)
|
|
|
|
# index_contacts_on_pubsub_token (pubsub_token) UNIQUE
|
|
|
|
# uniq_email_per_account_contact (email,account_id) UNIQUE
|
|
|
|
# uniq_identifier_per_account_contact (identifier,account_id) UNIQUE
|
2019-11-30 13:39:55 +00:00
|
|
|
#
|
|
|
|
|
2019-08-14 09:48:44 +00:00
|
|
|
class Contact < ApplicationRecord
|
2019-10-16 22:18:48 +00:00
|
|
|
include Pubsubable
|
2020-01-07 17:29:17 +00:00
|
|
|
include Avatarable
|
2020-02-02 17:04:16 +00:00
|
|
|
include AvailabilityStatusable
|
2021-01-03 14:37:57 +00:00
|
|
|
include Labelable
|
2020-04-18 08:17:51 +00:00
|
|
|
|
2019-08-14 09:48:44 +00:00
|
|
|
validates :account_id, presence: true
|
2020-04-03 07:34:58 +00:00
|
|
|
validates :email, allow_blank: true, uniqueness: { scope: [:account_id], case_sensitive: false }
|
|
|
|
validates :identifier, allow_blank: true, uniqueness: { scope: [:account_id] }
|
2021-03-29 17:56:20 +00:00
|
|
|
validates :phone_number, allow_blank: true, uniqueness: { scope: [:account_id] }
|
2019-08-14 09:48:44 +00:00
|
|
|
|
|
|
|
belongs_to :account
|
2019-10-20 19:10:18 +00:00
|
|
|
has_many :conversations, dependent: :destroy
|
2019-10-27 07:44:36 +00:00
|
|
|
has_many :contact_inboxes, dependent: :destroy
|
|
|
|
has_many :inboxes, through: :contact_inboxes
|
2020-06-27 16:04:53 +00:00
|
|
|
has_many :messages, as: :sender, dependent: :destroy
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2020-08-22 18:35:07 +00:00
|
|
|
before_validation :prepare_email_attribute
|
2020-12-13 16:53:56 +00:00
|
|
|
after_create_commit :dispatch_create_event, :ip_lookup
|
2020-06-16 14:09:57 +00:00
|
|
|
after_update_commit :dispatch_update_event
|
2020-04-03 07:34:58 +00:00
|
|
|
|
2019-10-27 07:44:36 +00:00
|
|
|
def get_source_id(inbox_id)
|
|
|
|
contact_inboxes.find_by!(inbox_id: inbox_id).source_id
|
|
|
|
end
|
|
|
|
|
2019-08-14 09:48:44 +00:00
|
|
|
def push_event_data
|
|
|
|
{
|
2020-06-02 17:29:02 +00:00
|
|
|
additional_attributes: additional_attributes,
|
|
|
|
email: email,
|
2019-08-14 09:48:44 +00:00
|
|
|
id: id,
|
2020-06-02 17:29:02 +00:00
|
|
|
identifier: identifier,
|
2019-08-14 09:48:44 +00:00
|
|
|
name: name,
|
2020-06-02 17:29:02 +00:00
|
|
|
phone_number: phone_number,
|
|
|
|
pubsub_token: pubsub_token,
|
2020-01-07 17:29:17 +00:00
|
|
|
thumbnail: avatar_url,
|
2020-06-02 17:29:02 +00:00
|
|
|
type: 'contact'
|
2019-08-14 09:48:44 +00:00
|
|
|
}
|
|
|
|
end
|
2020-02-26 04:14:24 +00:00
|
|
|
|
|
|
|
def webhook_data
|
|
|
|
{
|
|
|
|
id: id,
|
2020-04-18 08:17:51 +00:00
|
|
|
name: name,
|
2020-05-03 06:47:27 +00:00
|
|
|
avatar: avatar_url,
|
|
|
|
type: 'contact'
|
2020-02-26 04:14:24 +00:00
|
|
|
}
|
|
|
|
end
|
2020-04-03 07:34:58 +00:00
|
|
|
|
2020-10-27 20:44:36 +00:00
|
|
|
def ip_lookup
|
|
|
|
return unless account.feature_enabled?('ip_lookup')
|
|
|
|
|
|
|
|
ContactIpLookupJob.perform_later(self)
|
|
|
|
end
|
|
|
|
|
2020-08-22 18:35:07 +00:00
|
|
|
def prepare_email_attribute
|
|
|
|
# So that the db unique constraint won't throw error when email is ''
|
|
|
|
self.email = nil if email.blank?
|
2020-04-03 07:34:58 +00:00
|
|
|
email.downcase! if email.present?
|
|
|
|
end
|
2020-04-18 08:17:51 +00:00
|
|
|
|
|
|
|
def dispatch_create_event
|
|
|
|
Rails.configuration.dispatcher.dispatch(CONTACT_CREATED, Time.zone.now, contact: self)
|
|
|
|
end
|
|
|
|
|
|
|
|
def dispatch_update_event
|
|
|
|
Rails.configuration.dispatcher.dispatch(CONTACT_UPDATED, Time.zone.now, contact: self)
|
|
|
|
end
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|