]> nmode's Git Repositories - signal-cli/commitdiff
Attempt to refresh recipients uuid if sending fails
authorAsamK <asamk@gmx.de>
Sun, 2 May 2021 22:04:49 +0000 (00:04 +0200)
committerAsamK <asamk@gmx.de>
Mon, 3 May 2021 16:43:45 +0000 (18:43 +0200)
lib/src/main/java/org/asamk/signal/manager/Manager.java
lib/src/main/java/org/asamk/signal/manager/storage/recipients/RecipientStore.java

index 7f0c48dc232b2ebc9e598e3517876f5acc76cb22..1e7040b1c5d45f5d33be67fbe38c57f3e26a2cf5 100644 (file)
@@ -118,6 +118,7 @@ import org.whispersystems.signalservice.api.profiles.ProfileAndCredential;
 import org.whispersystems.signalservice.api.profiles.SignalServiceProfile;
 import org.whispersystems.signalservice.api.push.SignalServiceAddress;
 import org.whispersystems.signalservice.api.push.exceptions.MissingConfigurationException;
 import org.whispersystems.signalservice.api.profiles.SignalServiceProfile;
 import org.whispersystems.signalservice.api.push.SignalServiceAddress;
 import org.whispersystems.signalservice.api.push.exceptions.MissingConfigurationException;
+import org.whispersystems.signalservice.api.push.exceptions.UnregisteredUserException;
 import org.whispersystems.signalservice.api.util.InvalidNumberException;
 import org.whispersystems.signalservice.api.util.PhoneNumberFormatter;
 import org.whispersystems.signalservice.api.util.SleepTimer;
 import org.whispersystems.signalservice.api.util.InvalidNumberException;
 import org.whispersystems.signalservice.api.util.PhoneNumberFormatter;
 import org.whispersystems.signalservice.api.util.SleepTimer;
@@ -1301,10 +1302,20 @@ public class Manager implements Closeable {
         return signalServiceAddresses.stream().map(this::resolveRecipient).collect(Collectors.toSet());
     }
 
         return signalServiceAddresses.stream().map(this::resolveRecipient).collect(Collectors.toSet());
     }
 
-    private Map<String, UUID> getRegisteredUsers(final Set<String> numbersMissingUuid) throws IOException {
+    private RecipientId refreshRegisteredUser(RecipientId recipientId) throws IOException {
+        final var address = resolveSignalServiceAddress(recipientId);
+        if (!address.getNumber().isPresent()) {
+            return recipientId;
+        }
+        final var number = address.getNumber().get();
+        final var uuidMap = getRegisteredUsers(Set.of(number));
+        return resolveRecipientTrusted(new SignalServiceAddress(uuidMap.getOrDefault(number, null), number));
+    }
+
+    private Map<String, UUID> getRegisteredUsers(final Set<String> numbers) throws IOException {
         try {
             return accountManager.getRegisteredUsers(ServiceConfig.getIasKeyStore(),
         try {
             return accountManager.getRegisteredUsers(ServiceConfig.getIasKeyStore(),
-                    numbersMissingUuid,
+                    numbers,
                     serviceEnvironmentConfig.getCdsMrenclave());
         } catch (Quote.InvalidQuoteFormatException | UnauthenticatedQuoteException | SignatureException | UnauthenticatedResponseException | InvalidKeyException e) {
             throw new IOException(e);
                     serviceEnvironmentConfig.getCdsMrenclave());
         } catch (Quote.InvalidQuoteFormatException | UnauthenticatedQuoteException | SignatureException | UnauthenticatedResponseException | InvalidKeyException e) {
             throw new IOException(e);
@@ -1418,10 +1429,16 @@ public class Manager implements Closeable {
     ) throws IOException {
         var messageSender = createMessageSender();
 
     ) throws IOException {
         var messageSender = createMessageSender();
 
+        final var recipientId = resolveRecipient(address);
         try {
         try {
-            return messageSender.sendMessage(address,
-                    unidentifiedAccessHelper.getAccessFor(resolveRecipient(address)),
-                    message);
+            try {
+                return messageSender.sendMessage(address, unidentifiedAccessHelper.getAccessFor(recipientId), message);
+            } catch (UnregisteredUserException e) {
+                final var newRecipientId = refreshRegisteredUser(recipientId);
+                return messageSender.sendMessage(resolveSignalServiceAddress(newRecipientId),
+                        unidentifiedAccessHelper.getAccessFor(newRecipientId),
+                        message);
+            }
         } catch (UntrustedIdentityException e) {
             return SendMessageResult.identityFailure(address, e.getIdentityKey());
         }
         } catch (UntrustedIdentityException e) {
             return SendMessageResult.identityFailure(address, e.getIdentityKey());
         }
index 0051e734fd86314b40a92176ef2a07ab47c04c91..54f4af76da7c4b8a81820c8f0b7547a53a16ea5b 100644 (file)
@@ -301,7 +301,16 @@ public class RecipientStore implements ContactsStore, ProfileStore {
         }
 
         if (byUuid.isEmpty()) {
         }
 
         if (byUuid.isEmpty()) {
-            logger.debug("Got recipient existing with number, updating with high trust uuid");
+            if (byNumber.get().getAddress().getUuid().isPresent()) {
+                logger.debug(
+                        "Got recipient existing with number, but different uuid, so stripping its number and adding new recipient");
+
+                updateRecipientAddressLocked(byNumber.get().getRecipientId(),
+                        new SignalServiceAddress(byNumber.get().getAddress().getUuid().get(), null));
+                return new Pair<>(addNewRecipientLocked(address), Optional.empty());
+            }
+
+            logger.debug("Got recipient existing with number and no uuid, updating with high trust uuid");
             updateRecipientAddressLocked(byNumber.get().getRecipientId(), address);
             return new Pair<>(byNumber.get().getRecipientId(), Optional.empty());
         }
             updateRecipientAddressLocked(byNumber.get().getRecipientId(), address);
             return new Pair<>(byNumber.get().getRecipientId(), Optional.empty());
         }