We should automatically become friends with a contact if we are friends with one of their other devices.

This commit is contained in:
Mikunj 2019-11-06 13:40:55 +11:00
parent b8818cbce4
commit ed6ca6a64a

View File

@ -58,29 +58,24 @@ fun getFriendCount(context: Context, devices: Set<String>): Int {
} }
fun shouldAutomaticallyBecomeFriendsWithDevice(publicKey: String, context: Context): Promise<Boolean, Exception> { fun shouldAutomaticallyBecomeFriendsWithDevice(publicKey: String, context: Context): Promise<Boolean, Exception> {
val lokiThreadDatabase = DatabaseFactory.getLokiThreadDatabase(context)
val storageAPI = LokiStorageAPI.shared
// If this public key is our primary device then we should become friends // If this public key is our primary device then we should become friends
if (publicKey == TextSecurePreferences.getMasterHexEncodedPublicKey(context)) { if (publicKey == TextSecurePreferences.getMasterHexEncodedPublicKey(context)) {
return Promise.of(true) 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 the public key doesn't have any other devices then go through regular friend request logic
if (primaryDevicePublicKey == null) { 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 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)) { 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 // If we are friends with any of the other devices then we should become friends
val primaryDevice = Recipient.from(context, Address.fromSerialized(primaryDevicePublicKey), false) isFriendsWithAnyLinkedDevice(context, Address.fromSerialized(primaryDevicePublicKey))
val primaryDeviceThreadID = DatabaseFactory.getThreadDatabase(context).getThreadIdIfExistsFor(primaryDevice)
primaryDeviceThreadID >= 0 && lokiThreadDatabase.getFriendRequestStatus(primaryDeviceThreadID) == LokiThreadFriendRequestStatus.FRIENDS
} }
} }
@ -153,9 +148,13 @@ fun isOneOfOurDevices(context: Context, address: Address): Promise<Boolean, Exce
} }
fun isFriendsWithAnyLinkedDevice(context: Context, recipient: Recipient): Promise<Boolean, Exception> { fun isFriendsWithAnyLinkedDevice(context: Context, recipient: Recipient): Promise<Boolean, Exception> {
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<Boolean, Exception> {
if (!address.isPhone) { return Promise.of(true) }
return getAllDeviceFriendRequestStatuses(context, address.serialize()).map { map ->
for (status in map.values) { for (status in map.values) {
if (status == LokiThreadFriendRequestStatus.FRIENDS) { if (status == LokiThreadFriendRequestStatus.FRIENDS) {
return@map true return@map true