From 7c812e833f21aee7027e75c4eae4d1df5ce8ed04 Mon Sep 17 00:00:00 2001 From: Valentin Perignon Date: Mon, 12 Jun 2023 16:22:15 +0200 Subject: [PATCH 1/4] fix: Mailto without "to" --- .package.resolved | 2 +- Mail/Views/New Message/ComposeMessageView.swift | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.package.resolved b/.package.resolved index 6fb7ff0da..95c653fa3 100644 --- a/.package.resolved +++ b/.package.resolved @@ -41,7 +41,7 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/Infomaniak/ios-core", "state" : { - "revision" : "cf001ba55dbf5f152353d66b55cd46350bd4b895" + "revision" : "e5fe8e03aa06375f20c8e40f4fae3a6af6e4d75e" } }, { diff --git a/Mail/Views/New Message/ComposeMessageView.swift b/Mail/Views/New Message/ComposeMessageView.swift index 1f78e5c05..7782a4a43 100644 --- a/Mail/Views/New Message/ComposeMessageView.swift +++ b/Mail/Views/New Message/ComposeMessageView.swift @@ -428,7 +428,7 @@ extension ComposeMessageView { static func mailTo(urlComponents: URLComponents, mailboxManager: MailboxManager) -> ComposeMessageView { let draft = Draft.mailTo(subject: urlComponents.getQueryItem(named: "subject"), body: urlComponents.getQueryItem(named: "body"), - to: [Recipient(email: urlComponents.path, name: "")], + to: Recipient.createListUsing(from: urlComponents, name: "to"), cc: Recipient.createListUsing(from: urlComponents, name: "cc"), bcc: Recipient.createListUsing(from: urlComponents, name: "bcc")) return ComposeMessageView(mailboxManager: mailboxManager, draft: draft) From ee4e84d1e9f27fa89d6ef0070742292d3f2ffd0d Mon Sep 17 00:00:00 2001 From: Valentin Perignon Date: Tue, 13 Jun 2023 11:14:25 +0200 Subject: [PATCH 2/4] feat: Handle different syntax of MailTo --- .../New Message/ComposeMessageView.swift | 7 ++++--- MailCore/Models/Recipient.swift | 19 ++++++++++++++++++- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/Mail/Views/New Message/ComposeMessageView.swift b/Mail/Views/New Message/ComposeMessageView.swift index 7782a4a43..0033326bb 100644 --- a/Mail/Views/New Message/ComposeMessageView.swift +++ b/Mail/Views/New Message/ComposeMessageView.swift @@ -241,9 +241,9 @@ struct ComposeMessageView: View { } .customAlert(isPresented: $alert.isShowing) { switch alert.state { - case .link(let handler): + case let .link(handler): AddLinkView(actionHandler: handler) - case .emptySubject(let handler): + case let .emptySubject(handler): EmptySubjectView(actionHandler: handler) case .none: EmptyView() @@ -428,7 +428,8 @@ extension ComposeMessageView { static func mailTo(urlComponents: URLComponents, mailboxManager: MailboxManager) -> ComposeMessageView { let draft = Draft.mailTo(subject: urlComponents.getQueryItem(named: "subject"), body: urlComponents.getQueryItem(named: "body"), - to: Recipient.createListUsing(from: urlComponents, name: "to"), + to: Recipient.createListUsing(listOfAddresses: urlComponents.path) + + Recipient.createListUsing(from: urlComponents, name: "to"), cc: Recipient.createListUsing(from: urlComponents, name: "cc"), bcc: Recipient.createListUsing(from: urlComponents, name: "bcc")) return ComposeMessageView(mailboxManager: mailboxManager, draft: draft) diff --git a/MailCore/Models/Recipient.swift b/MailCore/Models/Recipient.swift index 98a62cf05..3137164b7 100644 --- a/MailCore/Models/Recipient.swift +++ b/MailCore/Models/Recipient.swift @@ -20,6 +20,7 @@ import Foundation import MailResources import Nuke import RealmSwift +import SwiftRegex import SwiftUI public extension URLComponents { @@ -52,7 +53,23 @@ public class Recipient: EmbeddedObject, Codable { } public static func createListUsing(from urlComponents: URLComponents, name: String) -> [Recipient] { - return urlComponents.getQueryItem(named: name)?.split(separator: ",").map { Recipient(email: "\($0)", name: "") } ?? [] + return createListUsing(listOfAddresses: urlComponents.getQueryItem(named: name)) + } + + public static func createListUsing(listOfAddresses: String?) -> [Recipient] { + guard let addresses = listOfAddresses?.components(separatedBy: CharacterSet(charactersIn: ",;")) else { return [] } + + var recipients = [Recipient]() + for address in addresses { + if Constants.isEmailAddress(address) { + recipients.append(Recipient(email: address, name: "")) + } else if let matches = Regex(pattern: "(.+)<(.+)>")?.matches(in: address).first, + matches.count >= 3, Constants.isEmailAddress(matches[2]) { + recipients.append(Recipient(email: matches[2], name: matches[1].removingPercentEncoding ?? matches[1])) + } + } + + return recipients } public var isCurrentUser: Bool { From f5524bf945049c5bc345839cc10bcc1a20c5e113 Mon Sep 17 00:00:00 2001 From: Valentin Perignon Date: Tue, 13 Jun 2023 13:08:28 +0200 Subject: [PATCH 3/4] fix: Signature position with mailto: --- Mail/Views/New Message/ComposeMessageView.swift | 14 +++++++++----- MailCore/Models/Recipient.swift | 6 +++--- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/Mail/Views/New Message/ComposeMessageView.swift b/Mail/Views/New Message/ComposeMessageView.swift index 0033326bb..2fb77d36b 100644 --- a/Mail/Views/New Message/ComposeMessageView.swift +++ b/Mail/Views/New Message/ComposeMessageView.swift @@ -369,12 +369,16 @@ struct ComposeMessageView: View { } let html = "

