]> nmode's Git Repositories - signal-cli/blob - lib/src/main/java/org/asamk/signal/manager/api/RecipientAddress.java
Add labels to Containerfile
[signal-cli] / lib / src / main / java / org / asamk / signal / manager / api / RecipientAddress.java
1 package org.asamk.signal.manager.api;
2
3 import org.whispersystems.signalservice.api.push.ServiceId;
4 import org.whispersystems.signalservice.api.push.SignalServiceAddress;
5
6 import java.util.Optional;
7 import java.util.UUID;
8
9 public record RecipientAddress(Optional<UUID> uuid, Optional<String> number, Optional<String> username) {
10
11 public static final UUID UNKNOWN_UUID = ServiceId.UNKNOWN.uuid();
12
13 /**
14 * Construct a RecipientAddress.
15 *
16 * @param uuid The UUID of the user, if available.
17 * @param number The phone number of the user, if available.
18 */
19 public RecipientAddress {
20 uuid = uuid.isPresent() && uuid.get().equals(UNKNOWN_UUID) ? Optional.empty() : uuid;
21 if (uuid.isEmpty() && number.isEmpty() && username.isEmpty()) {
22 throw new AssertionError("Must have either a UUID or E164 number!");
23 }
24 }
25
26 public RecipientAddress(UUID uuid, String e164) {
27 this(Optional.ofNullable(uuid), Optional.ofNullable(e164), Optional.empty());
28 }
29
30 public RecipientAddress(UUID uuid, String e164, String username) {
31 this(Optional.ofNullable(uuid), Optional.ofNullable(e164), Optional.ofNullable(username));
32 }
33
34 public RecipientAddress(SignalServiceAddress address) {
35 this(Optional.of(address.getServiceId().uuid()), address.getNumber(), Optional.empty());
36 }
37
38 public RecipientAddress(UUID uuid) {
39 this(Optional.of(uuid), Optional.empty(), Optional.empty());
40 }
41
42 public ServiceId getServiceId() {
43 return ServiceId.from(uuid.orElse(UNKNOWN_UUID));
44 }
45
46 public String getIdentifier() {
47 if (uuid.isPresent()) {
48 return uuid.get().toString();
49 } else if (number.isPresent()) {
50 return number.get();
51 } else if (username.isPresent()) {
52 return username.get();
53 } else {
54 throw new AssertionError("Given the checks in the constructor, this should not be possible.");
55 }
56 }
57
58 public String getLegacyIdentifier() {
59 if (number.isPresent()) {
60 return number.get();
61 } else if (uuid.isPresent()) {
62 return uuid.get().toString();
63 } else if (username.isPresent()) {
64 return username.get();
65 } else {
66 throw new AssertionError("Given the checks in the constructor, this should not be possible.");
67 }
68 }
69
70 public boolean matches(RecipientAddress other) {
71 return (uuid.isPresent() && other.uuid.isPresent() && uuid.get().equals(other.uuid.get()))
72 || (number.isPresent() && other.number.isPresent() && number.get().equals(other.number.get()))
73 || (username.isPresent() && other.username.isPresent() && username.get().equals(other.username.get()));
74 }
75 }