-
Notifications
You must be signed in to change notification settings - Fork 327
Expand file tree
/
Copy pathImapToDbSynchronizer.php
More file actions
599 lines (536 loc) Β· 20 KB
/
Copy pathImapToDbSynchronizer.php
File metadata and controls
599 lines (536 loc) Β· 20 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Mail\Service\Sync;
use Horde_Imap_Client;
use Horde_Imap_Client_Base;
use Horde_Imap_Client_Exception;
use Horde_Imap_Client_Ids;
use OCA\Mail\Account;
use OCA\Mail\Contracts\IMailManager;
use OCA\Mail\Db\Mailbox;
use OCA\Mail\Db\MailboxMapper;
use OCA\Mail\Db\MessageMapper as DatabaseMessageMapper;
use OCA\Mail\Db\Tag;
use OCA\Mail\Db\TagMapper;
use OCA\Mail\Events\NewMessagesSynchronized;
use OCA\Mail\Events\SynchronizationEvent;
use OCA\Mail\Exception\ClientException;
use OCA\Mail\Exception\IncompleteSyncException;
use OCA\Mail\Exception\MailboxDoesNotSupportModSequencesException;
use OCA\Mail\Exception\MailboxLockedException;
use OCA\Mail\Exception\MailboxNotCachedException;
use OCA\Mail\Exception\ServiceException;
use OCA\Mail\Exception\UidValidityChangedException;
use OCA\Mail\IMAP\IMAPClientFactory;
use OCA\Mail\IMAP\MessageMapper as ImapMessageMapper;
use OCA\Mail\IMAP\Sync\Request;
use OCA\Mail\IMAP\Sync\Synchronizer;
use OCA\Mail\Model\IMAPMessage;
use OCA\Mail\Service\Classification\NewMessagesClassifier;
use OCA\Mail\Support\PerformanceLogger;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\EventDispatcher\IEventDispatcher;
use Psr\Log\LoggerInterface;
use Throwable;
use function array_chunk;
use function array_filter;
use function array_map;
use function register_shutdown_function;
use function sprintf;
class ImapToDbSynchronizer {
/** @var int */
public const MAX_NEW_MESSAGES = 5000;
/** @var DatabaseMessageMapper */
private $dbMapper;
/** @var IMAPClientFactory */
private $clientFactory;
/** @var ImapMessageMapper */
private $imapMapper;
/** @var MailboxMapper */
private $mailboxMapper;
/** @var Synchronizer */
private $synchronizer;
/** @var IEventDispatcher */
private $dispatcher;
/** @var PerformanceLogger */
private $performanceLogger;
/** @var LoggerInterface */
private $logger;
/** @var IMailManager */
private $mailManager;
private TagMapper $tagMapper;
private NewMessagesClassifier $newMessagesClassifier;
public function __construct(DatabaseMessageMapper $dbMapper,
IMAPClientFactory $clientFactory,
ImapMessageMapper $imapMapper,
MailboxMapper $mailboxMapper,
DatabaseMessageMapper $messageMapper,
Synchronizer $synchronizer,
IEventDispatcher $dispatcher,
PerformanceLogger $performanceLogger,
LoggerInterface $logger,
IMailManager $mailManager,
TagMapper $tagMapper,
NewMessagesClassifier $newMessagesClassifier) {
$this->dbMapper = $dbMapper;
$this->clientFactory = $clientFactory;
$this->imapMapper = $imapMapper;
$this->mailboxMapper = $mailboxMapper;
$this->synchronizer = $synchronizer;
$this->dispatcher = $dispatcher;
$this->performanceLogger = $performanceLogger;
$this->logger = $logger;
$this->mailManager = $mailManager;
$this->tagMapper = $tagMapper;
$this->newMessagesClassifier = $newMessagesClassifier;
}
/**
* @throws ClientException
* @throws ServiceException
*/
public function syncAccount(Account $account,
LoggerInterface $logger,
bool $force = false,
int $criteria = Horde_Imap_Client::SYNC_NEWMSGSUIDS | Horde_Imap_Client::SYNC_FLAGSUIDS | Horde_Imap_Client::SYNC_VANISHEDUIDS): void {
$rebuildThreads = false;
$trashMailboxId = $account->getMailAccount()->getTrashMailboxId();
$snoozeMailboxId = $account->getMailAccount()->getSnoozeMailboxId();
$sentMailboxId = $account->getMailAccount()->getSentMailboxId();
$trashRetentionDays = $account->getMailAccount()->getTrashRetentionDays();
$client = $this->clientFactory->getClient($account);
foreach ($this->mailboxMapper->findAll($account) as $mailbox) {
$syncTrash = $trashMailboxId === $mailbox->getId() && $trashRetentionDays !== null;
$syncSnooze = $snoozeMailboxId === $mailbox->getId();
$syncSent = $sentMailboxId === $mailbox->getId() || $mailbox->isSpecialUse('sent');
if (!$syncTrash && !$mailbox->isInbox() && !$syncSnooze && !$mailbox->getSyncInBackground() && !$syncSent) {
$logger->debug('Skipping mailbox sync for ' . $mailbox->getId());
continue;
}
$logger->debug('Syncing ' . $mailbox->getId());
if ($this->sync(
$account,
$client,
$mailbox,
$logger,
$criteria,
null,
$force,
true
)) {
$rebuildThreads = true;
}
}
$client->logout();
$this->dispatcher->dispatchTyped(
new SynchronizationEvent(
$account,
$logger,
$rebuildThreads,
)
);
}
/**
* Clear all cached data of a mailbox
*
* @param Account $account
* @param Mailbox $mailbox
*
* @throws MailboxLockedException
* @throws ServiceException
*/
public function clearCache(Account $account,
Mailbox $mailbox): void {
$id = $account->getId() . ':' . $mailbox->getName();
try {
$this->mailboxMapper->lockForNewSync($mailbox);
$this->mailboxMapper->lockForChangeSync($mailbox);
$this->mailboxMapper->lockForVanishedSync($mailbox);
$this->resetCache($account, $mailbox);
} catch (Throwable $e) {
throw new ServiceException("Could not clear mailbox cache for $id: " . $e->getMessage(), 0, $e);
} finally {
$this->mailboxMapper->unlockFromNewSync($mailbox);
$this->mailboxMapper->unlockFromChangedSync($mailbox);
$this->mailboxMapper->unlockFromVanishedSync($mailbox);
}
}
/**
* Wipe all cached messages of a mailbox from the database
*
* Warning: the caller has to ensure the mailbox is locked
*
* @param Account $account
* @param Mailbox $mailbox
*/
private function resetCache(Account $account, Mailbox $mailbox): void {
$id = $account->getId() . ':' . $mailbox->getName();
$this->dbMapper->deleteAll($mailbox);
$this->logger->debug("All messages of $id cleared");
$mailbox->setSyncNewToken(null);
$mailbox->setSyncChangedToken(null);
$mailbox->setSyncVanishedToken(null);
$this->mailboxMapper->update($mailbox);
}
/**
* @throws ClientException
* @throws MailboxNotCachedException
* @throws ServiceException
* @return bool whether to rebuild threads or not
*/
public function sync(Account $account,
Horde_Imap_Client_Base $client,
Mailbox $mailbox,
LoggerInterface $logger,
int $criteria = Horde_Imap_Client::SYNC_NEWMSGSUIDS | Horde_Imap_Client::SYNC_FLAGSUIDS | Horde_Imap_Client::SYNC_VANISHEDUIDS,
?array $knownUids = null,
bool $force = false,
bool $batchSync = false): bool {
$rebuildThreads = true;
if ($mailbox->getSelectable() === false) {
return $rebuildThreads;
}
$client->login(); // Need to login before fetching capabilities.
// There is no partial sync when using QRESYNC. As per RFC the client will always pull
// all changes. This is a cheap operation when using QRESYNC as the server keeps track
// of a client's state through the sync token. We could just update the sync tokens and
// call it a day because Horde caches unrelated/unrequested changes until the next
// operation. However, our cache is not reliable as some instance might use APCu which
// isn't shared between cron and web requests.
$hasQresync = false;
if ($client->capability->isEnabled('QRESYNC')) {
$this->logger->debug('Forcing full sync due to QRESYNC');
$hasQresync = true;
$criteria |= Horde_Imap_Client::SYNC_NEWMSGSUIDS
| Horde_Imap_Client::SYNC_FLAGSUIDS
| Horde_Imap_Client::SYNC_VANISHEDUIDS;
}
if ($force || ($criteria & Horde_Imap_Client::SYNC_NEWMSGSUIDS)) {
$logger->debug('Locking mailbox ' . $mailbox->getId() . ' for new messages sync');
$this->mailboxMapper->lockForNewSync($mailbox);
}
if ($force || ($criteria & Horde_Imap_Client::SYNC_FLAGSUIDS)) {
$logger->debug('Locking mailbox ' . $mailbox->getId() . ' for changed messages sync');
$this->mailboxMapper->lockForChangeSync($mailbox);
}
if ($force || ($criteria & Horde_Imap_Client::SYNC_VANISHEDUIDS)) {
$logger->debug('Locking mailbox ' . $mailbox->getId() . ' for vanished messages sync');
$this->mailboxMapper->lockForVanishedSync($mailbox);
}
register_shutdown_function(function () use ($force, $criteria, $logger, $mailbox) {
$this->unlockMailbox($force, $criteria, $logger, $mailbox);
});
try {
if ($force
|| $mailbox->getSyncNewToken() === null
|| $mailbox->getSyncChangedToken() === null
|| $mailbox->getSyncVanishedToken() === null) {
$logger->debug('Running initial sync for ' . $mailbox->getId());
$this->runInitialSync($client, $account, $mailbox, $logger);
} else {
try {
$logger->debug('Running partial sync for ' . $mailbox->getId() . ' with criteria ' . $criteria);
// Only rebuild threads if there were new or vanished messages
$rebuildThreads = $this->runPartialSync($client, $account, $mailbox, $logger, $hasQresync, $criteria, $knownUids);
} catch (UidValidityChangedException $e) {
$logger->warning('Mailbox UID validity changed. Wiping cache and performing full sync for ' . $mailbox->getId());
$this->resetCache($account, $mailbox);
$logger->debug('Running initial sync for ' . $mailbox->getId() . ' after cache reset');
$this->runInitialSync($client, $account, $mailbox, $logger);
} catch (MailboxDoesNotSupportModSequencesException $e) {
$logger->warning('Mailbox does not support mod-sequences error occured. Wiping cache and performing full sync for ' . $mailbox->getId(), [
'exception' => $e,
]);
$this->resetCache($account, $mailbox);
$logger->debug('Running initial sync for ' . $mailbox->getId() . ' after cache reset - no mod-sequences error');
$this->runInitialSync($client, $account, $mailbox, $logger);
}
}
} catch (ServiceException $e) {
// Just rethrow, don't wrap into another exception
throw $e;
} catch (Throwable $e) {
throw new ServiceException('Sync failed for ' . $account->getId() . ':' . $mailbox->getName() . ': ' . $e->getMessage(), 0, $e);
} finally {
$this->unlockMailbox($force, $criteria, $logger, $mailbox);
}
if (!$batchSync) {
$this->dispatcher->dispatchTyped(
new SynchronizationEvent(
$account,
$this->logger,
$rebuildThreads,
)
);
}
return $rebuildThreads;
}
private function unlockMailbox(bool $force, int $criteria, LoggerInterface $logger, Mailbox $mailbox): void {
if (($force || ($criteria & Horde_Imap_Client::SYNC_VANISHEDUIDS))
&& $mailbox->getSyncVanishedLock() !== null) {
$logger->debug('Unlocking mailbox ' . $mailbox->getId() . ' from vanished messages sync');
$this->mailboxMapper->unlockFromVanishedSync($mailbox);
}
if (($force || ($criteria & Horde_Imap_Client::SYNC_FLAGSUIDS))
&& $mailbox->getSyncChangedLock() !== null) {
$logger->debug('Unlocking mailbox ' . $mailbox->getId() . ' from changed messages sync');
$this->mailboxMapper->unlockFromChangedSync($mailbox);
}
if (($force || ($criteria & Horde_Imap_Client::SYNC_NEWMSGSUIDS))
&& $mailbox->getSyncNewLock() !== null) {
$logger->debug('Unlocking mailbox ' . $mailbox->getId() . ' from new messages sync');
$this->mailboxMapper->unlockFromNewSync($mailbox);
}
}
/**
* @throws ServiceException
* @throws IncompleteSyncException
*/
private function runInitialSync(
Horde_Imap_Client_Base $client,
Account $account,
Mailbox $mailbox,
LoggerInterface $logger): void {
$perf = $this->performanceLogger->startWithLogger(
'Initial sync ' . $account->getId() . ':' . $mailbox->getName(),
$logger
);
// Use a no-cache client for findAll. Horde accumulates cache state on
// every iteration of the findAll loop, causing a memory leak on large
// mailboxes (commit e50c214ff). Do NOT logout $client β the caller owns
// it and we need it below for getSyncToken.
$noCacheClient = $this->clientFactory->getClient($account, false);
try {
$highestKnownUid = $this->dbMapper->findHighestUid($mailbox);
try {
$imapMessages = $this->imapMapper->findAll(
$noCacheClient,
$mailbox->getName(),
self::MAX_NEW_MESSAGES,
$highestKnownUid ?? 0,
$logger,
$perf,
$account->getUserId(),
);
$perf->step(sprintf('fetch %d messages from IMAP', count($imapMessages)));
} catch (Horde_Imap_Client_Exception $e) {
throw new ServiceException('Can not get messages from mailbox ' . $mailbox->getName() . ': ' . $e->getMessage(), 0, $e);
}
foreach (array_chunk($imapMessages['messages'], 500) as $chunk) {
$messages = array_map(static fn (IMAPMessage $imapMessage) => $imapMessage->toDbMessage($mailbox->getId(), $account->getMailAccount()), $chunk);
$this->dbMapper->insertBulk($account, ...$messages);
$perf->step(sprintf('persist %d messages in database', count($chunk)));
// Free the memory
unset($messages);
}
if (!$imapMessages['all']) {
// We might need more attempts to fill the cache
$loggingMailboxId = $account->getId() . ':' . $mailbox->getName();
$total = $imapMessages['total'];
$cached = count($this->dbMapper->findAllUids($mailbox));
$perf->step('find number of cached UIDs');
$perf->end();
throw new IncompleteSyncException("Initial sync is not complete for $loggingMailboxId ($cached of $total messages cached).");
}
} finally {
$noCacheClient->logout();
}
// Use the cache-enabled $client passed in for the sync token. The no-cache
// client does not activate CONDSTORE/QRESYNC (commit 7980d9e40), so its
// token lacks HIGHESTMODSEQ β causing the first partial sync to resolve ALL
// UIDs and run OOM on large mailboxes. Do NOT logout $client here; the
// caller (syncAccount) owns and closes it.
$syncToken = $client->getSyncToken($mailbox->getName());
$mailbox->setSyncNewToken($syncToken);
$mailbox->setSyncChangedToken($syncToken);
$mailbox->setSyncVanishedToken($syncToken);
$this->mailboxMapper->update($mailbox);
$perf->end();
}
/**
* @param int[] $knownUids
*
* @throws ServiceException
* @throws UidValidityChangedException
* @return bool whether there are new or vanished messages
*/
private function runPartialSync(
Horde_Imap_Client_Base $client,
Account $account,
Mailbox $mailbox,
LoggerInterface $logger,
bool $hasQresync,
int $criteria,
?array $knownUids = null): bool {
$newOrVanished = false;
$perf = $this->performanceLogger->startWithLogger(
'partial sync ' . $account->getId() . ':' . $mailbox->getName(),
$logger
);
$uids = $knownUids ?? $this->dbMapper->findAllUids($mailbox);
$perf->step('get all known UIDs');
$requestId = base64_encode(random_bytes(16));
if ($criteria & Horde_Imap_Client::SYNC_NEWMSGSUIDS) {
$response = $this->synchronizer->sync(
$client,
new Request(
$requestId,
$mailbox->getName(),
$mailbox->getSyncNewToken(),
$uids
),
$account->getUserId(),
$hasQresync,
$logger,
Horde_Imap_Client::SYNC_NEWMSGSUIDS,
);
$perf->step('get new messages via Horde');
$highestKnownUid = $this->dbMapper->findHighestUid($mailbox);
if ($highestKnownUid === null) {
// Everything is relevant
$newMessages = $response->getNewMessages();
} else {
// Filter out anything that is already in the DB. Ideally this never happens, but if there is an error
// during a consecutive chunk INSERT, the sync token won't be updated. In that case the same message(s)
// will be seen as *new* and therefore cause conflicts.
$newMessages = array_filter($response->getNewMessages(), static fn (IMAPMessage $imapMessage) => $imapMessage->getUid() > $highestKnownUid);
}
$importantTag = null;
try {
$importantTag = $this->tagMapper->getTagByImapLabel(Tag::LABEL_IMPORTANT, $account->getUserId());
} catch (DoesNotExistException $e) {
$this->logger->error('Could not find important tag for ' . $account->getUserId() . ' ' . $e->getMessage(), [
'exception' => $e,
]);
}
foreach (array_chunk($newMessages, 500) as $chunk) {
$dbMessages = array_map(static fn (IMAPMessage $imapMessage) => $imapMessage->toDbMessage($mailbox->getId(), $account->getMailAccount()), $chunk);
$this->dbMapper->insertBulk($account, ...$dbMessages);
if ($importantTag) {
$classified = $this->newMessagesClassifier->classifyNewMessages(
$dbMessages,
$mailbox,
$account,
$importantTag,
);
if ($classified) {
$perf->step('classified a chunk of new messages');
} else {
$perf->step('skipped classification');
}
}
$this->dispatcher->dispatch(
NewMessagesSynchronized::class,
new NewMessagesSynchronized($account, $mailbox, $dbMessages)
);
$perf->step('emitted NewMessagesSynchronized event');
}
$perf->step('persist new messages');
$mailbox->setSyncNewToken($client->getSyncToken($mailbox->getName()));
$newOrVanished = $newMessages !== [];
}
if ($criteria & Horde_Imap_Client::SYNC_FLAGSUIDS) {
$response = $this->synchronizer->sync(
$client,
new Request(
$requestId,
$mailbox->getName(),
$mailbox->getSyncChangedToken(),
$uids
),
$account->getUserId(),
$hasQresync,
$logger,
Horde_Imap_Client::SYNC_FLAGSUIDS,
);
$perf->step('get changed messages via Horde');
$permflagsEnabled = $this->mailManager->isPermflagsEnabled($client, $account, $mailbox->getName());
foreach (array_chunk($response->getChangedMessages(), 500) as $chunk) {
$this->dbMapper->updateBulk($account, $permflagsEnabled, ...array_map(static fn (IMAPMessage $imapMessage) => $imapMessage->toDbMessage($mailbox->getId(), $account->getMailAccount()), $chunk));
}
$perf->step('persist changed messages');
// If a list of UIDs was *provided* (as opposed to loaded from the DB,
// we can not assume that all changes were detected, hence this is kinda
// a silent sync and we don't update the change token until the next full
// mailbox sync
if ($knownUids === null) {
$mailbox->setSyncChangedToken($client->getSyncToken($mailbox->getName()));
}
}
if ($criteria & Horde_Imap_Client::SYNC_VANISHEDUIDS) {
$response = $this->synchronizer->sync(
$client,
new Request(
$requestId,
$mailbox->getName(),
$mailbox->getSyncVanishedToken(),
$uids
),
$account->getUserId(),
$hasQresync,
$logger,
Horde_Imap_Client::SYNC_VANISHEDUIDS,
);
$perf->step('get vanished messages via Horde');
foreach (array_chunk($response->getVanishedMessageUids(), 500) as $chunk) {
$this->dbMapper->deleteByUid($mailbox, ...$chunk);
}
$perf->step('delete vanished messages');
// If a list of UIDs was *provided* (as opposed to loaded from the DB,
// we can not assume that all changes were detected, hence this is kinda
// a silent sync and we don't update the vanish token until the next full
// mailbox sync
if ($knownUids === null) {
$mailbox->setSyncVanishedToken($client->getSyncToken($mailbox->getName()));
}
$newOrVanished = $newOrVanished || !empty($response->getVanishedMessageUids());
}
$this->mailboxMapper->update($mailbox);
$perf->end();
return $newOrVanished;
}
/**
* Run a (rather costly) sync to delete cached messages which are not present on IMAP anymore.
*
* @throws MailboxLockedException
* @throws ServiceException
*/
public function repairSync(
Account $account,
Mailbox $mailbox,
LoggerInterface $logger,
): void {
$this->mailboxMapper->lockForVanishedSync($mailbox);
$perf = $this->performanceLogger->startWithLogger(
'Repair sync for ' . $account->getId() . ':' . $mailbox->getName(),
$logger,
);
// Need to use a client without a cache here (to disable QRESYNC entirely)
$client = $this->clientFactory->getClient($account, false);
try {
$knownUids = $this->dbMapper->findAllUids($mailbox);
$hordeMailbox = new \Horde_Imap_Client_Mailbox($mailbox->getName());
$phantomVanishedUids = $client->vanished($hordeMailbox, 0, [
'ids' => new Horde_Imap_Client_Ids($knownUids),
])->ids;
if (count($phantomVanishedUids) > 0) {
$this->dbMapper->deleteByUid($mailbox, ...$phantomVanishedUids);
}
} catch (Throwable $e) {
$message = sprintf(
'Repair sync failed for %d:%s: %s',
$account->getId(),
$mailbox->getName(),
$e->getMessage(),
);
throw new ServiceException($message, 0, $e);
} finally {
$this->mailboxMapper->unlockFromVanishedSync($mailbox);
$client->logout();
}
$perf->end();
}
}