\(signature.content)
" - switch signature.position { - case .beforeReplyMessage: - $draft.body.wrappedValue.insert(contentsOf: html, at: draft.body.startIndex) - case .afterReplyMessage: - $draft.body.wrappedValue.append(contentsOf: html) + var signaturePosition = draft.body.endIndex + if messageReply != nil { + switch signature.position { + case .beforeReplyMessage: + signaturePosition = draft.body.startIndex + case .afterReplyMessage: + signaturePosition = draft.body.endIndex + } } + $draft.body.wrappedValue.insert(contentsOf: html, at: signaturePosition) } } diff --git a/MailCore/Models/Recipient.swift b/MailCore/Models/Recipient.swift index 3137164b7..b50518a35 100644 --- a/MailCore/Models/Recipient.swift +++ b/MailCore/Models/Recipient.swift @@ -63,9 +63,9 @@ public class Recipient: EmbeddedObject, Codable { for address in addresses { if Constants.isEmailAddress(address) { recipients.append(Recipient(email: address, name: "")) - } else if let matches = Regex(pattern: "(.+)<(.+)>")?.matches(in: address).first, - matches.count >= 3, Constants.isEmailAddress(matches[2]) { - recipients.append(Recipient(email: matches[2], name: matches[1].removingPercentEncoding ?? matches[1])) + } else if let match = Regex(pattern: "(.+)<(.+)>")?.matches(in: address).first, + match.count >= 3, Constants.isEmailAddress(match[2]) { + recipients.append(Recipient(email: match[2], name: match[1].removingPercentEncoding ?? match[1])) } } From 592cd883478b88c211682b4a64d180df2453d1fe Mon Sep 17 00:00:00 2001 From: Valentin Perignon Date: Tue, 13 Jun 2023 13:54:54 +0200 Subject: [PATCH 4/4] feat: Handle carriage return --- Mail/Views/New Message/ComposeMessageView.swift | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Mail/Views/New Message/ComposeMessageView.swift b/Mail/Views/New Message/ComposeMessageView.swift index 2fb77d36b..77b0e5925 100644 --- a/Mail/Views/New Message/ComposeMessageView.swift +++ b/Mail/Views/New Message/ComposeMessageView.swift @@ -431,9 +431,11 @@ extension ComposeMessageView { static func mailTo(urlComponents: URLComponents, mailboxManager: MailboxManager) -> ComposeMessageView { let draft = Draft.mailTo(subject: urlComponents.getQueryItem(named: "subject"), - body: urlComponents.getQueryItem(named: "body"), + body: urlComponents.getQueryItem(named: "body")? + .replacingOccurrences(of: "\r", with: "") + .replacingOccurrences(of: "\n", with: "
"), to: Recipient.createListUsing(listOfAddresses: urlComponents.path) - + Recipient.createListUsing(from: urlComponents, name: "to"), + + Recipient.createListUsing(from: urlComponents, name: "to"), cc: Recipient.createListUsing(from: urlComponents, name: "cc"), bcc: Recipient.createListUsing(from: urlComponents, name: "bcc")) return ComposeMessageView(mailboxManager: mailboxManager, draft: draft)