Correctly update UI when an outgoing friend request is accepted from a secondary device.

This commit is contained in:
Mikunj 2019-10-29 12:13:22 +11:00
parent 9ea2a6c8e4
commit 10ec395915
3 changed files with 38 additions and 12 deletions

View File

@ -128,6 +128,7 @@ import org.thoughtcrime.securesms.contactshare.SimpleTextWatcher;
import org.thoughtcrime.securesms.crypto.IdentityKeyParcelable; import org.thoughtcrime.securesms.crypto.IdentityKeyParcelable;
import org.thoughtcrime.securesms.crypto.SecurityEvent; import org.thoughtcrime.securesms.crypto.SecurityEvent;
import org.thoughtcrime.securesms.database.Address; import org.thoughtcrime.securesms.database.Address;
import org.thoughtcrime.securesms.database.Database;
import org.thoughtcrime.securesms.database.DatabaseFactory; import org.thoughtcrime.securesms.database.DatabaseFactory;
import org.thoughtcrime.securesms.database.DraftDatabase; import org.thoughtcrime.securesms.database.DraftDatabase;
import org.thoughtcrime.securesms.database.DraftDatabase.Draft; import org.thoughtcrime.securesms.database.DraftDatabase.Draft;
@ -245,6 +246,7 @@ import java.util.Date;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Set;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
@ -2187,8 +2189,22 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity
@Override @Override
public void handleThreadFriendRequestStatusChanged(long threadID) { public void handleThreadFriendRequestStatusChanged(long threadID) {
if (threadID != this.threadId) { return; } Util.runOnMain(() -> {
new Handler(getMainLooper()).post(this::updateInputPanel); 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<String> 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() { private void updateInputPanel() {

View File

@ -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. // it must be a friend request accepted message. Declining a friend request doesn't send a message.
lokiThreadDatabase.setFriendRequestStatus(threadID, LokiThreadFriendRequestStatus.FRIENDS); lokiThreadDatabase.setFriendRequestStatus(threadID, LokiThreadFriendRequestStatus.FRIENDS);
// Update the last message if needed // 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) { private void updateFriendRequestStatusIfNeeded(@NonNull SignalServiceEnvelope envelope, @NonNull SignalServiceContent content, @NonNull SignalServiceDataMessage message) {

View File

@ -41,8 +41,7 @@ fun getAllDeviceFriendRequestStatus(context: Context, hexEncodedPublicKey: Strin
fun getAllDevicePublicKeys(context: Context, hexEncodedPublicKey: String, storageAPI: LokiStorageAPI, block: (devicePublicKey: String, isFriend: Boolean, friendCount: Int) -> Unit) { fun getAllDevicePublicKeys(context: Context, hexEncodedPublicKey: String, storageAPI: LokiStorageAPI, block: (devicePublicKey: String, isFriend: Boolean, friendCount: Int) -> Unit) {
val userHexEncodedPublicKey = TextSecurePreferences.getLocalNumber(context) val userHexEncodedPublicKey = TextSecurePreferences.getLocalNumber(context)
storageAPI.getAllDevicePublicKeys(hexEncodedPublicKey).success { items -> val devices = getAllDevicePublicKeys(hexEncodedPublicKey, storageAPI).toMutableSet()
val devices = items.toMutableSet()
if (hexEncodedPublicKey != userHexEncodedPublicKey) { if (hexEncodedPublicKey != userHexEncodedPublicKey) {
devices.remove(userHexEncodedPublicKey) devices.remove(userHexEncodedPublicKey)
} }
@ -51,6 +50,15 @@ fun getAllDevicePublicKeys(context: Context, hexEncodedPublicKey: String, storag
block(device, friends.contains(device), friends.count()) block(device, friends.contains(device), friends.count())
} }
} }
fun getAllDevicePublicKeys(hexEncodedPublicKey: String, storageAPI: LokiStorageAPI): Set<String> {
val future = SettableFuture<Set<String>>()
storageAPI.getAllDevicePublicKeys(hexEncodedPublicKey).success { future.set(it) }.fail { future.setException(it) }
return try {
future.get()
} catch (e: Exception) {
setOf()
}
} }
fun shouldAutomaticallyBecomeFriendsWithDevice(publicKey: String, context: Context): Boolean { fun shouldAutomaticallyBecomeFriendsWithDevice(publicKey: String, context: Context): Boolean {