package org.asamk.signal.manager.api;
-import org.whispersystems.signalservice.api.push.SignalServiceAddress;
import org.whispersystems.signalservice.api.util.UuidUtil;
import java.util.Optional;
import java.util.UUID;
-public record RecipientAddress(Optional<UUID> uuid, Optional<String> number, Optional<String> username) {
+public record RecipientAddress(
+ Optional<String> aci, Optional<String> pni, Optional<String> number, Optional<String> username
+) {
public static final UUID UNKNOWN_UUID = UuidUtil.UNKNOWN_UUID;
/**
* Construct a RecipientAddress.
*
- * @param uuid The UUID of the user, if available.
+ * @param aci The ACI of the user, if available.
+ * @param pni The PNI of the user, if available.
* @param number The phone number of the user, if available.
*/
public RecipientAddress {
- uuid = uuid.isPresent() && uuid.get().equals(UNKNOWN_UUID) ? Optional.empty() : uuid;
- if (uuid.isEmpty() && number.isEmpty() && username.isEmpty()) {
- throw new AssertionError("Must have either a UUID, username or E164 number!");
+ if (aci.isEmpty() && pni.isEmpty() && number.isEmpty() && username.isEmpty()) {
+ throw new AssertionError("Must have either a ACI, PNI, username or E164 number!");
}
}
- public RecipientAddress(UUID uuid, String e164) {
- this(Optional.ofNullable(uuid), Optional.ofNullable(e164), Optional.empty());
+ public RecipientAddress(String e164) {
+ this(null, null, e164, null);
}
- public RecipientAddress(UUID uuid, String e164, String username) {
- this(Optional.ofNullable(uuid), Optional.ofNullable(e164), Optional.ofNullable(username));
+ public RecipientAddress(UUID uuid) {
+ this(uuid.toString(), null, null, null);
}
- public RecipientAddress(SignalServiceAddress address) {
- this(Optional.of(address.getServiceId().getRawUuid()), address.getNumber(), Optional.empty());
+ public RecipientAddress(String aci, String pni, String e164, String username) {
+ this(Optional.ofNullable(aci),
+ Optional.ofNullable(pni),
+ Optional.ofNullable(e164),
+ Optional.ofNullable(username));
}
- public RecipientAddress(UUID uuid) {
- this(Optional.of(uuid), Optional.empty(), Optional.empty());
+ public Optional<UUID> uuid() {
+ return aci.map(UUID::fromString);
}
public String getIdentifier() {
- if (uuid.isPresent()) {
- return uuid.get().toString();
+ if (aci.isPresent()) {
+ return aci.get();
} else if (number.isPresent()) {
return number.get();
+ } else if (pni.isPresent()) {
+ return pni.get();
} else if (username.isPresent()) {
return username.get();
} else {
public String getLegacyIdentifier() {
if (number.isPresent()) {
return number.get();
- } else if (uuid.isPresent()) {
- return uuid.get().toString();
- } else if (username.isPresent()) {
- return username.get();
} else {
- throw new AssertionError("Given the checks in the constructor, this should not be possible.");
+ return getIdentifier();
}
}
public boolean matches(RecipientAddress other) {
- return (uuid.isPresent() && other.uuid.isPresent() && uuid.get().equals(other.uuid.get()))
+ return (aci.isPresent() && other.aci.isPresent() && aci.get().equals(other.aci.get()))
+ || (
+ pni.isPresent() && other.pni.isPresent() && pni.get().equals(other.pni.get())
+ )
|| (number.isPresent() && other.number.isPresent() && number.get().equals(other.number.get()))
|| (username.isPresent() && other.username.isPresent() && username.get().equals(other.username.get()));
}
static Single fromAddress(RecipientAddress address) {
if (address.number().isPresent()) {
return new Number(address.number().get());
- } else if (address.uuid().isPresent()) {
- return new Uuid(address.uuid().get());
+ } else if (address.aci().isPresent()) {
+ return new Uuid(UUID.fromString(address.aci().get()));
} else if (address.username().isPresent()) {
return new Username(address.username().get());
}
@Override
public RecipientAddress toPartialRecipientAddress() {
- return new RecipientAddress(null, number);
+ return new RecipientAddress(number);
}
}
@Override
public RecipientAddress toPartialRecipientAddress() {
- return new RecipientAddress(null, null, username);
+ return new RecipientAddress(null, null, null, username);
}
}
if (exception instanceof UntrustedIdentityException) {
logger.debug("Keeping message with untrusted identity in message cache");
final var address = ((UntrustedIdentityException) exception).getSender();
- if (envelope.getSourceServiceId().isEmpty() && address.uuid().isPresent()) {
+ if (envelope.getSourceServiceId().isEmpty() && address.aci().isPresent()) {
final var recipientId = account.getRecipientResolver()
- .resolveRecipient(ACI.from(address.uuid().get()));
+ .resolveRecipient(ACI.parseOrThrow(address.aci().get()));
try {
cachedMessage[0] = account.getMessageCache()
.replaceSender(cachedMessage[0], recipientId);
return account.getRecipientStore().resolveRecipientTrusted(aci, finalUsername.getUsername());
} catch (IOException e) {
throw new UnregisteredRecipientException(new org.asamk.signal.manager.api.RecipientAddress(null,
+ null,
null,
username));
}
try {
aciMap = getRegisteredUsers(Set.of(number), true);
} catch (NumberFormatException e) {
- throw new UnregisteredRecipientException(new org.asamk.signal.manager.api.RecipientAddress(null, number));
+ throw new UnregisteredRecipientException(new org.asamk.signal.manager.api.RecipientAddress(number));
}
final var user = aciMap.get(number);
if (user == null) {
- throw new UnregisteredRecipientException(new org.asamk.signal.manager.api.RecipientAddress(null, number));
+ throw new UnregisteredRecipientException(new org.asamk.signal.manager.api.RecipientAddress(number));
}
return user.getServiceId();
}
}
public RecipientAddress(org.asamk.signal.manager.api.RecipientAddress address) {
- this(address.uuid().map(ACI::from), Optional.empty(), address.number(), address.username());
+ this(address.aci().map(ACI::parseOrNull),
+ address.pni().map(PNI::parseOrNull),
+ address.number(),
+ address.username());
}
public RecipientAddress(ServiceId serviceId) {
}
public org.asamk.signal.manager.api.RecipientAddress toApiRecipientAddress() {
- return new org.asamk.signal.manager.api.RecipientAddress(serviceId().map(ServiceId::getRawUuid),
+ return new org.asamk.signal.manager.api.RecipientAddress(aci().map(ServiceId::toString),
+ pni().map(ServiceId::toString),
number(),
username());
}
if (byNumber.isEmpty() || byNumber.get().address().serviceId().isEmpty()) {
final var serviceId = serviceIdSupplier.get();
if (serviceId == null) {
- throw new UnregisteredRecipientException(new org.asamk.signal.manager.api.RecipientAddress(null,
- number));
+ throw new UnregisteredRecipientException(new org.asamk.signal.manager.api.RecipientAddress(number));
}
return resolveRecipient(serviceId);
final var aci = aciSupplier.get();
if (aci == null) {
throw new UnregisteredRecipientException(new org.asamk.signal.manager.api.RecipientAddress(null,
+ null,
null,
username));
}
return null;
}
return Recipient.newBuilder()
- .withAddress(new RecipientAddress(null, n))
+ .withAddress(new RecipientAddress(n))
.withContact(new Contact(contactName,
null,
null,
(String) group.get("Description").getValue(),
GroupInviteLinkUrl.fromUri((String) group.get("GroupInviteLink").getValue()),
((List<String>) group.get("Members").getValue()).stream()
- .map(m -> new RecipientAddress(null, m))
+ .map(m -> new RecipientAddress(m))
.collect(Collectors.toSet()),
((List<String>) group.get("PendingMembers").getValue()).stream()
- .map(m -> new RecipientAddress(null, m))
+ .map(m -> new RecipientAddress(m))
.collect(Collectors.toSet()),
((List<String>) group.get("RequestingMembers").getValue()).stream()
- .map(m -> new RecipientAddress(null, m))
+ .map(m -> new RecipientAddress(m))
.collect(Collectors.toSet()),
((List<String>) group.get("Admins").getValue()).stream()
- .map(m -> new RecipientAddress(null, m))
+ .map(m -> new RecipientAddress(m))
.collect(Collectors.toSet()),
((List<String>) group.get("Banned").getValue()).stream()
- .map(m -> new RecipientAddress(null, m))
+ .map(m -> new RecipientAddress(m))
.collect(Collectors.toSet()),
(boolean) group.get("IsBlocked").getValue(),
(int) group.get("MessageExpirationTimer").getValue(),
try {
this.dbusMsgHandler = messageReceived -> {
final var extras = messageReceived.getExtras();
- final var envelope = new MessageEnvelope(Optional.of(new RecipientAddress(null,
- messageReceived.getSender())),
+ final var envelope = new MessageEnvelope(Optional.of(new RecipientAddress(messageReceived.getSender())),
0,
messageReceived.getTimestamp(),
0,
connection.addSigHandler(Signal.MessageReceivedV2.class, signal, this.dbusMsgHandler);
this.dbusEditMsgHandler = messageReceived -> {
final var extras = messageReceived.getExtras();
- final var envelope = new MessageEnvelope(Optional.of(new RecipientAddress(null,
- messageReceived.getSender())),
+ final var envelope = new MessageEnvelope(Optional.of(new RecipientAddress(messageReceived.getSender())),
0,
messageReceived.getTimestamp(),
0,
case "delivery" -> MessageEnvelope.Receipt.Type.DELIVERY;
default -> MessageEnvelope.Receipt.Type.UNKNOWN;
};
- final var envelope = new MessageEnvelope(Optional.of(new RecipientAddress(null,
- receiptReceived.getSender())),
+ final var envelope = new MessageEnvelope(Optional.of(new RecipientAddress(receiptReceived.getSender())),
0,
receiptReceived.getTimestamp(),
0,
this.dbusSyncHandler = syncReceived -> {
final var extras = syncReceived.getExtras();
- final var envelope = new MessageEnvelope(Optional.of(new RecipientAddress(null,
- syncReceived.getSource())),
+ final var envelope = new MessageEnvelope(Optional.of(new RecipientAddress(syncReceived.getSource())),
0,
syncReceived.getTimestamp(),
0,
syncReceived.getTimestamp(),
syncReceived.getDestination().isEmpty()
? Optional.empty()
- : Optional.of(new RecipientAddress(null, syncReceived.getDestination())),
+ : Optional.of(new RecipientAddress(syncReceived.getDestination())),
Set.of(),
Optional.of(new MessageEnvelope.Data(syncReceived.getTimestamp(),
syncReceived.getGroupId().length > 0
final List<DBusMap<String, Variant<?>>> mentions = getValue(extras, "mentions");
return mentions.stream()
- .map(a -> new MessageEnvelope.Data.Mention(new RecipientAddress(null, getValue(a, "recipient")),
+ .map(a -> new MessageEnvelope.Data.Mention(new RecipientAddress(this.<String>getValue(a, "recipient")),
getValue(a, "start"),
getValue(a, "length")))
.toList();