Skip to content

feat: Post uids ranges #1808

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

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
3 changes: 2 additions & 1 deletion MailCore/API/Endpoint/Endpoint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion MailCore/API/MailApiFetcher/MailApiFetchable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ public protocol MailApiExtendedFetchable {
func messagesDelta<Flags: DeltaFlags>(
mailboxUuid: String,
folderId: String,
signature: String
signature: String,
uids: String?
) async throws -> MessagesDelta<Flags>

func message(message: Message) async throws -> Message
Expand Down
15 changes: 9 additions & 6 deletions MailCore/API/MailApiFetcher/MailApiFetcher+Extended.swift
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,15 @@ public extension MailApiFetcher {
}

func messagesDelta<Flags: DeltaFlags>(mailboxUuid: String, folderId: String,
signature: String) async throws -> MessagesDelta<Flags> {
try await perform(request: authenticatedRequest(.messagesDelta(
mailboxUuid: mailboxUuid,
folderId: folderId,
signature: signature
)))
signature: String, uids: String? = nil) async throws -> MessagesDelta<Flags> {
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 {
Expand Down
34 changes: 33 additions & 1 deletion MailCore/Cache/MailboxManager/MailboxManager+Thread.swift
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,8 @@ public extension MailboxManager {
let messagesDelta: MessagesDelta<MessageFlags> = try await apiFetcher.messagesDelta(
mailboxUuid: mailbox.uuid,
folderId: folder.remoteId,
signature: signature
signature: signature,
uids: getMessagesUidsRanges(folder: folder)
)

try messagesDelta.ensureValidDelta()
Expand All @@ -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
Expand Down
Loading