]> nmode's Git Repositories - signal-cli/blob - lib/src/main/java/org/asamk/signal/manager/storage/recipients/RecipientAddress.java
33b8a5f98514568df18466de6d18fd50e3fce544
[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.ServiceId;
4 import org.whispersystems.signalservice.api.push.ServiceId.ACI;
5 import org.whispersystems.signalservice.api.push.ServiceId.PNI;
6 import org.whispersystems.signalservice.api.push.SignalServiceAddress;
7
8 import java.util.Optional;
9
10 public record RecipientAddress(
11 Optional<ACI> aci, Optional<PNI> pni, Optional<String> number, Optional<String> username
12 ) {
13
14 /**
15 * Construct a RecipientAddress.
16 *
17 * @param aci The ACI of the user, if available.
18 * @param pni The PNI of the user, if available.
19 * @param number The phone number of the user, if available.
20 * @param username The username of the user, if available.
21 */
22 public RecipientAddress {
23 if (aci.isPresent() && aci.get().isUnknown()) {
24 aci = Optional.empty();
25 }
26 if (pni.isPresent() && pni.get().isUnknown()) {
27 pni = Optional.empty();
28 }
29 if (aci.isEmpty() && pni.isEmpty() && number.isEmpty()) {
30 throw new AssertionError("Must have either a ServiceId or E164 number!");
31 }
32 }
33
34 public RecipientAddress(Optional<ServiceId> serviceId, Optional<String> number) {
35 this(serviceId.filter(s -> s instanceof ACI).map(s -> (ACI) s),
36 serviceId.filter(s -> s instanceof PNI).map(s -> (PNI) s),
37 number,
38 Optional.empty());
39 }
40
41 public RecipientAddress(ACI aci, String e164) {
42 this(Optional.ofNullable(aci), Optional.empty(), Optional.ofNullable(e164), Optional.empty());
43 }
44
45 public RecipientAddress(String e164) {
46 this(Optional.empty(), Optional.empty(), Optional.ofNullable(e164), Optional.empty());
47 }
48
49 public RecipientAddress(ACI aci, PNI pni, String e164) {
50 this(Optional.ofNullable(aci), Optional.ofNullable(pni), Optional.ofNullable(e164), Optional.empty());
51 }
52
53 public RecipientAddress(ACI aci, PNI pni, String e164, String username) {
54 this(Optional.ofNullable(aci),
55 Optional.ofNullable(pni),
56 Optional.ofNullable(e164),
57 Optional.ofNullable(username));
58 }
59
60 public RecipientAddress(SignalServiceAddress address) {
61 this(address.getServiceId() instanceof ACI ? Optional.of((ACI) address.getServiceId()) : Optional.empty(),
62 address.getServiceId() instanceof PNI ? Optional.of((PNI) address.getServiceId()) : Optional.empty(),
63 address.getNumber(),
64 Optional.empty());
65 }
66
67 public RecipientAddress(org.asamk.signal.manager.api.RecipientAddress address) {
68 this(address.uuid().map(ACI::from), Optional.empty(), address.number(), address.username());
69 }
70
71 public RecipientAddress(ServiceId serviceId) {
72 this(Optional.of(serviceId), Optional.empty());
73 }
74
75 public RecipientAddress withIdentifiersFrom(RecipientAddress address) {
76 return new RecipientAddress(address.aci.or(this::aci),
77 address.pni.or(this::pni),
78 address.number.or(this::number),
79 address.username.or(this::username));
80 }
81
82 public RecipientAddress removeIdentifiersFrom(RecipientAddress address) {
83 return new RecipientAddress(address.aci.equals(this.aci) ? Optional.empty() : this.aci,
84 address.pni.equals(this.pni) ? Optional.empty() : this.pni,
85 address.number.equals(this.number) ? Optional.empty() : this.number,
86 address.username.equals(this.username) ? Optional.empty() : this.username);
87 }
88
89 public Optional<ServiceId> serviceId() {
90 return aci.map(aci -> (ServiceId) aci).or(this::pni);
91 }
92
93 public String getIdentifier() {
94 if (aci.isPresent()) {
95 return aci.get().toString();
96 } else if (pni.isPresent()) {
97 return pni.get().toString();
98 } else if (number.isPresent()) {
99 return number.get();
100 } else {
101 throw new AssertionError("Given the checks in the constructor, this should not be possible.");
102 }
103 }
104
105 public String getLegacyIdentifier() {
106 if (number.isPresent()) {
107 return number.get();
108 } else if (aci.isPresent()) {
109 return aci.get().toString();
110 } else if (pni.isPresent()) {
111 return pni.get().toString();
112 } else {
113 throw new AssertionError("Given the checks in the constructor, this should not be possible.");
114 }
115 }
116
117 public boolean matches(RecipientAddress other) {
118 return (aci.isPresent() && other.aci.isPresent() && aci.get().equals(other.aci.get())) || (
119 pni.isPresent() && other.pni.isPresent() && pni.get().equals(other.pni.get())
120 ) || (
121 number.isPresent() && other.number.isPresent() && number.get().equals(other.number.get())
122 );
123 }
124
125 public boolean hasSingleIdentifier() {
126 final var identifiersCount = aci().map(s -> 1).orElse(0) + pni().map(s -> 1).orElse(0) + number().map(s -> 1)
127 .orElse(0) + username().map(s -> 1).orElse(0);
128 return identifiersCount == 1;
129 }
130
131 public boolean hasIdentifiersOf(RecipientAddress address) {
132 return (address.aci.isEmpty() || address.aci.equals(aci))
133 && (address.pni.isEmpty() || address.pni.equals(pni))
134 && (address.number.isEmpty() || address.number.equals(number))
135 && (address.username.isEmpty() || address.username.equals(username));
136 }
137
138 public boolean hasAdditionalIdentifiersThan(RecipientAddress address) {
139 return (
140 aci.isPresent() && (
141 address.aci.isEmpty() || !address.aci.equals(aci)
142
143 )
144 ) || (
145 pni.isPresent() && (
146 address.pni.isEmpty() || !address.pni.equals(pni)
147 )
148 ) || (
149 number.isPresent() && (
150 address.number.isEmpty() || !address.number.equals(number)
151 )
152 ) || (
153 username.isPresent() && (
154 address.username.isEmpty() || !address.username.equals(username)
155 )
156 );
157 }
158
159 public boolean hasOnlyPniAndNumber() {
160 return pni.isPresent() && aci.isEmpty() && number.isPresent();
161 }
162
163 public SignalServiceAddress toSignalServiceAddress() {
164 return new SignalServiceAddress(aci.orElse(ACI.UNKNOWN), number);
165 }
166
167 public org.asamk.signal.manager.api.RecipientAddress toApiRecipientAddress() {
168 return new org.asamk.signal.manager.api.RecipientAddress(aci().map(ServiceId::getRawUuid),
169 number(),
170 username());
171 }
172 }