diff --git a/FlowCrypt.xcodeproj/project.pbxproj b/FlowCrypt.xcodeproj/project.pbxproj index f8e095658..2c812e224 100644 --- a/FlowCrypt.xcodeproj/project.pbxproj +++ b/FlowCrypt.xcodeproj/project.pbxproj @@ -11,9 +11,9 @@ 04B472951ECE29F600B8266F /* MyMenuViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 04B472921ECE29F600B8266F /* MyMenuViewController.swift */; }; 04B472961ECE29F600B8266F /* SideMenuNavigationController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 04B472931ECE29F600B8266F /* SideMenuNavigationController.swift */; }; 211392A5266511E6009202EC /* PubLookup.swift in Sources */ = {isa = PBXBuildFile; fileRef = 211392A4266511E6009202EC /* PubLookup.swift */; }; - 21C7DF0526697DA500C44800 /* PromiseKitExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21C7DF0426697DA500C44800 /* PromiseKitExtension.swift */; }; 21C7DEFC26669A3700C44800 /* CalendarExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21C7DEFB26669A3700C44800 /* CalendarExtension.swift */; }; 21C7DEFE26669CE100C44800 /* DateFormattingExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9F56BD3723438C7000A7371A /* DateFormattingExtensions.swift */; }; + 21C7DF0526697DA500C44800 /* PromiseKitExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21C7DF0426697DA500C44800 /* PromiseKitExtension.swift */; }; 21CE25E62650070300ADFF4B /* WKDURLsConstructor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21CE25E52650070300ADFF4B /* WKDURLsConstructor.swift */; }; 21EA3B2326565B5D00691848 /* DomainRulesTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21EA3B2226565B5D00691848 /* DomainRulesTests.swift */; }; 21EA3B2F26565B7400691848 /* domain_rules.json in Resources */ = {isa = PBXBuildFile; fileRef = 21EA3B2E26565B7400691848 /* domain_rules.json */; }; @@ -234,7 +234,6 @@ D274724124F97C5C006BA6EF /* CacheService.swift in Sources */ = {isa = PBXBuildFile; fileRef = D274724024F97C5C006BA6EF /* CacheService.swift */; }; D274724424FD1932006BA6EF /* FolderObject.swift in Sources */ = {isa = PBXBuildFile; fileRef = D274724324FD1932006BA6EF /* FolderObject.swift */; }; D27B911924EFE79F002DF0A1 /* LocalContactsProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = D27B911824EFE79F002DF0A1 /* LocalContactsProvider.swift */; }; - D27B911B24EFE7BE002DF0A1 /* RemoteContactsProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = D27B911A24EFE7BE002DF0A1 /* RemoteContactsProvider.swift */; }; D27B911D24EFE806002DF0A1 /* ContactObject.swift in Sources */ = {isa = PBXBuildFile; fileRef = D27B911C24EFE806002DF0A1 /* ContactObject.swift */; }; D27B911F24EFE828002DF0A1 /* Contact.swift in Sources */ = {isa = PBXBuildFile; fileRef = D27B911E24EFE828002DF0A1 /* Contact.swift */; }; D28325DD24895F5700D311BD /* Colors.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 949ED9412303E3B400530579 /* Colors.xcassets */; }; @@ -361,8 +360,8 @@ 113F04B20ECC35FC59A81A6C /* Pods-FlowCryptTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-FlowCryptTests.release.xcconfig"; path = "Target Support Files/Pods-FlowCryptTests/Pods-FlowCryptTests.release.xcconfig"; sourceTree = ""; }; 11C1375F41411882DC4C9431 /* Pods-FlowCryptUIApplication.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-FlowCryptUIApplication.release.xcconfig"; path = "Target Support Files/Pods-FlowCryptUIApplication/Pods-FlowCryptUIApplication.release.xcconfig"; sourceTree = ""; }; 211392A4266511E6009202EC /* PubLookup.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PubLookup.swift; sourceTree = ""; }; - 21C7DF0426697DA500C44800 /* PromiseKitExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PromiseKitExtension.swift; sourceTree = ""; }; 21C7DEFB26669A3700C44800 /* CalendarExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CalendarExtension.swift; sourceTree = ""; }; + 21C7DF0426697DA500C44800 /* PromiseKitExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PromiseKitExtension.swift; sourceTree = ""; }; 21CE25E52650070300ADFF4B /* WKDURLsConstructor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WKDURLsConstructor.swift; sourceTree = ""; }; 21EA3B15265647C400691848 /* OrganisationalRule.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OrganisationalRule.swift; sourceTree = ""; }; 21EA3B2226565B5D00691848 /* DomainRulesTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DomainRulesTests.swift; sourceTree = ""; }; @@ -604,7 +603,6 @@ D274724024F97C5C006BA6EF /* CacheService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CacheService.swift; sourceTree = ""; }; D274724324FD1932006BA6EF /* FolderObject.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FolderObject.swift; sourceTree = ""; }; D27B911824EFE79F002DF0A1 /* LocalContactsProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocalContactsProvider.swift; sourceTree = ""; }; - D27B911A24EFE7BE002DF0A1 /* RemoteContactsProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RemoteContactsProvider.swift; sourceTree = ""; }; D27B911C24EFE806002DF0A1 /* ContactObject.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContactObject.swift; sourceTree = ""; }; D27B911E24EFE828002DF0A1 /* Contact.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Contact.swift; sourceTree = ""; }; D28655922423B4EE0066F52E /* MyMenuViewDecorator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MyMenuViewDecorator.swift; sourceTree = ""; }; @@ -1510,7 +1508,6 @@ children = ( D2FC1C0524D82C9F003B949D /* ContactsService.swift */, D27B911824EFE79F002DF0A1 /* LocalContactsProvider.swift */, - D27B911A24EFE7BE002DF0A1 /* RemoteContactsProvider.swift */, D27B912024EFE842002DF0A1 /* Models */, ); path = "Contacts Services"; @@ -2396,7 +2393,6 @@ 9F31ABA0232C071700CF87EA /* GlobalRouter.swift in Sources */, 9F5C2A92257E94DF00DE9B4B /* Imap+MessageOperations.swift in Sources */, D27B911F24EFE828002DF0A1 /* Contact.swift in Sources */, - D27B911B24EFE7BE002DF0A1 /* RemoteContactsProvider.swift in Sources */, 32DCA1414EEA727B86C337D5 /* Core.swift in Sources */, 9FB22CF725715DC50026EE64 /* KeyServiceErrorHandler.swift in Sources */, 9F8220D526336626004B2009 /* Logger.swift in Sources */, diff --git a/FlowCrypt/Functionality/Mail Provider/Backup Provider/Gmail+Backup.swift b/FlowCrypt/Functionality/Mail Provider/Backup Provider/Gmail+Backup.swift index 753b04a16..90d15d85b 100644 --- a/FlowCrypt/Functionality/Mail Provider/Backup Provider/Gmail+Backup.swift +++ b/FlowCrypt/Functionality/Mail Provider/Backup Provider/Gmail+Backup.swift @@ -24,7 +24,7 @@ extension GmailService: BackupProvider { Logger.logVerbose("[GmailService] searching done, found \(backupMessages.count) backup messages") let uniqueMessages = Set(backupMessages) let attachments = uniqueMessages - .compactMap { (message) -> [(String, String)]? in + .compactMap { message -> [(String, String)]? in Logger.logVerbose("[GmailService] processing backup '\(message.subject ?? "-")' with \(message.attachmentIds.count) attachments") guard let identifier = message.identifier.stringId else { Logger.logVerbose("[GmailService] skipping this last backup?") diff --git a/FlowCrypt/Functionality/Services/Contacts Services/ContactsService.swift b/FlowCrypt/Functionality/Services/Contacts Services/ContactsService.swift index f270faeaa..9206feb13 100644 --- a/FlowCrypt/Functionality/Services/Contacts Services/ContactsService.swift +++ b/FlowCrypt/Functionality/Services/Contacts Services/ContactsService.swift @@ -17,6 +17,10 @@ enum ContactsError: Error { protocol ContactsServiceType: PublicKeyProvider, ContactsProviderType { } +protocol ContactsProviderType { + func searchContact(with email: String) -> Promise +} + protocol PublicKeyProvider { func retrievePubKey(for email: String) -> String? } @@ -25,14 +29,14 @@ protocol PublicKeyProvider { struct ContactsService: ContactsServiceType { let localContactsProvider: LocalContactsProviderType - let remoteContactsProvider: ContactsProviderType + let pubLookup: PubLookupType init( localContactsProvider: LocalContactsProviderType = LocalContactsProvider(storage: DataService.shared.storage), - remoteContactsProvider: ContactsProviderType = RemoteContactsProvider() + pubLookup: PubLookupType = PubLookup() ) { self.localContactsProvider = localContactsProvider - self.remoteContactsProvider = remoteContactsProvider + self.pubLookup = pubLookup } } @@ -45,8 +49,8 @@ extension ContactsService: ContactsProviderType { } private func searchRemote(for email: String) -> Promise { - remoteContactsProvider - .searchContact(with: email) + pubLookup + .lookup(with: email) .then { contact in self.localContactsProvider.save(contact: contact) } diff --git a/FlowCrypt/Functionality/Services/Contacts Services/RemoteContactsProvider.swift b/FlowCrypt/Functionality/Services/Contacts Services/RemoteContactsProvider.swift deleted file mode 100644 index 2f116ecff..000000000 --- a/FlowCrypt/Functionality/Services/Contacts Services/RemoteContactsProvider.swift +++ /dev/null @@ -1,60 +0,0 @@ -// -// RemoteContactsProvider.swift -// FlowCrypt -// -// Created by Anton Kharchevskyi on 21/08/2020. -// Copyright © 2020 FlowCrypt Limited. All rights reserved. -// - -import Foundation -import Promises - -protocol ContactsProviderType { - func searchContact(with email: String) -> Promise -} - -struct RemoteContactsProvider { - let api: AttesterApiType - - init( - api: AttesterApiType = AttesterApi() - ) { - self.api = api - } -} - -extension RemoteContactsProvider: ContactsProviderType { - func searchContact(with email: String) -> Promise { - Promise { resolve, _ in - let keyDetails = try awaitPromise(self.api.lookupEmail(email: email)) - let contact = try awaitPromise(self.parseKey(keyDetails: keyDetails, for: email)) - resolve(contact) - } - } - - private func parseKey(keyDetails: [KeyDetails], for email: String) -> Promise { - - guard let keyDetail = keyDetails.first else { - return Promise(ContactsError.keyMissing) - } - - let keyIds = keyDetails.flatMap(\.ids) - let longids = keyIds.map(\.longid) - let fingerprints = keyIds.map(\.fingerprint) - - let contact = Contact( - email: email, - name: keyDetail.users.first ?? email, - pubKey: keyDetail.public, - pubKeyLastSig: nil, // TODO: - will be provided later - pubkeyLastChecked: Date(), - pubkeyExpiresOn: nil, // TODO: - will be provided later - longids: longids, - lastUsed: nil, - fingerprints: fingerprints, - pubkeyCreated: Date(timeIntervalSince1970: Double(keyDetail.created)), - algo: keyDetail.algo - ) - return Promise(contact) - } -} diff --git a/FlowCrypt/Functionality/WKDURLs/PubLookup.swift b/FlowCrypt/Functionality/WKDURLs/PubLookup.swift index fea913028..56c00a549 100644 --- a/FlowCrypt/Functionality/WKDURLs/PubLookup.swift +++ b/FlowCrypt/Functionality/WKDURLs/PubLookup.swift @@ -9,10 +9,10 @@ import Promises protocol PubLookupType { - func lookupEmail(_ email: String) -> Promise<[String]> + func lookup(with email: String) -> Promise } -class PubLookup { +class PubLookup: PubLookupType { private let wkd: WKDURLsApiType private let attesterApi: AttesterApiType @@ -24,7 +24,15 @@ class PubLookup { self.attesterApi = attesterApi } - func lookupEmail(_ email: String) -> Promise<[KeyDetails]> { + func lookup(with email: String) -> Promise { + Promise { resolve, _ in + let keyDetails = try awaitPromise(self.getKeyDetails(email)) + let contact = try awaitPromise(self.parseKey(keyDetails: keyDetails, for: email)) + resolve(contact) + } + } + + private func getKeyDetails(_ email: String) -> Promise<[KeyDetails]> { Promise<[KeyDetails]> { [weak self] resolve, _ in guard let self = self else { @@ -41,4 +49,33 @@ class PubLookup { resolve(attesterResult) } } + + private func parseKey(keyDetails: [KeyDetails], for email: String) -> Promise { + + // TODO: - we are blindly choosing .first public key, in the future we should return [Contact] + // and have some intelligent code in the consumers to choose the right public key + // for whatever purpose it's used for. + guard let keyDetail = keyDetails.first else { + return Promise(ContactsError.keyMissing) + } + + let keyIds = keyDetails.flatMap(\.ids) + let longids = keyIds.map(\.longid) + let fingerprints = keyIds.map(\.fingerprint) + + let contact = Contact( + email: email, + name: keyDetail.users.first ?? email, + pubKey: keyDetail.public, + pubKeyLastSig: nil, // TODO: - will be provided later + pubkeyLastChecked: Date(), + pubkeyExpiresOn: nil, // TODO: - will be provided later + longids: longids, + lastUsed: nil, + fingerprints: fingerprints, + pubkeyCreated: Date(timeIntervalSince1970: Double(keyDetail.created)), + algo: keyDetail.algo + ) + return Promise(contact) + } }