add background body prefetch for recent messages (last 30 days)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-13 21:34:38 +01:00
parent deb4c1d15c
commit a75738a79d

View File

@@ -121,6 +121,8 @@ public final class SyncCoordinator {
emit(.newMessages(count: envelopes.count, mailbox: remoteMailbox.name))
}
await prefetchBodies(mailboxId: mailboxId)
try store.updateMailboxSync(
id: mailboxId,
uidValidity: status.uidValidity,
@@ -128,6 +130,26 @@ public final class SyncCoordinator {
)
}
/// Fetch full bodies for recent messages that don't have bodyText yet
private func prefetchBodies(mailboxId: String) async {
let thirtyDaysAgo = ISO8601DateFormatter().string(
from: Calendar.current.date(byAdding: .day, value: -30, to: Date())!
)
do {
let messages = try store.messages(mailboxId: mailboxId)
let recent = messages.filter { $0.bodyText == nil && $0.date >= thirtyDaysAgo }
for message in recent.prefix(50) {
guard !Task.isCancelled else { break }
let (text, html) = try await imapClient.fetchBody(uid: message.uid)
if text != nil || html != nil {
try store.storeBody(messageId: message.id, text: text, html: html)
}
}
} catch {
// Background prefetch failure is non-fatal
}
}
private func envelopeToRecord(
_ envelope: FetchedEnvelope, accountId: String, mailboxId: String
) -> MessageRecord {