]> nmode's Git Repositories - signal-cli/blob - lib/src/main/java/org/asamk/signal/manager/api/RecipientAddress.java
Add aci,pni to API RecipientAddress
[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.util.UuidUtil;
4
5 import java.util.Optional;
6 import java.util.UUID;
7
8 public record RecipientAddress(
9 Optional<String> aci, Optional<String> pni, Optional<String> number, Optional<String> username
10 ) {
11
12 public static final UUID UNKNOWN_UUID = UuidUtil.UNKNOWN_UUID;
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 */
21 public RecipientAddress {
22 if (aci.isEmpty() && pni.isEmpty() && number.isEmpty() && username.isEmpty()) {
23 throw new AssertionError("Must have either a ACI, PNI, username or E164 number!");
24 }
25 }
26
27 public RecipientAddress(String e164) {
28 this(null, null, e164, null);
29 }
30
31 public RecipientAddress(UUID uuid) {
32 this(uuid.toString(), null, null, null);
33 }
34
35 public RecipientAddress(String aci, String pni, String e164, String username) {
36 this(Optional.ofNullable(aci),
37 Optional.ofNullable(pni),
38 Optional.ofNullable(e164),
39 Optional.ofNullable(username));
40 }
41
42 public Optional<UUID> uuid() {
43 return aci.map(UUID::fromString);
44 }
45
46 public String getIdentifier() {
47 if (aci.isPresent()) {
48 return aci.get();
49 } else if (number.isPresent()) {
50 return number.get();
51 } else if (pni.isPresent()) {
52 return pni.get();
53 } else if (username.isPresent()) {
54 return username.get();
55 } else {
56 throw new AssertionError("Given the checks in the constructor, this should not be possible.");
57 }
58 }
59
60 public String getLegacyIdentifier() {
61 if (number.isPresent()) {
62 return number.get();
63 } else {
64 return getIdentifier();
65 }
66 }
67
68 public boolean matches(RecipientAddress other) {
69 return (aci.isPresent() && other.aci.isPresent() && aci.get().equals(other.aci.get()))
70 || (
71 pni.isPresent() && other.pni.isPresent() && pni.get().equals(other.pni.get())
72 )
73 || (number.isPresent() && other.number.isPresent() && number.get().equals(other.number.get()))
74 || (username.isPresent() && other.username.isPresent() && username.get().equals(other.username.get()));
75 }
76 }