Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,16 @@ struct ContactsService: ContactsServiceType {
extension ContactsService: ContactsProviderType {
func searchContact(with email: String) -> Promise<RecipientWithPubKeys> {
guard let contact = localContactsProvider.searchRecipient(with: email) else {
return searchRemote(for: email)
return pubLookup.lookup(with: email).then { recipient in
localContactsProvider.save(recipient: recipient)
}
}
pubLookup.lookup(with: email).then { recipient in
localContactsProvider.updateKeys(for: recipient)
}
return Promise(contact)
}

private func searchRemote(for email: String) -> Promise<RecipientWithPubKeys> {
pubLookup
.lookup(with: email)
.then { recipient in
self.localContactsProvider.save(recipient: recipient)
}
}
}

extension ContactsService: PublicKeyProvider {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ protocol LocalContactsProviderType: PublicKeyProvider {
func searchRecipient(with email: String) -> RecipientWithPubKeys?
func save(recipient: RecipientWithPubKeys)
func remove(recipient: RecipientWithPubKeys)
func updateKeys(for recipient: RecipientWithPubKeys)
func getAllRecipients() -> [RecipientWithPubKeys]
}

Expand Down Expand Up @@ -56,6 +57,22 @@ extension LocalContactsProvider: LocalContactsProviderType {
)
}

func updateKeys(for recipient: RecipientWithPubKeys) {
guard let recipientObject = find(with: recipient.email) else {
localContactsCache.save(RecipientObject(recipient))
return
}

recipient.pubKeys
.forEach { pubKey in
if let index = recipientObject.pubKeys.firstIndex(where: { $0.primaryFingerprint == pubKey.fingerprint }) {
update(pubKey: pubKey, for: recipientObject, at: index)
} else {
add(pubKey: pubKey, to: recipientObject)
}
}
}

func searchRecipient(with email: String) -> RecipientWithPubKeys? {
guard let recipientObject = find(with: email) else { return nil }
return RecipientWithPubKeys(recipientObject)
Expand All @@ -76,7 +93,7 @@ extension LocalContactsProvider: LocalContactsProviderType {
func removePubKey(with fingerprint: String, for email: String) {
find(with: email)?
.pubKeys
.filter { $0.fingerprint == fingerprint }
.filter { $0.primaryFingerprint == fingerprint }
.forEach { key in
try? localContactsCache.realm.write {
localContactsCache.realm.delete(key)
Expand All @@ -90,4 +107,22 @@ extension LocalContactsProvider {
localContactsCache.realm.object(ofType: RecipientObject.self,
forPrimaryKey: email)
}

private func add(pubKey: PubKey, to recipient: RecipientObject) {
guard let pubKeyObject = try? PubKeyObject(pubKey) else { return }
try? localContactsCache.realm.write {
recipient.pubKeys.append(pubKeyObject)
}
}

private func update(pubKey: PubKey, for recipient: RecipientObject, at index: Int) {
guard let existingKeyLastSig = recipient.pubKeys[index].lastSig,
let updateKeyLastSig = pubKey.lastSig,
updateKeyLastSig > existingKeyLastSig
else { return }

try? localContactsCache.realm.write {
recipient.pubKeys[index].update(from: pubKey)
}
}
}
18 changes: 16 additions & 2 deletions FlowCrypt/Models/Realm Models/PubKeyObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ final class PubKeyObject: Object {
self.expiresOn = expiresOn
self.created = created

self.longids.append(objectsIn: fingerprints)
self.longids.append(objectsIn: longids)
self.fingerprints.append(objectsIn: fingerprints)

guard let primaryFingerprint = self.fingerprints.first else {
Expand All @@ -64,5 +64,19 @@ extension PubKeyObject {
}

extension PubKeyObject {
var fingerprint: String? { fingerprints.first }
func update(from key: PubKey) {
self.armored = key.armored
self.lastSig = key.lastSig
self.lastChecked = key.lastChecked
self.expiresOn = key.expiresOn
self.created = key.created

let longids = List<String>()
longids.append(objectsIn: key.longids)
self.longids = longids

let fingerprints = List<String>()
fingerprints.append(objectsIn: key.fingerprints)
self.fingerprints = fingerprints
}
}
8 changes: 4 additions & 4 deletions Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ PODS:
- PINRemoteImage/PINCache (3.0.3):
- PINCache (~> 3.0.3)
- PINRemoteImage/Core
- SwiftFormat/CLI (0.48.16)
- SwiftLint (0.44.0)
- SwiftFormat/CLI (0.48.17)
- SwiftLint (0.45.0)
- SwiftyRSA (1.7.0):
- SwiftyRSA/ObjC (= 1.7.0)
- SwiftyRSA/ObjC (1.7.0)
Expand Down Expand Up @@ -64,8 +64,8 @@ SPEC CHECKSUMS:
PINCache: 7a8fc1a691173d21dbddbf86cd515de6efa55086
PINOperation: 00c935935f1e8cf0d1e2d6b542e75b88fc3e5e20
PINRemoteImage: f1295b29f8c5e640e25335a1b2bd9d805171bd01
SwiftFormat: b8a60419c4a8680f67faa80e2da710fd9dc85ca5
SwiftLint: e96c0a8c770c7ebbc4d36c55baf9096bb65c4584
SwiftFormat: 0a9044eb365d74d4a0a2cefa5fe44a4cbef382a7
SwiftLint: e5c7f1fba68eccfc51509d5b2ce1699f5502e0c7
SwiftyRSA: 8c6dd1ea7db1b8dc4fb517a202f88bb1354bc2c6
Texture: 2e8ab2519452515f7f5a520f5a8f7e0a413abfa3

Expand Down