fix(mail) : stop le spam GlitchTip de sync (double-encodage UTF7 + dossiers fantômes)
Deux causes racines généraient ~170 erreurs/cycle (toutes les 10 min) sur la prod : "syncFolder[...] listMessages failed: Folder ... not found". 1. Double-encodage UTF7-IMAP : listFolders() stocke le chemin brut UTF7-IMAP, mais ImapMailProvider rappelait getFolder($path) qui ré-encode UTF8->UTF7-IMAP (webklex Client::getFolderByPath, utf7=false). Le caractère de shift "&" était ré-encodé, rendant introuvables les dossiers à accents/specials. Fix : getFolder($path, null, utf7: true) partout dans ImapMailProvider. 2. Dossiers fantômes jamais purgés : syncFolderStructure() gardait en DB les dossiers disparus du serveur, re-tentés à chaque cycle. Fix : syncFolderStructure() retourne le set des chemins présents sur le serveur ; doSyncAll() skip silencieusement les dossiers DB absents (conservés en DB pour les liens messages/tâches). Fallback historique si listFolders échoue. Test : testSyncAllSkipsFoldersNoLongerPresentOnServer.
This commit is contained in:
@@ -64,14 +64,22 @@ final class MailSyncService
|
||||
}
|
||||
}
|
||||
|
||||
public function syncFolderStructure(): void
|
||||
/**
|
||||
* Synchronise the local folder list with the server.
|
||||
*
|
||||
* @return null|array<string, true> the set of folder paths currently present
|
||||
* on the server, or null when the remote list
|
||||
* could not be fetched (the caller then falls
|
||||
* back to syncing every known folder)
|
||||
*/
|
||||
public function syncFolderStructure(): ?array
|
||||
{
|
||||
try {
|
||||
$remoteFolders = $this->provider->listFolders();
|
||||
} catch (MailProviderException $e) {
|
||||
$this->logger->error('syncFolderStructure: listFolders failed: '.$e->getMessage());
|
||||
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
|
||||
$remotePathSet = [];
|
||||
@@ -95,16 +103,7 @@ final class MailSyncService
|
||||
|
||||
$this->entityManager->flush();
|
||||
|
||||
$allDbFolders = $this->folderRepository->findAllOrderedByPath();
|
||||
|
||||
foreach ($allDbFolders as $dbFolder) {
|
||||
if (!isset($remotePathSet[$dbFolder->getPath()])) {
|
||||
$this->logger->warning(sprintf(
|
||||
'syncFolderStructure: folder "%s" no longer exists on server — keeping in DB for safety',
|
||||
$dbFolder->getPath()
|
||||
));
|
||||
}
|
||||
}
|
||||
return $remotePathSet;
|
||||
}
|
||||
|
||||
public function syncFolder(MailFolder $folder): MailSyncReport
|
||||
@@ -259,7 +258,7 @@ final class MailSyncService
|
||||
|
||||
private function doSyncAll(DateTimeImmutable $startedAt): MailSyncReport
|
||||
{
|
||||
$this->syncFolderStructure();
|
||||
$remotePathSet = $this->syncFolderStructure();
|
||||
|
||||
$totalCreated = 0;
|
||||
$totalUpdated = 0;
|
||||
@@ -270,6 +269,13 @@ final class MailSyncService
|
||||
$folders = $this->folderRepository->findAllOrderedByPath();
|
||||
|
||||
foreach ($folders as $folder) {
|
||||
// Skip folders that no longer exist on the server. They are kept in
|
||||
// the DB (linked messages and tasks still reference them) but retrying
|
||||
// listMessages every cycle only floods the logs with "Folder not found".
|
||||
if (null !== $remotePathSet && !isset($remotePathSet[$folder->getPath()])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
$report = $this->syncFolder($folder);
|
||||
$totalCreated += $report->createdCount;
|
||||
|
||||
@@ -103,7 +103,11 @@ final class ImapMailProvider implements MailProviderInterface
|
||||
$client = $this->getClient();
|
||||
|
||||
try {
|
||||
$folder = $client->getFolder($folderPath);
|
||||
// Folder paths are stored exactly as the server returns them (raw
|
||||
// UTF7-IMAP). Pass utf7: true so webklex matches them as-is instead of
|
||||
// re-encoding UTF8 -> UTF7-IMAP, which double-encodes the "&" shift
|
||||
// character and makes folders with accents/specials unresolvable.
|
||||
$folder = $client->getFolder($folderPath, null, true);
|
||||
if (null === $folder) {
|
||||
throw MailProviderException::operationFailed('listMessages', sprintf('Folder %s not found', $folderPath));
|
||||
}
|
||||
@@ -138,7 +142,7 @@ final class ImapMailProvider implements MailProviderInterface
|
||||
$client = $this->getClient();
|
||||
|
||||
try {
|
||||
$folder = $client->getFolder($folderPath);
|
||||
$folder = $client->getFolder($folderPath, null, true);
|
||||
if (null === $folder) {
|
||||
throw MailProviderException::operationFailed('fetchMessage', sprintf('Folder %s not found', $folderPath));
|
||||
}
|
||||
@@ -183,7 +187,7 @@ final class ImapMailProvider implements MailProviderInterface
|
||||
$client = $this->getClient();
|
||||
|
||||
try {
|
||||
$folder = $client->getFolder($folderPath);
|
||||
$folder = $client->getFolder($folderPath, null, true);
|
||||
if (null === $folder) {
|
||||
throw MailProviderException::operationFailed('markRead', sprintf('Folder %s not found', $folderPath));
|
||||
}
|
||||
@@ -213,7 +217,7 @@ final class ImapMailProvider implements MailProviderInterface
|
||||
$client = $this->getClient();
|
||||
|
||||
try {
|
||||
$folder = $client->getFolder($folderPath);
|
||||
$folder = $client->getFolder($folderPath, null, true);
|
||||
if (null === $folder) {
|
||||
throw MailProviderException::operationFailed('markFlagged', sprintf('Folder %s not found', $folderPath));
|
||||
}
|
||||
@@ -243,7 +247,7 @@ final class ImapMailProvider implements MailProviderInterface
|
||||
$client = $this->getClient();
|
||||
|
||||
try {
|
||||
$folder = $client->getFolder($folderPath);
|
||||
$folder = $client->getFolder($folderPath, null, true);
|
||||
if (null === $folder) {
|
||||
throw MailProviderException::operationFailed('moveMessage', sprintf('Folder %s not found', $folderPath));
|
||||
}
|
||||
@@ -269,7 +273,7 @@ final class ImapMailProvider implements MailProviderInterface
|
||||
$client = $this->getClient();
|
||||
|
||||
try {
|
||||
$folder = $client->getFolder($folderPath);
|
||||
$folder = $client->getFolder($folderPath, null, true);
|
||||
if (null === $folder) {
|
||||
throw MailProviderException::operationFailed('fetchAttachment', sprintf('Folder %s not found', $folderPath));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user