Files
sharedinbox/lib/core/sync/account_sync_manager.dart
T
Thomas GüttlerandClaude Sonnet 4.6 72e2b599bf Fix API mismatches, add Linux desktop entry point, reply prefill
API fixes (against vendored enough_mail 2.1.7):
- listMailboxes() returns List<Mailbox> directly — remove .mailboxes
- Use statusMailbox() for unread/total counts per mailbox
- fetchMessages(MessageSequence.fromAll(), ...) replaces nonexistent
  fetchAllMessages(); fetchMessage() takes isUidSequence flag
- FetchImapResult.messages are already MimeMessages — no need to
  re-parse rawData; use msg.decodeTextPlainPart() / decodeTextHtmlPart()
- msg.hasAttachments() (method) not msg.body?.hasAttachments (field)
- SmtpClient clientDomain = sender domain, not display name; quit()
  instead of nonexistent disconnect(); STARTTLS wrapped in try/catch
- ContentInfo.size is nullable; use a.fileName / a.size getters

Other fixes:
- main.dart: move sync start to initState, not build()
- account_list_screen: remove dead/invalid Riverpod select() code
- account_sync_manager: subscribe to account changes; cancel sub on
  dispose; use Future.any([newMsg, 25-min timeout]) for IDLE
- email_repository: add getEmail(id) to interface + impl
- email_detail_screen: load header + body together via Future.wait;
  reply prefills To/Cc/Subject correctly
- compose_screen + router: thread prefillCc through

Add Linux desktop entry point:
- linux/CMakeLists.txt, main.cc, my_application.h/.cc (GTK3 runner)

Add flake.lock (generated by nix flake update).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-16 07:51:52 +02:00

133 lines
3.4 KiB
Dart

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';
import '../../data/imap/imap_client_factory.dart';
/// Manages one IMAP IDLE connection per account.
/// On a new-message notification it triggers a re-sync then goes back to IDLE.
class AccountSyncManager {
AccountSyncManager(this._accounts, this._mailboxes, this._emails);
final AccountRepository _accounts;
final MailboxRepository _mailboxes;
final EmailRepository _emails;
final Map<String, _AccountSync> _active = {};
StreamSubscription<List<Account>>? _accountsSub;
void start() {
_accountsSub = _accounts.observeAccounts().listen((accounts) {
final currentIds = accounts.map((a) => a.id).toSet();
// Start sync for newly added accounts.
for (final account in accounts) {
if (!_active.containsKey(account.id)) {
final sync = _AccountSync(account, _accounts, _mailboxes, _emails);
_active[account.id] = sync;
sync.start();
}
}
// Stop sync for removed accounts.
for (final id in _active.keys.toList()) {
if (!currentIds.contains(id)) {
_active.remove(id)?.stop();
}
}
});
}
void dispose() {
_accountsSub?.cancel();
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;
} catch (_) {
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;
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;
}
}
}