Fix ui not being dismissed on message send failure.

Fix prekey bundle not being sent with the device pairing request.
This commit is contained in:
Mikunj 2019-10-02 14:22:09 +10:00
parent 72059328b3
commit e07d8ddb52
5 changed files with 43 additions and 40 deletions

View File

@ -1204,18 +1204,14 @@ public class PushDecryptJob extends BaseJob implements InjectableType {
} }
private void sendBackgroundMessage(String contactHexEncodedPublicKey) { private void sendBackgroundMessage(String contactHexEncodedPublicKey) {
new Handler(Looper.getMainLooper()).post(new Runnable() { Util.runOnMain(() -> {
SignalServiceMessageSender messageSender = ApplicationContext.getInstance(context).communicationModule.provideSignalMessageSender();
@Override SignalServiceAddress address = new SignalServiceAddress(contactHexEncodedPublicKey);
public void run() { SignalServiceDataMessage message = new SignalServiceDataMessage(System.currentTimeMillis(), "");
SignalServiceMessageSender messageSender = ApplicationContext.getInstance(context).communicationModule.provideSignalMessageSender(); try {
SignalServiceAddress address = new SignalServiceAddress(contactHexEncodedPublicKey); messageSender.sendMessage(0, address, Optional.absent(), message); // The message ID doesn't matter
SignalServiceDataMessage message = new SignalServiceDataMessage(System.currentTimeMillis(), ""); } catch (Exception e) {
try { Log.d("Loki", "Failed to send background message to: " + contactHexEncodedPublicKey + ".");
messageSender.sendMessage(0, address, Optional.absent(), message); // The message ID doesn't matter
} catch (Exception e) {
Log.d("Loki", "Failed to send background message to: " + contactHexEncodedPublicKey + ".");
}
} }
}); });
} }

View File

