]> nmode's Git Repositories - signal-cli/commitdiff
Add aci,pni to API RecipientAddress
authorAsamK <asamk@gmx.de>
Sat, 13 Apr 2024 19:29:45 +0000 (21:29 +0200)
committerAsamK <asamk@gmx.de>
Mon, 15 Apr 2024 17:23:50 +0000 (19:23 +0200)
lib/src/main/java/org/asamk/signal/manager/api/RecipientAddress.java
lib/src/main/java/org/asamk/signal/manager/api/RecipientIdentifier.java
lib/src/main/java/org/asamk/signal/manager/helper/ReceiveHelper.java
lib/src/main/java/org/asamk/signal/manager/helper/RecipientHelper.java
lib/src/main/java/org/asamk/signal/manager/storage/recipients/RecipientAddress.java
lib/src/main/java/org/asamk/signal/manager/storage/recipients/RecipientStore.java
src/main/java/org/asamk/signal/dbus/DbusManagerImpl.java

index c94a21acb47563f28785ccc5f717403d7824a1d6..dd8a6f3c30005e4291ccc86a1ee5ea19074e4cc8 100644 (file)
@@ -1,49 +1,55 @@
 package org.asamk.signal.manager.api;
 
-import org.whispersystems.signalservice.api.push.SignalServiceAddress;
 import org.whispersystems.signalservice.api.util.UuidUtil;
 
 import java.util.Optional;
 import java.util.UUID;
 
