1 package org
.asamk
.signal
.manager
.storage
.recipients
;
3 import org
.whispersystems
.signalservice
.api
.push
.PNI
;
4 import org
.whispersystems
.signalservice
.api
.push
.ServiceId
;
5 import org
.whispersystems
.signalservice
.api
.push
.SignalServiceAddress
;
7 import java
.util
.Optional
;
9 public record RecipientAddress(Optional
<ServiceId
> serviceId
, Optional
<PNI
> pni
, Optional
<String
> number
) {
12 * Construct a RecipientAddress.
14 * @param serviceId The ACI or PNI of the user, if available.
15 * @param number The phone number of the user, if available.
17 public RecipientAddress
{
18 if (serviceId
.isPresent() && serviceId
.get().equals(ServiceId
.UNKNOWN
)) {
19 serviceId
= Optional
.empty();
21 if (pni
.isPresent() && pni
.get().equals(ServiceId
.UNKNOWN
)) {
22 pni
= Optional
.empty();
24 if (serviceId
.isEmpty() && pni
.isPresent()) {
25 serviceId
= Optional
.of(pni
.get());
27 if (serviceId
.isEmpty() && number
.isEmpty()) {
28 throw new AssertionError("Must have either a ServiceId or E164 number!");
32 public RecipientAddress(Optional
<ServiceId
> serviceId
, Optional
<String
> number
) {
33 this(serviceId
, Optional
.empty(), number
);
36 public RecipientAddress(ServiceId serviceId
, String e164
) {
37 this(Optional
.ofNullable(serviceId
), Optional
.empty(), Optional
.ofNullable(e164
));
40 public RecipientAddress(ServiceId serviceId
, PNI pni
, String e164
) {
41 this(Optional
.ofNullable(serviceId
), Optional
.ofNullable(pni
), Optional
.ofNullable(e164
));
44 public RecipientAddress(SignalServiceAddress address
) {
45 this(Optional
.of(address
.getServiceId()), Optional
.empty(), address
.getNumber());
48 public RecipientAddress(ServiceId serviceId
) {
49 this(Optional
.of(serviceId
), Optional
.empty());
52 public ServiceId
getServiceId() {
53 return serviceId
.orElse(ServiceId
.UNKNOWN
);
56 public String
getIdentifier() {
57 if (serviceId
.isPresent()) {
58 return serviceId
.get().toString();
59 } else if (number
.isPresent()) {
62 throw new AssertionError("Given the checks in the constructor, this should not be possible.");
66 public String
getLegacyIdentifier() {
67 if (number
.isPresent()) {
69 } else if (serviceId
.isPresent()) {
70 return serviceId
.get().toString();
72 throw new AssertionError("Given the checks in the constructor, this should not be possible.");
76 public boolean matches(RecipientAddress other
) {
77 return (serviceId
.isPresent() && other
.serviceId
.isPresent() && serviceId
.get().equals(other
.serviceId
.get()))
79 pni
.isPresent() && other
.serviceId
.isPresent() && pni
.get().equals(other
.serviceId
.get())
82 serviceId
.isPresent() && other
.pni
.isPresent() && serviceId
.get().equals(other
.pni
.get())
85 pni
.isPresent() && other
.pni
.isPresent() && pni
.get().equals(other
.pni
.get())
88 number
.isPresent() && other
.number
.isPresent() && number
.get().equals(other
.number
.get())
92 public SignalServiceAddress
toSignalServiceAddress() {
93 return new SignalServiceAddress(getServiceId(), number
);
96 public org
.asamk
.signal
.manager
.api
.RecipientAddress
toApiRecipientAddress() {
97 return new org
.asamk
.signal
.manager
.api
.RecipientAddress(serviceId().map(ServiceId
::uuid
), number());