TF-2464 Save both EmailDelivery and EmailState in the keychain
Signed-off-by: dab246 <tdvu@linagora.com> (cherry picked from commit a37fe7ea8bcc38eb96df39f46128c9e1f7af046e)
This commit is contained in:
@@ -8,5 +8,5 @@ struct KeychainCredentials {
|
||||
protocol KeychainControllerDelegate: AnyObject {
|
||||
func retrieveSharingSessionFromKeychain(accountId: String) -> KeychainSharingSession?
|
||||
func retrieveSharingSessions() -> [KeychainCredentials]
|
||||
func updateEmailStateToKeychain(accountId: String, newState: String)
|
||||
func updateEmailDeliveryStateToKeychain(accountId: String, newEmailDeliveryState: String)
|
||||
}
|
||||
|
||||
@@ -40,10 +40,10 @@ class KeychainController: KeychainControllerDelegate {
|
||||
}
|
||||
}
|
||||
|
||||
func updateEmailStateToKeychain(accountId: String, newState: String) {
|
||||
func updateEmailDeliveryStateToKeychain(accountId: String, newEmailDeliveryState: String) {
|
||||
do {
|
||||
if let sharingSession = retrieveSharingSessionFromKeychain(accountId: accountId) {
|
||||
let newSharingSession = sharingSession.updateEmailState(newState: newState)
|
||||
let newSharingSession = sharingSession.updateEmailDeliveryState(newEmailDeliveryState: newEmailDeliveryState)
|
||||
try keychain.set(newSharingSession.toJson() ?? "", key: accountId)
|
||||
}
|
||||
} catch {}
|
||||
|
||||
@@ -6,6 +6,7 @@ struct KeychainSharingSession: Codable {
|
||||
let authenticationType: AuthenticationType
|
||||
let apiUrl: String
|
||||
let emailState: String?
|
||||
let emailDeliveryState: String?
|
||||
let tokenOIDC: TokenOidc?
|
||||
let basicAuth: String?
|
||||
let tokenEndpoint: String?
|
||||
@@ -13,13 +14,14 @@ struct KeychainSharingSession: Codable {
|
||||
}
|
||||
|
||||
extension KeychainSharingSession {
|
||||
func updateEmailState(newState: String) -> KeychainSharingSession {
|
||||
func updateEmailDeliveryState(newEmailDeliveryState: String) -> KeychainSharingSession {
|
||||
return KeychainSharingSession(
|
||||
accountId: self.accountId,
|
||||
userName: self.userName,
|
||||
authenticationType: self.authenticationType,
|
||||
apiUrl: self.apiUrl,
|
||||
emailState: newState,
|
||||
emailState: emailState,
|
||||
emailDeliveryState: newEmailDeliveryState,
|
||||
tokenOIDC: self.tokenOIDC,
|
||||
basicAuth: self.basicAuth,
|
||||
tokenEndpoint: self.tokenEndpoint,
|
||||
@@ -34,6 +36,7 @@ extension KeychainSharingSession {
|
||||
authenticationType: self.authenticationType,
|
||||
apiUrl: self.apiUrl,
|
||||
emailState: self.emailState,
|
||||
emailDeliveryState: self.emailDeliveryState,
|
||||
tokenOIDC: TokenOidc(
|
||||
token: newTokenOidc.token,
|
||||
tokenId: newTokenOidc.tokenId,
|
||||
|
||||
@@ -21,7 +21,7 @@ class NotificationService: UNNotificationServiceExtension {
|
||||
|
||||
guard let payloadData = request.content.userInfo as? [String: Any],
|
||||
!keychainController.retrieveSharingSessions().isEmpty else {
|
||||
self.modifiedContent?.body = NSLocalizedString(newEmailDefaultMessageKey, comment: "Localizable")
|
||||
self.modifiedContent?.body = NSLocalizedString(newNotificationDefaultMessageKey, comment: "Localizable")
|
||||
return self.notify()
|
||||
}
|
||||
|
||||
@@ -46,24 +46,28 @@ class NotificationService: UNNotificationServiceExtension {
|
||||
let mapStateChanges: [String: [TypeName: String]] = PayloadParser.shared.parsingPayloadNotification(payloadData: payloadData)
|
||||
|
||||
if (mapStateChanges.isEmpty) {
|
||||
self.modifiedContent?.body = NSLocalizedString(newEmailDefaultMessageKey, comment: "Localizable")
|
||||
self.modifiedContent?.body = NSLocalizedString(newNotificationDefaultMessageKey, comment: "Localizable")
|
||||
return self.notify()
|
||||
} else {
|
||||
guard let currentAccountId = mapStateChanges.keys.first,
|
||||
let keychainSharingSession = keychainController.retrieveSharingSessionFromKeychain(accountId: currentAccountId),
|
||||
keychainSharingSession.tokenOIDC != nil || keychainSharingSession.basicAuth != nil,
|
||||
let listStateOfAccount = mapStateChanges[currentAccountId],
|
||||
let newEmailState = listStateOfAccount[TypeName.emailDelivery],
|
||||
let oldEmailState = keychainSharingSession.emailState,
|
||||
newEmailState != oldEmailState,
|
||||
keychainSharingSession.tokenOIDC != nil || keychainSharingSession.basicAuth != nil else {
|
||||
let newEmailDeliveryState = listStateOfAccount[TypeName.emailDelivery] else {
|
||||
self.modifiedContent?.body = NSLocalizedString(newNotificationDefaultMessageKey, comment: "Localizable")
|
||||
return self.notify()
|
||||
}
|
||||
|
||||
guard let oldEmailDeliveryState = keychainSharingSession.emailDeliveryState ?? keychainSharingSession.emailState,
|
||||
newEmailDeliveryState != oldEmailDeliveryState else {
|
||||
self.modifiedContent?.body = NSLocalizedString(newEmailDefaultMessageKey, comment: "Localizable")
|
||||
return self.notify()
|
||||
}
|
||||
|
||||
JmapClient.shared.getNewEmails(
|
||||
apiUrl: keychainSharingSession.apiUrl,
|
||||
accountId: keychainSharingSession.accountId,
|
||||
sinceState: oldEmailState,
|
||||
sinceState: oldEmailDeliveryState,
|
||||
authenticationType: keychainSharingSession.authenticationType,
|
||||
tokenOidc: keychainSharingSession.tokenOIDC,
|
||||
basicAuth: keychainSharingSession.basicAuth,
|
||||
@@ -74,7 +78,10 @@ class NotificationService: UNNotificationServiceExtension {
|
||||
self.modifiedContent?.body = NSLocalizedString(self.newNotificationDefaultMessageKey, comment: "Localizable")
|
||||
return self.notify()
|
||||
} else {
|
||||
self.keychainController.updateEmailStateToKeychain(accountId: keychainSharingSession.accountId, newState: newEmailState)
|
||||
self.keychainController.updateEmailDeliveryStateToKeychain(
|
||||
accountId: keychainSharingSession.accountId,
|
||||
newEmailDeliveryState: newEmailDeliveryState
|
||||
)
|
||||
|
||||
if (emails.count > 1) {
|
||||
for email in emails {
|
||||
|
||||
Reference in New Issue
Block a user