2022-10-13 22:12:04 +00:00
|
|
|
# This Builder will create a contact and contact inbox with specified attributes.
|
|
|
|
# If an existing identified contact exisits, it will be returned.
|
|
|
|
# for contact inbox logic it uses the contact inbox builder
|
|
|
|
|
|
|
|
class ContactInboxWithContactBuilder
|
|
|
|
pattr_initialize [:inbox!, :contact_attributes!, :source_id, :hmac_verified]
|
2020-04-05 16:41:27 +00:00
|
|
|
|
|
|
|
def perform
|
2022-10-13 22:12:04 +00:00
|
|
|
find_or_create_contact_and_contact_inbox
|
|
|
|
# in case of race conditions where contact is created by another thread
|
|
|
|
# we will try to find the contact and create a contact inbox
|
|
|
|
rescue ActiveRecord::RecordNotUnique
|
|
|
|
find_or_create_contact_and_contact_inbox
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_or_create_contact_and_contact_inbox
|
|
|
|
@contact_inbox = inbox.contact_inboxes.find_by(source_id: source_id) if source_id.present?
|
|
|
|
return @contact_inbox if @contact_inbox
|
2020-04-05 16:41:27 +00:00
|
|
|
|
2022-10-13 22:12:04 +00:00
|
|
|
ActiveRecord::Base.transaction(requires_new: true) do
|
|
|
|
build_contact_with_contact_inbox
|
|
|
|
update_contact_avatar(@contact) unless @contact.avatar.attached?
|
|
|
|
@contact_inbox
|
|
|
|
end
|
2020-04-05 16:41:27 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2022-10-13 22:12:04 +00:00
|
|
|
def build_contact_with_contact_inbox
|
|
|
|
@contact = find_contact || create_contact
|
|
|
|
@contact_inbox = create_contact_inbox
|
|
|
|
end
|
|
|
|
|
2020-04-05 16:41:27 +00:00
|
|
|
def account
|
|
|
|
@account ||= inbox.account
|
|
|
|
end
|
|
|
|
|
2022-10-13 22:12:04 +00:00
|
|
|
def create_contact_inbox
|
|
|
|
ContactInboxBuilder.new(
|
|
|
|
contact: @contact,
|
|
|
|
inbox: @inbox,
|
|
|
|
source_id: @source_id,
|
|
|
|
hmac_verified: hmac_verified
|
|
|
|
).perform
|
2020-05-14 17:21:07 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def update_contact_avatar(contact)
|
2022-07-21 17:27:12 +00:00
|
|
|
::Avatar::AvatarFromUrlJob.perform_later(contact, contact_attributes[:avatar_url]) if contact_attributes[:avatar_url]
|
2020-05-14 17:21:07 +00:00
|
|
|
end
|
|
|
|
|
2021-01-18 06:13:31 +00:00
|
|
|
def create_contact
|
|
|
|
account.contacts.create!(
|
2021-06-15 14:39:17 +00:00
|
|
|
name: contact_attributes[:name] || ::Haikunator.haikunate(1000),
|
2021-01-18 06:13:31 +00:00
|
|
|
phone_number: contact_attributes[:phone_number],
|
|
|
|
email: contact_attributes[:email],
|
|
|
|
identifier: contact_attributes[:identifier],
|
2021-12-19 05:29:03 +00:00
|
|
|
additional_attributes: contact_attributes[:additional_attributes],
|
|
|
|
custom_attributes: contact_attributes[:custom_attributes]
|
2021-01-18 06:13:31 +00:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_contact
|
2021-08-11 19:58:07 +00:00
|
|
|
contact = find_contact_by_identifier(contact_attributes[:identifier])
|
|
|
|
contact ||= find_contact_by_email(contact_attributes[:email])
|
|
|
|
contact ||= find_contact_by_phone_number(contact_attributes[:phone_number])
|
|
|
|
contact
|
|
|
|
end
|
2021-01-18 06:13:31 +00:00
|
|
|
|
2021-08-11 19:58:07 +00:00
|
|
|
def find_contact_by_identifier(identifier)
|
|
|
|
return if identifier.blank?
|
2020-04-29 20:11:13 +00:00
|
|
|
|
2021-08-11 19:58:07 +00:00
|
|
|
account.contacts.find_by(identifier: identifier)
|
|
|
|
end
|
2021-01-18 06:13:31 +00:00
|
|
|
|
2021-08-11 19:58:07 +00:00
|
|
|
def find_contact_by_email(email)
|
|
|
|
return if email.blank?
|
2021-04-20 17:12:14 +00:00
|
|
|
|
2021-08-11 19:58:07 +00:00
|
|
|
account.contacts.find_by(email: email.downcase)
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_contact_by_phone_number(phone_number)
|
|
|
|
return if phone_number.blank?
|
|
|
|
|
|
|
|
account.contacts.find_by(phone_number: phone_number)
|
2021-01-18 06:13:31 +00:00
|
|
|
end
|
2020-04-05 16:41:27 +00:00
|
|
|
end
|