diff --git a/MailCore/API/Endpoint/Endpoint.swift b/MailCore/API/Endpoint/Endpoint.swift index 89a5f43b59..a6c9787161 100644 --- a/MailCore/API/Endpoint/Endpoint.swift +++ b/MailCore/API/Endpoint/Endpoint.swift @@ -55,7 +55,8 @@ public extension Endpoint { } private static var mailHost: Endpoint { - return Endpoint(hostKeypath: \.mailHost, path: "") + return Endpoint(host: "mail-mr-6689.preprod.dev.infomaniak.ch", path: "") +// return Endpoint(hostKeypath: \.mailHost, path: "") } private static var base: Endpoint { diff --git a/MailCore/API/MailApiFetcher/MailApiFetchable.swift b/MailCore/API/MailApiFetcher/MailApiFetchable.swift index 4064f9c145..028f37c968 100644 --- a/MailCore/API/MailApiFetcher/MailApiFetchable.swift +++ b/MailCore/API/MailApiFetcher/MailApiFetchable.swift @@ -110,7 +110,8 @@ public protocol MailApiExtendedFetchable { func messagesDelta( mailboxUuid: String, folderId: String, - signature: String + signature: String, + uids: String? ) async throws -> MessagesDelta func message(message: Message) async throws -> Message diff --git a/MailCore/API/MailApiFetcher/MailApiFetcher+Extended.swift b/MailCore/API/MailApiFetcher/MailApiFetcher+Extended.swift index 18e795d711..71af0e2ae8 100644 --- a/MailCore/API/MailApiFetcher/MailApiFetcher+Extended.swift +++ b/MailCore/API/MailApiFetcher/MailApiFetcher+Extended.swift @@ -138,12 +138,15 @@ public extension MailApiFetcher { } func messagesDelta(mailboxUuid: String, folderId: String, - signature: String) async throws -> MessagesDelta { - try await perform(request: authenticatedRequest(.messagesDelta( - mailboxUuid: mailboxUuid, - folderId: folderId, - signature: signature - ))) + signature: String, uids: String? = nil) async throws -> MessagesDelta { + let method = uids == nil ? HTTPMethod.get : HTTPMethod.post + return try await perform(request: authenticatedRequest(.messagesDelta( + mailboxUuid: mailboxUuid, + folderId: folderId, + signature: signature + ), + method: method, + parameters: uids)) } func message(message: Message) async throws -> Message { diff --git a/MailCore/Cache/MailboxManager/MailboxManager+Thread.swift b/MailCore/Cache/MailboxManager/MailboxManager+Thread.swift index 8788a209d9..a698093383 100644 --- a/MailCore/Cache/MailboxManager/MailboxManager+Thread.swift +++ b/MailCore/Cache/MailboxManager/MailboxManager+Thread.swift @@ -178,7 +178,8 @@ public extension MailboxManager { let messagesDelta: MessagesDelta = try await apiFetcher.messagesDelta( mailboxUuid: mailbox.uuid, folderId: folder.remoteId, - signature: signature + signature: signature, + uids: getMessagesUidsRanges(folder: folder) ) try messagesDelta.ensureValidDelta() @@ -189,6 +190,37 @@ public extension MailboxManager { } } + private func getMessagesUidsRanges(folder: Folder) -> String? { + let messages = folder.messages.sorted { + guard let firstUid = $0.shortUid, let secondUid = $1.shortUid else { return false } + return firstUid < secondUid + } + guard !messages.isEmpty else { return nil } + + var uids = "" + var prevUid = -1 + var wasInRange = false + for (index, message) in messages.enumerated() { + guard let shortUid = message.shortUid else { continue } + let nextUid = shortUid + if index == 0 { + uids += String(nextUid) + prevUid = nextUid + } else { + let isInRange = nextUid == prevUid + 1 + if !isInRange { + uids += wasInRange ? ":\(prevUid),\(nextUid)" : ",\(nextUid)" + } + prevUid = nextUid + wasInRange = isInRange + } + } + if wasInRange { + uids += ":\(prevUid)" + } + return uids + } + /// This function get all the messages uids from the chosen folder private func fetchOldMessagesUids(folder: Folder) async throws -> String { /// Get ALL uids