mirror of
https://github.com/AsamK/signal-cli
synced 2025-08-29 10:30:38 +00:00
Convert RecipientAddress to record
This commit is contained in:
parent
c3a9022bec
commit
8867a7b9ee
14 changed files with 59 additions and 91 deletions
|
@ -1436,13 +1436,13 @@ public class ManagerImpl implements Manager {
|
|||
|
||||
private SignalServiceAddress resolveSignalServiceAddress(RecipientId recipientId) {
|
||||
final var address = account.getRecipientStore().resolveRecipientAddress(recipientId);
|
||||
if (address.getUuid().isPresent()) {
|
||||
if (address.uuid().isPresent()) {
|
||||
return address.toSignalServiceAddress();
|
||||
}
|
||||
|
||||
// Address in recipient store doesn't have a uuid, this shouldn't happen
|
||||
// Try to retrieve the uuid from the server
|
||||
final var number = address.getNumber().get();
|
||||
final var number = address.number().get();
|
||||
final ACI aci;
|
||||
try {
|
||||
aci = getRegisteredUser(number);
|
||||
|
|
|
@ -36,10 +36,10 @@ public sealed interface RecipientIdentifier {
|
|||
}
|
||||
|
||||
static Single fromAddress(RecipientAddress address) {
|
||||
if (address.getNumber().isPresent()) {
|
||||
return new Number(address.getNumber().get());
|
||||
} else if (address.getUuid().isPresent()) {
|
||||
return new Uuid(address.getUuid().get());
|
||||
if (address.number().isPresent()) {
|
||||
return new Number(address.number().get());
|
||||
} else if (address.uuid().isPresent()) {
|
||||
return new Uuid(address.uuid().get());
|
||||
}
|
||||
throw new AssertionError("RecipientAddress without identifier");
|
||||
}
|
||||
|
|
|
@ -6,27 +6,21 @@ import org.whispersystems.signalservice.api.push.SignalServiceAddress;
|
|||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
public class RecipientAddress {
|
||||
public record RecipientAddress(Optional<UUID> uuid, Optional<String> number) {
|
||||
|
||||
public static final UUID UNKNOWN_UUID = ACI.UNKNOWN.uuid();
|
||||
|
||||
private final Optional<UUID> uuid;
|
||||
private final Optional<String> e164;
|
||||
|
||||
/**
|
||||
* Construct a RecipientAddress.
|
||||
*
|
||||
* @param uuid The UUID of the user, if available.
|
||||
* @param e164 The phone number of the user, if available.
|
||||
* @param number The phone number of the user, if available.
|
||||
*/
|
||||
public RecipientAddress(Optional<UUID> uuid, Optional<String> e164) {
|
||||
public RecipientAddress {
|
||||
uuid = uuid.isPresent() && uuid.get().equals(UNKNOWN_UUID) ? Optional.empty() : uuid;
|
||||
if (uuid.isEmpty() && e164.isEmpty()) {
|
||||
if (uuid.isEmpty() && number.isEmpty()) {
|
||||
throw new AssertionError("Must have either a UUID or E164 number!");
|
||||
}
|
||||
|
||||
this.uuid = uuid;
|
||||
this.e164 = e164;
|
||||
}
|
||||
|
||||
public RecipientAddress(UUID uuid, String e164) {
|
||||
|
@ -41,27 +35,19 @@ public class RecipientAddress {
|
|||
this(Optional.of(uuid), Optional.empty());
|
||||
}
|
||||
|
||||
public Optional<String> getNumber() {
|
||||
return e164;
|
||||
}
|
||||
|
||||
public Optional<UUID> getUuid() {
|
||||
return uuid;
|
||||
}
|
||||
|
||||
public String getIdentifier() {
|
||||
if (uuid.isPresent()) {
|
||||
return uuid.get().toString();
|
||||
} else if (e164.isPresent()) {
|
||||
return e164.get();
|
||||
} else if (number.isPresent()) {
|
||||
return number.get();
|
||||
} else {
|
||||
throw new AssertionError("Given the checks in the constructor, this should not be possible.");
|
||||
}
|
||||
}
|
||||
|
||||
public String getLegacyIdentifier() {
|
||||
if (e164.isPresent()) {
|
||||
return e164.get();
|
||||
if (number.isPresent()) {
|
||||
return number.get();
|
||||
} else if (uuid.isPresent()) {
|
||||
return uuid.get().toString();
|
||||
} else {
|
||||
|
@ -71,30 +57,12 @@ public class RecipientAddress {
|
|||
|
||||
public boolean matches(RecipientAddress other) {
|
||||
return (uuid.isPresent() && other.uuid.isPresent() && uuid.get().equals(other.uuid.get())) || (
|
||||
e164.isPresent() && other.e164.isPresent() && e164.get().equals(other.e164.get())
|
||||
number.isPresent() && other.number.isPresent() && number.get().equals(other.number.get())
|
||||
);
|
||||
}
|
||||
|
||||
public SignalServiceAddress toSignalServiceAddress() {
|
||||
return new SignalServiceAddress(ACI.from(uuid.orElse(UNKNOWN_UUID)),
|
||||
org.whispersystems.libsignal.util.guava.Optional.fromNullable(e164.orElse(null)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(final Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
|
||||
final RecipientAddress that = (RecipientAddress) o;
|
||||
|
||||
if (!uuid.equals(that.uuid)) return false;
|
||||
return e164.equals(that.e164);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = uuid.hashCode();
|
||||
result = 31 * result + e164.hashCode();
|
||||
return result;
|
||||
org.whispersystems.libsignal.util.guava.Optional.fromNullable(number.orElse(null)));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -151,7 +151,7 @@ public class RecipientStore implements RecipientResolver, ContactsStore, Profile
|
|||
synchronized (recipients) {
|
||||
byNumber = findByNumberLocked(number);
|
||||
}
|
||||
if (byNumber.isEmpty() || byNumber.get().getAddress().getUuid().isEmpty()) {
|
||||
if (byNumber.isEmpty() || byNumber.get().getAddress().uuid().isEmpty()) {
|
||||
final var aci = aciSupplier.get();
|
||||
if (aci == null) {
|
||||
throw new UnregisteredUserException(number, null);
|
||||
|
@ -234,7 +234,7 @@ public class RecipientStore implements RecipientResolver, ContactsStore, Profile
|
|||
storeRecipientLocked(recipientId,
|
||||
Recipient.newBuilder()
|
||||
.withRecipientId(recipientId)
|
||||
.withAddress(new RecipientAddress(recipient.getAddress().getUuid().orElse(null)))
|
||||
.withAddress(new RecipientAddress(recipient.getAddress().uuid().orElse(null)))
|
||||
.build());
|
||||
}
|
||||
}
|
||||
|
@ -320,24 +320,24 @@ public class RecipientStore implements RecipientResolver, ContactsStore, Profile
|
|||
private Pair<RecipientId, Optional<RecipientId>> resolveRecipientLocked(
|
||||
RecipientAddress address, boolean isHighTrust
|
||||
) {
|
||||
final var byNumber = address.getNumber().isEmpty()
|
||||
final var byNumber = address.number().isEmpty()
|
||||
? Optional.<Recipient>empty()
|
||||
: findByNumberLocked(address.getNumber().get());
|
||||
final var byUuid = address.getUuid().isEmpty()
|
||||
: findByNumberLocked(address.number().get());
|
||||
final var byUuid = address.uuid().isEmpty()
|
||||
? Optional.<Recipient>empty()
|
||||
: findByUuidLocked(address.getUuid().get());
|
||||
: findByUuidLocked(address.uuid().get());
|
||||
|
||||
if (byNumber.isEmpty() && byUuid.isEmpty()) {
|
||||
logger.debug("Got new recipient, both uuid and number are unknown");
|
||||
|
||||
if (isHighTrust || address.getUuid().isEmpty() || address.getNumber().isEmpty()) {
|
||||
if (isHighTrust || address.uuid().isEmpty() || address.number().isEmpty()) {
|
||||
return new Pair<>(addNewRecipientLocked(address), Optional.empty());
|
||||
}
|
||||
|
||||
return new Pair<>(addNewRecipientLocked(new RecipientAddress(address.getUuid().get())), Optional.empty());
|
||||
return new Pair<>(addNewRecipientLocked(new RecipientAddress(address.uuid().get())), Optional.empty());
|
||||
}
|
||||
|
||||
if (!isHighTrust || address.getUuid().isEmpty() || address.getNumber().isEmpty() || byNumber.equals(byUuid)) {
|
||||
if (!isHighTrust || address.uuid().isEmpty() || address.number().isEmpty() || byNumber.equals(byUuid)) {
|
||||
return new Pair<>(byUuid.or(() -> byNumber).map(Recipient::getRecipientId).get(), Optional.empty());
|
||||
}
|
||||
|
||||
|
@ -348,12 +348,12 @@ public class RecipientStore implements RecipientResolver, ContactsStore, Profile
|
|||
}
|
||||
|
||||
if (byUuid.isEmpty()) {
|
||||
if (byNumber.get().getAddress().getUuid().isPresent()) {
|
||||
if (byNumber.get().getAddress().uuid().isPresent()) {
|
||||
logger.debug(
|
||||
"Got recipient existing with number, but different uuid, so stripping its number and adding new recipient");
|
||||
|
||||
updateRecipientAddressLocked(byNumber.get().getRecipientId(),
|
||||
new RecipientAddress(byNumber.get().getAddress().getUuid().get()));
|
||||
new RecipientAddress(byNumber.get().getAddress().uuid().get()));
|
||||
return new Pair<>(addNewRecipientLocked(address), Optional.empty());
|
||||
}
|
||||
|
||||
|
@ -362,12 +362,12 @@ public class RecipientStore implements RecipientResolver, ContactsStore, Profile
|
|||
return new Pair<>(byNumber.get().getRecipientId(), Optional.empty());
|
||||
}
|
||||
|
||||
if (byNumber.get().getAddress().getUuid().isPresent()) {
|
||||
if (byNumber.get().getAddress().uuid().isPresent()) {
|
||||
logger.debug(
|
||||
"Got separate recipients for high trust number and uuid, recipient for number has different uuid, so stripping its number");
|
||||
|
||||
updateRecipientAddressLocked(byNumber.get().getRecipientId(),
|
||||
new RecipientAddress(byNumber.get().getAddress().getUuid().get()));
|
||||
new RecipientAddress(byNumber.get().getAddress().uuid().get()));
|
||||
updateRecipientAddressLocked(byUuid.get().getRecipientId(), address);
|
||||
return new Pair<>(byUuid.get().getRecipientId(), Optional.empty());
|
||||
}
|
||||
|
@ -430,9 +430,9 @@ public class RecipientStore implements RecipientResolver, ContactsStore, Profile
|
|||
private Optional<Recipient> findByNumberLocked(final String number) {
|
||||
return recipients.entrySet()
|
||||
.stream()
|
||||
.filter(entry -> entry.getValue().getAddress().getNumber().isPresent() && number.equals(entry.getValue()
|
||||
.filter(entry -> entry.getValue().getAddress().number().isPresent() && number.equals(entry.getValue()
|
||||
.getAddress()
|
||||
.getNumber()
|
||||
.number()
|
||||
.get()))
|
||||
.findFirst()
|
||||
.map(Map.Entry::getValue);
|
||||
|
@ -441,9 +441,9 @@ public class RecipientStore implements RecipientResolver, ContactsStore, Profile
|
|||
private Optional<Recipient> findByUuidLocked(final UUID uuid) {
|
||||
return recipients.entrySet()
|
||||
.stream()
|
||||
.filter(entry -> entry.getValue().getAddress().getUuid().isPresent() && uuid.equals(entry.getValue()
|
||||
.filter(entry -> entry.getValue().getAddress().uuid().isPresent() && uuid.equals(entry.getValue()
|
||||
.getAddress()
|
||||
.getUuid()
|
||||
.uuid()
|
||||
.get()))
|
||||
.findFirst()
|
||||
.map(Map.Entry::getValue);
|
||||
|
@ -479,8 +479,8 @@ public class RecipientStore implements RecipientResolver, ContactsStore, Profile
|
|||
.map(Enum::name)
|
||||
.collect(Collectors.toSet()));
|
||||
return new Storage.Recipient(pair.getKey().id(),
|
||||
recipient.getAddress().getNumber().orElse(null),
|
||||
recipient.getAddress().getUuid().map(UUID::toString).orElse(null),
|
||||
recipient.getAddress().number().orElse(null),
|
||||
recipient.getAddress().uuid().map(UUID::toString).orElse(null),
|
||||
recipient.getProfileKey() == null
|
||||
? null
|
||||
: base64.encodeToString(recipient.getProfileKey().serialize()),
|
||||
|
|
|
@ -43,8 +43,8 @@ public class ListContactsCommand implements JsonRpcLocalCommand {
|
|||
final var jsonContacts = contacts.stream().map(contactPair -> {
|
||||
final var address = contactPair.first();
|
||||
final var contact = contactPair.second();
|
||||
return new JsonContact(address.getNumber().orElse(null),
|
||||
address.getUuid().map(UUID::toString).orElse(null),
|
||||
return new JsonContact(address.number().orElse(null),
|
||||
address.uuid().map(UUID::toString).orElse(null),
|
||||
contact.getName(),
|
||||
contact.isBlocked(),
|
||||
contact.getMessageExpirationTime());
|
||||
|
|
|
@ -41,8 +41,8 @@ public class ListGroupsCommand implements JsonRpcLocalCommand {
|
|||
|
||||
private static Set<JsonGroupMember> resolveJsonMembers(Set<RecipientAddress> addresses) {
|
||||
return addresses.stream()
|
||||
.map(address -> new JsonGroupMember(address.getNumber().orElse(null),
|
||||
address.getUuid().map(UUID::toString).orElse(null)))
|
||||
.map(address -> new JsonGroupMember(address.number().orElse(null),
|
||||
address.uuid().map(UUID::toString).orElse(null)))
|
||||
.collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
|
|
|
@ -31,7 +31,7 @@ public class ListIdentitiesCommand implements JsonRpcLocalCommand {
|
|||
|
||||
private static void printIdentityFingerprint(PlainTextWriter writer, Identity theirId) {
|
||||
writer.println("{}: {} Added: {} Fingerprint: {} Safety Number: {}",
|
||||
theirId.recipient().getNumber().orElse(null),
|
||||
theirId.recipient().number().orElse(null),
|
||||
theirId.trustLevel(),
|
||||
theirId.dateAdded(),
|
||||
Hex.toString(theirId.getFingerprint()),
|
||||
|
@ -67,8 +67,8 @@ public class ListIdentitiesCommand implements JsonRpcLocalCommand {
|
|||
final var address = id.recipient();
|
||||
var safetyNumber = Util.formatSafetyNumber(id.safetyNumber());
|
||||
var scannableSafetyNumber = id.scannableSafetyNumber();
|
||||
return new JsonIdentity(address.getNumber().orElse(null),
|
||||
address.getUuid().map(UUID::toString).orElse(null),
|
||||
return new JsonIdentity(address.number().orElse(null),
|
||||
address.uuid().map(UUID::toString).orElse(null),
|
||||
Hex.toString(id.getFingerprint()),
|
||||
safetyNumber,
|
||||
scannableSafetyNumber == null
|
||||
|
|
|
@ -679,7 +679,7 @@ public class DbusSignalImpl implements Signal {
|
|||
public List<String> listNumbers() {
|
||||
return Stream.concat(m.getIdentities().stream().map(Identity::recipient),
|
||||
m.getContacts().stream().map(Pair::first))
|
||||
.map(a -> a.getNumber().orElse(null))
|
||||
.map(a -> a.number().orElse(null))
|
||||
.filter(Objects::nonNull)
|
||||
.distinct()
|
||||
.collect(Collectors.toList());
|
||||
|
@ -698,7 +698,7 @@ public class DbusSignalImpl implements Signal {
|
|||
// Try profiles if no contact name was found
|
||||
for (var identity : m.getIdentities()) {
|
||||
final var address = identity.recipient();
|
||||
var number = address.getNumber().orElse(null);
|
||||
var number = address.number().orElse(null);
|
||||
if (number != null) {
|
||||
Profile profile = null;
|
||||
try {
|
||||
|
|
|
@ -9,8 +9,8 @@ public record JsonMention(@Deprecated String name, String number, String uuid, i
|
|||
static JsonMention from(MessageEnvelope.Data.Mention mention) {
|
||||
final var address = mention.recipient();
|
||||
return new JsonMention(address.getLegacyIdentifier(),
|
||||
address.getNumber().orElse(null),
|
||||
address.getUuid().map(UUID::toString).orElse(null),
|
||||
address.number().orElse(null),
|
||||
address.uuid().map(UUID::toString).orElse(null),
|
||||
mention.start(),
|
||||
mention.length());
|
||||
}
|
||||
|
|
|
@ -34,14 +34,14 @@ public record JsonMessageEnvelope(
|
|||
if (envelope.sourceAddress().isPresent()) {
|
||||
final var sourceAddress = envelope.sourceAddress().get();
|
||||
source = sourceAddress.getLegacyIdentifier();
|
||||
sourceNumber = sourceAddress.getNumber().orElse(null);
|
||||
sourceUuid = sourceAddress.getUuid().map(UUID::toString).orElse(null);
|
||||
sourceNumber = sourceAddress.number().orElse(null);
|
||||
sourceUuid = sourceAddress.uuid().map(UUID::toString).orElse(null);
|
||||
sourceDevice = envelope.sourceDevice();
|
||||
} else if (exception instanceof UntrustedIdentityException e) {
|
||||
final var sender = e.getSender();
|
||||
source = sender.getLegacyIdentifier();
|
||||
sourceNumber = sender.getNumber().orElse(null);
|
||||
sourceUuid = sender.getUuid().map(UUID::toString).orElse(null);
|
||||
sourceNumber = sender.number().orElse(null);
|
||||
sourceUuid = sender.uuid().map(UUID::toString).orElse(null);
|
||||
sourceDevice = e.getSenderDevice();
|
||||
} else {
|
||||
source = null;
|
||||
|
|
|
@ -23,8 +23,8 @@ public record JsonQuote(
|
|||
final var id = quote.id();
|
||||
final var address = quote.author();
|
||||
final var author = address.getLegacyIdentifier();
|
||||
final var authorNumber = address.getNumber().orElse(null);
|
||||
final var authorUuid = address.getUuid().map(UUID::toString).orElse(null);
|
||||
final var authorNumber = address.number().orElse(null);
|
||||
final var authorUuid = address.uuid().map(UUID::toString).orElse(null);
|
||||
final var text = quote.text().orElse(null);
|
||||
|
||||
final var mentions = quote.mentions().size() > 0 ? quote.mentions()
|
||||
|
|
|
@ -17,8 +17,8 @@ public record JsonReaction(
|
|||
final var emoji = reaction.emoji();
|
||||
final var address = reaction.targetAuthor();
|
||||
final var targetAuthor = address.getLegacyIdentifier();
|
||||
final var targetAuthorNumber = address.getNumber().orElse(null);
|
||||
final var targetAuthorUuid = address.getUuid().map(UUID::toString).orElse(null);
|
||||
final var targetAuthorNumber = address.number().orElse(null);
|
||||
final var targetAuthorUuid = address.uuid().map(UUID::toString).orElse(null);
|
||||
final var targetSentTimestamp = reaction.targetSentTimestamp();
|
||||
final var isRemove = reaction.isRemove();
|
||||
return new JsonReaction(emoji,
|
||||
|
|
|
@ -17,8 +17,8 @@ record JsonSyncDataMessage(
|
|||
if (transcriptMessage.destination().isPresent()) {
|
||||
final var address = transcriptMessage.destination().get();
|
||||
return new JsonSyncDataMessage(address.getLegacyIdentifier(),
|
||||
address.getNumber().orElse(null),
|
||||
address.getUuid().map(UUID::toString).orElse(null),
|
||||
address.number().orElse(null),
|
||||
address.uuid().map(UUID::toString).orElse(null),
|
||||
JsonDataMessage.from(transcriptMessage.message()));
|
||||
|
||||
} else {
|
||||
|
|
|
@ -11,8 +11,8 @@ record JsonSyncReadMessage(
|
|||
static JsonSyncReadMessage from(MessageEnvelope.Sync.Read readMessage) {
|
||||
final var senderAddress = readMessage.sender();
|
||||
final var sender = senderAddress.getLegacyIdentifier();
|
||||
final var senderNumber = senderAddress.getNumber().orElse(null);
|
||||
final var senderUuid = senderAddress.getUuid().map(UUID::toString).orElse(null);
|
||||
final var senderNumber = senderAddress.number().orElse(null);
|
||||
final var senderUuid = senderAddress.uuid().map(UUID::toString).orElse(null);
|
||||
final var timestamp = readMessage.timestamp();
|
||||
return new JsonSyncReadMessage(sender, senderNumber, senderUuid, timestamp);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue