Change sender_id to contact_id in conversations (#167)
* change sender_id to contact_id in conversations * Fix failing tests * Fix seeds * fix specs * Fix issues in facebook messenger
This commit is contained in:
@@ -1,30 +1,34 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class Integrations::Facebook::DeliveryStatus
|
||||
def initialize(params)
|
||||
@params = params
|
||||
end
|
||||
module Integrations
|
||||
module Facebook
|
||||
class DeliveryStatus
|
||||
def initialize(params)
|
||||
@params = params
|
||||
end
|
||||
|
||||
def perform
|
||||
update_message_status
|
||||
end
|
||||
def perform
|
||||
update_message_status
|
||||
end
|
||||
|
||||
private
|
||||
private
|
||||
|
||||
def sender_id
|
||||
@params.sender['id']
|
||||
end
|
||||
def sender_id
|
||||
@params.sender['id']
|
||||
end
|
||||
|
||||
def contact_id
|
||||
@contact ||= ::Contact.find_by(source_id: sender_id)
|
||||
end
|
||||
def contact
|
||||
Contact.find_by(source_id: sender_id)
|
||||
end
|
||||
|
||||
def conversation
|
||||
@conversation ||= ::Conversation.find_by(sender_id: contact_id)
|
||||
end
|
||||
def conversation
|
||||
@conversation ||= ::Conversation.find_by(contact_id: contact.id)
|
||||
end
|
||||
|
||||
def update_message_status
|
||||
conversation.user_last_seen_at = @params.at
|
||||
conversation.save!
|
||||
def update_message_status
|
||||
conversation.user_last_seen_at = @params.at
|
||||
conversation.save!
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,43 +1,47 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class Integrations::Facebook::MessageCreator
|
||||
attr_reader :response
|
||||
module Integrations
|
||||
module Facebook
|
||||
class MessageCreator
|
||||
attr_reader :response
|
||||
|
||||
def initialize(response)
|
||||
@response = response
|
||||
end
|
||||
def initialize(response)
|
||||
@response = response
|
||||
end
|
||||
|
||||
def perform
|
||||
# begin
|
||||
if outgoing_message_via_echo?
|
||||
create_outgoing_message
|
||||
else
|
||||
create_incoming_message
|
||||
end
|
||||
# rescue => e
|
||||
# Raven.capture_exception(e)
|
||||
# end
|
||||
end
|
||||
def perform
|
||||
# begin
|
||||
if outgoing_message_via_echo?
|
||||
create_outgoing_message
|
||||
else
|
||||
create_incoming_message
|
||||
end
|
||||
# rescue => e
|
||||
# Raven.capture_exception(e)
|
||||
# end
|
||||
end
|
||||
|
||||
private
|
||||
private
|
||||
|
||||
def outgoing_message_via_echo?
|
||||
response.echo? && !response.sent_from_chatwoot_app?
|
||||
# this means that it is an outgoing message from page, but not sent from chatwoot.
|
||||
# User can send from fb page directly on mobile messenger, so this case should be handled as outgoing message
|
||||
end
|
||||
def outgoing_message_via_echo?
|
||||
response.echo? && !response.sent_from_chatwoot_app?
|
||||
# this means that it is an outgoing message from page, but not sent from chatwoot.
|
||||
# User can send from fb page directly on mobile messenger, so this case should be handled as outgoing message
|
||||
end
|
||||
|
||||
def create_outgoing_message
|
||||
Channel::FacebookPage.where(page_id: response.sender_id).each do |page|
|
||||
mb = Messages::Outgoing::EchoBuilder.new(response, page.inbox, true)
|
||||
mb.perform
|
||||
end
|
||||
end
|
||||
def create_outgoing_message
|
||||
Channel::FacebookPage.where(page_id: response.sender_id).each do |page|
|
||||
mb = Messages::Outgoing::EchoBuilder.new(response, page.inbox, true)
|
||||
mb.perform
|
||||
end
|
||||
end
|
||||
|
||||
def create_incoming_message
|
||||
Channel::FacebookPage.where(page_id: response.recipient_id).each do |page|
|
||||
mb = Messages::IncomingMessageBuilder.new(response, page.inbox)
|
||||
mb.perform
|
||||
def create_incoming_message
|
||||
Channel::FacebookPage.where(page_id: response.recipient_id).each do |page|
|
||||
mb = Messages::IncomingMessageBuilder.new(response, page.inbox)
|
||||
mb.perform
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,48 +1,52 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class Integrations::Facebook::MessageParser
|
||||
def initialize(response_json)
|
||||
@response = response_json
|
||||
end
|
||||
module Integrations
|
||||
module Facebook
|
||||
class MessageParser
|
||||
def initialize(response_json)
|
||||
@response = response_json
|
||||
end
|
||||
|
||||
def sender_id
|
||||
@response.sender['id']
|
||||
end
|
||||
def sender_id
|
||||
@response.sender['id']
|
||||
end
|
||||
|
||||
def recipient_id
|
||||
@response.recipient['id']
|
||||
end
|
||||
def recipient_id
|
||||
@response.recipient['id']
|
||||
end
|
||||
|
||||
def time_stamp
|
||||
@response.sent_at
|
||||
end
|
||||
def time_stamp
|
||||
@response.sent_at
|
||||
end
|
||||
|
||||
def content
|
||||
@response.text
|
||||
end
|
||||
def content
|
||||
@response.text
|
||||
end
|
||||
|
||||
def sequence
|
||||
@response.seq
|
||||
end
|
||||
def sequence
|
||||
@response.seq
|
||||
end
|
||||
|
||||
def attachments
|
||||
@response.attachments
|
||||
end
|
||||
def attachments
|
||||
@response.attachments
|
||||
end
|
||||
|
||||
def identifier
|
||||
@response.id
|
||||
end
|
||||
def identifier
|
||||
@response.id
|
||||
end
|
||||
|
||||
def echo?
|
||||
@response.echo?
|
||||
end
|
||||
def echo?
|
||||
@response.echo?
|
||||
end
|
||||
|
||||
def app_id
|
||||
@response.app_id
|
||||
end
|
||||
def app_id
|
||||
@response.app_id
|
||||
end
|
||||
|
||||
def sent_from_chatwoot_app?
|
||||
app_id && app_id == ENV['fb_app_id'].to_i
|
||||
def sent_from_chatwoot_app?
|
||||
app_id && app_id == ENV['fb_app_id'].to_i
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -1,63 +1,61 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class Integrations::Widget::IncomingMessageBuilder
|
||||
# params = {
|
||||
# contact_id: 1,
|
||||
# inbox_id: 1,
|
||||
# content: "Hello world"
|
||||
# }
|
||||
module Integrations
|
||||
module Widget
|
||||
class Integrations::Widget::IncomingMessageBuilder
|
||||
# params = {
|
||||
# contact_id: 1,
|
||||
# inbox_id: 1,
|
||||
# content: "Hello world"
|
||||
# }
|
||||
|
||||
attr_accessor :options, :message
|
||||
attr_accessor :options, :message
|
||||
|
||||
def initialize(options)
|
||||
@options = options
|
||||
end
|
||||
def initialize(options)
|
||||
@options = options
|
||||
end
|
||||
|
||||
def perform
|
||||
ActiveRecord::Base.transaction do
|
||||
build_conversation
|
||||
build_message
|
||||
def perform
|
||||
ActiveRecord::Base.transaction do
|
||||
build_message
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def inbox
|
||||
@inbox ||= Inbox.find(options[:inbox_id])
|
||||
end
|
||||
|
||||
def contact
|
||||
@contact ||= Contact.find(options[:contact_id])
|
||||
end
|
||||
|
||||
def conversation
|
||||
@conversation ||= Conversation.find_by(conversation_params) || Conversation.create!(conversation_params)
|
||||
end
|
||||
|
||||
def build_message
|
||||
@message = conversation.messages.new(message_params)
|
||||
@message.save!
|
||||
end
|
||||
|
||||
def conversation_params
|
||||
{
|
||||
account_id: inbox.account_id,
|
||||
inbox_id: inbox.id,
|
||||
contact_id: options[:contact_id]
|
||||
}
|
||||
end
|
||||
|
||||
def message_params
|
||||
{
|
||||
account_id: conversation.account_id,
|
||||
inbox_id: conversation.inbox_id,
|
||||
message_type: 0,
|
||||
content: options[:content]
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def inbox
|
||||
@inbox ||= Inbox.find(options[:inbox_id])
|
||||
end
|
||||
|
||||
def contact
|
||||
@contact ||= Contact.find(options[:contact_id])
|
||||
end
|
||||
|
||||
def build_conversation
|
||||
@conversation ||=
|
||||
if (conversation = Conversation.find_by(conversation_params))
|
||||
conversation
|
||||
else
|
||||
Conversation.create!(conversation_params)
|
||||
end
|
||||
end
|
||||
|
||||
def build_message
|
||||
@message = @conversation.messages.new(message_params)
|
||||
@message.save!
|
||||
end
|
||||
|
||||
def conversation_params
|
||||
{
|
||||
account_id: inbox.account_id,
|
||||
inbox_id: inbox.id,
|
||||
sender_id: options[:contact_id]
|
||||
}
|
||||
end
|
||||
|
||||
def message_params
|
||||
{
|
||||
account_id: @conversation.account_id,
|
||||
inbox_id: @conversation.inbox_id,
|
||||
message_type: 0,
|
||||
content: options[:content]
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user