]> nmode's Git Repositories - signal-cli/blob - lib/src/main/java/org/asamk/signal/manager/api/RecipientAddress.java
Implement replying to stories
[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.push.ServiceId;
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 = ServiceId.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()), address.getNumber());
32 }
33
34 public RecipientAddress(UUID uuid) {
35 this(Optional.of(uuid), Optional.empty());
36 }
37
38 public ServiceId getServiceId() {
39 return ServiceId.from(uuid.orElse(UNKNOWN_UUID));
40 }
41
42 public String getIdentifier() {
43 if (uuid.isPresent()) {
44 return uuid.get().toString();
45 } else if (number.isPresent()) {
46 return number.get();
47 } else {
48 throw new AssertionError("Given the checks in the constructor, this should not be possible.");
49 }
50 }
51
52 public String getLegacyIdentifier() {
53 if (number.isPresent()) {
54 return number.get();
55 } else if (uuid.isPresent()) {
56 return uuid.get().toString();
57 } else {
58 throw new AssertionError("Given the checks in the constructor, this should not be possible.");
59 }
60 }
61
62 public boolean matches(RecipientAddress other) {
63 return (uuid.isPresent() && other.uuid.isPresent() && uuid.get().equals(other.uuid.get())) || (
64 number.isPresent() && other.number.isPresent() && number.get().equals(other.number.get())
65 );
66 }
67 }