Refactor JsonMessageEnvelope to remove unnecessary number canonicalization

This commit is contained in:
AsamK 2022-01-29 15:02:30 +01:00
parent 380c892e24
commit e284b99076

View file

@ -4,9 +4,9 @@ import com.fasterxml.jackson.annotation.JsonInclude;
import org.asamk.signal.manager.Manager; import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.UntrustedIdentityException; import org.asamk.signal.manager.UntrustedIdentityException;
import org.asamk.signal.manager.api.InvalidNumberException;
import org.asamk.signal.manager.api.MessageEnvelope; import org.asamk.signal.manager.api.MessageEnvelope;
import org.asamk.signal.manager.api.RecipientIdentifier; import org.asamk.signal.manager.api.RecipientIdentifier;
import org.asamk.signal.manager.storage.recipients.RecipientAddress;
import java.util.UUID; import java.util.UUID;
@ -27,35 +27,35 @@ public record JsonMessageEnvelope(
public static JsonMessageEnvelope from( public static JsonMessageEnvelope from(
MessageEnvelope envelope, Throwable exception, Manager m MessageEnvelope envelope, Throwable exception, Manager m
) { ) {
final RecipientAddress sourceAddress;
final Integer sourceDevice;
if (envelope.sourceAddress().isPresent()) {
sourceAddress = envelope.sourceAddress().get();
sourceDevice = envelope.sourceDevice();
} else if (exception instanceof UntrustedIdentityException e) {
sourceAddress = e.getSender();
sourceDevice = e.getSenderDevice();
} else {
sourceAddress = null;
sourceDevice = null;
}
final String source; final String source;
final String sourceNumber; final String sourceNumber;
final String sourceUuid; final String sourceUuid;
final Integer sourceDevice; final String sourceName;
if (envelope.sourceAddress().isPresent()) { if (sourceAddress != null) {
final var sourceAddress = envelope.sourceAddress().get();
source = sourceAddress.getLegacyIdentifier(); source = sourceAddress.getLegacyIdentifier();
sourceNumber = sourceAddress.number().orElse(null); sourceNumber = sourceAddress.number().orElse(null);
sourceUuid = sourceAddress.uuid().map(UUID::toString).orElse(null); sourceUuid = sourceAddress.uuid().map(UUID::toString).orElse(null);
sourceDevice = envelope.sourceDevice(); sourceName = m.getContactOrProfileName(RecipientIdentifier.Single.fromAddress(envelope.sourceAddress()
} else if (exception instanceof UntrustedIdentityException e) { .get()));
final var sender = e.getSender();
source = sender.getLegacyIdentifier();
sourceNumber = sender.number().orElse(null);
sourceUuid = sender.uuid().map(UUID::toString).orElse(null);
sourceDevice = e.getSenderDevice();
} else { } else {
source = null; source = null;
sourceNumber = null; sourceNumber = null;
sourceUuid = null; sourceUuid = null;
sourceDevice = null; sourceName = null;
} }
String name;
try {
name = m.getContactOrProfileName(RecipientIdentifier.Single.fromString(source, m.getSelfNumber()));
} catch (InvalidNumberException | NullPointerException e) {
name = null;
}
final var sourceName = name;
final var timestamp = envelope.timestamp(); final var timestamp = envelope.timestamp();
final var receiptMessage = envelope.receipt().map(JsonReceiptMessage::from).orElse(null); final var receiptMessage = envelope.receipt().map(JsonReceiptMessage::from).orElse(null);
final var typingMessage = envelope.typing().map(JsonTypingMessage::from).orElse(null); final var typingMessage = envelope.typing().map(JsonTypingMessage::from).orElse(null);