]> nmode's Git Repositories - signal-cli/blobdiff - lib/src/main/java/org/asamk/signal/manager/api/RecipientAddress.java
Implement support for usernames
[signal-cli] / lib / src / main / java / org / asamk / signal / manager / api / RecipientAddress.java
index 920eafc55a60ba3fc4d717877104c94dd45f9705..ee3bded9cb148f14cb912bc87427db4881936923 100644 (file)
@@ -6,7 +6,7 @@ import org.whispersystems.signalservice.api.push.SignalServiceAddress;
 import java.util.Optional;
 import java.util.UUID;
 
-public record RecipientAddress(Optional<UUID> uuid, Optional<String> number) {
+public record RecipientAddress(Optional<UUID> uuid, Optional<String> number, Optional<String> username) {
 
     public static final UUID UNKNOWN_UUID = ServiceId.UNKNOWN.uuid();
 
@@ -18,21 +18,25 @@ public record RecipientAddress(Optional<UUID> uuid, Optional<String> number) {
      */
     public RecipientAddress {
         uuid = uuid.isPresent() && uuid.get().equals(UNKNOWN_UUID) ? Optional.empty() : uuid;
-        if (uuid.isEmpty() && number.isEmpty()) {
+        if (uuid.isEmpty() && number.isEmpty() && username.isEmpty()) {
             throw new AssertionError("Must have either a UUID or E164 number!");
         }
     }
 
     public RecipientAddress(UUID uuid, String e164) {
-        this(Optional.ofNullable(uuid), Optional.ofNullable(e164));
+        this(Optional.ofNullable(uuid), Optional.ofNullable(e164), Optional.empty());
+    }
+
+    public RecipientAddress(UUID uuid, String e164, String username) {
+        this(Optional.ofNullable(uuid), Optional.ofNullable(e164), Optional.ofNullable(username));
     }
 
     public RecipientAddress(SignalServiceAddress address) {
-        this(Optional.of(address.getServiceId().uuid()), address.getNumber());
+        this(Optional.of(address.getServiceId().uuid()), address.getNumber(), Optional.empty());
     }
 
     public RecipientAddress(UUID uuid) {
-        this(Optional.of(uuid), Optional.empty());
+        this(Optional.of(uuid), Optional.empty(), Optional.empty());
     }
 
     public ServiceId getServiceId() {
@@ -44,6 +48,8 @@ public record RecipientAddress(Optional<UUID> uuid, Optional<String> number) {
             return uuid.get().toString();
         } else if (number.isPresent()) {
             return number.get();
+        } else if (username.isPresent()) {
+            return username.get();
         } else {
             throw new AssertionError("Given the checks in the constructor, this should not be possible.");
         }
@@ -54,14 +60,16 @@ public record RecipientAddress(Optional<UUID> uuid, Optional<String> number) {
             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.");
         }
     }
 
     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 (uuid.isPresent() && other.uuid.isPresent() && uuid.get().equals(other.uuid.get()))
+                || (number.isPresent() && other.number.isPresent() && number.get().equals(other.number.get()))
+                || (username.isPresent() && other.username.isPresent() && username.get().equals(other.username.get()));
     }
 }