|
|
|
|
@@ -1,54 +1,43 @@
|
|
|
|
|
require 'open-uri'
|
|
|
|
|
class Messages::MessageBuilder
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
This class creates both outgoing messages from chatwoot and echo outgoing messages based on the flag `outgoing_echo`
|
|
|
|
|
Assumptions
|
|
|
|
|
1. Incase of an outgoing message which is echo, fb_id will NOT be nil,
|
|
|
|
|
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.
|
|
|
|
|
=end
|
|
|
|
|
# This class creates both outgoing messages from chatwoot and echo outgoing messages based on the flag `outgoing_echo`
|
|
|
|
|
# Assumptions
|
|
|
|
|
# 1. Incase of an outgoing message which is echo, fb_id will NOT be nil,
|
|
|
|
|
# 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.
|
|
|
|
|
|
|
|
|
|
attr_reader :response
|
|
|
|
|
|
|
|
|
|
def initialize response, inbox, outgoing_echo=false
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
def perform #for incoming
|
|
|
|
|
begin
|
|
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
|
build_contact
|
|
|
|
|
build_conversation
|
|
|
|
|
build_message
|
|
|
|
|
end
|
|
|
|
|
#build_attachments
|
|
|
|
|
rescue => e
|
|
|
|
|
Raven.capture_exception(e)
|
|
|
|
|
#change this asap
|
|
|
|
|
return true
|
|
|
|
|
|
|
|
|
|
def perform # for incoming
|
|
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
|
build_contact
|
|
|
|
|
build_conversation
|
|
|
|
|
build_message
|
|
|
|
|
end
|
|
|
|
|
# build_attachments
|
|
|
|
|
rescue StandardError => e
|
|
|
|
|
Raven.capture_exception(e)
|
|
|
|
|
# change this asap
|
|
|
|
|
true
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
|
|
def build_attachments
|
|
|
|
|
|
|
|
|
|
end
|
|
|
|
|
def build_attachments; end
|
|
|
|
|
|
|
|
|
|
def contact
|
|
|
|
|
@contact ||= @inbox.contacts.find_by(source_id: @sender_id)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def build_contact
|
|
|
|
|
if contact.nil?
|
|
|
|
|
@contact = @inbox.contacts.create!(contact_params)
|
|
|
|
|
end
|
|
|
|
|
@contact = @inbox.contacts.create!(contact_params) if contact.nil?
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def build_message
|
|
|
|
|
@@ -61,11 +50,11 @@ Assumptions
|
|
|
|
|
|
|
|
|
|
def build_conversation
|
|
|
|
|
@conversation ||=
|
|
|
|
|
if (conversation = Conversation.find_by(conversation_params))
|
|
|
|
|
conversation
|
|
|
|
|
else
|
|
|
|
|
Conversation.create!(conversation_params)
|
|
|
|
|
end
|
|
|
|
|
if (conversation = Conversation.find_by(conversation_params))
|
|
|
|
|
conversation
|
|
|
|
|
else
|
|
|
|
|
Conversation.create!(conversation_params)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def attachment_params(attachment)
|
|
|
|
|
@@ -91,7 +80,8 @@ Assumptions
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def location_params(attachment)
|
|
|
|
|
lat, long = attachment['payload']['coordinates']['lat'], attachment['payload']['coordinates']['long']
|
|
|
|
|
lat = attachment['payload']['coordinates']['lat']
|
|
|
|
|
long = attachment['payload']['coordinates']['long']
|
|
|
|
|
{
|
|
|
|
|
external_url: attachment['url'],
|
|
|
|
|
coordinates_lat: lat,
|
|
|
|
|
@@ -134,10 +124,10 @@ Assumptions
|
|
|
|
|
Raven.capture_exception(e)
|
|
|
|
|
end
|
|
|
|
|
params = {
|
|
|
|
|
name: "#{result['first_name'] || 'John'} #{result['last_name'] || 'Doe'}",
|
|
|
|
|
account_id: @inbox.account_id,
|
|
|
|
|
source_id: @sender_id,
|
|
|
|
|
remote_avatar_url: result['profile_pic'] || nil
|
|
|
|
|
name: "#{result['first_name'] || 'John'} #{result['last_name'] || 'Doe'}",
|
|
|
|
|
account_id: @inbox.account_id,
|
|
|
|
|
source_id: @sender_id,
|
|
|
|
|
remote_avatar_url: result['profile_pic'] || nil
|
|
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|