2019-11-30 13:39:55 +00:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: contact_inboxes
|
|
|
|
#
|
2021-01-17 17:14:03 +00:00
|
|
|
# id :bigint not null, primary key
|
|
|
|
# hmac_verified :boolean default(FALSE)
|
2021-11-22 18:02:17 +00:00
|
|
|
# pubsub_token :string
|
2021-01-17 17:14:03 +00:00
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
|
|
|
# contact_id :bigint
|
|
|
|
# inbox_id :bigint
|
|
|
|
# source_id :string not null
|
2019-11-30 13:39:55 +00:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# index_contact_inboxes_on_contact_id (contact_id)
|
|
|
|
# index_contact_inboxes_on_inbox_id (inbox_id)
|
|
|
|
# index_contact_inboxes_on_inbox_id_and_source_id (inbox_id,source_id) UNIQUE
|
2021-11-22 18:02:17 +00:00
|
|
|
# index_contact_inboxes_on_pubsub_token (pubsub_token) UNIQUE
|
2019-11-30 13:39:55 +00:00
|
|
|
# index_contact_inboxes_on_source_id (source_id)
|
|
|
|
#
|
|
|
|
|
2019-10-27 07:44:36 +00:00
|
|
|
class ContactInbox < ApplicationRecord
|
2021-11-22 18:02:17 +00:00
|
|
|
include Pubsubable
|
2019-10-27 07:44:36 +00:00
|
|
|
validates :inbox_id, presence: true
|
|
|
|
validates :contact_id, presence: true
|
|
|
|
validates :source_id, presence: true
|
2021-02-03 13:54:51 +00:00
|
|
|
validate :valid_source_id_format?
|
2019-10-27 07:44:36 +00:00
|
|
|
|
|
|
|
belongs_to :contact
|
|
|
|
belongs_to :inbox
|
2020-01-09 07:36:40 +00:00
|
|
|
|
2021-11-18 05:02:29 +00:00
|
|
|
has_many :conversations, dependent: :destroy_async
|
2020-04-10 11:12:37 +00:00
|
|
|
|
|
|
|
def webhook_data
|
|
|
|
{
|
|
|
|
id: id,
|
|
|
|
contact: contact.try(:webhook_data),
|
|
|
|
inbox: inbox.webhook_data,
|
|
|
|
account: inbox.account.webhook_data,
|
|
|
|
current_conversation: current_conversation.try(:webhook_data),
|
|
|
|
source_id: source_id
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def current_conversation
|
|
|
|
conversations.last
|
|
|
|
end
|
2021-02-03 13:54:51 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def validate_twilio_source_id
|
|
|
|
# https://www.twilio.com/docs/glossary/what-e164#regex-matching-for-e164
|
2021-04-15 09:43:01 +00:00
|
|
|
if inbox.channel.medium == 'sms' && !/\+[1-9]\d{1,14}\z/.match?(source_id)
|
|
|
|
errors.add(:source_id, 'invalid source id for twilio sms inbox. valid Regex /\+[1-9]\d{1,14}\z/')
|
|
|
|
elsif inbox.channel.medium == 'whatsapp' && !/whatsapp:\+[1-9]\d{1,14}\z/.match?(source_id)
|
|
|
|
errors.add(:source_id, 'invalid source id for twilio whatsapp inbox. valid Regex /whatsapp:\+[1-9]\d{1,14}\z/')
|
2021-02-03 13:54:51 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate_email_source_id
|
2021-10-14 07:25:46 +00:00
|
|
|
errors.add(:source_id, "invalid source id for Email inbox. valid Regex #{Devise.email_regexp}") unless Devise.email_regexp.match?(source_id)
|
2021-02-03 13:54:51 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def valid_source_id_format?
|
|
|
|
validate_twilio_source_id if inbox.channel_type == 'Channel::TwilioSms'
|
|
|
|
validate_email_source_id if inbox.channel_type == 'Channel::Email'
|
|
|
|
end
|
2019-10-27 07:44:36 +00:00
|
|
|
end
|