mirror of
https://github.com/lingble/chatwoot.git
synced 2025-11-02 12:08:01 +00:00
fix: search for nil in-reply-to messages (#7286)
This commit is contained in:
@@ -35,9 +35,9 @@ class Imap::ImapMailbox
|
||||
end
|
||||
|
||||
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?
|
||||
@inbox.conversations.where("additional_attributes->>'in_reply_to' = ?", in_reply_to).first
|
||||
else
|
||||
@@ -45,6 +45,16 @@ class Imap::ImapMailbox
|
||||
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
|
||||
@inbound_mail.in_reply_to
|
||||
end
|
||||
@@ -52,8 +62,6 @@ class Imap::ImapMailbox
|
||||
def find_message_by_references
|
||||
message_to_return = nil
|
||||
|
||||
return if @inbound_mail.references.blank?
|
||||
|
||||
references = Array.wrap(@inbound_mail.references)
|
||||
|
||||
references.each do |message_id|
|
||||
@@ -64,18 +72,22 @@ class Imap::ImapMailbox
|
||||
end
|
||||
|
||||
def find_or_create_conversation
|
||||
@conversation = find_conversation_by_in_reply_to || ::Conversation.create!({ account_id: @account.id,
|
||||
inbox_id: @inbox.id,
|
||||
contact_id: @contact.id,
|
||||
contact_inbox_id: @contact_inbox.id,
|
||||
additional_attributes: {
|
||||
source: 'email',
|
||||
in_reply_to: in_reply_to,
|
||||
mail_subject: @processed_mail.subject,
|
||||
initiated_at: {
|
||||
timestamp: Time.now.utc
|
||||
}
|
||||
} })
|
||||
@conversation = find_conversation_by_in_reply_to || find_conversation_by_reference_ids || ::Conversation.create!(
|
||||
{
|
||||
account_id: @account.id,
|
||||
inbox_id: @inbox.id,
|
||||
contact_id: @contact.id,
|
||||
contact_inbox_id: @contact_inbox.id,
|
||||
additional_attributes: {
|
||||
source: 'email',
|
||||
in_reply_to: in_reply_to,
|
||||
mail_subject: @processed_mail.subject,
|
||||
initiated_at: {
|
||||
timestamp: Time.now.utc
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
end
|
||||
|
||||
def find_or_create_contact
|
||||
|
||||
@@ -79,6 +79,42 @@ RSpec.describe Imap::ImapMailbox do
|
||||
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
|
||||
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')
|
||||
|
||||
Reference in New Issue
Block a user