mirror of
https://github.com/AsamK/signal-cli
synced 2025-08-29 18:40:39 +00:00
Add aci,pni to API RecipientAddress
This commit is contained in:
parent
e0cd5b987e
commit
e456d06cb0
7 changed files with 59 additions and 53 deletions
|
@ -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()));
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue