2019-10-20 19:10:18 +00:00
|
|
|
# This class creates both outgoing messages from chatwoot and echo outgoing messages based on the flag `outgoing_echo`
|
|
|
|
# Assumptions
|
2020-02-09 10:17:48 +00:00
|
|
|
# 1. Incase of an outgoing message which is echo, source_id will NOT be nil,
|
2019-10-20 19:10:18 +00:00
|
|
|
# based on this we are showing "not sent from chatwoot" message in frontend
|
|
|
|
# Hence there is no need to set user_id in message for outgoing echo messages.
|
|
|
|
|
2020-01-09 07:36:40 +00:00
|
|
|
class Messages::MessageBuilder
|
|
|
|
attr_reader :response
|
|
|
|
|
|
|
|
def initialize(response, inbox, outgoing_echo = false)
|
|
|
|
@response = response
|
|
|
|
@inbox = inbox
|
|
|
|
@sender_id = (outgoing_echo ? @response.recipient_id : @response.sender_id)
|
|
|
|
@message_type = (outgoing_echo ? :outgoing : :incoming)
|
|
|
|
end
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2020-01-09 07:36:40 +00:00
|
|
|
def perform
|
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
build_contact
|
|
|
|
build_message
|
2019-10-20 19:10:18 +00:00
|
|
|
end
|
2020-01-09 07:36:40 +00:00
|
|
|
rescue StandardError => e
|
|
|
|
Raven.capture_exception(e)
|
|
|
|
true
|
|
|
|
end
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2020-01-09 07:36:40 +00:00
|
|
|
private
|
2019-08-31 06:22:09 +00:00
|
|
|
|
2020-01-09 07:36:40 +00:00
|
|
|
def contact
|
|
|
|
@contact ||= @inbox.contact_inboxes.find_by(source_id: @sender_id)&.contact
|
|
|
|
end
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2020-01-09 07:36:40 +00:00
|
|
|
def build_contact
|
|
|
|
return if contact.present?
|
2019-10-27 07:44:36 +00:00
|
|
|
|
2020-01-09 07:36:40 +00:00
|
|
|
@contact = Contact.create!(contact_params.except(:remote_avatar_url))
|
2020-04-03 07:34:58 +00:00
|
|
|
ContactAvatarJob.perform_later(@contact, contact_params[:remote_avatar_url]) if contact_params[:remote_avatar_url]
|
2020-01-09 07:36:40 +00:00
|
|
|
@contact_inbox = ContactInbox.create(contact: contact, inbox: @inbox, source_id: @sender_id)
|
|
|
|
end
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2020-01-09 07:36:40 +00:00
|
|
|
def build_message
|
|
|
|
@message = conversation.messages.create!(message_params)
|
|
|
|
(response.attachments || []).each do |attachment|
|
2020-04-17 15:45:20 +00:00
|
|
|
attachment_obj = @message.attachments.new(attachment_params(attachment).except(:remote_file_url))
|
2020-01-09 07:36:40 +00:00
|
|
|
attachment_obj.save!
|
|
|
|
attach_file(attachment_obj, attachment_params(attachment)[:remote_file_url]) if attachment_params(attachment)[:remote_file_url]
|
2019-10-20 19:10:18 +00:00
|
|
|
end
|
2020-01-09 07:36:40 +00:00
|
|
|
end
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2020-01-09 07:36:40 +00:00
|
|
|
def attach_file(attachment, file_url)
|
|
|
|
file_resource = LocalResource.new(file_url)
|
|
|
|
attachment.file.attach(io: file_resource.file, filename: file_resource.tmp_filename, content_type: file_resource.encoding)
|
|
|
|
end
|
2019-10-05 09:15:32 +00:00
|
|
|
|
2020-01-09 07:36:40 +00:00
|
|
|
def conversation
|
|
|
|
@conversation ||= Conversation.find_by(conversation_params) || build_conversation
|
|
|
|
end
|
2019-10-05 09:15:32 +00:00
|
|
|
|
2020-01-09 07:36:40 +00:00
|
|
|
def build_conversation
|
|
|
|
@contact_inbox ||= contact.contact_inboxes.find_by!(source_id: @sender_id)
|
|
|
|
Conversation.create!(conversation_params.merge(
|
|
|
|
contact_inbox_id: @contact_inbox.id
|
|
|
|
))
|
|
|
|
end
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2020-01-09 07:36:40 +00:00
|
|
|
def attachment_params(attachment)
|
|
|
|
file_type = attachment['type'].to_sym
|
|
|
|
params = { file_type: file_type, account_id: @message.account_id }
|
2019-10-05 09:15:32 +00:00
|
|
|
|
2020-01-09 07:36:40 +00:00
|
|
|
if [:image, :file, :audio, :video].include? file_type
|
|
|
|
params.merge!(file_type_params(attachment))
|
|
|
|
elsif file_type == :location
|
|
|
|
params.merge!(location_params(attachment))
|
|
|
|
elsif file_type == :fallback
|
|
|
|
params.merge!(fallback_params(attachment))
|
2019-10-20 19:10:18 +00:00
|
|
|
end
|
2019-10-05 09:15:32 +00:00
|
|
|
|
2020-01-09 07:36:40 +00:00
|
|
|
params
|
|
|
|
end
|
2019-10-05 09:15:32 +00:00
|
|
|
|
2020-01-09 07:36:40 +00:00
|
|
|
def file_type_params(attachment)
|
|
|
|
{
|
|
|
|
external_url: attachment['payload']['url'],
|
|
|
|
remote_file_url: attachment['payload']['url']
|
|
|
|
}
|
|
|
|
end
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2020-01-09 07:36:40 +00:00
|
|
|
def location_params(attachment)
|
|
|
|
lat = attachment['payload']['coordinates']['lat']
|
|
|
|
long = attachment['payload']['coordinates']['long']
|
|
|
|
{
|
|
|
|
external_url: attachment['url'],
|
|
|
|
coordinates_lat: lat,
|
|
|
|
coordinates_long: long,
|
|
|
|
fallback_title: attachment['title']
|
|
|
|
}
|
|
|
|
end
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2020-01-09 07:36:40 +00:00
|
|
|
def fallback_params(attachment)
|
|
|
|
{
|
|
|
|
fallback_title: attachment['title'],
|
|
|
|
external_url: attachment['url']
|
|
|
|
}
|
|
|
|
end
|
2019-10-20 19:10:18 +00:00
|
|
|
|
2020-01-09 07:36:40 +00:00
|
|
|
def conversation_params
|
|
|
|
{
|
|
|
|
account_id: @inbox.account_id,
|
|
|
|
inbox_id: @inbox.id,
|
|
|
|
contact_id: contact.id
|
|
|
|
}
|
|
|
|
end
|
2019-10-20 19:10:18 +00:00
|
|
|
|
2020-01-09 07:36:40 +00:00
|
|
|
def message_params
|
|
|
|
{
|
|
|
|
account_id: conversation.account_id,
|
|
|
|
inbox_id: conversation.inbox_id,
|
|
|
|
message_type: @message_type,
|
|
|
|
content: response.content,
|
2020-02-09 10:17:48 +00:00
|
|
|
source_id: response.identifier
|
2020-01-09 07:36:40 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def contact_params
|
|
|
|
begin
|
|
|
|
k = Koala::Facebook::API.new(@inbox.channel.page_access_token) if @inbox.facebook?
|
|
|
|
result = k.get_object(@sender_id) || {}
|
|
|
|
rescue Exception => e
|
|
|
|
result = {}
|
|
|
|
Raven.capture_exception(e)
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
2020-01-09 07:36:40 +00:00
|
|
|
{
|
|
|
|
name: "#{result['first_name'] || 'John'} #{result['last_name'] || 'Doe'}",
|
|
|
|
account_id: @inbox.account_id,
|
|
|
|
remote_avatar_url: result['profile_pic'] || ''
|
|
|
|
}
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
end
|