2019-10-30 09:59:11 +11:00
|
|
|
@file:JvmName("MultiDeviceUtilities")
|
2019-10-01 16:12:37 +10:00
|
|
|
package org.thoughtcrime.securesms.loki
|
|
|
|
|
|
|
|
import android.content.Context
|
2019-10-29 11:23:30 +11:00
|
|
|
import android.os.Handler
|
2019-11-01 12:15:40 +11:00
|
|
|
import nl.komponents.kovenant.*
|
|
|
|
import nl.komponents.kovenant.functional.bind
|
2019-10-31 11:36:52 +11:00
|
|
|
import nl.komponents.kovenant.functional.map
|
2019-10-01 16:12:37 +10:00
|
|
|
import org.thoughtcrime.securesms.ApplicationContext
|
2019-10-23 15:29:56 +11:00
|
|
|
import org.thoughtcrime.securesms.crypto.IdentityKeyUtil
|
2019-10-03 09:28:08 +10:00
|
|
|
import org.thoughtcrime.securesms.database.Address
|
2019-10-02 14:22:09 +10:00
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory
|
2019-10-01 16:12:37 +10:00
|
|
|
import org.thoughtcrime.securesms.logging.Log
|
2019-10-03 09:28:08 +10:00
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient
|
|
|
|
import org.thoughtcrime.securesms.util.TextSecurePreferences
|
2019-10-01 16:12:37 +10:00
|
|
|
import org.whispersystems.libsignal.util.guava.Optional
|
|
|
|
import org.whispersystems.signalservice.api.crypto.UnidentifiedAccessPair
|
|
|
|
import org.whispersystems.signalservice.api.messages.SignalServiceDataMessage
|
|
|
|
import org.whispersystems.signalservice.api.push.SignalServiceAddress
|
2019-10-03 09:28:08 +10:00
|
|
|
import org.whispersystems.signalservice.loki.api.LokiStorageAPI
|
2019-10-07 15:30:20 +11:00
|
|
|
import org.whispersystems.signalservice.loki.api.PairingAuthorisation
|
2019-10-03 09:28:08 +10:00
|
|
|
import org.whispersystems.signalservice.loki.messaging.LokiThreadFriendRequestStatus
|
2019-11-01 12:15:40 +11:00
|
|
|
import org.whispersystems.signalservice.loki.utilities.recover
|
2019-10-23 15:29:56 +11:00
|
|
|
import org.whispersystems.signalservice.loki.utilities.retryIfNeeded
|
2019-10-03 09:28:08 +10:00
|
|
|
|
2019-11-01 12:15:40 +11:00
|
|
|
fun getAllDeviceFriendRequestStatuses(context: Context, hexEncodedPublicKey: String): Promise<Map<String, LokiThreadFriendRequestStatus>, Exception> {
|
2019-10-23 12:40:25 +11:00
|
|
|
val lokiThreadDatabase = DatabaseFactory.getLokiThreadDatabase(context)
|
2019-11-01 12:15:40 +11:00
|
|
|
return LokiStorageAPI.shared.getAllDevicePublicKeys(hexEncodedPublicKey).map { keys ->
|
|
|
|
val map = mutableMapOf<String, LokiThreadFriendRequestStatus>()
|
|
|
|
for (devicePublicKey in keys) {
|
|
|
|
val device = Recipient.from(context, Address.fromSerialized(devicePublicKey), false)
|
|
|
|
val threadID = DatabaseFactory.getThreadDatabase(context).getThreadIdIfExistsFor(device)
|
|
|
|
val friendRequestStatus = if (threadID < 0) LokiThreadFriendRequestStatus.NONE else lokiThreadDatabase.getFriendRequestStatus(threadID)
|
|
|
|
map[devicePublicKey] = friendRequestStatus
|
|
|
|
}
|
|
|
|
map
|
|
|
|
}.recover { mutableMapOf() }
|
2019-10-23 12:40:25 +11:00
|
|
|
}
|
|
|
|
|
2019-10-31 11:36:52 +11:00
|
|
|
fun getAllDevicePublicKeysWithFriendStatus(context: Context, hexEncodedPublicKey: String): Promise<Map<String, Boolean>, Unit> {
|
2019-10-08 10:43:09 +11:00
|
|
|
val userHexEncodedPublicKey = TextSecurePreferences.getLocalNumber(context)
|
2019-11-01 12:15:40 +11:00
|
|
|
return LokiStorageAPI.shared.getAllDevicePublicKeys(hexEncodedPublicKey).map { keys ->
|
2019-10-31 11:36:52 +11:00
|
|
|
val devices = keys.toMutableSet()
|
|
|
|
if (hexEncodedPublicKey != userHexEncodedPublicKey) {
|
|
|
|
devices.remove(userHexEncodedPublicKey)
|
|
|
|
}
|
|
|
|
val friends = getFriendPublicKeys(context, devices)
|
|
|
|
val friendMap = mutableMapOf<String, Boolean>()
|
|
|
|
for (device in devices) {
|
|
|
|
friendMap[device] = friends.contains(device)
|
|
|
|
}
|
|
|
|
friendMap
|
|
|
|
}.toFailVoid()
|
2019-10-29 12:13:22 +11:00
|
|
|
}
|
|
|
|
|
2019-10-30 09:59:11 +11:00
|
|
|
fun getFriendCount(context: Context, devices: Set<String>): Int {
|
|
|
|
return getFriendPublicKeys(context, devices).count()
|
2019-10-04 16:14:25 +10:00
|
|
|
}
|
|
|
|
|
2019-11-01 12:15:40 +11:00
|
|
|
fun shouldAutomaticallyBecomeFriendsWithDevice(publicKey: String, context: Context): Promise<Boolean, Exception> {
|
|
|
|
// If this public key is our primary device then we should become friends
|
|
|
|
if (publicKey == TextSecurePreferences.getMasterHexEncodedPublicKey(context)) {
|
|
|
|
return Promise.of(true)
|
2019-10-03 09:28:08 +10:00
|
|
|
}
|
2019-10-30 09:59:11 +11:00
|
|
|
|
2019-11-06 13:40:55 +11:00
|
|
|
return LokiStorageAPI.shared.getPrimaryDevicePublicKey(publicKey).bind { primaryDevicePublicKey ->
|
2019-11-01 12:15:40 +11:00
|
|
|
// If the public key doesn't have any other devices then go through regular friend request logic
|
|
|
|
if (primaryDevicePublicKey == null) {
|
2019-11-06 13:40:55 +11:00
|
|
|
return@bind Promise.of(false)
|
2019-11-01 12:15:40 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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)) {
|
2019-11-06 13:40:55 +11:00
|
|
|
return@bind Promise.of(true)
|
2019-11-01 12:15:40 +11:00
|
|
|
}
|
|
|
|
|
2019-11-06 13:40:55 +11:00
|
|
|
// If we are friends with any of the other devices then we should become friends
|
|
|
|
isFriendsWithAnyLinkedDevice(context, Address.fromSerialized(primaryDevicePublicKey))
|
2019-11-01 12:15:40 +11:00
|
|
|
}
|
2019-10-03 09:28:08 +10:00
|
|
|
}
|
2019-10-01 16:12:37 +10:00
|
|
|
|
2019-10-07 16:15:06 +11:00
|
|
|
fun sendPairingAuthorisationMessage(context: Context, contactHexEncodedPublicKey: String, authorisation: PairingAuthorisation): Promise<Unit, Exception> {
|
2019-10-02 14:22:09 +10:00
|
|
|
val messageSender = ApplicationContext.getInstance(context).communicationModule.provideSignalMessageSender()
|
|
|
|
val address = SignalServiceAddress(contactHexEncodedPublicKey)
|
|
|
|
val message = SignalServiceDataMessage.newBuilder().withBody("").withPairingAuthorisation(authorisation)
|
|
|
|
// A REQUEST should always act as a friend request. A GRANT should always be replying back as a normal message.
|
2019-10-07 15:30:20 +11:00
|
|
|
if (authorisation.type == PairingAuthorisation.Type.REQUEST) {
|
2019-10-02 14:22:09 +10:00
|
|
|
val preKeyBundle = DatabaseFactory.getLokiPreKeyBundleDatabase(context).generatePreKeyBundle(address.number)
|
|
|
|
message.asFriendRequest(true).withPreKeyBundle(preKeyBundle)
|
|
|
|
}
|
|
|
|
return try {
|
2019-10-08 10:43:09 +11:00
|
|
|
Log.d("Loki", "Sending authorisation message to: $contactHexEncodedPublicKey.")
|
2019-10-02 14:22:09 +10:00
|
|
|
val result = messageSender.sendMessage(0, address, Optional.absent<UnidentifiedAccessPair>(), message.build())
|
|
|
|
if (result.success == null) {
|
|
|
|
val exception = when {
|
2019-10-08 10:43:09 +11:00
|
|
|
result.isNetworkFailure -> "Failed to send authorisation message due to a network error."
|
|
|
|
else -> "Failed to send authorisation message."
|
2019-10-02 12:20:18 +10:00
|
|
|
}
|
2019-10-02 14:22:09 +10:00
|
|
|
throw Exception(exception)
|
2019-10-01 16:12:37 +10:00
|
|
|
}
|
2019-10-02 14:22:09 +10:00
|
|
|
Promise.ofSuccess(Unit)
|
|
|
|
} catch (e: Exception) {
|
|
|
|
Log.d("Loki", "Failed to send authorisation message to: $contactHexEncodedPublicKey.")
|
|
|
|
Promise.ofFail(e)
|
2019-10-01 16:12:37 +10:00
|
|
|
}
|
2019-10-23 15:29:56 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
fun signAndSendPairingAuthorisationMessage(context: Context, pairingAuthorisation: PairingAuthorisation) {
|
|
|
|
val userPrivateKey = IdentityKeyUtil.getIdentityKeyPair(context).privateKey.serialize()
|
|
|
|
val signedPairingAuthorisation = pairingAuthorisation.sign(PairingAuthorisation.Type.GRANT, userPrivateKey)
|
|
|
|
if (signedPairingAuthorisation == null || signedPairingAuthorisation.type != PairingAuthorisation.Type.GRANT) {
|
|
|
|
Log.d("Loki", "Failed to sign pairing authorization.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
retryIfNeeded(8) {
|
2019-11-01 15:49:26 +11:00
|
|
|
sendPairingAuthorisationMessage(context, pairingAuthorisation.secondaryDevicePublicKey, signedPairingAuthorisation)
|
2019-10-23 15:29:56 +11:00
|
|
|
}.fail {
|
|
|
|
Log.d("Loki", "Failed to send pairing authorization message to ${pairingAuthorisation.secondaryDevicePublicKey}.")
|
|
|
|
}
|
|
|
|
DatabaseFactory.getLokiAPIDatabase(context).insertOrUpdatePairingAuthorisation(signedPairingAuthorisation)
|
2019-11-04 11:17:34 +11:00
|
|
|
TextSecurePreferences.setMultiDevice(context, true)
|
2019-10-29 11:23:30 +11:00
|
|
|
// Call function after a short delay
|
|
|
|
Handler().postDelayed({
|
|
|
|
LokiStorageAPI.shared.updateUserDeviceMappings().fail {
|
|
|
|
Log.w("Loki", "Failed to update device mapping")
|
|
|
|
}
|
|
|
|
}, 100)
|
|
|
|
|
2019-10-24 17:16:53 +11:00
|
|
|
}
|
|
|
|
|
2019-11-01 12:15:40 +11:00
|
|
|
fun isOneOfOurDevices(context: Context, address: Address): Promise<Boolean, Exception> {
|
2019-10-24 17:16:53 +11:00
|
|
|
if (address.isGroup || address.isEmail || address.isMmsGroup) {
|
2019-11-01 12:15:40 +11:00
|
|
|
return Promise.of(false)
|
2019-10-24 17:16:53 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
val ourPublicKey = TextSecurePreferences.getLocalNumber(context)
|
2019-11-01 12:15:40 +11:00
|
|
|
return LokiStorageAPI.shared.getAllDevicePublicKeys(ourPublicKey).map { devices ->
|
|
|
|
devices.contains(address.serialize())
|
|
|
|
}
|
2019-10-25 16:14:15 +11:00
|
|
|
}
|
|
|
|
|
2019-11-01 12:15:40 +11:00
|
|
|
fun isFriendsWithAnyLinkedDevice(context: Context, recipient: Recipient): Promise<Boolean, Exception> {
|
2019-11-06 13:40:55 +11:00
|
|
|
return isFriendsWithAnyLinkedDevice(context, recipient.address)
|
|
|
|
}
|
|
|
|
|
|
|
|
fun isFriendsWithAnyLinkedDevice(context: Context, address: Address): Promise<Boolean, Exception> {
|
|
|
|
if (!address.isPhone) { return Promise.of(true) }
|
2019-10-25 16:14:15 +11:00
|
|
|
|
2019-11-06 13:40:55 +11:00
|
|
|
return getAllDeviceFriendRequestStatuses(context, address.serialize()).map { map ->
|
2019-11-01 12:15:40 +11:00
|
|
|
for (status in map.values) {
|
|
|
|
if (status == LokiThreadFriendRequestStatus.FRIENDS) {
|
|
|
|
return@map true
|
|
|
|
}
|
2019-10-25 16:14:15 +11:00
|
|
|
}
|
2019-11-01 12:15:40 +11:00
|
|
|
false
|
2019-10-25 16:14:15 +11:00
|
|
|
}
|
2019-11-01 12:15:40 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
fun hasPendingFriendRequestWithAnyLinkedDevice(context: Context, recipient: Recipient): Promise<Boolean, Exception> {
|
|
|
|
if (recipient.isGroupRecipient) { return Promise.of(false) }
|
|
|
|
|
|
|
|
return getAllDeviceFriendRequestStatuses(context, recipient.address.serialize()).map { map ->
|
|
|
|
for (status in map.values) {
|
|
|
|
if (status == LokiThreadFriendRequestStatus.REQUEST_SENDING || status == LokiThreadFriendRequestStatus.REQUEST_SENT || status == LokiThreadFriendRequestStatus.REQUEST_RECEIVED) {
|
|
|
|
return@map true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fun shouldEnableUserInput(context: Context, recipient: Recipient): Promise<Boolean, Exception> {
|
|
|
|
// Input should be enabled if we don't have any pending requests OR we're friends with any linked device
|
|
|
|
return hasPendingFriendRequestWithAnyLinkedDevice(context, recipient).bind { hasPendingFriendRequest ->
|
|
|
|
if (!hasPendingFriendRequest) Promise.of(true) else isFriendsWithAnyLinkedDevice(context, recipient)
|
|
|
|
}.recover { true }
|
|
|
|
}
|