Skip to content

Commit

Permalink
Fix an issue when support agent replies to the email notifications an…
Browse files Browse the repository at this point in the history
…d conversation moved to another mailbox - closes #3455
  • Loading branch information
freescout-help-desk committed Oct 13, 2023
1 parent 6094bdd commit 99bab47
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions app/Console/Commands/FetchEmails.php
Original file line number Diff line number Diff line change
Expand Up @@ -561,10 +561,18 @@ public function processMessage($message, $message_id, $mailbox, $mailboxes, $ext
}

// Make sure that prev_thread belongs to the current mailbox.
// It may happen when forwarding conversation for example.
if ($prev_thread) {
// Problems may arise when forwarding conversation for example.
//
// For replies to email notifications it's allowed to have prev_thread in
// another mailbox as conversation can be moved.
// https://github.com/freescout-helpdesk/freescout/issues/3455
if ($prev_thread && $message_from_customer) {
if ($prev_thread->conversation->mailbox_id != $mailbox->id) {
// https://github.com/freescout-helpdesk/freescout/issues/2807
// Behaviour of email sent to multiple mailboxes:
// If a user from either mailbox replies, then a new conversation is created
// in the other mailbox with another new conversation ID.
//
// Try to get thread by generated message ID.
if ($in_reply_to) {
$prev_thread = Thread::where('message_id', \MailHelper::generateMessageId($in_reply_to, $mailbox->id.$in_reply_to))->first();
Expand Down Expand Up @@ -748,6 +756,15 @@ public function processMessage($message, $message_id, $mailbox, $mailboxes, $ext
return;
}

// Save user thread only if there prev_thread is set.
// https://github.com/freescout-helpdesk/freescout/issues/3455
if (!$prev_thread) {
$this->logError("Support agent's reply to the email notification could not be processed as previous thread could not be determined.");
$this->setSeen($message, $mailbox);

return;
}

if (\Eventy::filter('fetch_emails.should_save_thread', true, $data) !== false) {
$new_thread = $this->saveUserThread($data['mailbox'], $data['message_id'], $data['prev_thread'], $data['user'], $data['from'], $data['to'], $data['cc'], $data['bcc'], $data['body'], $data['attachments'], $data['message']->getHeader(), $data['date']);
} else {
Expand Down

0 comments on commit 99bab47

Please sign in to comment.