]> nmode's Git Repositories - signal-cli/commitdiff
Split unregistered recipients
authorAsamK <asamk@gmx.de>
Sun, 28 Jan 2024 21:12:52 +0000 (22:12 +0100)
committerAsamK <asamk@gmx.de>
Sun, 28 Jan 2024 22:04:34 +0000 (23:04 +0100)
lib/src/main/java/org/asamk/signal/manager/storage/recipients/RecipientAddress.java
lib/src/main/java/org/asamk/signal/manager/storage/recipients/RecipientStore.java

index 33b8a5f98514568df18466de6d18fd50e3fce544..b655e067d89e7491cc3e5fa73e200e781cf29c5b 100644 (file)
@@ -42,6 +42,10 @@ public record RecipientAddress(
         this(Optional.ofNullable(aci), Optional.empty(), Optional.ofNullable(e164), Optional.empty());
     }
 
         this(Optional.ofNullable(aci), Optional.empty(), Optional.ofNullable(e164), Optional.empty());
     }
 
+    public RecipientAddress(PNI pni, String e164) {
+        this(Optional.empty(), Optional.ofNullable(pni), Optional.ofNullable(e164), Optional.empty());
+    }
+
     public RecipientAddress(String e164) {
         this(Optional.empty(), Optional.empty(), Optional.ofNullable(e164), Optional.empty());
     }
     public RecipientAddress(String e164) {
         this(Optional.empty(), Optional.empty(), Optional.ofNullable(e164), Optional.empty());
     }
index 6be22754a33f0163d72960cd97e0cd4671079bcd..68ea4895807bd244b2466214cefb55a5bd73732b 100644 (file)
@@ -108,18 +108,8 @@ public class RecipientStore implements RecipientIdCreator, RecipientResolver, Re
     }
 
     public RecipientAddress resolveRecipientAddress(RecipientId recipientId) {
     }
 
     public RecipientAddress resolveRecipientAddress(RecipientId recipientId) {
-        final var sql = (
-                """
-                SELECT r.number, r.aci, r.pni, r.username
-                FROM %s r
-                WHERE r._id = ?
-                """
-        ).formatted(TABLE_RECIPIENT);
         try (final var connection = database.getConnection()) {
         try (final var connection = database.getConnection()) {
-            try (final var statement = connection.prepareStatement(sql)) {
-                statement.setLong(1, recipientId.id());
-                return Utils.executeQuerySingleRow(statement, this::getRecipientAddressFromResultSet);
-            }
+            return resolveRecipientAddress(connection, recipientId);
         } catch (SQLException e) {
             throw new RuntimeException("Failed read from recipient store", e);
         }
         } catch (SQLException e) {
             throw new RuntimeException("Failed read from recipient store", e);
         }
@@ -848,6 +838,9 @@ public class RecipientStore implements RecipientIdCreator, RecipientResolver, Re
             statement.setLong(12, recipientId.id());
             statement.executeUpdate();
         }
             statement.setLong(12, recipientId.id());
             statement.executeUpdate();
         }
+        if (contact != null && contact.unregisteredTimestamp() != null) {
+            markUnregisteredAndSplitIfNecessary(connection, recipientId);
+        }
         rotateStorageId(connection, recipientId);
     }
 
         rotateStorageId(connection, recipientId);
     }
 
@@ -878,7 +871,8 @@ public class RecipientStore implements RecipientIdCreator, RecipientResolver, Re
             for (final var number : unregisteredUsers) {
                 final var recipient = findByNumber(connection, number);
                 if (recipient.isPresent()) {
             for (final var number : unregisteredUsers) {
                 final var recipient = findByNumber(connection, number);
                 if (recipient.isPresent()) {
-                    markUnregistered(connection, recipient.get().id());
+                    final var recipientId = recipient.get().id();
+                    markUnregisteredAndSplitIfNecessary(connection, recipientId);
                 }
             }
             connection.commit();
                 }
             }
             connection.commit();
@@ -887,6 +881,18 @@ public class RecipientStore implements RecipientIdCreator, RecipientResolver, Re
         }
     }
 
         }
     }
 
+    private void markUnregisteredAndSplitIfNecessary(
+            final Connection connection, final RecipientId recipientId
+    ) throws SQLException {
+        markUnregistered(connection, recipientId);
+        final var address = resolveRecipientAddress(connection, recipientId);
+        if (address.aci().isPresent() && address.pni().isPresent()) {
+            final var numberAddress = new RecipientAddress(address.pni().get(), address.number().orElse(null));
+            updateRecipientAddress(connection, recipientId, address.removeIdentifiersFrom(numberAddress));
+            addNewRecipient(connection, numberAddress);
+        }
+    }
+
     private void markRegistered(
             final Connection connection, final RecipientId recipientId
     ) throws SQLException {
     private void markRegistered(
             final Connection connection, final RecipientId recipientId
     ) throws SQLException {
@@ -1001,6 +1007,22 @@ public class RecipientStore implements RecipientIdCreator, RecipientResolver, Re
         rotateStorageId(connection, recipientId);
     }
 
         rotateStorageId(connection, recipientId);
     }
 
+    private RecipientAddress resolveRecipientAddress(
+            final Connection connection, final RecipientId recipientId
+    ) throws SQLException {
+        final var sql = (
+                """
+                SELECT r.number, r.aci, r.pni, r.username
+                FROM %s r
+                WHERE r._id = ?
+                """
+        ).formatted(TABLE_RECIPIENT);
+        try (final var statement = connection.prepareStatement(sql)) {
+            statement.setLong(1, recipientId.id());
+            return Utils.executeQuerySingleRow(statement, this::getRecipientAddressFromResultSet);
+        }
+    }
+
     private RecipientId resolveRecipientTrusted(RecipientAddress address, boolean isSelf) {
         final Pair<RecipientId, List<RecipientId>> pair;
         synchronized (recipientsLock) {
     private RecipientId resolveRecipientTrusted(RecipientAddress address, boolean isSelf) {
         final Pair<RecipientId, List<RecipientId>> pair;
         synchronized (recipientsLock) {