]> nmode's Git Repositories - signal-cli/commitdiff
Fix storing of username
authorAsamK <asamk@gmx.de>
Sun, 12 Nov 2023 17:50:56 +0000 (18:50 +0100)
committerAsamK <asamk@gmx.de>
Tue, 21 Nov 2023 19:40:49 +0000 (20:40 +0100)
lib/src/main/java/org/asamk/signal/manager/storage/recipients/MergeRecipientHelper.java
lib/src/main/java/org/asamk/signal/manager/storage/recipients/RecipientStore.java
lib/src/test/java/org/asamk/signal/manager/storage/recipients/MergeRecipientHelperTest.java

index 0afde555ac0a66fd8be6d0fd68cf97cb23ef54d9..d9fecaa524be46874f2cad2568ea28a9f19cf09f 100644 (file)
@@ -17,12 +17,12 @@ public class MergeRecipientHelper {
     static Pair<RecipientId, List<RecipientId>> resolveRecipientTrustedLocked(
             Store store, RecipientAddress address
     ) throws SQLException {
     static Pair<RecipientId, List<RecipientId>> resolveRecipientTrustedLocked(
             Store store, RecipientAddress address
     ) throws SQLException {
-        // address has serviceId and number, optionally also pni
+        // address has at least one of serviceId/pni and optionally number/username
 
         final var recipients = store.findAllByAddress(address);
 
         if (recipients.isEmpty()) {
 
         final var recipients = store.findAllByAddress(address);
 
         if (recipients.isEmpty()) {
-            logger.debug("Got new recipient, serviceId, PNI and number are unknown");
+            logger.debug("Got new recipient, serviceId, PNI, number, username are unknown");
             return new Pair<>(store.addNewRecipient(address), List.of());
         }
 
             return new Pair<>(store.addNewRecipient(address), List.of());
         }
 
@@ -47,7 +47,7 @@ public class MergeRecipientHelper {
             }
 
             logger.debug(
             }
 
             logger.debug(
-                    "Got recipient {} existing with number/pni, but different serviceId, so stripping its number and adding new recipient",
+                    "Got recipient {} existing with number/pni/username, but different serviceId, so stripping its number and adding new recipient",
                     recipient.id());
             store.updateRecipientAddress(recipient.id(), recipient.address().removeIdentifiersFrom(address));
 
                     recipient.id());
             store.updateRecipientAddress(recipient.id(), recipient.address().removeIdentifiersFrom(address));
 
index 9a016136638082a00c2a05097a3833d198cf3fa5..fd2eb5da6a504ee6e76578d2ca5a0b677d372a81 100644 (file)
@@ -790,8 +790,8 @@ public class RecipientStore implements RecipientIdCreator, RecipientResolver, Re
     ) throws SQLException {
         final var sql = (
                 """
     ) throws SQLException {
         final var sql = (
                 """
-                INSERT INTO %s (number, uuid, pni)
-                VALUES (?, ?, ?)
+                INSERT INTO %s (number, uuid, pni, username)
+                VALUES (?, ?, ?, ?)
                 RETURNING _id
                 """
         ).formatted(TABLE_RECIPIENT);
                 RETURNING _id
                 """
         ).formatted(TABLE_RECIPIENT);
@@ -800,6 +800,7 @@ public class RecipientStore implements RecipientIdCreator, RecipientResolver, Re
             statement.setBytes(2,
                     address.serviceId().map(ServiceId::getRawUuid).map(UuidUtil::toByteArray).orElse(null));
             statement.setBytes(3, address.pni().map(PNI::getRawUuid).map(UuidUtil::toByteArray).orElse(null));
             statement.setBytes(2,
                     address.serviceId().map(ServiceId::getRawUuid).map(UuidUtil::toByteArray).orElse(null));
             statement.setBytes(3, address.pni().map(PNI::getRawUuid).map(UuidUtil::toByteArray).orElse(null));
+            statement.setString(4, address.username().orElse(null));
             final var generatedKey = Utils.executeQueryForOptional(statement, Utils::getIdMapper);
             if (generatedKey.isPresent()) {
                 final var recipientId = new RecipientId(generatedKey.get(), this);
             final var generatedKey = Utils.executeQueryForOptional(statement, Utils::getIdMapper);
             if (generatedKey.isPresent()) {
                 final var recipientId = new RecipientId(generatedKey.get(), this);
@@ -817,7 +818,7 @@ public class RecipientStore implements RecipientIdCreator, RecipientResolver, Re
             final var sql = (
                     """
                     UPDATE %s
             final var sql = (
                     """
                     UPDATE %s
-                    SET number = NULL, uuid = NULL, pni = NULL
+                    SET number = NULL, uuid = NULL, pni = NULL, username = NULL
                     WHERE _id = ?
                     """
             ).formatted(TABLE_RECIPIENT);
                     WHERE _id = ?
                     """
             ).formatted(TABLE_RECIPIENT);
index 770054f32aba08ac401d0981dd6eb3dc9b6e2f7a..eae8958f21915205e7041283c935a416bdc29642 100644 (file)
@@ -23,19 +23,24 @@ class MergeRecipientHelperTest {
     static final PNI PNI_B = PNI.from(UUID.randomUUID());
     static final String NUMBER_A = "+AAA";
     static final String NUMBER_B = "+BBB";
     static final PNI PNI_B = PNI.from(UUID.randomUUID());
     static final String NUMBER_A = "+AAA";
     static final String NUMBER_B = "+BBB";
+    static final String USERNAME_A = "USER.1";
+    static final String USERNAME_B = "USER.2";
 
 
-    static final PartialAddresses ADDR_A = new PartialAddresses(SERVICE_ID_A, PNI_A, NUMBER_A);
-    static final PartialAddresses ADDR_B = new PartialAddresses(SERVICE_ID_B, PNI_B, NUMBER_B);
+    static final PartialAddresses ADDR_A = new PartialAddresses(SERVICE_ID_A, PNI_A, NUMBER_A, USERNAME_A);
+    static final PartialAddresses ADDR_B = new PartialAddresses(SERVICE_ID_B, PNI_B, NUMBER_B, USERNAME_B);
 
     static final T[] testInstancesNone = new T[]{
             new T(Set.of(), ADDR_A.FULL, Set.of(rec(1000000, ADDR_A.FULL))),
             new T(Set.of(), ADDR_A.ACI_NUM, Set.of(rec(1000000, ADDR_A.ACI_NUM))),
             new T(Set.of(), ADDR_A.ACI_PNI, Set.of(rec(1000000, ADDR_A.ACI_PNI))),
             new T(Set.of(), ADDR_A.PNI_NUM, Set.of(rec(1000000, ADDR_A.PNI_NUM))),
 
     static final T[] testInstancesNone = new T[]{
             new T(Set.of(), ADDR_A.FULL, Set.of(rec(1000000, ADDR_A.FULL))),
             new T(Set.of(), ADDR_A.ACI_NUM, Set.of(rec(1000000, ADDR_A.ACI_NUM))),
             new T(Set.of(), ADDR_A.ACI_PNI, Set.of(rec(1000000, ADDR_A.ACI_PNI))),
             new T(Set.of(), ADDR_A.PNI_NUM, Set.of(rec(1000000, ADDR_A.PNI_NUM))),
+            new T(Set.of(), ADDR_A.ACI_USERNAME, Set.of(rec(1000000, ADDR_A.ACI_USERNAME))),
+            new T(Set.of(), ADDR_A.FULL_USERNAME, Set.of(rec(1000000, ADDR_A.FULL_USERNAME))),
     };
 
     static final T[] testInstancesSingle = new T[]{
             new T(Set.of(rec(1, ADDR_A.FULL)), ADDR_A.FULL, Set.of(rec(1, ADDR_A.FULL))),
     };
 
     static final T[] testInstancesSingle = new T[]{
             new T(Set.of(rec(1, ADDR_A.FULL)), ADDR_A.FULL, Set.of(rec(1, ADDR_A.FULL))),
+            new T(Set.of(rec(1, ADDR_A.FULL_USERNAME)), ADDR_A.FULL, Set.of(rec(1, ADDR_A.FULL_USERNAME))),
             new T(Set.of(rec(1, ADDR_A.ACI)), ADDR_A.FULL, Set.of(rec(1, ADDR_A.FULL))),
             new T(Set.of(rec(1, ADDR_A.PNI)), ADDR_A.FULL, Set.of(rec(1, ADDR_A.FULL))),
             new T(Set.of(rec(1, ADDR_A.NUM)), ADDR_A.FULL, Set.of(rec(1, ADDR_A.FULL))),
             new T(Set.of(rec(1, ADDR_A.ACI)), ADDR_A.FULL, Set.of(rec(1, ADDR_A.FULL))),
             new T(Set.of(rec(1, ADDR_A.PNI)), ADDR_A.FULL, Set.of(rec(1, ADDR_A.FULL))),
             new T(Set.of(rec(1, ADDR_A.NUM)), ADDR_A.FULL, Set.of(rec(1, ADDR_A.FULL))),
@@ -72,6 +77,10 @@ class MergeRecipientHelperTest {
             new T(Set.of(rec(1, ADDR_A.ACI_PNI)), ADDR_A.ACI_PNI, Set.of(rec(1, ADDR_A.ACI_PNI))),
 
             new T(Set.of(rec(1, ADDR_A.FULL)), ADDR_B.FULL, Set.of(rec(1, ADDR_A.FULL), rec(1000000, ADDR_B.FULL))),
             new T(Set.of(rec(1, ADDR_A.ACI_PNI)), ADDR_A.ACI_PNI, Set.of(rec(1, ADDR_A.ACI_PNI))),
 
             new T(Set.of(rec(1, ADDR_A.FULL)), ADDR_B.FULL, Set.of(rec(1, ADDR_A.FULL), rec(1000000, ADDR_B.FULL))),
+
+            new T(Set.of(rec(1, ADDR_A.FULL)), ADDR_A.ACI_USERNAME, Set.of(rec(1, ADDR_A.FULL_USERNAME))),
+            new T(Set.of(rec(1, ADDR_A.ACI)), ADDR_A.ACI_USERNAME, Set.of(rec(1, ADDR_A.ACI_USERNAME))),
+            new T(Set.of(rec(1, ADDR_A.ACI_NUM)), ADDR_A.ACI_USERNAME, Set.of(rec(1, ADDR_A.ACI_NUM_USERNAME))),
     };
 
     static final T[] testInstancesTwo = new T[]{
     };
 
     static final T[] testInstancesTwo = new T[]{
@@ -225,22 +234,28 @@ class MergeRecipientHelperTest {
 
     private record PartialAddresses(
             RecipientAddress FULL,
 
     private record PartialAddresses(
             RecipientAddress FULL,
+            RecipientAddress FULL_USERNAME,
             RecipientAddress ACI,
             RecipientAddress PNI,
             RecipientAddress NUM,
             RecipientAddress ACI_NUM,
             RecipientAddress ACI,
             RecipientAddress PNI,
             RecipientAddress NUM,
             RecipientAddress ACI_NUM,
+            RecipientAddress ACI_NUM_USERNAME,
             RecipientAddress PNI_NUM,
             RecipientAddress PNI_NUM,
-            RecipientAddress ACI_PNI
+            RecipientAddress ACI_PNI,
+            RecipientAddress ACI_USERNAME
     ) {
 
     ) {
 
-        PartialAddresses(ServiceId serviceId, PNI pni, String number) {
+        PartialAddresses(ServiceId serviceId, PNI pni, String number, String username) {
             this(new RecipientAddress(serviceId, pni, number),
             this(new RecipientAddress(serviceId, pni, number),
+                    new RecipientAddress(serviceId, pni, number, username),
                     new RecipientAddress(serviceId, null, null),
                     new RecipientAddress(null, pni, null),
                     new RecipientAddress(null, null, number),
                     new RecipientAddress(serviceId, null, number),
                     new RecipientAddress(serviceId, null, null),
                     new RecipientAddress(null, pni, null),
                     new RecipientAddress(null, null, number),
                     new RecipientAddress(serviceId, null, number),
+                    new RecipientAddress(serviceId, null, number, username),
                     new RecipientAddress(null, pni, number),
                     new RecipientAddress(null, pni, number),
-                    new RecipientAddress(serviceId, pni, null));
+                    new RecipientAddress(serviceId, pni, null),
+                    new RecipientAddress(serviceId, null, null, username));
         }
     }
 }
         }
     }
 }