@ -10,6 +10,8 @@ import org.whispersystems.signalservice.loki.api.LokiDeviceLinkingSessionListene
import org.whispersystems.signalservice.loki.api.LokiPairingAuthorisation import org.whispersystems.signalservice.loki.api.LokiPairingAuthorisation
import org.whispersystems.signalservice.loki.api.LokiStorageAPI import org.whispersystems.signalservice.loki.api.LokiStorageAPI
import org.whispersystems.signalservice.loki.utilities.retryIfNeeded import org.whispersystems.signalservice.loki.utilities.retryIfNeeded
import org.thoughtcrime.securesms.util.Util
class DeviceLinkingDialog private constructor(private val context: Context, private val mode: DeviceLinkingView.Mode, private val delegate: DeviceLinkingDialogDelegate? = null): DeviceLinkingViewDelegate, LokiDeviceLinkingSessionListener { class DeviceLinkingDialog private constructor(private val context: Context, private val mode: DeviceLinkingView.Mode, private val delegate: DeviceLinkingDialogDelegate? = null): DeviceLinkingViewDelegate, LokiDeviceLinkingSessionListener {
private lateinit var view: DeviceLinkingView private lateinit var view: DeviceLinkingView
@ -93,14 +95,18 @@ class DeviceLinkingDialog private constructor(private val context: Context, priv
// region Loki Device Session Listener // region Loki Device Session Listener
override fun onDeviceLinkingRequestReceived(authorisation: LokiPairingAuthorisation) { override fun onDeviceLinkingRequestReceived(authorisation: LokiPairingAuthorisation) {
view.requestUserAuthorization(authorisation) Util.runOnMain {
view.requestUserAuthorization(authorisation)
}
// Stop listening to any more requests // Stop listening to any more requests
LokiDeviceLinkingSession.shared.stopListeningForLinkingRequests() LokiDeviceLinkingSession.shared.stopListeningForLinkingRequests()
} }
override fun onDeviceLinkRequestAccepted(authorisation: LokiPairingAuthorisation) { override fun onDeviceLinkRequestAccepted(authorisation: LokiPairingAuthorisation) {
view.onDeviceLinkAuthorized(authorisation) Util.runOnMain {
view.onDeviceLinkAuthorized(authorisation)
}
// Stop listening to any more requests // Stop listening to any more requests
LokiDeviceLinkingSession.shared.stopListeningForLinkingRequests() LokiDeviceLinkingSession.shared.stopListeningForLinkingRequests()

View File

@ -108,8 +108,6 @@ class DeviceLinkingView private constructor(context: Context, attrs: AttributeSe
return return
} }
this.pairingAuthorisation = authorisation
spinner.visibility = View.GONE spinner.visibility = View.GONE
val titleTextViewLayoutParams = titleTextView.layoutParams as LayoutParams val titleTextViewLayoutParams = titleTextView.layoutParams as LayoutParams
titleTextViewLayoutParams.topMargin = toPx(16, resources) titleTextViewLayoutParams.topMargin = toPx(16, resources)
@ -120,6 +118,8 @@ class DeviceLinkingView private constructor(context: Context, attrs: AttributeSe
val hexEncodedPublicKey = authorisation.secondaryDevicePubKey.removing05PrefixIfNeeded() val hexEncodedPublicKey = authorisation.secondaryDevicePubKey.removing05PrefixIfNeeded()
mnemonicTextView.text = MnemonicCodec(languageFileDirectory).encode(hexEncodedPublicKey).split(" ").slice(0 until 3).joinToString(" ") mnemonicTextView.text = MnemonicCodec(languageFileDirectory).encode(hexEncodedPublicKey).split(" ").slice(0 until 3).joinToString(" ")
authorizeButton.visibility = View.VISIBLE authorizeButton.visibility = View.VISIBLE
this.pairingAuthorisation = authorisation
} }
private fun authorize() { private fun authorize() {

View File

@ -232,7 +232,7 @@ class SeedActivity : BaseActionBarActivity() {
// Send the request to the other user // Send the request to the other user
CoroutineScope(Dispatchers.Main).launch { CoroutineScope(Dispatchers.Main).launch {
retryIfNeeded(3) { retryIfNeeded(3) {
sendAuthorisationMessage(this@SeedActivity, authorisation.primaryDevicePubKey, authorisation) sendAuthorisationMessage(this@SeedActivity, authorisation.primaryDevicePubKey, authorisation).get()
}.failUi { }.failUi {
dialog.dismiss() dialog.dismiss()
resetRegistration() resetRegistration()

View File

@ -6,6 +6,7 @@ import android.os.Looper
import nl.komponents.kovenant.Promise import nl.komponents.kovenant.Promise
import nl.komponents.kovenant.deferred import nl.komponents.kovenant.deferred
import org.thoughtcrime.securesms.ApplicationContext import org.thoughtcrime.securesms.ApplicationContext
import org.thoughtcrime.securesms.database.DatabaseFactory
import org.thoughtcrime.securesms.logging.Log import org.thoughtcrime.securesms.logging.Log
import org.whispersystems.libsignal.util.guava.Optional import org.whispersystems.libsignal.util.guava.Optional
import org.whispersystems.signalservice.api.crypto.UnidentifiedAccessPair import org.whispersystems.signalservice.api.crypto.UnidentifiedAccessPair
@ -14,30 +15,30 @@ import org.whispersystems.signalservice.api.push.SignalServiceAddress
import org.whispersystems.signalservice.loki.api.LokiPairingAuthorisation import org.whispersystems.signalservice.loki.api.LokiPairingAuthorisation
fun sendAuthorisationMessage(context: Context, contactHexEncodedPublicKey: String, authorisation: LokiPairingAuthorisation): Promise<Unit, Exception> { fun sendAuthorisationMessage(context: Context, contactHexEncodedPublicKey: String, authorisation: LokiPairingAuthorisation): Promise<Unit, Exception> {
val deferred = deferred<Unit, Exception>() val messageSender = ApplicationContext.getInstance(context).communicationModule.provideSignalMessageSender()
Handler(Looper.getMainLooper()).post { val address = SignalServiceAddress(contactHexEncodedPublicKey)
val messageSender = ApplicationContext.getInstance(context).communicationModule.provideSignalMessageSender() val message = SignalServiceDataMessage.newBuilder().withBody("").withPairingAuthorisation(authorisation)
val address = SignalServiceAddress(contactHexEncodedPublicKey)
// A REQUEST should always act as a friend request. A GRANT should always be replying back as a normal message.
val message = SignalServiceDataMessage.newBuilder().withBody("").withPairingAuthorisation(authorisation).asFriendRequest(authorisation.type == LokiPairingAuthorisation.Type.REQUEST).build()
try {
Log.d("Loki", "Sending authorisation message to $contactHexEncodedPublicKey")
val result = messageSender.sendMessage(0, address, Optional.absent<UnidentifiedAccessPair>(), message)
if (result.success == null) {
val exception = when {
result.isNetworkFailure -> "Failed to send authorisation message because of a Network Error"
else -> "Failed to send authorisation message"
}
throw Exception(exception) // A REQUEST should always act as a friend request. A GRANT should always be replying back as a normal message.
} if (authorisation.type == LokiPairingAuthorisation.Type.REQUEST) {
val preKeyBundle = DatabaseFactory.getLokiPreKeyBundleDatabase(context).generatePreKeyBundle(address.number)
deferred.resolve(Unit) message.asFriendRequest(true).withPreKeyBundle(preKeyBundle)
} catch (e: Exception) {
Log.d("Loki", "Failed to send authorisation message to: $contactHexEncodedPublicKey.")
deferred.reject(e)
}
} }
return deferred.promise return try {
Log.d("Loki", "Sending authorisation message to $contactHexEncodedPublicKey")
val result = messageSender.sendMessage(0, address, Optional.absent<UnidentifiedAccessPair>(), message.build())
if (result.success == null) {
val exception = when {
result.isNetworkFailure -> "Failed to send authorisation message because of a Network Error"
else -> "Failed to send authorisation message"
}
throw Exception(exception)
}
Promise.ofSuccess(Unit)
} catch (e: Exception) {
Log.d("Loki", "Failed to send authorisation message to: $contactHexEncodedPublicKey.")
Promise.ofFail(e)
}
} }