-public record RecipientAddress(Optional<UUID> uuid, Optional<String> number, Optional<String> username) {
+public record RecipientAddress(
+        Optional<String> aci, Optional<String> pni, Optional<String> number, Optional<String> username
+) {
 
     public static final UUID UNKNOWN_UUID = UuidUtil.UNKNOWN_UUID;
 
     /**
      * Construct a RecipientAddress.
      *
-     * @param uuid   The UUID of the user, if available.
+     * @param aci    The ACI of the user, if available.
+     * @param pni    The PNI of the user, if available.
      * @param number The phone number of the user, if available.
      */
     public RecipientAddress {
-        uuid = uuid.isPresent() && uuid.get().equals(UNKNOWN_UUID) ? Optional.empty() : uuid;
-        if (uuid.isEmpty() && number.isEmpty() && username.isEmpty()) {
-            throw new AssertionError("Must have either a UUID, username or E164 number!");
+        if (aci.isEmpty() && pni.isEmpty() && number.isEmpty() && username.isEmpty()) {
+            throw new AssertionError("Must have either a ACI, PNI, username or E164 number!");
         }
     }
 
-    public RecipientAddress(UUID uuid, String e164) {
-        this(Optional.ofNullable(uuid), Optional.ofNullable(e164), Optional.empty());
+    public RecipientAddress(String e164) {
+        this(null, null, e164, null);
     }
 
-    public RecipientAddress(UUID uuid, String e164, String username) {
-        this(Optional.ofNullable(uuid), Optional.ofNullable(e164), Optional.ofNullable(username));
+    public RecipientAddress(UUID uuid) {
+        this(uuid.toString(), null, null, null);
     }
 
-    public RecipientAddress(SignalServiceAddress address) {
-        this(Optional.of(address.getServiceId().getRawUuid()), address.getNumber(), Optional.empty());
+    public RecipientAddress(String aci, String pni, String e164, String username) {
+        this(Optional.ofNullable(aci),
+                Optional.ofNullable(pni),
+                Optional.ofNullable(e164),
+                Optional.ofNullable(username));
     }
 
-    public RecipientAddress(UUID uuid) {
-        this(Optional.of(uuid), Optional.empty(), Optional.empty());
+    public Optional<UUID> uuid() {
+        return aci.map(UUID::fromString);
     }
 
     public String getIdentifier() {
-        if (uuid.isPresent()) {
-            return uuid.get().toString();
+        if (aci.isPresent()) {
+            return aci.get();
         } else if (number.isPresent()) {
             return number.get();
+        } else if (pni.isPresent()) {
+            return pni.get();
         } else if (username.isPresent()) {
             return username.get();
         } else {
@@ -54,17 +60,16 @@ public record RecipientAddress(Optional<UUID> uuid, Optional<String> number, Opt
     public String getLegacyIdentifier() {
         if (number.isPresent()) {
             return number.get();
-        } else if (uuid.isPresent()) {
-            return uuid.get().toString();
-        } else if (username.isPresent()) {
-            return username.get();
         } else {
-            throw new AssertionError("Given the checks in the constructor, this should not be possible.");
+            return getIdentifier();
         }
     }
 
     public boolean matches(RecipientAddress other) {
-        return (uuid.isPresent() && other.uuid.isPresent() && uuid.get().equals(other.uuid.get()))
+        return (aci.isPresent() && other.aci.isPresent() && aci.get().equals(other.aci.get()))
+                || (
+                pni.isPresent() && other.pni.isPresent() && pni.get().equals(other.pni.get())
+        )
                 || (number.isPresent() && other.number.isPresent() && number.get().equals(other.number.get()))
                 || (username.isPresent() && other.username.isPresent() && username.get().equals(other.username.get()));
     }
index fc82205805226aedf326d640fbe4fff7fa53c368..823881c79c98900c5ff98e0b492bad383a06383c 100644 (file)
@@ -47,8 +47,8 @@ public sealed interface RecipientIdentifier {
         static Single fromAddress(RecipientAddress address) {
             if (address.number().isPresent()) {
                 return new Number(address.number().get());
-            } else if (address.uuid().isPresent()) {
-                return new Uuid(address.uuid().get());
+            } else if (address.aci().isPresent()) {
+                return new Uuid(UUID.fromString(address.aci().get()));
             } else if (address.username().isPresent()) {
                 return new Username(address.username().get());
             }
@@ -80,7 +80,7 @@ public sealed interface RecipientIdentifier {
 
         @Override
         public RecipientAddress toPartialRecipientAddress() {
-            return new RecipientAddress(null, number);
+            return new RecipientAddress(number);
         }
     }
 
@@ -93,7 +93,7 @@ public sealed interface RecipientIdentifier {
 
         @Override
         public RecipientAddress toPartialRecipientAddress() {
-            return new RecipientAddress(null, null, username);
+            return new RecipientAddress(null, null, null, username);
         }
     }
 
index a40c134d69083050dbf29705b4ffaa7769342a36..baa0d583b8f4caccb7adab1dd58fc19cf233d3ef 100644 (file)
@@ -229,9 +229,9 @@ public class ReceiveHelper {
                     if (exception instanceof UntrustedIdentityException) {
                         logger.debug("Keeping message with untrusted identity in message cache");
                         final var address = ((UntrustedIdentityException) exception).getSender();
-                        if (envelope.getSourceServiceId().isEmpty() && address.uuid().isPresent()) {
+                        if (envelope.getSourceServiceId().isEmpty() && address.aci().isPresent()) {
                             final var recipientId = account.getRecipientResolver()
-                                    .resolveRecipient(ACI.from(address.uuid().get()));
+                                    .resolveRecipient(ACI.parseOrThrow(address.aci().get()));
                             try {
                                 cachedMessage[0] = account.getMessageCache()
                                         .replaceSender(cachedMessage[0], recipientId);
index deb4e01bc717b43fe20586441cd7afc5b7da1262..3bdd822768f64aaec4370613c8d28b799ea2bd6f 100644 (file)
@@ -108,6 +108,7 @@ public class RecipientHelper {
                 return account.getRecipientStore().resolveRecipientTrusted(aci, finalUsername.getUsername());
             } catch (IOException e) {
                 throw new UnregisteredRecipientException(new org.asamk.signal.manager.api.RecipientAddress(null,
+                        null,
                         null,
                         username));
             }
@@ -196,11 +197,11 @@ public class RecipientHelper {
         try {
             aciMap = getRegisteredUsers(Set.of(number), true);
         } catch (NumberFormatException e) {
-            throw new UnregisteredRecipientException(new org.asamk.signal.manager.api.RecipientAddress(null, number));
+            throw new UnregisteredRecipientException(new org.asamk.signal.manager.api.RecipientAddress(number));
         }
         final var user = aciMap.get(number);
         if (user == null) {
-            throw new UnregisteredRecipientException(new org.asamk.signal.manager.api.RecipientAddress(null, number));
+            throw new UnregisteredRecipientException(new org.asamk.signal.manager.api.RecipientAddress(number));
         }
         return user.getServiceId();
     }
index 7eacb6a42f54961f63d7dfb85e3e3e5d1735a35d..56adb0a68f78524fd3bc0161a99af0035fa62ff4 100644 (file)
@@ -69,7 +69,10 @@ public record RecipientAddress(
     }
 
     public RecipientAddress(org.asamk.signal.manager.api.RecipientAddress address) {
-        this(address.uuid().map(ACI::from), Optional.empty(), address.number(), address.username());
+        this(address.aci().map(ACI::parseOrNull),
+                address.pni().map(PNI::parseOrNull),
+                address.number(),
+                address.username());
     }
 
     public RecipientAddress(ServiceId serviceId) {
@@ -169,7 +172,8 @@ public record RecipientAddress(
     }
 
     public org.asamk.signal.manager.api.RecipientAddress toApiRecipientAddress() {
-        return new org.asamk.signal.manager.api.RecipientAddress(serviceId().map(ServiceId::getRawUuid),
+        return new org.asamk.signal.manager.api.RecipientAddress(aci().map(ServiceId::toString),
+                pni().map(ServiceId::toString),
                 number(),
                 username());
     }
index c291342eed8ca845d75d2b950d02eaeb3b443a1a..5c12a1ec66fcd5e25e05f46e66ae65c3474b2be7 100644 (file)
@@ -215,8 +215,7 @@ public class RecipientStore implements RecipientIdCreator, RecipientResolver, Re
         if (byNumber.isEmpty() || byNumber.get().address().serviceId().isEmpty()) {
             final var serviceId = serviceIdSupplier.get();
             if (serviceId == null) {
-                throw new UnregisteredRecipientException(new org.asamk.signal.manager.api.RecipientAddress(null,
-                        number));
+                throw new UnregisteredRecipientException(new org.asamk.signal.manager.api.RecipientAddress(number));
             }
 
             return resolveRecipient(serviceId);
@@ -247,6 +246,7 @@ public class RecipientStore implements RecipientIdCreator, RecipientResolver, Re
             final var aci = aciSupplier.get();
             if (aci == null) {
                 throw new UnregisteredRecipientException(new org.asamk.signal.manager.api.RecipientAddress(null,
+                        null,
                         null,
                         username));
             }
index b428fb32522afe87a09d667df41ffa5811910fe5..4573b861734a05bd1ac6570c0404b78393d18f41 100644 (file)
@@ -690,7 +690,7 @@ public class DbusManagerImpl implements Manager {
                 return null;
             }
             return Recipient.newBuilder()
-                    .withAddress(new RecipientAddress(null, n))
+                    .withAddress(new RecipientAddress(n))
                     .withContact(new Contact(contactName,
                             null,
                             null,
@@ -731,19 +731,19 @@ public class DbusManagerImpl implements Manager {
                     (String) group.get("Description").getValue(),
                     GroupInviteLinkUrl.fromUri((String) group.get("GroupInviteLink").getValue()),
                     ((List<String>) group.get("Members").getValue()).stream()
-                            .map(m -> new RecipientAddress(null, m))
+                            .map(m -> new RecipientAddress(m))
                             .collect(Collectors.toSet()),
                     ((List<String>) group.get("PendingMembers").getValue()).stream()
-                            .map(m -> new RecipientAddress(null, m))
+                            .map(m -> new RecipientAddress(m))
                             .collect(Collectors.toSet()),
                     ((List<String>) group.get("RequestingMembers").getValue()).stream()
-                            .map(m -> new RecipientAddress(null, m))
+                            .map(m -> new RecipientAddress(m))
                             .collect(Collectors.toSet()),
                     ((List<String>) group.get("Admins").getValue()).stream()
-                            .map(m -> new RecipientAddress(null, m))
+                            .map(m -> new RecipientAddress(m))
                             .collect(Collectors.toSet()),
                     ((List<String>) group.get("Banned").getValue()).stream()
-                            .map(m -> new RecipientAddress(null, m))
+                            .map(m -> new RecipientAddress(m))
                             .collect(Collectors.toSet()),
                     (boolean) group.get("IsBlocked").getValue(),
                     (int) group.get("MessageExpirationTimer").getValue(),
@@ -854,8 +854,7 @@ public class DbusManagerImpl implements Manager {
         try {
             this.dbusMsgHandler = messageReceived -> {
                 final var extras = messageReceived.getExtras();
-                final var envelope = new MessageEnvelope(Optional.of(new RecipientAddress(null,
-                        messageReceived.getSender())),
+                final var envelope = new MessageEnvelope(Optional.of(new RecipientAddress(messageReceived.getSender())),
                         0,
                         messageReceived.getTimestamp(),
                         0,
@@ -896,8 +895,7 @@ public class DbusManagerImpl implements Manager {
             connection.addSigHandler(Signal.MessageReceivedV2.class, signal, this.dbusMsgHandler);
             this.dbusEditMsgHandler = messageReceived -> {
                 final var extras = messageReceived.getExtras();
-                final var envelope = new MessageEnvelope(Optional.of(new RecipientAddress(null,
-                        messageReceived.getSender())),
+                final var envelope = new MessageEnvelope(Optional.of(new RecipientAddress(messageReceived.getSender())),
                         0,
                         messageReceived.getTimestamp(),
                         0,
@@ -945,8 +943,7 @@ public class DbusManagerImpl implements Manager {
                     case "delivery" -> MessageEnvelope.Receipt.Type.DELIVERY;
                     default -> MessageEnvelope.Receipt.Type.UNKNOWN;
                 };
-                final var envelope = new MessageEnvelope(Optional.of(new RecipientAddress(null,
-                        receiptReceived.getSender())),
+                final var envelope = new MessageEnvelope(Optional.of(new RecipientAddress(receiptReceived.getSender())),
                         0,
                         receiptReceived.getTimestamp(),
                         0,
@@ -967,8 +964,7 @@ public class DbusManagerImpl implements Manager {
 
             this.dbusSyncHandler = syncReceived -> {
                 final var extras = syncReceived.getExtras();
-                final var envelope = new MessageEnvelope(Optional.of(new RecipientAddress(null,
-                        syncReceived.getSource())),
+                final var envelope = new MessageEnvelope(Optional.of(new RecipientAddress(syncReceived.getSource())),
                         0,
                         syncReceived.getTimestamp(),
                         0,
@@ -982,7 +978,7 @@ public class DbusManagerImpl implements Manager {
                                 syncReceived.getTimestamp(),
                                 syncReceived.getDestination().isEmpty()
                                         ? Optional.empty()
-                                        : Optional.of(new RecipientAddress(null, syncReceived.getDestination())),
+                                        : Optional.of(new RecipientAddress(syncReceived.getDestination())),
                                 Set.of(),
                                 Optional.of(new MessageEnvelope.Data(syncReceived.getTimestamp(),
                                         syncReceived.getGroupId().length > 0
@@ -1081,7 +1077,7 @@ public class DbusManagerImpl implements Manager {
 
         final List<DBusMap<String, Variant<?>>> mentions = getValue(extras, "mentions");
         return mentions.stream()
-                .map(a -> new MessageEnvelope.Data.Mention(new RecipientAddress(null, getValue(a, "recipient")),
+                .map(a -> new MessageEnvelope.Data.Mention(new RecipientAddress(this.<String>getValue(a, "recipient")),
                         getValue(a, "start"),
                         getValue(a, "length")))
                 .toList();