Fix: Find mailbox with cc email (#4372)

This commit is contained in:
Tejaswini Chile 2022-04-08 11:20:19 +05:30 committed by GitHub
parent 14e6a5d6b0
commit 57359be37e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 103 additions and 12 deletions

View file

@ -0,0 +1,15 @@
class EmailChannelFinder
def initialize(email_object)
@email_object = email_object
end
def perform
channel = nil
recipient_mails = @email_object.to.to_a + @email_object.cc.to_a
recipient_mails.each do |email|
channel = Channel::Email.find_by('lower(email) = ? OR lower(forward_to_email) = ?', email.downcase, email.downcase)
break if channel.present?
end
channel
end
end

View file

@ -21,17 +21,20 @@ class ApplicationMailbox < ActionMailbox::Base
def self.support_mail? def self.support_mail?
proc do |inbound_mail_obj| proc do |inbound_mail_obj|
is_a_support_email = false is_a_support_email = false
inbound_mail_obj.mail.to&.each do |email|
channel = Channel::Email.find_by('lower(email) = ? OR lower(forward_to_email) = ?', email.downcase, email.downcase) is_a_support_email = true if reply_to_mail?(inbound_mail_obj, is_a_support_email)
if channel.present?
is_a_support_email = true
break
end
end
is_a_support_email is_a_support_email
end end
end end
def self.reply_to_mail?(inbound_mail_obj, is_a_support_email)
return if is_a_support_email
channel = EmailChannelFinder.new(inbound_mail_obj.mail).perform
channel.present?
end
def self.catch_all_mail? def self.catch_all_mail?
proc { |_mail| true } proc { |_mail| true }
end end

View file

@ -2,6 +2,8 @@ module MailboxHelper
private private
def create_message def create_message
return if @conversation.messages.find_by(source_id: processed_mail.message_id).present?
@message = @conversation.messages.create( @message = @conversation.messages.create(
account_id: @conversation.account_id, account_id: @conversation.account_id,
sender: @conversation.contact, sender: @conversation.contact,

View file

@ -21,15 +21,17 @@ class SupportMailbox < ApplicationMailbox
private private
def find_channel def find_channel
mail.to.each do |email| find_channel_with_to_mail if @channel.blank?
@channel = Channel::Email.find_by('lower(email) = ? OR lower(forward_to_email) = ?', email.downcase, email.downcase)
break if @channel.present?
end
raise 'Email channel/inbox not found' if @channel.nil? raise 'Email channel/inbox not found' if @channel.nil?
@channel @channel
end end
def find_channel_with_to_mail
@channel = EmailChannelFinder.new(mail).perform
end
def load_account def load_account
@account = @channel.account @account = @channel.account
end end

View file

@ -0,0 +1,29 @@
require 'rails_helper'
describe ::EmailChannelFinder do
include ActionMailbox::TestHelper
let!(:channel_email) { create(:channel_email) }
describe '#perform' do
context 'with cc mail' do
let(:reply_cc_mail) { create_inbound_email_from_fixture('reply_cc.eml') }
it 'return channel with cc email' do
channel_email.update(email: 'test@example.com')
channel = described_class.new(reply_cc_mail.mail).perform
expect(channel).to eq(channel_email)
end
end
context 'with to mail' do
let(:reply_mail) { create_inbound_email_from_fixture('reply.eml') }
it 'return channel with to email' do
channel_email.update(email: 'test@example.com')
reply_mail.mail['to'] = 'test@example.com'
channel = described_class.new(reply_mail.mail).perform
expect(channel).to eq(channel_email)
end
end
end
end

View file

@ -5,7 +5,7 @@ Subject: Discussion: Let's debate these attachments
Date: Tue, 20 Apr 2020 04:20:20 -0400 Date: Tue, 20 Apr 2020 04:20:20 -0400
In-Reply-To: <0CB459E0-0336-41DA-BC88-E6E28C697DDB@chatwoot.com> In-Reply-To: <0CB459E0-0336-41DA-BC88-E6E28C697DDB@chatwoot.com>
References: <4e6e35f5a38b4_479f13bb90078178@small-app-01.mail> References: <4e6e35f5a38b4_479f13bb90078178@small-app-01.mail>
Message-Id: <0CB459E0-0336-41DA-BC88-E6E28C697DDB@chatwoot.com> Message-Id: <0CB459E0-0336-41DA-BC88-E6E28C697DDBF@chatwoot.com>
X-Mailer: Apple Mail (2.1244.3) X-Mailer: Apple Mail (2.1244.3)
--Apple-Mail=_33A037C7-4BB3-4772-AE52-FCF2D7535F74 --Apple-Mail=_33A037C7-4BB3-4772-AE52-FCF2D7535F74

30
spec/fixtures/files/reply_cc.eml vendored Normal file
View file

@ -0,0 +1,30 @@
MIME-Version: 1.0
Date: Thu, 31 Mar 2022 19:25:32 +0530
Message-ID: <CAFkiBVw2B=CPf=8=VqBQqssFp7XfGm4u6LpByHuMXWsPZCxPYQ@mail.gmail.com>
Subject: Discussion: Let's debate these attachments
From: Sony Mathew <sony@chatwoot.com>
To: <test123@example.com>
Cc: Replies <test@example.com>
Content-Type: multipart/alternative; boundary="00000000000001f49e05db8404a8"
--00000000000001f49e05db8404a8
Content-Type: text/plain; charset="UTF-8"
Hello,
Find the related inbox.
--
*Sony Mathew*
--00000000000001f49e05db8404a8
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">Hello,<div><br></div><div>Find the related inbox.<br clear=
=3D"all"><div><br></div>-- <br><div dir=3D"ltr" class=3D"gmail_signature" d=
ata-smartmail=3D"gmail_signature"><div dir=3D"ltr"><div><div dir=3D"ltr"><d=
iv><div><b>Sony Mathew</b><br></div><br></di=
v></div></div></div></div></div></div>
--00000000000001f49e05db8404a8--

View file

@ -6,6 +6,7 @@ RSpec.describe ApplicationMailbox, type: :mailbox do
describe 'route the inbound mail to appropriate mailbox' do describe 'route the inbound mail to appropriate mailbox' do
let(:welcome_mail) { create_inbound_email_from_fixture('welcome.eml') } let(:welcome_mail) { create_inbound_email_from_fixture('welcome.eml') }
let(:reply_mail) { create_inbound_email_from_fixture('reply.eml') } let(:reply_mail) { create_inbound_email_from_fixture('reply.eml') }
let(:reply_cc_mail) { create_inbound_email_from_fixture('reply_cc.eml') }
let(:reply_mail_without_uuid) { create_inbound_email_from_fixture('reply.eml') } let(:reply_mail_without_uuid) { create_inbound_email_from_fixture('reply.eml') }
let(:reply_mail_with_in_reply_to) { create_inbound_email_from_fixture('in_reply_to.eml') } let(:reply_mail_with_in_reply_to) { create_inbound_email_from_fixture('in_reply_to.eml') }
let(:support_mail) { create_inbound_email_from_fixture('support.eml') } let(:support_mail) { create_inbound_email_from_fixture('support.eml') }
@ -55,6 +56,14 @@ RSpec.describe ApplicationMailbox, type: :mailbox do
expect(dbl).to receive(:perform_processing).and_return(true) expect(dbl).to receive(:perform_processing).and_return(true)
described_class.route support_mail described_class.route support_mail
end end
it 'routes support emails to Support Mailbox with cc email' do
channel_email.update(email: 'test@example.com')
dbl = double
expect(SupportMailbox).to receive(:new).and_return(dbl)
expect(dbl).to receive(:perform_processing).and_return(true)
described_class.route reply_cc_mail
end
end end
end end
end end

View file

@ -148,6 +148,7 @@ RSpec.describe SupportMailbox, type: :mailbox do
expect(conversation_1.messages.count).to eq(1) expect(conversation_1.messages.count).to eq(1)
reply_mail_without_uuid.mail['In-Reply-To'] = 'conversation/6bdc3f4d-0bec-4515-a284-5d916fdde489/messages/123' reply_mail_without_uuid.mail['In-Reply-To'] = 'conversation/6bdc3f4d-0bec-4515-a284-5d916fdde489/messages/123'
reply_mail_without_uuid.mail['Message-Id'] = '0CB459E0-0336-41DA-BC88-E6E28C697SFC@chatwoot.com'
described_class.receive reply_mail_without_uuid described_class.receive reply_mail_without_uuid