-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Attachment title will be used to populate Draft Subject #1227
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,7 +26,7 @@ import UniformTypeIdentifiers | |
public protocol Attachable { | ||
var suggestedName: String? { get } | ||
var type: UTType? { get } | ||
func writeToTemporaryURL() async throws -> URL | ||
func writeToTemporaryURL() async throws -> (url: URL, title: String?) | ||
} | ||
|
||
/// Abstracts that some attachment was updated | ||
|
@@ -88,6 +88,25 @@ public final class AttachmentsManagerWorker { | |
liveAttachments.map { $0.detached() } | ||
} | ||
|
||
var attachmentsTitles: [String?]? { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe some doc about the DB modified on side effect |
||
didSet { | ||
let realm = backgroundRealm.getRealm() | ||
guard let draft = realm.object(ofType: Draft.self, forPrimaryKey: draftLocalUUID), | ||
!draft.isInvalidated, | ||
draft.subject.isEmpty else { | ||
return | ||
} | ||
|
||
guard let attachmentTitle = attachmentsTitles?.compactMap({ $0 }).first else { | ||
return | ||
} | ||
|
||
try? realm.write { | ||
draft.subject = attachmentTitle | ||
} | ||
} | ||
} | ||
|
||
public init(backgroundRealm: BackgroundRealm, draftLocalUUID: String, mailboxManager: MailboxManager) { | ||
self.backgroundRealm = backgroundRealm | ||
self.draftLocalUUID = draftLocalUUID | ||
|
@@ -165,7 +184,6 @@ public final class AttachmentsManagerWorker { | |
await updateDelegate?.contentWillChange() | ||
} | ||
|
||
@discardableResult | ||
func importAttachment(attachment: Attachable, disposition: AttachmentDisposition) async -> String? { | ||
guard let localAttachment = await createLocalAttachment(name: attachment.suggestedName ?? getDefaultFileName(), | ||
type: attachment.type, | ||
|
@@ -175,27 +193,26 @@ public final class AttachmentsManagerWorker { | |
|
||
let importTask = Task { () -> String? in | ||
do { | ||
let url = try await attachment.writeToTemporaryURL() | ||
let updatedAttachment = await updateLocalAttachment(url: url, attachment: localAttachment) | ||
let attachmentResult = try await attachment.writeToTemporaryURL() | ||
let attachmentURL = attachmentResult.url | ||
let attachmentTitle = attachmentResult.title | ||
|
||
let updatedAttachment = await updateLocalAttachment(url: attachmentURL, attachment: localAttachment) | ||
let totalSize = liveAttachments.map(\.size).reduce(0) { $0 + $1 } | ||
guard totalSize < Constants.maxAttachmentsSize else { | ||
await updateDelegate?.handleGlobalError(MailError.attachmentsSizeLimitReached) | ||
await removeAttachment(updatedAttachment.uuid) | ||
return nil | ||
} | ||
|
||
let remoteAttachment = try await sendAttachment(url: url, localAttachment: updatedAttachment) | ||
try await sendAttachment(url: attachmentURL, localAttachment: updatedAttachment) | ||
return attachmentTitle | ||
|
||
if disposition == .inline, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was dead code, so removed it. I now return the title instead |
||
let cid = remoteAttachment?.contentId { | ||
return cid | ||
} | ||
} catch { | ||
DDLogError("Error while creating attachment: \(error.localizedDescription)") | ||
await updateAttachmentUploadError(localAttachment, error: error) | ||
return nil | ||
} | ||
|
||
return nil | ||
} | ||
|
||
if let uploadTask = attachmentUploadTasks[localAttachment.uuid] { | ||
|
@@ -220,6 +237,7 @@ public final class AttachmentsManagerWorker { | |
return savedAttachment | ||
} | ||
|
||
@discardableResult | ||
func sendAttachment(url: URL, localAttachment: Attachment) async throws -> Attachment? { | ||
let data = try Data(contentsOf: url) | ||
let remoteAttachment = try await mailboxManager.apiFetcher.createAttachment( | ||
|
@@ -301,18 +319,23 @@ extension AttachmentsManagerWorker: AttachmentsManagerWorkable { | |
self.updateDelegate = updateDelegate | ||
} | ||
|
||
public func importAttachments(attachments: [Attachable], draft: Draft, disposition: AttachmentDisposition) async { | ||
public func importAttachments(attachments: [Attachable], | ||
draft: Draft, | ||
disposition: AttachmentDisposition) async { | ||
guard !attachments.isEmpty else { | ||
return | ||
} | ||
|
||
// Cap max number of attachments, API errors out at 100 | ||
let attachmentsSlice = attachments[safe: 0 ..< draft.availableAttachmentsSlots] | ||
|
||
await attachmentsSlice.concurrentForEach { attachment in | ||
await self.importAttachment(attachment: attachment, disposition: disposition) | ||
let titles: [String?] = await attachmentsSlice.concurrentMap { attachment in | ||
let title = await self.importAttachment(attachment: attachment, disposition: disposition) | ||
// TODO: - Manage inline attachment | ||
return title | ||
} | ||
|
||
attachmentsTitles = titles | ||
} | ||
|
||
public func removeAttachment(_ attachmentUUID: String) async { | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe a marginal improvement using a typealias for this
(url: URL, title: String?)