]> nmode's Git Repositories - signal-cli/blob - lib/src/main/java/org/asamk/signal/manager/storage/recipients/RecipientAddress.java
c3b18299c44393990007a5a1df37913a49be3e34
[signal-cli] / lib / src / main / java / org / asamk / signal / manager / storage / recipients / RecipientAddress.java
1 package org.asamk.signal.manager.storage.recipients;
2
3 import org.whispersystems.signalservice.api.push.PNI;
4 import org.whispersystems.signalservice.api.push.ServiceId;
5 import org.whispersystems.signalservice.api.push.SignalServiceAddress;
6
7 import java.util.Optional;
8
9 public record RecipientAddress(Optional<ServiceId> serviceId, Optional<PNI> pni, Optional<String> number) {
10
11 /**
12 * Construct a RecipientAddress.
13 *
14 * @param serviceId The ACI or PNI of the user, if available.
15 * @param number The phone number of the user, if available.
16 */
17 public RecipientAddress {
18 if (serviceId.isPresent() && serviceId.get().equals(ServiceId.UNKNOWN)) {
19 serviceId = Optional.empty();
20 }
21 if (pni.isPresent() && pni.get().equals(ServiceId.UNKNOWN)) {
22 pni = Optional.empty();
23 }
24 if (serviceId.isEmpty() && pni.isPresent()) {
25 serviceId = Optional.of(pni.get());
26 }
27 if (serviceId.isEmpty() && number.isEmpty()) {
28 throw new AssertionError("Must have either a ServiceId or E164 number!");
29 }
30 }
31
32 public RecipientAddress(Optional<ServiceId> serviceId, Optional<String> number) {
33 this(serviceId, Optional.empty(), number);
34 }
35
36 public RecipientAddress(ServiceId serviceId, String e164) {
37 this(Optional.ofNullable(serviceId), Optional.empty(), Optional.ofNullable(e164));
38 }
39
40 public RecipientAddress(ServiceId serviceId, PNI pni, String e164) {
41 this(Optional.ofNullable(serviceId), Optional.ofNullable(pni), Optional.ofNullable(e164));
42 }
43
44 public RecipientAddress(SignalServiceAddress address) {
45 this(Optional.of(address.getServiceId()), Optional.empty(), address.getNumber());
46 }
47
48 public RecipientAddress(ServiceId serviceId) {
49 this(Optional.of(serviceId), Optional.empty());
50 }
51
52 public ServiceId getServiceId() {
53 return serviceId.orElse(ServiceId.UNKNOWN);
54 }
55
56 public String getIdentifier() {
57 if (serviceId.isPresent()) {
58 return serviceId.get().toString();
59 } else if (number.isPresent()) {
60 return number.get();
61 } else {
62 throw new AssertionError("Given the checks in the constructor, this should not be possible.");
63 }
64 }
65
66 public String getLegacyIdentifier() {
67 if (number.isPresent()) {
68 return number.get();
69 } else if (serviceId.isPresent()) {
70 return serviceId.get().toString();
71 } else {
72 throw new AssertionError("Given the checks in the constructor, this should not be possible.");
73 }
74 }
75
76 public boolean matches(RecipientAddress other) {
77 return (serviceId.isPresent() && other.serviceId.isPresent() && serviceId.get().equals(other.serviceId.get()))
78 || (
79 pni.isPresent() && other.serviceId.isPresent() && pni.get().equals(other.serviceId.get())
80 )
81 || (
82 serviceId.isPresent() && other.pni.isPresent() && serviceId.get().equals(other.pni.get())
83 )
84 || (
85 pni.isPresent() && other.pni.isPresent() && pni.get().equals(other.pni.get())
86 )
87 || (
88 number.isPresent() && other.number.isPresent() && number.get().equals(other.number.get())
89 );
90 }
91
92 public SignalServiceAddress toSignalServiceAddress() {
93 return new SignalServiceAddress(getServiceId(), number);
94 }
95
96 public org.asamk.signal.manager.api.RecipientAddress toApiRecipientAddress() {
97 return new org.asamk.signal.manager.api.RecipientAddress(serviceId().map(ServiceId::uuid), number());
98 }
99 }