]> nmode's Git Repositories - signal-cli/commitdiff
Workaround issue with invalid address in recipient store
authorAsamK <asamk@gmx.de>
Sat, 23 Nov 2024 22:20:45 +0000 (23:20 +0100)
committerAsamK <asamk@gmx.de>
Sat, 23 Nov 2024 22:57:23 +0000 (23:57 +0100)
lib/src/main/java/org/asamk/signal/manager/storage/recipients/InvalidAddress.java [new file with mode: 0644]
lib/src/main/java/org/asamk/signal/manager/storage/recipients/RecipientAddress.java
lib/src/main/java/org/asamk/signal/manager/storage/recipients/RecipientStore.java

diff --git a/lib/src/main/java/org/asamk/signal/manager/storage/recipients/InvalidAddress.java b/lib/src/main/java/org/asamk/signal/manager/storage/recipients/InvalidAddress.java
new file mode 100644 (file)
index 0000000..991bc16
--- /dev/null
@@ -0,0 +1,8 @@
+package org.asamk.signal.manager.storage.recipients;
+
+public class InvalidAddress extends AssertionError {
+
+    InvalidAddress(String message) {
+        super(message);
+    }
+}
index 56adb0a68f78524fd3bc0161a99af0035fa62ff4..b80f81da57e5f9b10bbbc478111b29050c5aab58 100644 (file)
@@ -27,7 +27,7 @@ public record RecipientAddress(
             pni = Optional.empty();
         }
         if (aci.isEmpty() && pni.isEmpty() && number.isEmpty() && username.isEmpty()) {
-            throw new AssertionError("Must have either a ServiceId, username or E164 number!");
+            throw new InvalidAddress("Must have either a ServiceId, username or E164 number!");
         }
     }
 
index 00048560e91ffd59dd2fd34091c33aee036c8205..aa245369f990845bda281e030a408b28337320a5 100644 (file)
@@ -339,7 +339,7 @@ public class RecipientStore implements RecipientIdCreator, RecipientResolver, Re
                 """
                 SELECT r._id, r.given_name, r.family_name, r.nick_name, r.nick_name_given_name, r.nick_name_family_name, r.note, r.expiration_time, r.expiration_time_version, r.mute_until, r.hide_story, r.profile_sharing, r.color, r.blocked, r.archived, r.hidden, r.unregistered_timestamp
                 FROM %s r
-                WHERE (r.number IS NOT NULL OR r.aci IS NOT NULL) AND %s AND r.hidden = FALSE
+                WHERE (r.number IS NOT NULL OR r.pni IS NOT NULL OR r.aci IS NOT NULL) AND %s AND r.hidden = FALSE
                 """
         ).formatted(TABLE_RECIPIENT, SQL_IS_CONTACT);
         try (final var connection = database.getConnection()) {
@@ -392,6 +392,15 @@ public class RecipientStore implements RecipientIdCreator, RecipientResolver, Re
         try (final var statement = connection.prepareStatement(sql)) {
             statement.setBytes(1, storageId.getRaw());
             return Utils.executeQuerySingleRow(statement, this::getRecipientFromResultSet);
+        } catch (InvalidAddress e) {
+            try (final var statement = connection.prepareStatement("""
+                                                                   UPDATE %s SET aci=NULL, pni=NULL, username=NULL, number=NULL, storage_id=NULL WHERE storage_id = ?
+                                                                   """.formatted(TABLE_RECIPIENT))) {
+                statement.setBytes(1, storageId.getRaw());
+                statement.executeUpdate();
+            }
+            connection.commit();
+            throw e;
         }
     }
 
@@ -426,7 +435,7 @@ public class RecipientStore implements RecipientIdCreator, RecipientResolver, Re
                        r.discoverable,
                        r.storage_record
                 FROM %s r
-                WHERE (r.number IS NOT NULL OR r.aci IS NOT NULL) AND %s
+                WHERE (r.number IS NOT NULL OR r.pni IS NOT NULL OR r.aci IS NOT NULL) AND %s
                 """
         ).formatted(TABLE_RECIPIENT, sqlWhere.isEmpty() ? "TRUE" : String.join(" AND ", sqlWhere));
         final var selfAddress = selfAddressProvider.getSelfAddress();
@@ -512,7 +521,7 @@ public class RecipientStore implements RecipientIdCreator, RecipientResolver, Re
                 """
                 SELECT r._id
                 FROM %s r
-                WHERE (r.number IS NOT NULL OR r.aci IS NOT NULL)
+                WHERE (r.aci IS NOT NULL OR r.pni IS NOT NULL)
                 """
         ).formatted(TABLE_RECIPIENT);
         try (final var statement = connection.prepareStatement(sql)) {
@@ -525,7 +534,7 @@ public class RecipientStore implements RecipientIdCreator, RecipientResolver, Re
                 """
                 SELECT r._id
                 FROM %s r
-                WHERE r.storage_id IS NULL AND r.unregistered_timestamp IS NULL
+                WHERE r.storage_id IS NULL AND r.unregistered_timestamp IS NULL AND (r.aci IS NOT NULL OR r.pni IS NOT NULL)
                 """
         ).formatted(TABLE_RECIPIENT);
         final var updateSql = (