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:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user