]> nmode's Git Repositories - signal-cli/blobdiff - lib/src/main/java/org/asamk/signal/manager/api/RecipientAddress.java
Add aci,pni to API RecipientAddress
[signal-cli] / lib / src / main / java / org / asamk / signal / manager / api / RecipientAddress.java
index 920eafc55a60ba3fc4d717877104c94dd45f9705..dd8a6f3c30005e4291ccc86a1ee5ea19074e4cc8 100644 (file)
@@ -1,49 +1,57 @@
 package org.asamk.signal.manager.api;
 
-import org.whispersystems.signalservice.api.push.ServiceId;
-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) {
+public record RecipientAddress(
+        Optional<String> aci, Optional<String> pni, Optional<String> number, Optional<String> username
+) {
 
-    public static final UUID UNKNOWN_UUID = ServiceId.UNKNOWN.uuid();
+    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()) {
-            throw new AssertionError("Must have either a UUID 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));
+    public RecipientAddress(String e164) {
+        this(null, null, e164, null);
     }
 
-    public RecipientAddress(SignalServiceAddress address) {
-        this(Optional.of(address.getServiceId().uuid()), address.getNumber());
+    public RecipientAddress(UUID uuid) {
+        this(uuid.toString(), null, null, null);
     }
 
-    public RecipientAddress(UUID uuid) {
-        this(Optional.of(uuid), 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 ServiceId getServiceId() {
-        return ServiceId.from(uuid.orElse(UNKNOWN_UUID));
+    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 {
             throw new AssertionError("Given the checks in the constructor, this should not be possible.");
         }
@@ -52,16 +60,17 @@ public record RecipientAddress(Optional<UUID> uuid, Optional<String> number) {
     public String getLegacyIdentifier() {
         if (number.isPresent()) {
             return number.get();
-        } else if (uuid.isPresent()) {
-            return uuid.get().toString();
         } 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())) || (
-                number.isPresent() && other.number.isPresent() && number.get().equals(other.number.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()));
     }
 }