2026-04-16 07:35:56 +02:00
|
|
|
import 'dart:async';
|
|
|
|
|
|
|
|
|
|
import 'package:enough_mail/enough_mail.dart' as imap;
|
|
|
|
|
|
|
|
|
|
import '../models/account.dart';
|
|
|
|
|
import '../repositories/account_repository.dart';
|
|
|
|
|
import '../repositories/email_repository.dart';
|
|
|
|
|
import '../repositories/mailbox_repository.dart';
|
2026-04-16 08:11:29 +02:00
|
|
|
import '../utils/logger.dart';
|
2026-04-16 07:35:56 +02:00
|
|
|
import '../../data/imap/imap_client_factory.dart';
|
|
|
|
|
|
|
|
|
|
/// Manages one IMAP IDLE connection per account.
|
2026-04-16 07:51:52 +02:00
|
|
|
/// On a new-message notification it triggers a re-sync then goes back to IDLE.
|
2026-04-16 07:35:56 +02:00
|
|
|
class AccountSyncManager {
|
|
|
|
|
AccountSyncManager(this._accounts, this._mailboxes, this._emails);
|
|
|
|
|
|
|
|
|
|
final AccountRepository _accounts;
|
|
|
|
|
final MailboxRepository _mailboxes;
|
|
|
|
|
final EmailRepository _emails;
|
|
|
|
|
|
|
|
|
|
final Map<String, _AccountSync> _active = {};
|
2026-04-16 07:51:52 +02:00
|
|
|
StreamSubscription<List<Account>>? _accountsSub;
|
2026-04-16 07:35:56 +02:00
|
|
|
|
2026-04-16 07:51:52 +02:00
|
|
|
void start() {
|
|
|
|
|
_accountsSub = _accounts.observeAccounts().listen((accounts) {
|
|
|
|
|
final currentIds = accounts.map((a) => a.id).toSet();
|
|
|
|
|
|
|
|
|
|
// Start sync for newly added accounts.
|
2026-04-16 07:35:56 +02:00
|
|
|
for (final account in accounts) {
|
|
|
|
|
if (!_active.containsKey(account.id)) {
|
2026-04-16 07:51:52 +02:00
|
|
|
final sync = _AccountSync(account, _accounts, _mailboxes, _emails);
|
|
|
|
|
_active[account.id] = sync;
|
|
|
|
|
sync.start();
|
2026-04-16 07:35:56 +02:00
|
|
|
}
|
|
|
|
|
}
|
2026-04-16 07:51:52 +02:00
|
|
|
|
|
|
|
|
// Stop sync for removed accounts.
|
2026-04-16 07:35:56 +02:00
|
|
|
for (final id in _active.keys.toList()) {
|
2026-04-16 07:51:52 +02:00
|
|
|
if (!currentIds.contains(id)) {
|
2026-04-16 07:35:56 +02:00
|
|
|
_active.remove(id)?.stop();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void dispose() {
|
2026-04-16 07:51:52 +02:00
|
|
|
_accountsSub?.cancel();
|
2026-04-16 07:35:56 +02:00
|
|
|
for (final s in _active.values) {
|
|
|
|
|
s.stop();
|
|
|
|
|
}
|
|
|
|
|
_active.clear();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class _AccountSync {
|
|
|
|
|
_AccountSync(this.account, this._accounts, this._mailboxes, this._emails);
|
|
|
|
|
|
|
|
|
|
final Account account;
|
|
|
|
|
final AccountRepository _accounts;
|
|
|
|
|
final MailboxRepository _mailboxes;
|
|
|
|
|
final EmailRepository _emails;
|
|
|
|
|
|
|
|
|
|
imap.ImapClient? _idleClient;
|
|
|
|
|
bool _running = false;
|
|
|
|
|
int _backoffSeconds = 5;
|
|
|
|
|
|
|
|
|
|
void start() {
|
|
|
|
|
_running = true;
|
|
|
|
|
_loop();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void stop() {
|
|
|
|
|
_running = false;
|
|
|
|
|
_idleClient?.logout().ignore();
|
|
|
|
|
_idleClient = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> _loop() async {
|
|
|
|
|
while (_running) {
|
|
|
|
|
try {
|
|
|
|
|
await _sync();
|
|
|
|
|
await _idle();
|
|
|
|
|
_backoffSeconds = 5;
|
2026-04-16 08:11:29 +02:00
|
|
|
} catch (e, st) {
|
|
|
|
|
log(
|
|
|
|
|
'Sync failed for ${account.email}, retrying in ${_backoffSeconds}s',
|
|
|
|
|
error: e,
|
|
|
|
|
stackTrace: st,
|
|
|
|
|
);
|
2026-04-16 07:35:56 +02:00
|
|
|
await Future.delayed(Duration(seconds: _backoffSeconds));
|
|
|
|
|
_backoffSeconds = (_backoffSeconds * 2).clamp(5, 300);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> _sync() async {
|
|
|
|
|
await _mailboxes.syncMailboxes(account.id);
|
|
|
|
|
await _emails.syncEmails(account.id, 'INBOX');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> _idle() async {
|
|
|
|
|
if (!_running) return;
|
|
|
|
|
final password = await _accounts.getPassword(account.id);
|
|
|
|
|
final client = await connectImap(account, password);
|
|
|
|
|
_idleClient = client;
|
2026-04-16 07:51:52 +02:00
|
|
|
try {
|
|
|
|
|
await client.selectMailboxByPath('INBOX');
|
|
|
|
|
|
|
|
|
|
final newMessageCompleter = Completer<void>();
|
|
|
|
|
|
|
|
|
|
// Wake up when new messages arrive or messages are expunged.
|
|
|
|
|
final sub = client.eventBus
|
|
|
|
|
.on<imap.ImapEvent>()
|
|
|
|
|
.where(
|
|
|
|
|
(e) =>
|
|
|
|
|
e is imap.ImapMessagesExistEvent ||
|
|
|
|
|
e is imap.ImapExpungeEvent,
|
|
|
|
|
)
|
|
|
|
|
.listen((_) {
|
|
|
|
|
if (!newMessageCompleter.isCompleted) newMessageCompleter.complete();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await client.idleStart();
|
|
|
|
|
|
|
|
|
|
// Cap IDLE at 25 minutes to stay within the RFC 2177 recommendation.
|
|
|
|
|
await Future.any([
|
|
|
|
|
newMessageCompleter.future,
|
|
|
|
|
Future.delayed(const Duration(minutes: 25)),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
await client.idleDone();
|
|
|
|
|
await sub.cancel();
|
|
|
|
|
} finally {
|
|
|
|
|
await client.logout();
|
|
|
|
|
_idleClient = null;
|
|
|
|
|
}
|
2026-04-16 07:35:56 +02:00
|
|
|
}
|
|
|
|
|
}
|