fix: search for nil in-reply-to messages (#7286)
This commit is contained in:
@@ -35,9 +35,9 @@ class Imap::ImapMailbox
|
|||||||
end
|
end
|
||||||
|
|
||||||
def find_conversation_by_in_reply_to
|
def find_conversation_by_in_reply_to
|
||||||
return if in_reply_to.blank? && @inbound_mail.references.blank?
|
return if in_reply_to.blank?
|
||||||
|
|
||||||
message = @inbox.messages.find_by(source_id: in_reply_to) || find_message_by_references
|
message = @inbox.messages.find_by(source_id: in_reply_to)
|
||||||
if message.nil?
|
if message.nil?
|
||||||
@inbox.conversations.where("additional_attributes->>'in_reply_to' = ?", in_reply_to).first
|
@inbox.conversations.where("additional_attributes->>'in_reply_to' = ?", in_reply_to).first
|
||||||
else
|
else
|
||||||
@@ -45,6 +45,16 @@ class Imap::ImapMailbox
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def find_conversation_by_reference_ids
|
||||||
|
return if @inbound_mail.references.blank? && in_reply_to.present?
|
||||||
|
|
||||||
|
message = find_message_by_references
|
||||||
|
|
||||||
|
return if message.nil?
|
||||||
|
|
||||||
|
@inbox.conversations.find(message.conversation_id)
|
||||||
|
end
|
||||||
|
|
||||||
def in_reply_to
|
def in_reply_to
|
||||||
@inbound_mail.in_reply_to
|
@inbound_mail.in_reply_to
|
||||||
end
|
end
|
||||||
@@ -52,8 +62,6 @@ class Imap::ImapMailbox
|
|||||||
def find_message_by_references
|
def find_message_by_references
|
||||||
message_to_return = nil
|
message_to_return = nil
|
||||||
|
|
||||||
return if @inbound_mail.references.blank?
|
|
||||||
|
|
||||||
references = Array.wrap(@inbound_mail.references)
|
references = Array.wrap(@inbound_mail.references)
|
||||||
|
|
||||||
references.each do |message_id|
|
references.each do |message_id|
|
||||||
@@ -64,18 +72,22 @@ class Imap::ImapMailbox
|
|||||||
end
|
end
|
||||||
|
|
||||||
def find_or_create_conversation
|
def find_or_create_conversation
|
||||||
@conversation = find_conversation_by_in_reply_to || ::Conversation.create!({ account_id: @account.id,
|
@conversation = find_conversation_by_in_reply_to || find_conversation_by_reference_ids || ::Conversation.create!(
|
||||||
inbox_id: @inbox.id,
|
{
|
||||||
contact_id: @contact.id,
|
account_id: @account.id,
|
||||||
contact_inbox_id: @contact_inbox.id,
|
inbox_id: @inbox.id,
|
||||||
additional_attributes: {
|
contact_id: @contact.id,
|
||||||
source: 'email',
|
contact_inbox_id: @contact_inbox.id,
|
||||||
in_reply_to: in_reply_to,
|
additional_attributes: {
|
||||||
mail_subject: @processed_mail.subject,
|
source: 'email',
|
||||||
initiated_at: {
|
in_reply_to: in_reply_to,
|
||||||
timestamp: Time.now.utc
|
mail_subject: @processed_mail.subject,
|
||||||
}
|
initiated_at: {
|
||||||
} })
|
timestamp: Time.now.utc
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
def find_or_create_contact
|
def find_or_create_contact
|
||||||
|
|||||||
@@ -79,6 +79,42 @@ RSpec.describe Imap::ImapMailbox do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context 'when a new conversation with nil in_reply_to' do
|
||||||
|
let(:prev_conversation) { create(:conversation, account: account, inbox: channel.inbox, assignee: agent) }
|
||||||
|
let(:reply_mail) do
|
||||||
|
create_inbound_email_from_mail(from: 'email@gmail.com', to: 'imap@gmail.com', subject: 'Hello!', in_reply_to: nil)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'appends new email to the existing conversation' do
|
||||||
|
create(
|
||||||
|
:message,
|
||||||
|
content: 'Incoming Message',
|
||||||
|
message_type: 'incoming',
|
||||||
|
inbox: inbox,
|
||||||
|
account: account,
|
||||||
|
conversation: prev_conversation
|
||||||
|
)
|
||||||
|
create(
|
||||||
|
:message,
|
||||||
|
content: 'Outgoing Message',
|
||||||
|
message_type: 'outgoing',
|
||||||
|
inbox: inbox,
|
||||||
|
source_id: nil,
|
||||||
|
account: account,
|
||||||
|
conversation: prev_conversation
|
||||||
|
)
|
||||||
|
|
||||||
|
expect(prev_conversation.messages.size).to eq(2)
|
||||||
|
|
||||||
|
class_instance.process(reply_mail.mail, channel)
|
||||||
|
|
||||||
|
expect(prev_conversation.messages.size).to eq(2)
|
||||||
|
|
||||||
|
new_converstion_message = Conversation.last.messages.last.content_attributes
|
||||||
|
expect(new_converstion_message['email']['subject']).to eq('Hello!')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context 'when a reply for non existing email conversation' do
|
context 'when a reply for non existing email conversation' do
|
||||||
let(:reply_mail) do
|
let(:reply_mail) do
|
||||||
create_inbound_email_from_mail(from: 'email@gmail.com', to: 'imap@gmail.com', subject: 'Hello!', in_reply_to: 'test-in-reply-to')
|
create_inbound_email_from_mail(from: 'email@gmail.com', to: 'imap@gmail.com', subject: 'Hello!', in_reply_to: 'test-in-reply-to')
|
||||||
|
|||||||
Reference in New Issue
Block a user