]> nmode's Git Repositories - signal-cli/commitdiff
Improve source serviceId handling
authorAsamK <asamk@gmx.de>
Tue, 21 Nov 2023 18:59:06 +0000 (19:59 +0100)
committerAsamK <asamk@gmx.de>
Tue, 21 Nov 2023 18:59:06 +0000 (19:59 +0100)
lib/src/main/java/org/asamk/signal/manager/api/MessageEnvelope.java
lib/src/main/java/org/asamk/signal/manager/helper/IncomingMessageHandler.java
lib/src/main/java/org/asamk/signal/manager/helper/ReceiveHelper.java
lib/src/main/java/org/asamk/signal/manager/helper/RecipientHelper.java
lib/src/main/java/org/asamk/signal/manager/helper/SendHelper.java

index 938bdad88a921e9db5255bd3b0cd41ceb32dd645..dc157fe60a814641c4b7def3c38af9ec2016a8b5 100644 (file)
@@ -32,6 +32,7 @@ import org.whispersystems.signalservice.api.messages.multidevice.SentTranscriptM
 import org.whispersystems.signalservice.api.messages.multidevice.SignalServiceSyncMessage;
 import org.whispersystems.signalservice.api.messages.multidevice.ViewOnceOpenMessage;
 import org.whispersystems.signalservice.api.messages.multidevice.ViewedMessage;
+import org.whispersystems.signalservice.api.push.ServiceId;
 
 import java.io.File;
 import java.io.IOException;
