remove inline body fetch from fetchEnvelopes, bodies now come from MIME-aware prefetchBodies

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-03-14 13:41:33 +01:00
parent 9a42ae9574
commit 004d1ad027

View File

@@ -74,7 +74,6 @@ public actor IMAPClient: IMAPClientProtocol {
.flags,
.uid,
.rfc822Size,
.bodySection(peek: true, SectionSpecifier(kind: .text), nil),
],
[]
))
@@ -417,24 +416,16 @@ public actor IMAPClient: IMAPClientProtocol {
var currentEnvelope: Envelope?
var currentFlags: [Flag] = []
var currentSize: Int = 0
var bodyBuffer = ByteBuffer()
for response in responses {
switch response {
case .fetch(let fetchResponse):
switch fetchResponse {
case .start:
case .start, .startUID:
currentUID = nil
currentEnvelope = nil
currentFlags = []
currentSize = 0
bodyBuffer = ByteBuffer()
case .startUID:
currentUID = nil
currentEnvelope = nil
currentFlags = []
currentSize = 0
bodyBuffer = ByteBuffer()
case .simpleAttribute(let attr):
switch attr {
case .uid(let uid):
@@ -448,12 +439,7 @@ public actor IMAPClient: IMAPClientProtocol {
default:
break
}
case .streamingBegin:
break
case .streamingBytes(let bytes):
var mutableBytes = bytes
bodyBuffer.writeBuffer(&mutableBytes)
case .streamingEnd:
case .streamingBegin, .streamingBytes, .streamingEnd:
break
case .finish:
if let uid = currentUID {
@@ -461,8 +447,7 @@ public actor IMAPClient: IMAPClientProtocol {
uid: uid,
envelope: currentEnvelope,
flags: currentFlags,
size: currentSize,
bodyBuffer: bodyBuffer
size: currentSize
)
envelopes.append(envelope)
}
@@ -566,8 +551,7 @@ public actor IMAPClient: IMAPClientProtocol {
uid: Int,
envelope: Envelope?,
flags: [Flag],
size: Int,
bodyBuffer: ByteBuffer
size: Int
) -> FetchedEnvelope {
let subject = envelope?.subject.flatMap { String(buffer: $0) }
let date = envelope?.date.map { String($0) } ?? ""
@@ -577,28 +561,6 @@ public actor IMAPClient: IMAPClientProtocol {
let to = envelope?.to.compactMap { extractEmailAddress($0) } ?? []
let cc = envelope?.cc.compactMap { extractEmailAddress($0) } ?? []
let bodyText: String?
let bodyHtml: String?
if bodyBuffer.readableBytes > 0 {
let bodyString = String(buffer: bodyBuffer)
let lowered = bodyString.lowercased()
if lowered.contains("<html") || lowered.contains("<body") || lowered.contains("<div") {
bodyText = nil
bodyHtml = bodyString
} else {
bodyText = bodyString
bodyHtml = nil
}
} else {
bodyText = nil
bodyHtml = nil
}
let snippet: String? = bodyText.map { text in
let trimmed = text.trimmingCharacters(in: .whitespacesAndNewlines)
return String(trimmed.prefix(200))
}
return FetchedEnvelope(
uid: uid,
messageId: messageId,
@@ -609,9 +571,9 @@ public actor IMAPClient: IMAPClientProtocol {
to: to,
cc: cc,
date: date,
snippet: snippet,
bodyText: bodyText,
bodyHtml: bodyHtml,
snippet: nil,
bodyText: nil,
bodyHtml: nil,
isRead: flags.contains(.seen),
isFlagged: flags.contains(.flagged),
size: size