Skip to content

fix: Only mark a thread as moved locally when necessary #1784

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

Merged
merged 1 commit into from
May 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions MailCore/Cache/MailboxManager/MailboxManager+Local.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,19 @@ public extension MailboxManager {
}
}

func markMovedLocally(_ movedLocally: Bool, threads: [Thread]) async {
func markMovedLocallyIfNecessary(_ movedLocally: Bool, messages: [Message], folder: Folder?) async {
var threadsByMessages = [String: [Message]]()
for message in messages {
guard let thread = message.threads.first(where: { $0.folderId == folder?.remoteId }),
message.folderId == folder?.remoteId else { continue }

threadsByMessages[thread.uid, default: []].append(message)
}

try? writeTransaction { writableRealm in
for thread in threads {
guard let liveThread = writableRealm.object(ofType: Thread.self, forPrimaryKey: thread.uid) else {
for (threadUid, messages) in threadsByMessages {
guard let liveThread = writableRealm.object(ofType: Thread.self, forPrimaryKey: threadUid),
liveThread.messages.where({ $0.folderId.equals(liveThread.folderId) }).count == messages.count else {
continue
}

Expand Down
5 changes: 2 additions & 3 deletions MailCore/Cache/MailboxManager/MailboxManager+Message.swift
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,13 @@ public extension MailboxManager {
origin: Folder?,
action: @escaping (String, [Message]) async throws -> UndoResponse
) async throws -> UndoAction {
let originalThreads = messages.flatMap { $0.threads.filter { $0.folder == origin } }
await markMovedLocally(true, threads: originalThreads)
await markMovedLocallyIfNecessary(true, messages: messages, folder: origin)

let response = await apiFetcher.batchOver(values: messages, chunkSize: Constants.apiLimit) { chunk in
do {
return try await action(self.mailbox.uuid, chunk)
} catch {
await self.markMovedLocally(false, threads: originalThreads)
await self.markMovedLocallyIfNecessary(false, messages: messages, folder: origin)
}
return nil
}
Expand Down
Loading