From ebefb2e2015cc49012fa6d33403130725e05e932 Mon Sep 17 00:00:00 2001 From: Pranav Raj S Date: Fri, 11 Aug 2023 17:53:57 -0700 Subject: [PATCH] fix: Consider the emails where in-reply-to header has multiple values (#7715) - Return the first message id for now to avoid the errors and subsequently missing the message. - Use .last instead of .first to avoid expensive query. - Fix array response in response bot. Fixes: https://linear.app/chatwoot/issue/CW-2358/activerecordstatementinvalid-pgdatatypemismatch-error-argument-of-and --- app/presenters/mail_presenter.rb | 9 +++++++ .../partials/_conversation.json.jbuilder | 2 +- .../message_templates/response_bot_service.rb | 2 +- spec/fixtures/files/multiple_in_reply_to.eml | 18 +++++++++++++ spec/presenters/mail_presenter_spec.rb | 25 +++++++++++++++++++ 5 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 spec/fixtures/files/multiple_in_reply_to.eml diff --git a/app/presenters/mail_presenter.rb b/app/presenters/mail_presenter.rb index 98915c07f..f55b2adc7 100644 --- a/app/presenters/mail_presenter.rb +++ b/app/presenters/mail_presenter.rb @@ -106,6 +106,15 @@ class MailPresenter < SimpleDelegator } end + def in_reply_to + return if @mail.in_reply_to.blank? + + # Although the "in_reply_to" field in the email can potentially hold multiple values, + # our current system does not have the capability to handle this. + # FIX ME: Address this issue by returning the complete results and utilizing them for querying conversations. + @mail.in_reply_to.is_a?(Array) ? @mail.in_reply_to.first : @mail.in_reply_to + end + def from # changing to downcase to avoid case mismatch while finding contact (@mail.reply_to.presence || @mail.from).map(&:downcase) diff --git a/app/views/api/v1/conversations/partials/_conversation.json.jbuilder b/app/views/api/v1/conversations/partials/_conversation.json.jbuilder index b3957256b..eaa5097c3 100644 --- a/app/views/api/v1/conversations/partials/_conversation.json.jbuilder +++ b/app/views/api/v1/conversations/partials/_conversation.json.jbuilder @@ -17,7 +17,7 @@ json.meta do end json.id conversation.display_id -if conversation.messages.where(account_id: conversation.account_id).first.blank? +if conversation.messages.where(account_id: conversation.account_id).last.blank? json.messages [] else json.messages [ diff --git a/enterprise/app/services/enterprise/message_templates/response_bot_service.rb b/enterprise/app/services/enterprise/message_templates/response_bot_service.rb index 01c1eb832..154c92936 100644 --- a/enterprise/app/services/enterprise/message_templates/response_bot_service.rb +++ b/enterprise/app/services/enterprise/message_templates/response_bot_service.rb @@ -61,7 +61,7 @@ class Enterprise::MessageTemplates::ResponseBotService end def create_messages(response, conversation) - response = process_response_content(response) + response = process_response_content(response).first create_outgoing_message(response, conversation) end diff --git a/spec/fixtures/files/multiple_in_reply_to.eml b/spec/fixtures/files/multiple_in_reply_to.eml new file mode 100644 index 000000000..d62eb7e36 --- /dev/null +++ b/spec/fixtures/files/multiple_in_reply_to.eml @@ -0,0 +1,18 @@ +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_479f13bb90078171@small-app-01.mail> + <4e6e35f5a38b4_479f13bb90078131@small-app-01.mail> +To: "Replies" +References: <4e6e35f5a38b4_479f13bb90078178@small-app-01.mail> +Message-Id: <0CB459E0-0336-41DA-BC88-E6E28C697DDB@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 + +Let's talk about these images diff --git a/spec/presenters/mail_presenter_spec.rb b/spec/presenters/mail_presenter_spec.rb index 2b4f42ac3..ce9299a46 100644 --- a/spec/presenters/mail_presenter_spec.rb +++ b/spec/presenters/mail_presenter_spec.rb @@ -4,6 +4,8 @@ RSpec.describe MailPresenter do describe 'parsed mail decorator' do let(:mail) { create_inbound_email_from_fixture('welcome.eml').mail } + let(:multiple_in_reply_to_mail) { create_inbound_email_from_fixture('multiple_in_reply_to.eml').mail } + let(:mail_without_in_reply_to) { create_inbound_email_from_fixture('reply_cc.eml').mail } let(:html_mail) { create_inbound_email_from_fixture('welcome_html.eml').mail } let(:ascii_mail) { create_inbound_email_from_fixture('non_utf_encoded_mail.eml').mail } let(:decorated_mail) { described_class.new(mail) } @@ -74,5 +76,28 @@ RSpec.describe MailPresenter do 'أنظروا، أنا أحتاجها فقط لتقوم بالتدقيق في مقالتي الشخصية' ) end + + describe '#in_reply_to' do + context 'when "in_reply_to" is an array' do + it 'returns the first value from the array' do + mail_presenter = described_class.new(multiple_in_reply_to_mail) + expect(mail_presenter.in_reply_to).to eq('4e6e35f5a38b4_479f13bb90078171@small-app-01.mail') + end + end + + context 'when "in_reply_to" is not an array' do + it 'returns the value as is' do + mail_presenter = described_class.new(mail) + expect(mail_presenter.in_reply_to).to eq('4e6e35f5a38b4_479f13bb90078178@small-app-01.mail') + end + end + + context 'when "in_reply_to" is blank' do + it 'returns nil' do + mail_presenter = described_class.new(mail_without_in_reply_to) + expect(mail_presenter.in_reply_to).to be_nil + end + end + end end end