@@ -904,8 +905,9 @@ public record MessageEnvelope(
             final AttachmentFileProvider fileProvider,
             Exception exception
     ) {
-        final var source = !envelope.isUnidentifiedSender() && envelope.hasSourceServiceId()
-                ? recipientResolver.resolveRecipient(envelope.getSourceAddress())
+        final var serviceId = envelope.getSourceServiceId().map(ServiceId::parseOrNull).orElse(null);
+        final var source = !envelope.isUnidentifiedSender() && serviceId != null
+                ? recipientResolver.resolveRecipient(serviceId)
                 : envelope.isUnidentifiedSender() && content != null
                         ? recipientResolver.resolveRecipient(content.getSender())
                         : exception instanceof ProtocolException e
index 66a137673213d6a5edc607627cdc9a60e80af473..cf9c43a2a6dcd99bad2fc38768cb94c5d8cb690f 100644 (file)
@@ -131,15 +131,10 @@ public final class IncomingMessageHandler {
         final var actions = new ArrayList<HandleAction>();
         SignalServiceContent content = null;
         Exception exception = null;
-        try {
-            if (envelope.hasSourceServiceId()) {
+        envelope.getSourceServiceId().map(ServiceId::parseOrNull)
                 // Store uuid if we don't have it already
                 // uuid in envelope is sent by server
-                account.getRecipientTrustedResolver().resolveRecipientTrusted(envelope.getSourceAddress());
-            }
-        } catch (Exception e) {
-            exception = e;
-        }
+                .ifPresent(serviceId -> account.getRecipientResolver().resolveRecipient(serviceId));
         if (!envelope.isReceipt()) {
             try {
                 final var cipherResult = dependencies.getCipher()
@@ -488,7 +483,7 @@ public final class IncomingMessageHandler {
                         sender,
                         destination == null
                                 ? null
-                                : new DeviceAddress(context.getRecipientHelper().resolveRecipient(destination),
+                                : new DeviceAddress(account.getRecipientResolver().resolveRecipient(destination),
                                         destination.getServiceId(),
                                         0),
                         ignoreAttachments));
@@ -530,7 +525,7 @@ public final class IncomingMessageHandler {
             final var blockedListMessage = syncMessage.getBlockedList().get();
             for (var address : blockedListMessage.getAddresses()) {
                 context.getContactHelper()
-                        .setContactBlocked(context.getRecipientHelper().resolveRecipient(address), true);
+                        .setContactBlocked(account.getRecipientResolver().resolveRecipient(address), true);
             }
             for (var groupId : blockedListMessage.getGroupIds()
                     .stream()
@@ -654,7 +649,7 @@ public final class IncomingMessageHandler {
         if (source == null) {
             return false;
         }
-        final var recipientId = context.getRecipientHelper().resolveRecipient(source);
+        final var recipientId = account.getRecipientResolver().resolveRecipient(source);
         if (context.getContactHelper().isContactBlocked(recipientId)) {
             return true;
         }
@@ -694,7 +689,7 @@ public final class IncomingMessageHandler {
 
         final var message = content.getDataMessage().orElse(null);
 
-        final var recipientId = context.getRecipientHelper().resolveRecipient(source);
+        final var recipientId = account.getRecipientResolver().resolveRecipient(source);
         if (!group.isMember(recipientId) && !(
                 group.isPendingMember(recipientId) && message != null && message.isGroupV2Update()
         )) {
@@ -745,10 +740,11 @@ public final class IncomingMessageHandler {
                             }
 
                             if (groupInfo.getMembers().isPresent()) {
+                                final var recipientResolver = account.getRecipientResolver();
                                 groupV1.addMembers(groupInfo.getMembers()
                                         .get()
                                         .stream()
-                                        .map(context.getRecipientHelper()::resolveRecipient)
+                                        .map(recipientResolver::resolveRecipient)
                                         .collect(Collectors.toSet()));
                             }
 
@@ -921,8 +917,9 @@ public final class IncomingMessageHandler {
     }
 
     private SignalServiceAddress getSenderAddress(SignalServiceEnvelope envelope, SignalServiceContent content) {
-        if (!envelope.isUnidentifiedSender() && envelope.hasSourceServiceId()) {
-            return envelope.getSourceAddress();
+        final var serviceId = envelope.getSourceServiceId().map(ServiceId::parseOrNull).orElse(null);
+        if (!envelope.isUnidentifiedSender() && serviceId != null) {
+            return new SignalServiceAddress(serviceId);
         } else if (content != null) {
             return content.getSender();
         } else {
@@ -931,12 +928,13 @@ public final class IncomingMessageHandler {
     }
 
     private DeviceAddress getSender(SignalServiceEnvelope envelope, SignalServiceContent content) {
-        if (!envelope.isUnidentifiedSender() && envelope.hasSourceServiceId()) {
-            return new DeviceAddress(context.getRecipientHelper().resolveRecipient(envelope.getSourceAddress()),
-                    envelope.getSourceAddress().getServiceId(),
+        final var serviceId = envelope.getSourceServiceId().map(ServiceId::parseOrNull).orElse(null);
+        if (!envelope.isUnidentifiedSender() && serviceId != null) {
+            return new DeviceAddress(account.getRecipientResolver().resolveRecipient(serviceId),
+                    serviceId,
                     envelope.getSourceDevice());
         } else {
-            return new DeviceAddress(context.getRecipientHelper().resolveRecipient(content.getSender()),
+            return new DeviceAddress(account.getRecipientResolver().resolveRecipient(content.getSender()),
                     content.getSender().getServiceId(),
                     content.getSenderDevice());
         }
@@ -951,7 +949,7 @@ public final class IncomingMessageHandler {
             return new DeviceAddress(account.getSelfRecipientId(), account.getAci(), account.getDeviceId());
         }
         final var address = addressOptional.get();
-        return new DeviceAddress(context.getRecipientHelper().resolveRecipient(address),
+        return new DeviceAddress(account.getRecipientResolver().resolveRecipient(address),
                 address.getServiceId(),
                 account.getDeviceId());
     }
index d48f125ae8be26bffe026c09730186377d209dbb..713ca5d744aed1af492316610fa75f0abfe99429 100644 (file)
@@ -12,6 +12,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.whispersystems.signalservice.api.SignalWebSocket;
 import org.whispersystems.signalservice.api.messages.SignalServiceEnvelope;
+import org.whispersystems.signalservice.api.push.ServiceId;
 import org.whispersystems.signalservice.api.push.ServiceId.ACI;
 import org.whispersystems.signalservice.api.websocket.WebSocketConnectionState;
 import org.whispersystems.signalservice.api.websocket.WebSocketUnavailableException;
@@ -147,8 +148,10 @@ public class ReceiveHelper {
                     for (final var it : batch) {
                         SignalServiceEnvelope envelope1 = new SignalServiceEnvelope(it.getEnvelope(),
                                 it.getServerDeliveredTimestamp());
-                        final var recipientId = envelope1.hasSourceServiceId() ? account.getRecipientResolver()
-                                .resolveRecipient(envelope1.getSourceAddress()) : null;
+                        final var recipientId = envelope1.getSourceServiceId()
+                                .map(ServiceId::parseOrNull)
+                                .map(s -> account.getRecipientResolver().resolveRecipient(s))
+                                .orElse(null);
                         logger.trace("Storing new message from {}", recipientId);
                         // store message on disk, before acknowledging receipt to the server
                         cachedMessage[0] = account.getMessageCache().cacheMessage(envelope1, recipientId);
@@ -230,7 +233,7 @@ 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.hasSourceServiceId() && address.uuid().isPresent()) {
+                        if (envelope.getSourceServiceId().isEmpty() && address.uuid().isPresent()) {
                             final var recipientId = account.getRecipientResolver()
                                     .resolveRecipient(ACI.from(address.uuid().get()));
                             try {
@@ -282,7 +285,7 @@ public class ReceiveHelper {
                 cachedMessage.delete();
                 return null;
             }
-            if (!envelope.hasSourceServiceId()) {
+            if (envelope.getSourceServiceId().isEmpty()) {
                 final var identifier = ((UntrustedIdentityException) exception).getSender();
                 final var recipientId = account.getRecipientResolver()
                         .resolveRecipient(new RecipientAddress(identifier));
index 6bddde847279dc91d63e4ff02f8366c58eaee3a8..4a0bc9c431df90107137f8e248575bddff982833 100644 (file)
@@ -68,10 +68,6 @@ public class RecipientHelper {
                 .toSignalServiceAddress();
     }
 
-    public RecipientId resolveRecipient(final SignalServiceAddress address) {
-        return account.getRecipientResolver().resolveRecipient(address);
-    }
-
     public Set<RecipientId> resolveRecipients(Collection<RecipientIdentifier.Single> recipients) throws UnregisteredRecipientException {
         final var recipientIds = new HashSet<RecipientId>(recipients.size());
         for (var number : recipients) {
index 5ffb964634fcbdb8573fa822d3a36c20c1e19f0e..80eb4c06e36440fb235c7cc9b8a4219f65b855da 100644 (file)
@@ -475,9 +475,10 @@ public class SendHelper {
                 senderKeyTargets = Set.of();
             } else {
                 results.stream().filter(SendMessageResult::isSuccess).forEach(allResults::add);
+                final var recipientResolver = account.getRecipientResolver();
                 final var failedTargets = results.stream()
                         .filter(r -> !r.isSuccess())
-                        .map(r -> context.getRecipientHelper().resolveRecipient(r.getAddress()))
+                        .map(r -> recipientResolver.resolveRecipient(r.getAddress()))
                         .toList();
                 if (!failedTargets.isEmpty()) {
                     senderKeyTargets = new HashSet<>(senderKeyTargets);
@@ -724,7 +725,7 @@ public class SendHelper {
 
     private void handleSendMessageResult(final SendMessageResult r) {
         if (r.isSuccess() && !r.getSuccess().isUnidentified()) {
-            final var recipientId = context.getRecipientHelper().resolveRecipient(r.getAddress());
+            final var recipientId = account.getRecipientResolver().resolveRecipient(r.getAddress());
             final var profile = account.getProfileStore().getProfile(recipientId);
             if (profile != null && (
                     profile.getUnidentifiedAccessMode() == Profile.UnidentifiedAccessMode.ENABLED
@@ -738,7 +739,7 @@ public class SendHelper {
             }
         }
         if (r.isUnregisteredFailure()) {
-            final var recipientId = context.getRecipientHelper().resolveRecipient(r.getAddress());
+            final var recipientId = account.getRecipientResolver().resolveRecipient(r.getAddress());
             final var profile = account.getProfileStore().getProfile(recipientId);
             if (profile != null && (
                     profile.getUnidentifiedAccessMode() == Profile.UnidentifiedAccessMode.ENABLED
@@ -752,7 +753,7 @@ public class SendHelper {
             }
         }
         if (r.getIdentityFailure() != null) {
-            final var recipientId = context.getRecipientHelper().resolveRecipient(r.getAddress());
+            final var recipientId = account.getRecipientResolver().resolveRecipient(r.getAddress());
             context.getIdentityHelper()
                     .handleIdentityFailure(recipientId, r.getAddress().getServiceId(), r.getIdentityFailure());
         }