Add aci,pni to API RecipientAddress

This commit is contained in:
AsamK 2024-04-13 21:29:45 +02:00
parent e0cd5b987e
commit e456d06cb0
7 changed files with 59 additions and 53 deletions

View file

@ -1,49 +1,55 @@
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(UUID uuid, String e164, String username) {
this(Optional.ofNullable(uuid), Optional.ofNullable(e164), Optional.ofNullable(username));
}
public RecipientAddress(SignalServiceAddress address) {
this(Optional.of(address.getServiceId().getRawUuid()), address.getNumber(), Optional.empty());
public RecipientAddress(String e164) {
this(null, null, e164, null);
}
public RecipientAddress(UUID uuid) {
this(Optional.of(uuid), Optional.empty(), Optional.empty());
this(uuid.toString(), null, null, null);
}
public RecipientAddress(String aci, String pni, String e164, String username) {
this(Optional.ofNullable(aci),
Optional.ofNullable(pni),
Optional.ofNullable(e164),
Optional.ofNullable(username));
}
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 {
@ -54,17 +60,16 @@ public record RecipientAddress(Optional<UUID> uuid, Optional<String> number, Opt
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()));
}

View file

@ -47,8 +47,8 @@ public sealed interface RecipientIdentifier {
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());
}
@ -80,7 +80,7 @@ public sealed interface RecipientIdentifier {
@Override
public RecipientAddress toPartialRecipientAddress() {
return new RecipientAddress(null, number);
return new RecipientAddress(number);
}
}
@ -93,7 +93,7 @@ public sealed interface RecipientIdentifier {
@Override
public RecipientAddress toPartialRecipientAddress() {
return new RecipientAddress(null, null, username);
return new RecipientAddress(null, null, null, username);
}
}

View file

@ -229,9 +229,9 @@ public class ReceiveHelper {
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);

View file

@ -108,6 +108,7 @@ public class RecipientHelper {
return account.getRecipientStore().resolveRecipientTrusted(aci, finalUsername.getUsername());
} catch (IOException e) {
throw new UnregisteredRecipientException(new org.asamk.signal.manager.api.RecipientAddress(null,
null,
null,
username));
}
@ -196,11 +197,11 @@ public class RecipientHelper {
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();
}

View file

@ -69,7 +69,10 @@ public record RecipientAddress(
}
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) {
@ -169,7 +172,8 @@ public record RecipientAddress(
}
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());
}

View file

@ -215,8 +215,7 @@ public class RecipientStore implements RecipientIdCreator, RecipientResolver, Re
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);
@ -247,6 +246,7 @@ public class RecipientStore implements RecipientIdCreator, RecipientResolver, Re
final var aci = aciSupplier.get();
if (aci == null) {
throw new UnregisteredRecipientException(new org.asamk.signal.manager.api.RecipientAddress(null,
null,
null,
username));
}

View file

@ -690,7 +690,7 @@ public class DbusManagerImpl implements Manager {
return null;
}
return Recipient.newBuilder()
.withAddress(new RecipientAddress(null, n))
.withAddress(new RecipientAddress(n))
.withContact(new Contact(contactName,
null,
null,
@ -731,19 +731,19 @@ public class DbusManagerImpl implements Manager {
(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(),
@ -854,8 +854,7 @@ public class DbusManagerImpl implements Manager {
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,
@ -896,8 +895,7 @@ public class DbusManagerImpl implements Manager {
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,
@ -945,8 +943,7 @@ public class DbusManagerImpl implements Manager {
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,
@ -967,8 +964,7 @@ public class DbusManagerImpl implements Manager {
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,
@ -982,7 +978,7 @@ public class DbusManagerImpl implements Manager {
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
@ -1081,7 +1077,7 @@ public class DbusManagerImpl implements Manager {
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();