From ed6ca6a64ae7d7f2bfb58b0c382347b624278040 Mon Sep 17 00:00:00 2001 From: Mikunj Date: Wed, 6 Nov 2019 13:40:55 +1100 Subject: [PATCH] We should automatically become friends with a contact if we are friends with one of their other devices. --- .../securesms/loki/MultiDeviceUtilities.kt | 23 +++++++++---------- 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/org/thoughtcrime/securesms/loki/MultiDeviceUtilities.kt b/src/org/thoughtcrime/securesms/loki/MultiDeviceUtilities.kt index 7c73b73eac..7ea362e844 100644 --- a/src/org/thoughtcrime/securesms/loki/MultiDeviceUtilities.kt +++ b/src/org/thoughtcrime/securesms/loki/MultiDeviceUtilities.kt @@ -58,29 +58,24 @@ fun getFriendCount(context: Context, devices: Set): Int { } fun shouldAutomaticallyBecomeFriendsWithDevice(publicKey: String, context: Context): Promise { - val lokiThreadDatabase = DatabaseFactory.getLokiThreadDatabase(context) - val storageAPI = LokiStorageAPI.shared - // If this public key is our primary device then we should become friends if (publicKey == TextSecurePreferences.getMasterHexEncodedPublicKey(context)) { return Promise.of(true) } - return storageAPI.getPrimaryDevicePublicKey(publicKey).map { primaryDevicePublicKey -> + return LokiStorageAPI.shared.getPrimaryDevicePublicKey(publicKey).bind { primaryDevicePublicKey -> // If the public key doesn't have any other devices then go through regular friend request logic if (primaryDevicePublicKey == null) { - return@map false + return@bind Promise.of(false) } // If the primary device public key matches our primary device then we should become friends since this is our other device if (primaryDevicePublicKey == TextSecurePreferences.getMasterHexEncodedPublicKey(context)) { - return@map true + return@bind Promise.of(true) } - // If we are friends with the primary device then we should become friends - val primaryDevice = Recipient.from(context, Address.fromSerialized(primaryDevicePublicKey), false) - val primaryDeviceThreadID = DatabaseFactory.getThreadDatabase(context).getThreadIdIfExistsFor(primaryDevice) - primaryDeviceThreadID >= 0 && lokiThreadDatabase.getFriendRequestStatus(primaryDeviceThreadID) == LokiThreadFriendRequestStatus.FRIENDS + // If we are friends with any of the other devices then we should become friends + isFriendsWithAnyLinkedDevice(context, Address.fromSerialized(primaryDevicePublicKey)) } } @@ -153,9 +148,13 @@ fun isOneOfOurDevices(context: Context, address: Address): Promise { - if (recipient.isGroupRecipient) { return Promise.of(true) } + return isFriendsWithAnyLinkedDevice(context, recipient.address) +} - return getAllDeviceFriendRequestStatuses(context, recipient.address.serialize()).map { map -> +fun isFriendsWithAnyLinkedDevice(context: Context, address: Address): Promise { + if (!address.isPhone) { return Promise.of(true) } + + return getAllDeviceFriendRequestStatuses(context, address.serialize()).map { map -> for (status in map.values) { if (status == LokiThreadFriendRequestStatus.FRIENDS) { return@map true