]> nmode's Git Repositories - signal-cli/blob - lib/src/main/java/org/asamk/signal/manager/storage/recipients/RecipientAddress.java
7dc193a9b4b10814b664b3bd7d42db8a7706baa2
[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.isPresent() && serviceId.get() instanceof PNI sPNI) {
28 if (pni.isPresent() && !sPNI.equals(pni.get())) {
29 throw new AssertionError("Must not have two different PNIs!");
30 }
31 if (pni.isEmpty()) {
32 pni = Optional.of(sPNI);
33 }
34 }
35 if (serviceId.isEmpty() && number.isEmpty()) {
36 throw new AssertionError("Must have either a ServiceId or E164 number!");
37 }
38 }
39
40 public RecipientAddress(Optional<ServiceId> serviceId, Optional<String> number) {
41 this(serviceId, Optional.empty(), number);
42 }
43
44 public RecipientAddress(ServiceId serviceId, String e164) {
45 this(Optional.ofNullable(serviceId), Optional.empty(), Optional.ofNullable(e164));
46 }
47
48 public RecipientAddress(ServiceId serviceId, PNI pni, String e164) {
49 this(Optional.ofNullable(serviceId), Optional.ofNullable(pni), Optional.ofNullable(e164));
50 }
51
52 public RecipientAddress(SignalServiceAddress address) {
53 this(Optional.of(address.getServiceId()), Optional.empty(), address.getNumber());
54 }
55
56 public RecipientAddress(ServiceId serviceId) {
57 this(Optional.of(serviceId), Optional.empty());
58 }
59
60 public RecipientAddress withIdentifiersFrom(RecipientAddress address) {
61 return new RecipientAddress((
62 this.serviceId.isEmpty() || this.isServiceIdPNI() || this.serviceId.equals(address.pni)
63 ) && !address.isServiceIdPNI() ? address.serviceId : this.serviceId,
64 address.pni.or(this::pni),
65 address.number.or(this::number));
66 }
67
68 public RecipientAddress removeIdentifiersFrom(RecipientAddress address) {
69 return new RecipientAddress(address.serviceId.equals(this.serviceId) || address.pni.equals(this.serviceId)
70 ? Optional.empty()
71 : this.serviceId,
72 address.pni.equals(this.pni) || address.serviceId.equals(this.pni) ? Optional.empty() : this.pni,
73 address.number.equals(this.number) ? Optional.empty() : this.number);
74 }
75
76 public ServiceId getServiceId() {
77 return serviceId.orElse(ServiceId.UNKNOWN);
78 }
79
80 public String getIdentifier() {
81 if (serviceId.isPresent()) {
82 return serviceId.get().toString();
83 } else if (number.isPresent()) {
84 return number.get();
85 } else {
86 throw new AssertionError("Given the checks in the constructor, this should not be possible.");
87 }
88 }
89
90 public String getLegacyIdentifier() {
91 if (number.isPresent()) {
92 return number.get();
93 } else if (serviceId.isPresent()) {
94 return serviceId.get().toString();
95 } else {
96 throw new AssertionError("Given the checks in the constructor, this should not be possible.");
97 }
98 }
99
100 public boolean matches(RecipientAddress other) {
101 return (serviceId.isPresent() && other.serviceId.isPresent() && serviceId.get().equals(other.serviceId.get()))
102 || (
103 pni.isPresent() && other.serviceId.isPresent() && pni.get().equals(other.serviceId.get())
104 )
105 || (
106 serviceId.isPresent() && other.pni.isPresent() && serviceId.get().equals(other.pni.get())
107 )
108 || (
109 pni.isPresent() && other.pni.isPresent() && pni.get().equals(other.pni.get())
110 )
111 || (
112 number.isPresent() && other.number.isPresent() && number.get().equals(other.number.get())
113 );
114 }
115
116 public boolean hasSingleIdentifier() {
117 return serviceId().isEmpty() || number.isEmpty();
118 }
119
120 public boolean hasIdentifiersOf(RecipientAddress address) {
121 return (address.serviceId.isEmpty() || address.serviceId.equals(serviceId) || address.serviceId.equals(pni))
122 && (address.pni.isEmpty() || address.pni.equals(pni))
123 && (address.number.isEmpty() || address.number.equals(number));
124 }
125
126 public boolean hasAdditionalIdentifiersThan(RecipientAddress address) {
127 return (
128 serviceId.isPresent() && (
129 address.serviceId.isEmpty() || (
130 !address.serviceId.equals(serviceId) && !address.pni.equals(serviceId)
131 )
132 )
133 ) || (
134 pni.isPresent() && !address.serviceId.equals(pni) && (
135 address.pni.isEmpty() || !address.pni.equals(pni)
136 )
137 ) || (
138 number.isPresent() && (
139 address.number.isEmpty() || !address.number.equals(number)
140 )
141 );
142 }
143
144 public boolean hasOnlyPniAndNumber() {
145 return pni.isPresent() && serviceId.equals(pni) && number.isPresent();
146 }
147
148 public boolean isServiceIdPNI() {
149 return serviceId.isPresent() && (pni.isPresent() && serviceId.equals(pni));
150 }
151
152 public SignalServiceAddress toSignalServiceAddress() {
153 return new SignalServiceAddress(getServiceId(), number);
154 }
155
156 public org.asamk.signal.manager.api.RecipientAddress toApiRecipientAddress() {
157 return new org.asamk.signal.manager.api.RecipientAddress(serviceId().map(ServiceId::uuid), number());
158 }
159 }