Skip to content

Commit 870d97d

Browse files
fix: Only mark a thread as moved locally when necessary
1 parent 2c07d82 commit 870d97d

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

MailCore/Cache/MailboxManager/MailboxManager+Local.swift

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,19 @@ public extension MailboxManager {
6868
}
6969
}
7070

71-
func markMovedLocally(_ movedLocally: Bool, threads: [Thread]) async {
71+
func markMovedLocallyIfNecessary(_ movedLocally: Bool, messages: [Message], folder: Folder?) async {
72+
var threadsByMessages = [String: [Message]]()
73+
for message in messages {
74+
guard let thread = message.threads.first(where: { $0.folderId == folder?.remoteId }),
75+
message.folderId == folder?.remoteId else { continue }
76+
77+
threadsByMessages[thread.uid, default: []].append(message)
78+
}
79+
7280
try? writeTransaction { writableRealm in
73-
for thread in threads {
74-
guard let liveThread = writableRealm.object(ofType: Thread.self, forPrimaryKey: thread.uid) else {
81+
for (threadUid, messages) in threadsByMessages {
82+
guard let liveThread = writableRealm.object(ofType: Thread.self, forPrimaryKey: threadUid),
83+
liveThread.messages.where({ $0.folderId.equals(liveThread.folderId) }).count == messages.count else {
7584
continue
7685
}
7786

MailCore/Cache/MailboxManager/MailboxManager+Message.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,13 @@ public extension MailboxManager {
121121
origin: Folder?,
122122
action: @escaping (String, [Message]) async throws -> UndoResponse
123123
) async throws -> UndoAction {
124-
let originalThreads = messages.flatMap { $0.threads.filter { $0.folder == origin } }
125-
await markMovedLocally(true, threads: originalThreads)
124+
await markMovedLocallyIfNecessary(true, messages: messages, folder: origin)
126125

127126
let response = await apiFetcher.batchOver(values: messages, chunkSize: Constants.apiLimit) { chunk in
128127
do {
129128
return try await action(self.mailbox.uuid, chunk)
130129
} catch {
131-
await self.markMovedLocally(false, threads: originalThreads)
130+
await self.markMovedLocallyIfNecessary(false, messages: messages, folder: origin)
132131
}
133132
return nil
134133
}

0 commit comments

Comments
 (0)