- enough_mail: use uidFetchMessage/uidMarkSeen/uidMarkFlagged/uidMove/
uidMarkDeleted/uidExpunge, remove non-existent isUidSequence param,
fix SmtpClient construction and use quit() not disconnect()
- Drift: add @DataClassName('MailboxRow') to avoid ugly 'Mailboxe',
alias core model imports to resolve type name conflicts
- EmailsCompanion.insert: uid/receivedAt are required, not Value<T>
- Lint: remove unrecognised rules (prefer_const_collections,
avoid_returning_null_for_future), add missing mounted guards after await
- Tests: fix html_utils expectations to match trim() behaviour,
add explicit Map casts in email_model_test for avoid_dynamic_calls
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
77 lines
2.4 KiB
Dart
77 lines
2.4 KiB
Dart
import 'package:drift/drift.dart';
|
|
import 'package:enough_mail/enough_mail.dart' as imap;
|
|
|
|
import '../../core/models/mailbox.dart' as model;
|
|
import '../../core/repositories/account_repository.dart';
|
|
import '../../core/repositories/mailbox_repository.dart';
|
|
import '../../core/utils/logger.dart';
|
|
import '../db/database.dart';
|
|
import '../imap/imap_client_factory.dart';
|
|
|
|
class MailboxRepositoryImpl implements MailboxRepository {
|
|
MailboxRepositoryImpl(this._db, this._accounts);
|
|
|
|
final AppDatabase _db;
|
|
final AccountRepository _accounts;
|
|
|
|
@override
|
|
Stream<List<model.Mailbox>> observeMailboxes(String accountId) {
|
|
return (_db.select(_db.mailboxes)
|
|
..where((t) => t.accountId.equals(accountId))
|
|
..orderBy([(t) => OrderingTerm.asc(t.path)]))
|
|
.watch()
|
|
.map((rows) => rows.map(_toModel).toList());
|
|
}
|
|
|
|
@override
|
|
Future<void> syncMailboxes(String accountId) async {
|
|
final account = (await _accounts.getAccount(accountId))!;
|
|
final password = await _accounts.getPassword(accountId);
|
|
final client = await connectImap(account, password);
|
|
try {
|
|
final mailboxes = await client.listMailboxes(recursive: true);
|
|
for (final mb in mailboxes) {
|
|
final path = mb.path;
|
|
final id = '$accountId:$path';
|
|
|
|
// Fetch STATUS (unread + total counts). Some mailboxes (\Noselect)
|
|
// can't be selected — skip counts for those silently.
|
|
var unread = 0;
|
|
var total = 0;
|
|
try {
|
|
final status = await client.statusMailbox(
|
|
mb,
|
|
[imap.StatusFlags.messages, imap.StatusFlags.unseen],
|
|
);
|
|
unread = status.messagesUnseen;
|
|
total = status.messagesExists;
|
|
} catch (e) {
|
|
log('STATUS skipped for $path: $e');
|
|
}
|
|
|
|
await _db.into(_db.mailboxes).insertOnConflictUpdate(
|
|
MailboxesCompanion.insert(
|
|
id: id,
|
|
accountId: accountId,
|
|
path: path,
|
|
name: mb.name,
|
|
unreadCount: Value(unread),
|
|
totalCount: Value(total),
|
|
),
|
|
);
|
|
}
|
|
} finally {
|
|
await client.logout();
|
|
}
|
|
}
|
|
|
|
model.Mailbox _toModel(MailboxRow row) => model.Mailbox(
|
|
id: row.id,
|
|
accountId: row.accountId,
|
|
path: row.path,
|
|
name: row.name,
|
|
unreadCount: row.unreadCount,
|
|
totalCount: row.totalCount,
|
|
);
|
|
}
|