]> nmode's Git Repositories - signal-cli/blob - lib/src/main/java/org/asamk/signal/manager/storage/recipients/RecipientAddress.java
3bfdadc0e513da6438650a62ff412a20bdb01699
[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.ACI;
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) {
10
11 public static final UUID UNKNOWN_UUID = ACI.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()) {
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));
28 }
29
30 public RecipientAddress(SignalServiceAddress address) {
31 this(Optional.of(address.getServiceId().uuid()), Optional.ofNullable(address.getNumber().orNull()));
32 }
33
34 public RecipientAddress(UUID uuid) {
35 this(Optional.of(uuid), Optional.empty());
36 }
37
38 public String getIdentifier() {
39 if (uuid.isPresent()) {
40 return uuid.get().toString();
41 } else if (number.isPresent()) {
42 return number.get();
43 } else {
44 throw new AssertionError("Given the checks in the constructor, this should not be possible.");
45 }
46 }
47
48 public String getLegacyIdentifier() {
49 if (number.isPresent()) {
50 return number.get();
51 } else if (uuid.isPresent()) {
52 return uuid.get().toString();
53 } else {
54 throw new AssertionError("Given the checks in the constructor, this should not be possible.");
55 }
56 }
57
58 public boolean matches(RecipientAddress other) {
59 return (uuid.isPresent() && other.uuid.isPresent() && uuid.get().equals(other.uuid.get())) || (
60 number.isPresent() && other.number.isPresent() && number.get().equals(other.number.get())
61 );
62 }
63
64 public SignalServiceAddress toSignalServiceAddress() {
65 return new SignalServiceAddress(ACI.from(uuid.orElse(UNKNOWN_UUID)),
66 org.whispersystems.libsignal.util.guava.Optional.fromNullable(number.orElse(null)));
67 }
68 }