Skip to content

Commit

Permalink
Merge pull request #189 from SakiTakamachi/fix/gh-187-temporary
Browse files Browse the repository at this point in the history
Fix GH-187 Temporary fix for getting incorrect thread ID caused by iPhone bug
  • Loading branch information
mnapoli committed Sep 22, 2023
2 parents 1539b89 + 2a5d132 commit 0303c1b
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/EmailSynchronizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,24 +137,31 @@ public function synchronizeEmail(int $number, string $source): void
}
}
// Extract the thread ID from the "references" header
$threadId = null;
$firstReference = null;
$references = $parsedDocument->getHeaderValue('References');
if ($references) {
$references = preg_split('/(?<=>)/', $references);
$references = array_filter(array_map('trim', $references));
if (! empty($references)) {
$threadId = reset($references);
$firstReference = reset($references);
if (! $inReplyTo) {
// In old mails the `In-Reply-To` header didn't exist, instead it was at the end of the references
// Example: https://externals.io/message/2536#2784
$inReplyTo = end($references);
}
}
}
// We know it is a reply to an email but we weren't able to find the thread ID: let's find it from our database
if ($threadId === null && $inReplyTo !== null) {

$threadId = null;
if ($firstReference !== null) {
// When using the iPhone mailer, references may not have the root email of the thread.
// See https://github.com/mnapoli/externals/pull/189/files
$threadId = $this->findEmailThreadId($firstReference);
} else if ($inReplyTo !== null) {
// We know it is a reply to an email but we weren't able to find the thread ID: let's find it from our database
$threadId = $this->findEmailThreadId($inReplyTo);
}

// No thread ID: this is a new thread
if ($threadId === null) {
$threadId = $emailId;
Expand Down Expand Up @@ -214,10 +221,10 @@ private function parseDateTime(Message $parsedDocument): ?DateTimeInterface
return $date;
}

private function findEmailThreadId(string $inReplyTo): ?string
private function findEmailThreadId(string $targetId): ?string
{
try {
$email = $this->emailRepository->getById($inReplyTo);
$email = $this->emailRepository->getById($targetId);
} catch (NotFound) {
// We didn't find the thread, let's move on
return null;
Expand Down

0 comments on commit 0303c1b

Please sign in to comment.