From 10ec395915f76e600d0458b3d103b1aeaabd4885 Mon Sep 17 00:00:00 2001 From: Mikunj Date: Tue, 29 Oct 2019 12:13:22 +1100 Subject: [PATCH] Correctly update UI when an outgoing friend request is accepted from a secondary device. --- .../conversation/ConversationActivity.java | 20 ++++++++++++-- .../securesms/jobs/PushDecryptJob.java | 4 ++- .../securesms/loki/MultiDeviceUtilities.kt | 26 ++++++++++++------- 3 files changed, 38 insertions(+), 12 deletions(-) diff --git a/src/org/thoughtcrime/securesms/conversation/ConversationActivity.java b/src/org/thoughtcrime/securesms/conversation/ConversationActivity.java index 31114f6b80..df45d35688 100644 --- a/src/org/thoughtcrime/securesms/conversation/ConversationActivity.java +++ b/src/org/thoughtcrime/securesms/conversation/ConversationActivity.java @@ -128,6 +128,7 @@ import org.thoughtcrime.securesms.contactshare.SimpleTextWatcher; import org.thoughtcrime.securesms.crypto.IdentityKeyParcelable; import org.thoughtcrime.securesms.crypto.SecurityEvent; import org.thoughtcrime.securesms.database.Address; +import org.thoughtcrime.securesms.database.Database; import org.thoughtcrime.securesms.database.DatabaseFactory; import org.thoughtcrime.securesms.database.DraftDatabase; import org.thoughtcrime.securesms.database.DraftDatabase.Draft; @@ -245,6 +246,7 @@ import java.util.Date; import java.util.LinkedList; import java.util.List; import java.util.Locale; +import java.util.Set; import java.util.concurrent.ExecutionException; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; @@ -2187,8 +2189,22 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity @Override public void handleThreadFriendRequestStatusChanged(long threadID) { - if (threadID != this.threadId) { return; } - new Handler(getMainLooper()).post(this::updateInputPanel); + Util.runOnMain(() -> { + boolean shouldUpdateInputPanel = true; + if (threadID != this.threadId) { + Recipient threadRecipient = DatabaseFactory.getThreadDatabase(this).getRecipientForThreadId(threadID); + if (threadRecipient != null && !threadRecipient.isGroupRecipient()) { + // We should update our input if this thread is a part of the other threads device + Set devices = MultiDeviceUtilitiesKt.getAllDevicePublicKeys(threadRecipient.getAddress().serialize(), LokiStorageAPI.Companion.getShared()); + shouldUpdateInputPanel = devices.contains(recipient.getAddress().serialize()); + } else { + shouldUpdateInputPanel = false; + } + } + if (shouldUpdateInputPanel) { + this.updateInputPanel(); + } + }); } private void updateInputPanel() { diff --git a/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java b/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java index f3ba69fe73..ba8aab96d4 100644 --- a/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java +++ b/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java @@ -1099,7 +1099,9 @@ public class PushDecryptJob extends BaseJob implements InjectableType { // it must be a friend request accepted message. Declining a friend request doesn't send a message. lokiThreadDatabase.setFriendRequestStatus(threadID, LokiThreadFriendRequestStatus.FRIENDS); // Update the last message if needed - FriendRequestHandler.updateLastFriendRequestMessage(context, threadID, LokiMessageFriendRequestStatus.REQUEST_ACCEPTED); + String primaryDevice = MultiDeviceUtilitiesKt.getPrimaryDevicePublicKey(pubKey); + long primaryDeviceThreadID = primaryDevice == null ? threadID : DatabaseFactory.getThreadDatabase(context).getThreadIdFor(Recipient.from(context, Address.fromSerialized(primaryDevice), false)); + FriendRequestHandler.updateLastFriendRequestMessage(context, primaryDeviceThreadID, LokiMessageFriendRequestStatus.REQUEST_ACCEPTED); } private void updateFriendRequestStatusIfNeeded(@NonNull SignalServiceEnvelope envelope, @NonNull SignalServiceContent content, @NonNull SignalServiceDataMessage message) { diff --git a/src/org/thoughtcrime/securesms/loki/MultiDeviceUtilities.kt b/src/org/thoughtcrime/securesms/loki/MultiDeviceUtilities.kt index 1fbb4a480f..72811df37c 100644 --- a/src/org/thoughtcrime/securesms/loki/MultiDeviceUtilities.kt +++ b/src/org/thoughtcrime/securesms/loki/MultiDeviceUtilities.kt @@ -41,15 +41,23 @@ fun getAllDeviceFriendRequestStatus(context: Context, hexEncodedPublicKey: Strin fun getAllDevicePublicKeys(context: Context, hexEncodedPublicKey: String, storageAPI: LokiStorageAPI, block: (devicePublicKey: String, isFriend: Boolean, friendCount: Int) -> Unit) { val userHexEncodedPublicKey = TextSecurePreferences.getLocalNumber(context) - storageAPI.getAllDevicePublicKeys(hexEncodedPublicKey).success { items -> - val devices = items.toMutableSet() - if (hexEncodedPublicKey != userHexEncodedPublicKey) { - devices.remove(userHexEncodedPublicKey) - } - val friends = getFriendPublicKeys(context, devices) - for (device in devices) { - block(device, friends.contains(device), friends.count()) - } + val devices = getAllDevicePublicKeys(hexEncodedPublicKey, storageAPI).toMutableSet() + if (hexEncodedPublicKey != userHexEncodedPublicKey) { + devices.remove(userHexEncodedPublicKey) + } + val friends = getFriendPublicKeys(context, devices) + for (device in devices) { + block(device, friends.contains(device), friends.count()) + } +} + +fun getAllDevicePublicKeys(hexEncodedPublicKey: String, storageAPI: LokiStorageAPI): Set { + val future = SettableFuture>() + storageAPI.getAllDevicePublicKeys(hexEncodedPublicKey).success { future.set(it) }.fail { future.setException(it) } + return try { + future.get() + } catch (e: Exception) { + setOf() } }