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) { private SignalServiceAddress resolveSignalServiceAddress(RecipientId recipientId) {
final var address = account.getRecipientStore().resolveRecipientAddress(recipientId); final var address = account.getRecipientStore().resolveRecipientAddress(recipientId);
if (address.getUuid().isPresent()) { if (address.uuid().isPresent()) {
return address.toSignalServiceAddress(); return address.toSignalServiceAddress();
} }
// Address in recipient store doesn't have a uuid, this shouldn't happen // Address in recipient store doesn't have a uuid, this shouldn't happen
// Try to retrieve the uuid from the server // Try to retrieve the uuid from the server
final var number = address.getNumber().get(); final var number = address.number().get();
final ACI aci; final ACI aci;
try { try {
aci = getRegisteredUser(number); aci = getRegisteredUser(number);

View file

@ -36,10 +36,10 @@ public sealed interface RecipientIdentifier {
} }
static Single fromAddress(RecipientAddress address) { static Single fromAddress(RecipientAddress address) {
if (address.getNumber().isPresent()) { if (address.number().isPresent()) {
return new Number(address.getNumber().get()); return new Number(address.number().get());
} else if (address.getUuid().isPresent()) { } else if (address.uuid().isPresent()) {
return new Uuid(address.getUuid().get()); return new Uuid(address.uuid().get());
} }
throw new AssertionError("RecipientAddress without identifier"); 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.Optional;
import java.util.UUID; 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(); public static final UUID UNKNOWN_UUID = ACI.UNKNOWN.uuid();
private final Optional<UUID> uuid;
private final Optional<String> e164;
/** /**
* Construct a RecipientAddress. * Construct a RecipientAddress.
* *
* @param uuid The UUID of the user, if available. * @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; 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!"); throw new AssertionError("Must have either a UUID or E164 number!");
} }
this.uuid = uuid;
this.e164 = e164;
} }
public RecipientAddress(UUID uuid, String e164) { public RecipientAddress(UUID uuid, String e164) {
@ -41,27 +35,19 @@ public class RecipientAddress {
this(Optional.of(uuid), Optional.empty()); this(Optional.of(uuid), Optional.empty());
} }
public Optional<String> getNumber() {
return e164;
}
public Optional<UUID> getUuid() {
return uuid;
}
public String getIdentifier() { public String getIdentifier() {
if (uuid.isPresent()) { if (uuid.isPresent()) {
return uuid.get().toString(); return uuid.get().toString();
} else if (e164.isPresent()) { } else if (number.isPresent()) {
return e164.get(); return number.get();
} else { } else {
throw new AssertionError("Given the checks in the constructor, this should not be possible."); throw new AssertionError("Given the checks in the constructor, this should not be possible.");
} }
} }
public String getLegacyIdentifier() { public String getLegacyIdentifier() {
if (e164.isPresent()) { if (number.isPresent()) {
return e164.get(); return number.get();
} else if (uuid.isPresent()) { } else if (uuid.isPresent()) {
return uuid.get().toString(); return uuid.get().toString();
} else { } else {
@ -71,30 +57,12 @@ public class RecipientAddress {
public boolean matches(RecipientAddress other) { public boolean matches(RecipientAddress other) {
return (uuid.isPresent() && other.uuid.isPresent() && uuid.get().equals(other.uuid.get())) || ( 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() { public SignalServiceAddress toSignalServiceAddress() {
return new SignalServiceAddress(ACI.from(uuid.orElse(UNKNOWN_UUID)), return new SignalServiceAddress(ACI.from(uuid.orElse(UNKNOWN_UUID)),
org.whispersystems.libsignal.util.guava.Optional.fromNullable(e164.orElse(null))); org.whispersystems.libsignal.util.guava.Optional.fromNullable(number.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;
} }
} }

View file

@ -151,7 +151,7 @@ public class RecipientStore implements RecipientResolver, ContactsStore, Profile
synchronized (recipients) { synchronized (recipients) {
byNumber = findByNumberLocked(number); byNumber = findByNumberLocked(number);
} }
if (byNumber.isEmpty() || byNumber.get().getAddress().getUuid().isEmpty()) { if (byNumber.isEmpty() || byNumber.get().getAddress().uuid().isEmpty()) {
final var aci = aciSupplier.get(); final var aci = aciSupplier.get();
if (aci == null) { if (aci == null) {
throw new UnregisteredUserException(number, null); throw new UnregisteredUserException(number, null);
@ -234,7 +234,7 @@ public class RecipientStore implements RecipientResolver, ContactsStore, Profile
storeRecipientLocked(recipientId, storeRecipientLocked(recipientId,
Recipient.newBuilder() Recipient.newBuilder()
.withRecipientId(recipientId) .withRecipientId(recipientId)
.withAddress(new RecipientAddress(recipient.getAddress().getUuid().orElse(null))) .withAddress(new RecipientAddress(recipient.getAddress().uuid().orElse(null)))
.build()); .build());
} }
} }
@ -320,24 +320,24 @@ public class RecipientStore implements RecipientResolver, ContactsStore, Profile
private Pair<RecipientId, Optional<RecipientId>> resolveRecipientLocked( private Pair<RecipientId, Optional<RecipientId>> resolveRecipientLocked(
RecipientAddress address, boolean isHighTrust RecipientAddress address, boolean isHighTrust
) { ) {
final var byNumber = address.getNumber().isEmpty() final var byNumber = address.number().isEmpty()
? Optional.<Recipient>empty() ? Optional.<Recipient>empty()
: findByNumberLocked(address.getNumber().get()); : findByNumberLocked(address.number().get());
final var byUuid = address.getUuid().isEmpty() final var byUuid = address.uuid().isEmpty()
? Optional.<Recipient>empty() ? Optional.<Recipient>empty()
: findByUuidLocked(address.getUuid().get()); : findByUuidLocked(address.uuid().get());
if (byNumber.isEmpty() && byUuid.isEmpty()) { if (byNumber.isEmpty() && byUuid.isEmpty()) {
logger.debug("Got new recipient, both uuid and number are unknown"); 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(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()); 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 (byUuid.isEmpty()) {
if (byNumber.get().getAddress().getUuid().isPresent()) { if (byNumber.get().getAddress().uuid().isPresent()) {
logger.debug( logger.debug(
"Got recipient existing with number, but different uuid, so stripping its number and adding new recipient"); "Got recipient existing with number, but different uuid, so stripping its number and adding new recipient");
updateRecipientAddressLocked(byNumber.get().getRecipientId(), 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()); 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()); return new Pair<>(byNumber.get().getRecipientId(), Optional.empty());
} }
if (byNumber.get().getAddress().getUuid().isPresent()) { if (byNumber.get().getAddress().uuid().isPresent()) {
logger.debug( logger.debug(
"Got separate recipients for high trust number and uuid, recipient for number has different uuid, so stripping its number"); "Got separate recipients for high trust number and uuid, recipient for number has different uuid, so stripping its number");
updateRecipientAddressLocked(byNumber.get().getRecipientId(), updateRecipientAddressLocked(byNumber.get().getRecipientId(),
new RecipientAddress(byNumber.get().getAddress().getUuid().get())); new RecipientAddress(byNumber.get().getAddress().uuid().get()));
updateRecipientAddressLocked(byUuid.get().getRecipientId(), address); updateRecipientAddressLocked(byUuid.get().getRecipientId(), address);
return new Pair<>(byUuid.get().getRecipientId(), Optional.empty()); 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) { private Optional<Recipient> findByNumberLocked(final String number) {
return recipients.entrySet() return recipients.entrySet()
.stream() .stream()
.filter(entry -> entry.getValue().getAddress().getNumber().isPresent() && number.equals(entry.getValue() .filter(entry -> entry.getValue().getAddress().number().isPresent() && number.equals(entry.getValue()
.getAddress() .getAddress()
.getNumber() .number()
.get())) .get()))
.findFirst() .findFirst()
.map(Map.Entry::getValue); .map(Map.Entry::getValue);
@ -441,9 +441,9 @@ public class RecipientStore implements RecipientResolver, ContactsStore, Profile
private Optional<Recipient> findByUuidLocked(final UUID uuid) { private Optional<Recipient> findByUuidLocked(final UUID uuid) {
return recipients.entrySet() return recipients.entrySet()
.stream() .stream()
.filter(entry -> entry.getValue().getAddress().getUuid().isPresent() && uuid.equals(entry.getValue() .filter(entry -> entry.getValue().getAddress().uuid().isPresent() && uuid.equals(entry.getValue()
.getAddress() .getAddress()
.getUuid() .uuid()
.get())) .get()))
.findFirst() .findFirst()
.map(Map.Entry::getValue); .map(Map.Entry::getValue);
@ -479,8 +479,8 @@ public class RecipientStore implements RecipientResolver, ContactsStore, Profile
.map(Enum::name) .map(Enum::name)
.collect(Collectors.toSet())); .collect(Collectors.toSet()));
return new Storage.Recipient(pair.getKey().id(), return new Storage.Recipient(pair.getKey().id(),
recipient.getAddress().getNumber().orElse(null), recipient.getAddress().number().orElse(null),
recipient.getAddress().getUuid().map(UUID::toString).orElse(null), recipient.getAddress().uuid().map(UUID::toString).orElse(null),
recipient.getProfileKey() == null recipient.getProfileKey() == null
? null ? null
: base64.encodeToString(recipient.getProfileKey().serialize()), : base64.encodeToString(recipient.getProfileKey().serialize()),

View file

@ -43,8 +43,8 @@ public class ListContactsCommand implements JsonRpcLocalCommand {
final var jsonContacts = contacts.stream().map(contactPair -> { final var jsonContacts = contacts.stream().map(contactPair -> {
final var address = contactPair.first(); final var address = contactPair.first();
final var contact = contactPair.second(); final var contact = contactPair.second();
return new JsonContact(address.getNumber().orElse(null), return new JsonContact(address.number().orElse(null),
address.getUuid().map(UUID::toString).orElse(null), address.uuid().map(UUID::toString).orElse(null),
contact.getName(), contact.getName(),
contact.isBlocked(), contact.isBlocked(),
contact.getMessageExpirationTime()); contact.getMessageExpirationTime());

View file

@ -41,8 +41,8 @@ public class ListGroupsCommand implements JsonRpcLocalCommand {
private static Set<JsonGroupMember> resolveJsonMembers(Set<RecipientAddress> addresses) { private static Set<JsonGroupMember> resolveJsonMembers(Set<RecipientAddress> addresses) {
return addresses.stream() return addresses.stream()
.map(address -> new JsonGroupMember(address.getNumber().orElse(null), .map(address -> new JsonGroupMember(address.number().orElse(null),
address.getUuid().map(UUID::toString).orElse(null))) address.uuid().map(UUID::toString).orElse(null)))
.collect(Collectors.toSet()); .collect(Collectors.toSet());
} }

View file

@ -31,7 +31,7 @@ public class ListIdentitiesCommand implements JsonRpcLocalCommand {
private static void printIdentityFingerprint(PlainTextWriter writer, Identity theirId) { private static void printIdentityFingerprint(PlainTextWriter writer, Identity theirId) {
writer.println("{}: {} Added: {} Fingerprint: {} Safety Number: {}", writer.println("{}: {} Added: {} Fingerprint: {} Safety Number: {}",
theirId.recipient().getNumber().orElse(null), theirId.recipient().number().orElse(null),
theirId.trustLevel(), theirId.trustLevel(),
theirId.dateAdded(), theirId.dateAdded(),
Hex.toString(theirId.getFingerprint()), Hex.toString(theirId.getFingerprint()),
@ -67,8 +67,8 @@ public class ListIdentitiesCommand implements JsonRpcLocalCommand {
final var address = id.recipient(); final var address = id.recipient();
var safetyNumber = Util.formatSafetyNumber(id.safetyNumber()); var safetyNumber = Util.formatSafetyNumber(id.safetyNumber());
var scannableSafetyNumber = id.scannableSafetyNumber(); var scannableSafetyNumber = id.scannableSafetyNumber();
return new JsonIdentity(address.getNumber().orElse(null), return new JsonIdentity(address.number().orElse(null),
address.getUuid().map(UUID::toString).orElse(null), address.uuid().map(UUID::toString).orElse(null),
Hex.toString(id.getFingerprint()), Hex.toString(id.getFingerprint()),
safetyNumber, safetyNumber,
scannableSafetyNumber == null scannableSafetyNumber == null

View file

@ -679,7 +679,7 @@ public class DbusSignalImpl implements Signal {
public List<String> listNumbers() { public List<String> listNumbers() {
return Stream.concat(m.getIdentities().stream().map(Identity::recipient), return Stream.concat(m.getIdentities().stream().map(Identity::recipient),
m.getContacts().stream().map(Pair::first)) m.getContacts().stream().map(Pair::first))
.map(a -> a.getNumber().orElse(null)) .map(a -> a.number().orElse(null))
.filter(Objects::nonNull) .filter(Objects::nonNull)
.distinct() .distinct()
.collect(Collectors.toList()); .collect(Collectors.toList());
@ -698,7 +698,7 @@ public class DbusSignalImpl implements Signal {
// Try profiles if no contact name was found // Try profiles if no contact name was found
for (var identity : m.getIdentities()) { for (var identity : m.getIdentities()) {
final var address = identity.recipient(); final var address = identity.recipient();
var number = address.getNumber().orElse(null); var number = address.number().orElse(null);
if (number != null) { if (number != null) {
Profile profile = null; Profile profile = null;
try { try {

View file

@ -9,8 +9,8 @@ public record JsonMention(@Deprecated String name, String number, String uuid, i
static JsonMention from(MessageEnvelope.Data.Mention mention) { static JsonMention from(MessageEnvelope.Data.Mention mention) {
final var address = mention.recipient(); final var address = mention.recipient();
return new JsonMention(address.getLegacyIdentifier(), return new JsonMention(address.getLegacyIdentifier(),
address.getNumber().orElse(null), address.number().orElse(null),
address.getUuid().map(UUID::toString).orElse(null), address.uuid().map(UUID::toString).orElse(null),
mention.start(), mention.start(),
mention.length()); mention.length());
} }

View file

@ -34,14 +34,14 @@ public record JsonMessageEnvelope(
if (envelope.sourceAddress().isPresent()) { if (envelope.sourceAddress().isPresent()) {
final var sourceAddress = envelope.sourceAddress().get(); final var sourceAddress = envelope.sourceAddress().get();
source = sourceAddress.getLegacyIdentifier(); source = sourceAddress.getLegacyIdentifier();
sourceNumber = sourceAddress.getNumber().orElse(null); sourceNumber = sourceAddress.number().orElse(null);
sourceUuid = sourceAddress.getUuid().map(UUID::toString).orElse(null); sourceUuid = sourceAddress.uuid().map(UUID::toString).orElse(null);
sourceDevice = envelope.sourceDevice(); sourceDevice = envelope.sourceDevice();
} else if (exception instanceof UntrustedIdentityException e) { } else if (exception instanceof UntrustedIdentityException e) {
final var sender = e.getSender(); final var sender = e.getSender();
source = sender.getLegacyIdentifier(); source = sender.getLegacyIdentifier();
sourceNumber = sender.getNumber().orElse(null); sourceNumber = sender.number().orElse(null);
sourceUuid = sender.getUuid().map(UUID::toString).orElse(null); sourceUuid = sender.uuid().map(UUID::toString).orElse(null);
sourceDevice = e.getSenderDevice(); sourceDevice = e.getSenderDevice();
} else { } else {
source = null; source = null;

View file

@ -23,8 +23,8 @@ public record JsonQuote(
final var id = quote.id(); final var id = quote.id();
final var address = quote.author(); final var address = quote.author();
final var author = address.getLegacyIdentifier(); final var author = address.getLegacyIdentifier();
final var authorNumber = address.getNumber().orElse(null); final var authorNumber = address.number().orElse(null);
final var authorUuid = address.getUuid().map(UUID::toString).orElse(null); final var authorUuid = address.uuid().map(UUID::toString).orElse(null);
final var text = quote.text().orElse(null); final var text = quote.text().orElse(null);
final var mentions = quote.mentions().size() > 0 ? quote.mentions() final var mentions = quote.mentions().size() > 0 ? quote.mentions()

View file

@ -17,8 +17,8 @@ public record JsonReaction(
final var emoji = reaction.emoji(); final var emoji = reaction.emoji();
final var address = reaction.targetAuthor(); final var address = reaction.targetAuthor();
final var targetAuthor = address.getLegacyIdentifier(); final var targetAuthor = address.getLegacyIdentifier();
final var targetAuthorNumber = address.getNumber().orElse(null); final var targetAuthorNumber = address.number().orElse(null);
final var targetAuthorUuid = address.getUuid().map(UUID::toString).orElse(null); final var targetAuthorUuid = address.uuid().map(UUID::toString).orElse(null);
final var targetSentTimestamp = reaction.targetSentTimestamp(); final var targetSentTimestamp = reaction.targetSentTimestamp();
final var isRemove = reaction.isRemove(); final var isRemove = reaction.isRemove();
return new JsonReaction(emoji, return new JsonReaction(emoji,

View file

@ -17,8 +17,8 @@ record JsonSyncDataMessage(
if (transcriptMessage.destination().isPresent()) { if (transcriptMessage.destination().isPresent()) {
final var address = transcriptMessage.destination().get(); final var address = transcriptMessage.destination().get();
return new JsonSyncDataMessage(address.getLegacyIdentifier(), return new JsonSyncDataMessage(address.getLegacyIdentifier(),
address.getNumber().orElse(null), address.number().orElse(null),
address.getUuid().map(UUID::toString).orElse(null), address.uuid().map(UUID::toString).orElse(null),
JsonDataMessage.from(transcriptMessage.message())); JsonDataMessage.from(transcriptMessage.message()));
} else { } else {

View file

@ -11,8 +11,8 @@ record JsonSyncReadMessage(
static JsonSyncReadMessage from(MessageEnvelope.Sync.Read readMessage) { static JsonSyncReadMessage from(MessageEnvelope.Sync.Read readMessage) {
final var senderAddress = readMessage.sender(); final var senderAddress = readMessage.sender();
final var sender = senderAddress.getLegacyIdentifier(); final var sender = senderAddress.getLegacyIdentifier();
final var senderNumber = senderAddress.getNumber().orElse(null); final var senderNumber = senderAddress.number().orElse(null);
final var senderUuid = senderAddress.getUuid().map(UUID::toString).orElse(null); final var senderUuid = senderAddress.uuid().map(UUID::toString).orElse(null);
final var timestamp = readMessage.timestamp(); final var timestamp = readMessage.timestamp();
return new JsonSyncReadMessage(sender, senderNumber, senderUuid, timestamp); return new JsonSyncReadMessage(sender, senderNumber, senderUuid, timestamp);
} }