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; package org.asamk.signal.manager.api;
import org.whispersystems.signalservice.api.push.SignalServiceAddress;
import org.whispersystems.signalservice.api.util.UuidUtil; import org.whispersystems.signalservice.api.util.UuidUtil;
import java.util.Optional; import java.util.Optional;
import java.util.UUID; 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; public static final UUID UNKNOWN_UUID = UuidUtil.UNKNOWN_UUID;
/** /**
* Construct a RecipientAddress. * 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. * @param number The phone number of the user, if available.
*/ */
public RecipientAddress { public RecipientAddress {
uuid = uuid.isPresent() && uuid.get().equals(UNKNOWN_UUID) ? Optional.empty() : uuid; if (aci.isEmpty() && pni.isEmpty() && number.isEmpty() && username.isEmpty()) {
if (uuid.isEmpty() && number.isEmpty() && username.isEmpty()) { throw new AssertionError("Must have either a ACI, PNI, username or E164 number!");
throw new AssertionError("Must have either a UUID, username or E164 number!");
} }
} }
public RecipientAddress(UUID uuid, String e164) { public RecipientAddress(String e164) {
this(Optional.ofNullable(uuid), Optional.ofNullable(e164), Optional.empty()); 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(SignalServiceAddress address) {
this(Optional.of(address.getServiceId().getRawUuid()), address.getNumber(), Optional.empty());
} }
public RecipientAddress(UUID uuid) { 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() { public String getIdentifier() {
if (uuid.isPresent()) { if (aci.isPresent()) {
return uuid.get().toString(); return aci.get();
} else if (number.isPresent()) { } else if (number.isPresent()) {
return number.get(); return number.get();
} else if (pni.isPresent()) {
return pni.get();
} else if (username.isPresent()) { } else if (username.isPresent()) {
return username.get(); return username.get();
} else { } else {
@ -54,17 +60,16 @@ public record RecipientAddress(Optional<UUID> uuid, Optional<String> number, Opt
public String getLegacyIdentifier() { public String getLegacyIdentifier() {
if (number.isPresent()) { if (number.isPresent()) {
return number.get(); return number.get();
} else if (uuid.isPresent()) {
return uuid.get().toString();
} else if (username.isPresent()) {
return username.get();
} else { } else {
throw new AssertionError("Given the checks in the constructor, this should not be possible."); return getIdentifier();
} }
} }
public boolean matches(RecipientAddress other) { 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())) || (number.isPresent() && other.number.isPresent() && number.get().equals(other.number.get()))
|| (username.isPresent() && other.username.isPresent() && username.get().equals(other.username.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) { static Single fromAddress(RecipientAddress address) {
if (address.number().isPresent()) { if (address.number().isPresent()) {
return new Number(address.number().get()); return new Number(address.number().get());
} else if (address.uuid().isPresent()) { } else if (address.aci().isPresent()) {
return new Uuid(address.uuid().get()); return new Uuid(UUID.fromString(address.aci().get()));
} else if (address.username().isPresent()) { } else if (address.username().isPresent()) {
return new Username(address.username().get()); return new Username(address.username().get());
} }
@ -80,7 +80,7 @@ public sealed interface RecipientIdentifier {
@Override @Override
public RecipientAddress toPartialRecipientAddress() { public RecipientAddress toPartialRecipientAddress() {
return new RecipientAddress(null, number); return new RecipientAddress(number);
} }
} }
@ -93,7 +93,7 @@ public sealed interface RecipientIdentifier {
@Override @Override
public RecipientAddress toPartialRecipientAddress() { 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) { if (exception instanceof UntrustedIdentityException) {
logger.debug("Keeping message with untrusted identity in message cache"); logger.debug("Keeping message with untrusted identity in message cache");
final var address = ((UntrustedIdentityException) exception).getSender(); 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() final var recipientId = account.getRecipientResolver()
.resolveRecipient(ACI.from(address.uuid().get())); .resolveRecipient(ACI.parseOrThrow(address.aci().get()));
try { try {
cachedMessage[0] = account.getMessageCache() cachedMessage[0] = account.getMessageCache()
.replaceSender(cachedMessage[0], recipientId); .replaceSender(cachedMessage[0], recipientId);

View File

@ -108,6 +108,7 @@ public class RecipientHelper {
return account.getRecipientStore().resolveRecipientTrusted(aci, finalUsername.getUsername()); return account.getRecipientStore().resolveRecipientTrusted(aci, finalUsername.getUsername());
} catch (IOException e) { } catch (IOException e) {
throw new UnregisteredRecipientException(new org.asamk.signal.manager.api.RecipientAddress(null, throw new UnregisteredRecipientException(new org.asamk.signal.manager.api.RecipientAddress(null,
null,
null, null,
username)); username));
} }
@ -196,11 +197,11 @@ public class RecipientHelper {
try { try {
aciMap = getRegisteredUsers(Set.of(number), true); aciMap = getRegisteredUsers(Set.of(number), true);
} catch (NumberFormatException e) { } 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); final var user = aciMap.get(number);
if (user == null) { 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(); return user.getServiceId();
} }

View File

@ -69,7 +69,10 @@ public record RecipientAddress(
} }
public RecipientAddress(org.asamk.signal.manager.api.RecipientAddress address) { 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 RecipientAddress(ServiceId serviceId) {
@ -169,7 +172,8 @@ public record RecipientAddress(
} }
public org.asamk.signal.manager.api.RecipientAddress toApiRecipientAddress() { 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(), number(),
username()); username());
} }

View File

@ -215,8 +215,7 @@ public class RecipientStore implements RecipientIdCreator, RecipientResolver, Re
if (byNumber.isEmpty() || byNumber.get().address().serviceId().isEmpty()) { if (byNumber.isEmpty() || byNumber.get().address().serviceId().isEmpty()) {
final var serviceId = serviceIdSupplier.get(); final var serviceId = serviceIdSupplier.get();
if (serviceId == null) { if (serviceId == null) {
throw new UnregisteredRecipientException(new org.asamk.signal.manager.api.RecipientAddress(null, throw new UnregisteredRecipientException(new org.asamk.signal.manager.api.RecipientAddress(number));
number));
} }
return resolveRecipient(serviceId); return resolveRecipient(serviceId);
@ -247,6 +246,7 @@ public class RecipientStore implements RecipientIdCreator, RecipientResolver, Re
final var aci = aciSupplier.get(); final var aci = aciSupplier.get();
if (aci == null) { if (aci == null) {
throw new UnregisteredRecipientException(new org.asamk.signal.manager.api.RecipientAddress(null, throw new UnregisteredRecipientException(new org.asamk.signal.manager.api.RecipientAddress(null,
null,
null, null,
username)); username));
} }

View File

@ -690,7 +690,7 @@ public class DbusManagerImpl implements Manager {
return null; return null;
} }
return Recipient.newBuilder() return Recipient.newBuilder()
.withAddress(new RecipientAddress(null, n)) .withAddress(new RecipientAddress(n))
.withContact(new Contact(contactName, .withContact(new Contact(contactName,
null, null,
null, null,
@ -731,19 +731,19 @@ public class DbusManagerImpl implements Manager {
(String) group.get("Description").getValue(), (String) group.get("Description").getValue(),
GroupInviteLinkUrl.fromUri((String) group.get("GroupInviteLink").getValue()), GroupInviteLinkUrl.fromUri((String) group.get("GroupInviteLink").getValue()),
((List<String>) group.get("Members").getValue()).stream() ((List<String>) group.get("Members").getValue()).stream()
.map(m -> new RecipientAddress(null, m)) .map(m -> new RecipientAddress(m))
.collect(Collectors.toSet()), .collect(Collectors.toSet()),
((List<String>) group.get("PendingMembers").getValue()).stream() ((List<String>) group.get("PendingMembers").getValue()).stream()
.map(m -> new RecipientAddress(null, m)) .map(m -> new RecipientAddress(m))
.collect(Collectors.toSet()), .collect(Collectors.toSet()),
((List<String>) group.get("RequestingMembers").getValue()).stream() ((List<String>) group.get("RequestingMembers").getValue()).stream()
.map(m -> new RecipientAddress(null, m)) .map(m -> new RecipientAddress(m))
.collect(Collectors.toSet()), .collect(Collectors.toSet()),
((List<String>) group.get("Admins").getValue()).stream() ((List<String>) group.get("Admins").getValue()).stream()
.map(m -> new RecipientAddress(null, m)) .map(m -> new RecipientAddress(m))
.collect(Collectors.toSet()), .collect(Collectors.toSet()),
((List<String>) group.get("Banned").getValue()).stream() ((List<String>) group.get("Banned").getValue()).stream()
.map(m -> new RecipientAddress(null, m)) .map(m -> new RecipientAddress(m))
.collect(Collectors.toSet()), .collect(Collectors.toSet()),
(boolean) group.get("IsBlocked").getValue(), (boolean) group.get("IsBlocked").getValue(),
(int) group.get("MessageExpirationTimer").getValue(), (int) group.get("MessageExpirationTimer").getValue(),
@ -854,8 +854,7 @@ public class DbusManagerImpl implements Manager {
try { try {
this.dbusMsgHandler = messageReceived -> { this.dbusMsgHandler = messageReceived -> {
final var extras = messageReceived.getExtras(); final var extras = messageReceived.getExtras();
final var envelope = new MessageEnvelope(Optional.of(new RecipientAddress(null, final var envelope = new MessageEnvelope(Optional.of(new RecipientAddress(messageReceived.getSender())),
messageReceived.getSender())),
0, 0,
messageReceived.getTimestamp(), messageReceived.getTimestamp(),
0, 0,
@ -896,8 +895,7 @@ public class DbusManagerImpl implements Manager {
connection.addSigHandler(Signal.MessageReceivedV2.class, signal, this.dbusMsgHandler); connection.addSigHandler(Signal.MessageReceivedV2.class, signal, this.dbusMsgHandler);
this.dbusEditMsgHandler = messageReceived -> { this.dbusEditMsgHandler = messageReceived -> {
final var extras = messageReceived.getExtras(); final var extras = messageReceived.getExtras();
final var envelope = new MessageEnvelope(Optional.of(new RecipientAddress(null, final var envelope = new MessageEnvelope(Optional.of(new RecipientAddress(messageReceived.getSender())),
messageReceived.getSender())),
0, 0,
messageReceived.getTimestamp(), messageReceived.getTimestamp(),
0, 0,
@ -945,8 +943,7 @@ public class DbusManagerImpl implements Manager {
case "delivery" -> MessageEnvelope.Receipt.Type.DELIVERY; case "delivery" -> MessageEnvelope.Receipt.Type.DELIVERY;
default -> MessageEnvelope.Receipt.Type.UNKNOWN; default -> MessageEnvelope.Receipt.Type.UNKNOWN;
}; };
final var envelope = new MessageEnvelope(Optional.of(new RecipientAddress(null, final var envelope = new MessageEnvelope(Optional.of(new RecipientAddress(receiptReceived.getSender())),
receiptReceived.getSender())),
0, 0,
receiptReceived.getTimestamp(), receiptReceived.getTimestamp(),
0, 0,
@ -967,8 +964,7 @@ public class DbusManagerImpl implements Manager {
this.dbusSyncHandler = syncReceived -> { this.dbusSyncHandler = syncReceived -> {
final var extras = syncReceived.getExtras(); final var extras = syncReceived.getExtras();
final var envelope = new MessageEnvelope(Optional.of(new RecipientAddress(null, final var envelope = new MessageEnvelope(Optional.of(new RecipientAddress(syncReceived.getSource())),
syncReceived.getSource())),
0, 0,
syncReceived.getTimestamp(), syncReceived.getTimestamp(),
0, 0,
@ -982,7 +978,7 @@ public class DbusManagerImpl implements Manager {
syncReceived.getTimestamp(), syncReceived.getTimestamp(),
syncReceived.getDestination().isEmpty() syncReceived.getDestination().isEmpty()
? Optional.empty() ? Optional.empty()
: Optional.of(new RecipientAddress(null, syncReceived.getDestination())), : Optional.of(new RecipientAddress(syncReceived.getDestination())),
Set.of(), Set.of(),
Optional.of(new MessageEnvelope.Data(syncReceived.getTimestamp(), Optional.of(new MessageEnvelope.Data(syncReceived.getTimestamp(),
syncReceived.getGroupId().length > 0 syncReceived.getGroupId().length > 0
@ -1081,7 +1077,7 @@ public class DbusManagerImpl implements Manager {
final List<DBusMap<String, Variant<?>>> mentions = getValue(extras, "mentions"); final List<DBusMap<String, Variant<?>>> mentions = getValue(extras, "mentions");
return mentions.stream() 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, "start"),
getValue(a, "length"))) getValue(a, "length")))
.toList(); .toList();