Convert RecipientAddress to record

This commit is contained in:
AsamK 2021-12-06 18:55:37 +01:00
parent c3a9022bec
commit 8867a7b9ee
14 changed files with 59 additions and 91 deletions

View file

@ -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);

View file

@ -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");
}

View file

@ -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 uuid The UUID 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)));
}
}

View file

@ -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()),