2026-04-16 07:35:56 +02:00
|
|
|
/// Email header — stored locally after sync, body fetched on demand.
|
|
|
|
|
class Email {
|
|
|
|
|
final String id; // "<accountId>:<uid>"
|
|
|
|
|
final String accountId;
|
|
|
|
|
final String mailboxPath;
|
|
|
|
|
final int uid;
|
|
|
|
|
final String? subject;
|
|
|
|
|
final DateTime? sentAt;
|
|
|
|
|
final DateTime receivedAt;
|
|
|
|
|
final List<EmailAddress> from;
|
|
|
|
|
final List<EmailAddress> to;
|
|
|
|
|
final List<EmailAddress> cc;
|
|
|
|
|
final String? preview;
|
|
|
|
|
final bool isSeen;
|
|
|
|
|
final bool isFlagged;
|
|
|
|
|
final bool hasAttachment;
|
2026-04-24 15:12:04 +02:00
|
|
|
final String? threadId;
|
|
|
|
|
final String? messageId;
|
|
|
|
|
final String? inReplyTo;
|
|
|
|
|
// Space-separated RFC 2822 References header value.
|
|
|
|
|
final String? references;
|
2026-05-10 21:50:13 +02:00
|
|
|
final DateTime? snoozedUntil;
|
|
|
|
|
final String? snoozedFromMailboxPath;
|
2026-05-14 00:09:14 +02:00
|
|
|
// RFC 2369 List-Unsubscribe header value, e.g. "<mailto:...>, <https://...>".
|
|
|
|
|
final String? listUnsubscribeHeader;
|
2026-04-16 07:35:56 +02:00
|
|
|
|
|
|
|
|
const Email({
|
|
|
|
|
required this.id,
|
|
|
|
|
required this.accountId,
|
|
|
|
|
required this.mailboxPath,
|
|
|
|
|
required this.uid,
|
|
|
|
|
this.subject,
|
|
|
|
|
this.sentAt,
|
|
|
|
|
required this.receivedAt,
|
|
|
|
|
required this.from,
|
|
|
|
|
required this.to,
|
|
|
|
|
required this.cc,
|
|
|
|
|
this.preview,
|
|
|
|
|
required this.isSeen,
|
|
|
|
|
required this.isFlagged,
|
|
|
|
|
required this.hasAttachment,
|
2026-04-24 15:12:04 +02:00
|
|
|
this.threadId,
|
|
|
|
|
this.messageId,
|
|
|
|
|
this.inReplyTo,
|
|
|
|
|
this.references,
|
2026-05-10 21:50:13 +02:00
|
|
|
this.snoozedUntil,
|
|
|
|
|
this.snoozedFromMailboxPath,
|
2026-05-14 00:09:14 +02:00
|
|
|
this.listUnsubscribeHeader,
|
2026-04-24 15:12:04 +02:00
|
|
|
});
|
2026-05-09 15:35:17 +02:00
|
|
|
|
2026-05-10 17:38:43 +02:00
|
|
|
factory Email.fromJson(Map<String, dynamic> json) {
|
|
|
|
|
return Email(
|
|
|
|
|
id: json['id'] as String,
|
|
|
|
|
accountId: json['accountId'] as String,
|
|
|
|
|
mailboxPath: json['mailboxPath'] as String,
|
|
|
|
|
uid: json['uid'] as int,
|
|
|
|
|
subject: json['subject'] as String?,
|
|
|
|
|
sentAt: json['sentAt'] != null
|
|
|
|
|
? DateTime.parse(json['sentAt'] as String)
|
|
|
|
|
: null,
|
|
|
|
|
receivedAt: DateTime.parse(json['receivedAt'] as String),
|
|
|
|
|
from: (json['from'] as List<dynamic>)
|
|
|
|
|
.map((e) => EmailAddress.fromJson(e as Map<String, dynamic>))
|
|
|
|
|
.toList(),
|
|
|
|
|
to: (json['to'] as List<dynamic>)
|
|
|
|
|
.map((e) => EmailAddress.fromJson(e as Map<String, dynamic>))
|
|
|
|
|
.toList(),
|
|
|
|
|
cc: (json['cc'] as List<dynamic>)
|
|
|
|
|
.map((e) => EmailAddress.fromJson(e as Map<String, dynamic>))
|
|
|
|
|
.toList(),
|
|
|
|
|
preview: json['preview'] as String?,
|
|
|
|
|
isSeen: json['isSeen'] as bool,
|
|
|
|
|
isFlagged: json['isFlagged'] as bool,
|
|
|
|
|
hasAttachment: json['hasAttachment'] as bool,
|
|
|
|
|
threadId: json['threadId'] as String?,
|
|
|
|
|
messageId: json['messageId'] as String?,
|
|
|
|
|
inReplyTo: json['inReplyTo'] as String?,
|
|
|
|
|
references: json['references'] as String?,
|
2026-05-10 21:50:13 +02:00
|
|
|
snoozedUntil: json['snoozedUntil'] != null
|
|
|
|
|
? DateTime.parse(json['snoozedUntil'] as String)
|
|
|
|
|
: null,
|
|
|
|
|
snoozedFromMailboxPath: json['snoozedFromMailboxPath'] as String?,
|
2026-05-14 00:09:14 +02:00
|
|
|
listUnsubscribeHeader: json['listUnsubscribeHeader'] as String?,
|
2026-05-10 17:38:43 +02:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Map<String, dynamic> toJson() {
|
|
|
|
|
return {
|
|
|
|
|
'id': id,
|
|
|
|
|
'accountId': accountId,
|
|
|
|
|
'mailboxPath': mailboxPath,
|
|
|
|
|
'uid': uid,
|
|
|
|
|
'subject': subject,
|
|
|
|
|
'sentAt': sentAt?.toIso8601String(),
|
|
|
|
|
'receivedAt': receivedAt.toIso8601String(),
|
|
|
|
|
'from': from.map((e) => e.toJson()).toList(),
|
|
|
|
|
'to': to.map((e) => e.toJson()).toList(),
|
|
|
|
|
'cc': cc.map((e) => e.toJson()).toList(),
|
|
|
|
|
'preview': preview,
|
|
|
|
|
'isSeen': isSeen,
|
|
|
|
|
'isFlagged': isFlagged,
|
|
|
|
|
'hasAttachment': hasAttachment,
|
|
|
|
|
'threadId': threadId,
|
|
|
|
|
'messageId': messageId,
|
|
|
|
|
'inReplyTo': inReplyTo,
|
|
|
|
|
'references': references,
|
2026-05-10 21:50:13 +02:00
|
|
|
'snoozedUntil': snoozedUntil?.toIso8601String(),
|
|
|
|
|
'snoozedFromMailboxPath': snoozedFromMailboxPath,
|
2026-05-14 00:09:14 +02:00
|
|
|
'listUnsubscribeHeader': listUnsubscribeHeader,
|
2026-05-10 17:38:43 +02:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-09 15:35:17 +02:00
|
|
|
Email copyWith({
|
|
|
|
|
String? id,
|
|
|
|
|
String? accountId,
|
|
|
|
|
String? mailboxPath,
|
|
|
|
|
int? uid,
|
|
|
|
|
String? subject,
|
|
|
|
|
DateTime? sentAt,
|
|
|
|
|
DateTime? receivedAt,
|
|
|
|
|
List<EmailAddress>? from,
|
|
|
|
|
List<EmailAddress>? to,
|
|
|
|
|
List<EmailAddress>? cc,
|
|
|
|
|
String? preview,
|
|
|
|
|
bool? isSeen,
|
|
|
|
|
bool? isFlagged,
|
|
|
|
|
bool? hasAttachment,
|
|
|
|
|
String? threadId,
|
|
|
|
|
String? messageId,
|
|
|
|
|
String? inReplyTo,
|
|
|
|
|
String? references,
|
2026-05-10 21:50:13 +02:00
|
|
|
DateTime? snoozedUntil,
|
|
|
|
|
String? snoozedFromMailboxPath,
|
2026-05-14 00:09:14 +02:00
|
|
|
String? listUnsubscribeHeader,
|
2026-05-09 15:35:17 +02:00
|
|
|
}) {
|
|
|
|
|
return Email(
|
|
|
|
|
id: id ?? this.id,
|
|
|
|
|
accountId: accountId ?? this.accountId,
|
|
|
|
|
mailboxPath: mailboxPath ?? this.mailboxPath,
|
|
|
|
|
uid: uid ?? this.uid,
|
|
|
|
|
subject: subject ?? this.subject,
|
|
|
|
|
sentAt: sentAt ?? this.sentAt,
|
|
|
|
|
receivedAt: receivedAt ?? this.receivedAt,
|
|
|
|
|
from: from ?? this.from,
|
|
|
|
|
to: to ?? this.to,
|
|
|
|
|
cc: cc ?? this.cc,
|
|
|
|
|
preview: preview ?? this.preview,
|
|
|
|
|
isSeen: isSeen ?? this.isSeen,
|
|
|
|
|
isFlagged: isFlagged ?? this.isFlagged,
|
|
|
|
|
hasAttachment: hasAttachment ?? this.hasAttachment,
|
|
|
|
|
threadId: threadId ?? this.threadId,
|
|
|
|
|
messageId: messageId ?? this.messageId,
|
|
|
|
|
inReplyTo: inReplyTo ?? this.inReplyTo,
|
|
|
|
|
references: references ?? this.references,
|
2026-05-10 21:50:13 +02:00
|
|
|
snoozedUntil: snoozedUntil ?? this.snoozedUntil,
|
|
|
|
|
snoozedFromMailboxPath:
|
|
|
|
|
snoozedFromMailboxPath ?? this.snoozedFromMailboxPath,
|
2026-05-14 00:09:14 +02:00
|
|
|
listUnsubscribeHeader:
|
|
|
|
|
listUnsubscribeHeader ?? this.listUnsubscribeHeader,
|
2026-05-09 15:35:17 +02:00
|
|
|
);
|
|
|
|
|
}
|
2026-04-24 15:12:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// A group of related emails sharing the same thread.
|
|
|
|
|
class EmailThread {
|
|
|
|
|
final String threadId;
|
|
|
|
|
final String? subject;
|
|
|
|
|
final List<EmailAddress> participants;
|
|
|
|
|
final DateTime latestDate;
|
|
|
|
|
final int messageCount;
|
|
|
|
|
final bool hasUnread;
|
|
|
|
|
final bool isFlagged;
|
|
|
|
|
final String latestEmailId;
|
2026-04-24 16:43:35 +02:00
|
|
|
final String? preview;
|
2026-04-24 15:12:04 +02:00
|
|
|
final String accountId;
|
|
|
|
|
final String mailboxPath;
|
|
|
|
|
|
|
|
|
|
// All email IDs in this thread (oldest-first). Needed for batch operations.
|
|
|
|
|
final List<String> emailIds;
|
|
|
|
|
|
|
|
|
|
const EmailThread({
|
|
|
|
|
required this.threadId,
|
|
|
|
|
required this.subject,
|
|
|
|
|
required this.participants,
|
|
|
|
|
required this.latestDate,
|
|
|
|
|
required this.messageCount,
|
|
|
|
|
required this.hasUnread,
|
|
|
|
|
required this.isFlagged,
|
|
|
|
|
required this.latestEmailId,
|
2026-04-24 16:43:35 +02:00
|
|
|
this.preview,
|
2026-04-24 15:12:04 +02:00
|
|
|
required this.emailIds,
|
|
|
|
|
required this.accountId,
|
|
|
|
|
required this.mailboxPath,
|
2026-04-16 07:35:56 +02:00
|
|
|
});
|
2026-06-05 17:09:38 +02:00
|
|
|
|
|
|
|
|
/// Wraps a single [Email] as a one-message thread for uniform rendering.
|
|
|
|
|
factory EmailThread.fromEmail(Email e) => EmailThread(
|
|
|
|
|
threadId: e.threadId ?? e.id,
|
|
|
|
|
subject: e.subject,
|
|
|
|
|
participants: e.from,
|
|
|
|
|
latestDate: e.sentAt ?? e.receivedAt,
|
|
|
|
|
messageCount: 1,
|
|
|
|
|
hasUnread: !e.isSeen,
|
|
|
|
|
isFlagged: e.isFlagged,
|
|
|
|
|
latestEmailId: e.id,
|
|
|
|
|
preview: e.preview,
|
|
|
|
|
emailIds: [e.id],
|
|
|
|
|
accountId: e.accountId,
|
|
|
|
|
mailboxPath: e.mailboxPath,
|
|
|
|
|
);
|
2026-04-16 07:35:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class EmailAddress {
|
|
|
|
|
final String? name;
|
|
|
|
|
final String email;
|
|
|
|
|
|
|
|
|
|
const EmailAddress({this.name, required this.email});
|
|
|
|
|
|
2026-05-09 18:49:34 +02:00
|
|
|
factory EmailAddress.fromJson(Map<String, dynamic> json) {
|
|
|
|
|
return EmailAddress(
|
|
|
|
|
name: json['name'] as String?,
|
|
|
|
|
email: json['email'] as String,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Map<String, dynamic> toJson() {
|
2026-05-12 21:55:06 +02:00
|
|
|
return {if (name != null) 'name': name, 'email': email};
|
2026-05-09 18:49:34 +02:00
|
|
|
}
|
|
|
|
|
|
2026-04-16 07:35:56 +02:00
|
|
|
@override
|
|
|
|
|
String toString() => name != null ? '$name <$email>' : email;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-09 18:49:34 +02:00
|
|
|
class EmailHeader {
|
|
|
|
|
final String name;
|
|
|
|
|
final String value;
|
|
|
|
|
|
|
|
|
|
const EmailHeader({required this.name, required this.value});
|
|
|
|
|
|
|
|
|
|
factory EmailHeader.fromJson(Map<String, dynamic> json) {
|
|
|
|
|
return EmailHeader(
|
|
|
|
|
name: json['name'] as String,
|
|
|
|
|
value: json['value'] as String,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Map<String, dynamic> toJson() => {'name': name, 'value': value};
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-16 07:35:56 +02:00
|
|
|
/// Full message body — fetched on demand, cached in the local DB.
|
2026-05-15 12:53:13 +02:00
|
|
|
class MimePart {
|
|
|
|
|
final String contentType;
|
|
|
|
|
final String? filename;
|
|
|
|
|
final int? size;
|
|
|
|
|
final String? encoding;
|
|
|
|
|
final List<MimePart> children;
|
|
|
|
|
|
|
|
|
|
const MimePart({
|
|
|
|
|
required this.contentType,
|
|
|
|
|
this.filename,
|
|
|
|
|
this.size,
|
|
|
|
|
this.encoding,
|
|
|
|
|
this.children = const [],
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-16 07:35:56 +02:00
|
|
|
class EmailBody {
|
|
|
|
|
final String emailId;
|
|
|
|
|
final String? textBody;
|
|
|
|
|
final String? htmlBody;
|
|
|
|
|
final List<EmailAttachment> attachments;
|
2026-05-09 18:49:34 +02:00
|
|
|
final List<EmailHeader> headers;
|
2026-05-15 12:53:13 +02:00
|
|
|
final MimePart? mimeTree;
|
2026-04-16 07:35:56 +02:00
|
|
|
|
|
|
|
|
const EmailBody({
|
|
|
|
|
required this.emailId,
|
|
|
|
|
this.textBody,
|
|
|
|
|
this.htmlBody,
|
|
|
|
|
required this.attachments,
|
2026-05-09 18:49:34 +02:00
|
|
|
this.headers = const [],
|
2026-05-15 12:53:13 +02:00
|
|
|
this.mimeTree,
|
2026-04-16 07:35:56 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class EmailAttachment {
|
|
|
|
|
final String filename;
|
|
|
|
|
final String contentType;
|
|
|
|
|
final int size;
|
2026-04-20 18:08:09 +02:00
|
|
|
|
2026-04-18 17:04:25 +02:00
|
|
|
/// IMAP BODYSTRUCTURE part identifier (e.g. "2", "2.1") used for on-demand
|
|
|
|
|
/// download. Empty for attachments cached before this field was added.
|
|
|
|
|
final String fetchPartId;
|
2026-04-16 07:35:56 +02:00
|
|
|
|
|
|
|
|
const EmailAttachment({
|
|
|
|
|
required this.filename,
|
|
|
|
|
required this.contentType,
|
|
|
|
|
required this.size,
|
2026-04-18 17:04:25 +02:00
|
|
|
this.fetchPartId = '',
|
2026-04-16 07:35:56 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-20 06:32:33 +02:00
|
|
|
/// A pending local mutation (flag, move, delete) that has failed at least once
|
|
|
|
|
/// and may be stuck in the outbound queue.
|
|
|
|
|
class FailedMutation {
|
|
|
|
|
final int id;
|
|
|
|
|
final String accountId;
|
2026-04-20 18:08:09 +02:00
|
|
|
|
2026-04-20 06:32:33 +02:00
|
|
|
/// "flag_seen" | "flag_flagged" | "move" | "delete"
|
|
|
|
|
final String changeType;
|
|
|
|
|
final String resourceId;
|
|
|
|
|
final String lastError;
|
|
|
|
|
final int attempts;
|
|
|
|
|
final DateTime createdAt;
|
|
|
|
|
|
|
|
|
|
const FailedMutation({
|
|
|
|
|
required this.id,
|
|
|
|
|
required this.accountId,
|
|
|
|
|
required this.changeType,
|
|
|
|
|
required this.resourceId,
|
|
|
|
|
required this.lastError,
|
|
|
|
|
required this.attempts,
|
|
|
|
|
required this.createdAt,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-16 07:35:56 +02:00
|
|
|
/// Outgoing email — used for compose / reply.
|
|
|
|
|
class EmailDraft {
|
|
|
|
|
final EmailAddress from;
|
|
|
|
|
final List<EmailAddress> to;
|
|
|
|
|
final List<EmailAddress> cc;
|
|
|
|
|
final String subject;
|
|
|
|
|
final String body;
|
2026-04-20 18:08:09 +02:00
|
|
|
|
2026-04-18 17:04:25 +02:00
|
|
|
/// Local file-system paths of files to attach when sending.
|
|
|
|
|
final List<String> attachmentFilePaths;
|
2026-04-16 07:35:56 +02:00
|
|
|
|
|
|
|
|
const EmailDraft({
|
|
|
|
|
required this.from,
|
|
|
|
|
required this.to,
|
|
|
|
|
required this.cc,
|
|
|
|
|
required this.subject,
|
|
|
|
|
required this.body,
|
2026-04-18 17:04:25 +02:00
|
|
|
this.attachmentFilePaths = const [],
|
2026-04-16 07:35:56 +02:00
|
|
|
});
|
|
|
|
|
}
|
2026-04-21 11:32:50 +02:00
|
|
|
|
|
|
|
|
class SyncEmailsResult {
|
|
|
|
|
const SyncEmailsResult({
|
|
|
|
|
required this.fetched,
|
|
|
|
|
required this.skipped,
|
|
|
|
|
required this.bytesTransferred,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
final int fetched;
|
|
|
|
|
final int skipped;
|
|
|
|
|
final int bytesTransferred;
|
|
|
|
|
|
2026-05-12 21:55:06 +02:00
|
|
|
static const zero = SyncEmailsResult(
|
|
|
|
|
fetched: 0,
|
|
|
|
|
skipped: 0,
|
|
|
|
|
bytesTransferred: 0,
|
|
|
|
|
);
|
2026-04-21 11:32:50 +02:00
|
|
|
|
|
|
|
|
SyncEmailsResult operator +(SyncEmailsResult other) => SyncEmailsResult(
|
2026-06-02 17:10:16 +02:00
|
|
|
fetched: fetched + other.fetched,
|
|
|
|
|
skipped: skipped + other.skipped,
|
|
|
|
|
bytesTransferred: bytesTransferred + other.bytesTransferred,
|
|
|
|
|
);
|
2026-04-21 11:32:50 +02:00
|
|
|
}
|
2026-05-09 09:47:42 +02:00
|
|
|
|
|
|
|
|
class ReliabilityResult {
|
|
|
|
|
const ReliabilityResult({
|
|
|
|
|
required this.missingLocally,
|
|
|
|
|
required this.missingOnServer,
|
|
|
|
|
required this.flagMismatches,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
final List<String> missingLocally; // Server UIDs/IDs not in local DB
|
|
|
|
|
final List<String> missingOnServer; // Local UIDs/IDs not on server
|
|
|
|
|
final List<FlagMismatch> flagMismatches;
|
|
|
|
|
|
|
|
|
|
bool get isHealthy =>
|
|
|
|
|
missingLocally.isEmpty &&
|
|
|
|
|
missingOnServer.isEmpty &&
|
|
|
|
|
flagMismatches.isEmpty;
|
|
|
|
|
|
|
|
|
|
static const healthy = ReliabilityResult(
|
|
|
|
|
missingLocally: [],
|
|
|
|
|
missingOnServer: [],
|
|
|
|
|
flagMismatches: [],
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class FlagMismatch {
|
|
|
|
|
const FlagMismatch({
|
|
|
|
|
required this.id,
|
|
|
|
|
required this.serverSeen,
|
|
|
|
|
required this.localSeen,
|
|
|
|
|
required this.serverFlagged,
|
|
|
|
|
required this.localFlagged,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
final String id;
|
|
|
|
|
final bool serverSeen;
|
|
|
|
|
final bool localSeen;
|
|
|
|
|
final bool serverFlagged;
|
|
|
|
|
final bool localFlagged;
|
|
|
|
|
}
|