Files
sharedinbox/lib/data/repositories/mailbox_repository_impl.dart
T
Thomas GüttlerandClaude Sonnet 4.6 03d35387f7 Linting, tests, README, CI, and code quality improvements
Linting:
- analysis_options.yaml: flutter_lints base + 20 additional rules
  (prefer_single_quotes, avoid_print, cancel_subscriptions,
  unawaited_futures, empty_catches, always_declare_return_types, …)
- unused_import / dead_code treated as errors

pubspec.yaml:
- Remove 6 unused packages: freezed, freezed_annotation, json_annotation,
  json_serializable, riverpod_generator, collection
- Add test: ^1.25.0 for pure-Dart unit tests

Testable utilities (extracted from screen code):
- lib/core/utils/html_utils.dart — htmlToPlain()
- lib/core/utils/format_utils.dart — fmtSize()
- lib/core/utils/logger.dart — thin debugPrint wrapper

Unit tests (test/unit/, no device required):
- html_utils_test.dart — 13 cases covering tags, entities, edge cases
- format_utils_test.dart — B / KB / MB / GB formatting
- email_model_test.dart — EmailAddress JSON roundtrip, toString, EmailDraft
- account_sync_backoff_test.dart — exponential backoff + clamping

Taskfile:
- task test → dart test test/unit/ (fast, no device)
- task test-flutter → flutter test (widget tests)
- task check → analyze + unit tests in parallel

README rewrite:
- Covers user flow (download, add account)
- Developer flow: nix develop, direnv allow, task codegen, task check
- Platform status table (Linux done, others pending)
- Project layout diagram
- Schema-change workflow

CI (.github/workflows/ci.yml):
- analyze-and-test job: flutter analyze + dart test on every push/PR
- integration job: Nix + Stalwart on push to main
- build-linux job: flutter build linux --release

Logging:
- Replace all bare catch (_) {} with log() calls
- Sync backoff errors now print account email + retry delay
- STATUS failures on \Noselect mailboxes logged at debug level
- STARTTLS failures logged before continuing without TLS

.gitignore:
- Add .direnv/, .flutter-plugins-dependencies
- Add all platform generated-plugin wiring files
- Add .env / .env.local
- Add linux/build/

.env.example: documents optional STALWART_PORT overrides

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-16 08:11:29 +02:00

80 lines
2.5 KiB
Dart

import 'package:drift/drift.dart';
import 'package:enough_mail/enough_mail.dart' as imap;
import '../../core/models/mailbox.dart';
import '../../core/repositories/account_repository.dart';
import '../../core/repositories/mailbox_repository.dart';
import '../db/database.dart';
import '../db/database.dart' as db show Mailbox;
import '../../core/utils/logger.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<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 {
// listMailboxes() returns List<imap.Mailbox>
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) for each mailbox.
// Suppress errors — some mailboxes (e.g. \Noselect) can't be selected.
int unread = 0;
int total = 0;
try {
final status = await client.statusMailbox(
mb,
[imap.StatusFlags.messages, imap.StatusFlags.unseen],
);
unread = status.messagesUnseen;
total = status.messagesExists;
} catch (e) {
// \Noselect mailboxes can't be STATUSed — skip counts silently.
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();
}
}
Mailbox _toModel(db.Mailbox row) => Mailbox(
id: row.id,
accountId: row.accountId,
path: row.path,
name: row.name,
unreadCount: row.unreadCount,
totalCount: row.totalCount,
);
}