add attachment CRUD to MailStore, wire hasAttachments in Queries

This commit is contained in:
2026-03-14 13:29:58 +01:00
parent 17d47cfddd
commit 0a564a05fd
2 changed files with 61 additions and 1 deletions

View File

@@ -448,6 +448,66 @@ public final class MailStore: Sendable {
}
}
// MARK: - Attachments
public func insertAttachment(_ attachment: AttachmentRecord) throws {
try dbWriter.write { db in
try attachment.insert(db)
}
}
public func insertAttachments(_ attachments: [AttachmentRecord]) throws {
try dbWriter.write { db in
for attachment in attachments {
try attachment.insert(db)
}
}
}
public func attachments(messageId: String) throws -> [AttachmentRecord] {
try dbWriter.read { db in
try AttachmentRecord
.filter(Column("messageId") == messageId)
.order(Column("filename"))
.fetchAll(db)
}
}
public func updateAttachmentCachePath(id: String, cachePath: String) throws {
try dbWriter.write { db in
try db.execute(
sql: "UPDATE attachment SET cachePath = ? WHERE id = ?",
arguments: [cachePath, id]
)
}
}
public func updateHasAttachments(messageId: String, hasAttachments: Bool) throws {
try dbWriter.write { db in
try db.execute(
sql: "UPDATE message SET hasAttachments = ? WHERE id = ?",
arguments: [hasAttachments, messageId]
)
}
}
public func storeBodyWithAttachments(
messageId: String,
text: String?,
html: String?,
attachments: [AttachmentRecord]
) throws {
try dbWriter.write { db in
try db.execute(
sql: "UPDATE message SET bodyText = ?, bodyHtml = ?, hasAttachments = ? WHERE id = ?",
arguments: [text, html, !attachments.isEmpty, messageId]
)
for attachment in attachments {
try attachment.insert(db)
}
}
}
// MARK: - Task cache queries
public func insertTask(_ task: TaskRecord) throws {

View File

@@ -99,7 +99,7 @@ extension MailStore {
bodyHtml: record.bodyHtml,
isRead: record.isRead,
isFlagged: record.isFlagged,
hasAttachments: false
hasAttachments: record.hasAttachments
)
}