diff --git a/app/mailboxes/imap/imap_mailbox.rb b/app/mailboxes/imap/imap_mailbox.rb index a9ed0a7c9..f811eedfa 100644 --- a/app/mailboxes/imap/imap_mailbox.rb +++ b/app/mailboxes/imap/imap_mailbox.rb @@ -35,9 +35,9 @@ class Imap::ImapMailbox end def find_conversation_by_in_reply_to - return if in_reply_to.blank? + return if in_reply_to.blank? && @inbound_mail.references.blank? - message = @inbox.messages.find_by(source_id: in_reply_to) + message = @inbox.messages.find_by(source_id: in_reply_to) || find_message_by_references if message.nil? @inbox.conversations.where("additional_attributes->>'in_reply_to' = ?", in_reply_to).first else @@ -49,6 +49,20 @@ class Imap::ImapMailbox @inbound_mail.in_reply_to end + def find_message_by_references + message_to_return = nil + + return if @inbound_mail.references.blank? + + references = @inbound_mail.references + + references.each do |message_id| + message = @inbox.messages.find_by(source_id: message_id) + message_to_return = message if message.present? + end + message_to_return + end + def find_or_create_conversation @conversation = find_conversation_by_in_reply_to || ::Conversation.create!({ account_id: @account.id, inbox_id: @inbox.id, diff --git a/spec/fixtures/files/references.eml b/spec/fixtures/files/references.eml new file mode 100644 index 000000000..3cbf59b3c --- /dev/null +++ b/spec/fixtures/files/references.eml @@ -0,0 +1,16 @@ +From: Sony Mathew +Mime-Version: 1.0 (Apple Message framework v1244.3) +Content-Type: multipart/alternative; boundary="Apple-Mail=_33A037C7-4BB3-4772-AE52-FCF2D7535F74" +Subject: Discussion: Let's debate these attachments +Date: Tue, 20 Apr 2020 04:20:20 -0400 +In-Reply-To: <4e6e35f5a38b4_479f13bb90078178@small-app-01.mail> +References: <4e6e35f5a38b4_479f13bb90078178@small-app-01.mail> +Message-Id: <0CB459E0-0336-41DA-BC88-E6E28C697DDBF@chatwoot.com> +X-Mailer: Apple Mail (2.1244.3) + +--Apple-Mail=_33A037C7-4BB3-4772-AE52-FCF2D7535F74 +Content-Transfer-Encoding: quoted-printable +Content-Type: text/plain; + charset=utf-8 + +References Email diff --git a/spec/mailboxes/imap/imap_mailbox_spec.rb b/spec/mailboxes/imap/imap_mailbox_spec.rb index 49b51b45f..d741440c7 100644 --- a/spec/mailboxes/imap/imap_mailbox_spec.rb +++ b/spec/mailboxes/imap/imap_mailbox_spec.rb @@ -83,11 +83,34 @@ RSpec.describe Imap::ImapMailbox 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') end + let(:references_email) { create_inbound_email_from_fixture('references.eml') } it 'creates new email conversation with incoming in-reply-to' do class_instance.process(reply_mail.mail, channel) expect(conversation.additional_attributes['in_reply_to']).to eq(reply_mail.mail.in_reply_to) end + + it 'append email to conversation with references id' do + inbox = Inbox.last + message = create( + :message, + content: 'Incoming Message', + message_type: 'incoming', + inbox: inbox, + source_id: 'test-reference-id', + account: account, + conversation: conversation + ) + conversation = message.conversation + + expect(conversation.messages.size).to eq(1) + + class_instance.process(references_email.mail, inbox.channel) + + expect(conversation.messages.size).to eq(2) + expect(conversation.messages.last.content).to eq('References Email') + expect(references_email.mail.references).to include('test-reference-id') + end end end end