]> nmode's Git Repositories - signal-cli/blobdiff - lib/src/main/java/org/asamk/signal/manager/ManagerImpl.java
Remove libsignal-service from manager lib API
[signal-cli] / lib / src / main / java / org / asamk / signal / manager / ManagerImpl.java
index bbef8a069378e20e6d36a7bbc4c32923ba9dbc56..59cfbbaa11c7911cb1af674cd99c174c973a0d6a 100644 (file)
@@ -26,6 +26,7 @@ import org.asamk.signal.manager.api.Message;
 import org.asamk.signal.manager.api.Pair;
 import org.asamk.signal.manager.api.RecipientIdentifier;
 import org.asamk.signal.manager.api.SendGroupMessageResults;
+import org.asamk.signal.manager.api.SendMessageResult;
 import org.asamk.signal.manager.api.SendMessageResults;
 import org.asamk.signal.manager.api.TypingAction;
 import org.asamk.signal.manager.api.UpdateGroup;
@@ -69,14 +70,11 @@ import org.whispersystems.libsignal.InvalidKeyException;
 import org.whispersystems.libsignal.ecc.ECPublicKey;
 import org.whispersystems.libsignal.util.guava.Optional;
 import org.whispersystems.signalservice.api.SignalSessionLock;
-import org.whispersystems.signalservice.api.messages.SendMessageResult;
-import org.whispersystems.signalservice.api.messages.SignalServiceAttachmentRemoteId;
 import org.whispersystems.signalservice.api.messages.SignalServiceDataMessage;
 import org.whispersystems.signalservice.api.messages.SignalServiceEnvelope;
 import org.whispersystems.signalservice.api.messages.SignalServiceReceiptMessage;
 import org.whispersystems.signalservice.api.messages.SignalServiceTypingMessage;
 import org.whispersystems.signalservice.api.push.SignalServiceAddress;
-import org.whispersystems.signalservice.api.push.exceptions.UnregisteredUserException;
 import org.whispersystems.signalservice.api.util.DeviceNameUtil;
 import org.whispersystems.signalservice.api.util.InvalidNumberException;
 import org.whispersystems.signalservice.api.util.PhoneNumberFormatter;
@@ -383,7 +381,7 @@ public class ManagerImpl implements Manager {
     public void deleteAccount() throws IOException {
         try {
             pinHelper.removeRegistrationLockPin();
-        } catch (UnauthenticatedResponseException e) {
+        } catch (IOException e) {
             logger.warn("Failed to remove registration lock pin");
         }
         account.setRegistrationLockPin(null, null);
@@ -454,7 +452,7 @@ public class ManagerImpl implements Manager {
     }
 
     @Override
-    public void setRegistrationLockPin(java.util.Optional<String> pin) throws IOException, UnauthenticatedResponseException {
+    public void setRegistrationLockPin(java.util.Optional<String> pin) throws IOException {
         if (!account.isMasterDevice()) {
             throw new RuntimeException("Only master device can set a PIN");
         }
@@ -479,7 +477,7 @@ public class ManagerImpl implements Manager {
     }
 
     @Override
-    public Profile getRecipientProfile(RecipientIdentifier.Single recipient) throws UnregisteredUserException {
+    public Profile getRecipientProfile(RecipientIdentifier.Single recipient) throws IOException {
         return profileHelper.getRecipientProfile(resolveRecipient(recipient));
     }
 
@@ -583,13 +581,24 @@ public class ManagerImpl implements Manager {
             if (recipient instanceof RecipientIdentifier.Single single) {
                 final var recipientId = resolveRecipient(single);
                 final var result = sendHelper.sendMessage(messageBuilder, recipientId);
-                results.put(recipient, List.of(result));
+                results.put(recipient,
+                        List.of(SendMessageResult.from(result,
+                                account.getRecipientStore(),
+                                account.getRecipientStore()::resolveRecipientAddress)));
             } else if (recipient instanceof RecipientIdentifier.NoteToSelf) {
                 final var result = sendHelper.sendSelfMessage(messageBuilder);
-                results.put(recipient, List.of(result));
+                results.put(recipient,
+                        List.of(SendMessageResult.from(result,
+                                account.getRecipientStore(),
+                                account.getRecipientStore()::resolveRecipientAddress)));
             } else if (recipient instanceof RecipientIdentifier.Group group) {
-                final var result = sendHelper.sendAsGroupMessage(messageBuilder, group.groupId);
-                results.put(recipient, result);
+                final var result = sendHelper.sendAsGroupMessage(messageBuilder, group.groupId());
+                results.put(recipient,
+                        result.stream()
+                                .map(sendMessageResult -> SendMessageResult.from(sendMessageResult,
+                                        account.getRecipientStore(),
+                                        account.getRecipientStore()::resolveRecipientAddress))
+                                .collect(Collectors.toList()));
             }
         }
         return new SendMessageResults(timestamp, results);
@@ -605,7 +614,7 @@ public class ManagerImpl implements Manager {
                 final var recipientId = resolveRecipient((RecipientIdentifier.Single) recipient);
                 sendHelper.sendTypingMessage(message, recipientId);
             } else if (recipient instanceof RecipientIdentifier.Group) {
-                final var groupId = ((RecipientIdentifier.Group) recipient).groupId;
+                final var groupId = ((RecipientIdentifier.Group) recipient).groupId();
                 final var message = new SignalServiceTypingMessage(action, timestamp, Optional.of(groupId.serialize()));
                 sendHelper.sendGroupTypingMessage(message, groupId);
             }
@@ -706,7 +715,7 @@ public class ManagerImpl implements Manager {
     @Override
     public void setContactName(
             RecipientIdentifier.Single recipient, String name
-    ) throws NotMasterDeviceException, UnregisteredUserException {
+    ) throws NotMasterDeviceException, IOException {
         if (!account.isMasterDevice()) {
             throw new NotMasterDeviceException();
         }
@@ -813,11 +822,11 @@ public class ManagerImpl implements Manager {
         try {
             uuidMap = getRegisteredUsers(Set.of(number));
         } catch (NumberFormatException e) {
-            throw new UnregisteredUserException(number, e);
+            throw new IOException(number, e);
         }
         final var uuid = uuidMap.get(number);
         if (uuid == null) {
-            throw new UnregisteredUserException(number, null);
+            throw new IOException(number, null);
         }
         return uuid;
     }
@@ -905,11 +914,11 @@ public class ManagerImpl implements Manager {
         receiveThread = new Thread(() -> {
             while (!Thread.interrupted()) {
                 try {
-                    receiveMessagesInternal(1L, TimeUnit.HOURS, false, (envelope, decryptedContent, e) -> {
+                    receiveMessagesInternal(1L, TimeUnit.HOURS, false, (envelope, e) -> {
                         synchronized (messageHandlers) {
                             for (ReceiveMessageHandler h : messageHandlers) {
                                 try {
-                                    h.handleMessage(envelope, decryptedContent, e);
+                                    h.handleMessage(envelope, e);
                                 } catch (Exception ex) {
                                     logger.warn("Message handler failed, ignoring", ex);
                                 }
@@ -1134,14 +1143,14 @@ public class ManagerImpl implements Manager {
         final RecipientId recipientId;
         try {
             recipientId = resolveRecipient(recipient);
-        } catch (UnregisteredUserException e) {
+        } catch (IOException e) {
             return false;
         }
         return contactHelper.isContactBlocked(recipientId);
     }
 
     @Override
-    public File getAttachmentFile(SignalServiceAttachmentRemoteId attachmentId) {
+    public File getAttachmentFile(String attachmentId) {
         return attachmentHelper.getAttachmentFile(attachmentId);
     }
 
@@ -1164,7 +1173,7 @@ public class ManagerImpl implements Manager {
         final RecipientId recipientId;
         try {
             recipientId = resolveRecipient(recipient);
-        } catch (UnregisteredUserException e) {
+        } catch (IOException e) {
             return null;
         }
 
@@ -1220,7 +1229,7 @@ public class ManagerImpl implements Manager {
         IdentityInfo identity;
         try {
             identity = account.getIdentityKeyStore().getIdentity(resolveRecipient(recipient));
-        } catch (UnregisteredUserException e) {
+        } catch (IOException e) {
             identity = null;
         }
         return identity == null ? List.of() : List.of(toIdentity(identity));
@@ -1237,7 +1246,7 @@ public class ManagerImpl implements Manager {
         RecipientId recipientId;
         try {
             recipientId = resolveRecipient(recipient);
-        } catch (UnregisteredUserException e) {
+        } catch (IOException e) {
             return false;
         }
         return identityHelper.trustIdentityVerified(recipientId, fingerprint);
@@ -1254,7 +1263,7 @@ public class ManagerImpl implements Manager {
         RecipientId recipientId;
         try {
             recipientId = resolveRecipient(recipient);
-        } catch (UnregisteredUserException e) {
+        } catch (IOException e) {
             return false;
         }
         return identityHelper.trustIdentityVerifiedSafetyNumber(recipientId, safetyNumber);
@@ -1271,7 +1280,7 @@ public class ManagerImpl implements Manager {
         RecipientId recipientId;
         try {
             recipientId = resolveRecipient(recipient);
-        } catch (UnregisteredUserException e) {
+        } catch (IOException e) {
             return false;
         }
         return identityHelper.trustIdentityVerifiedSafetyNumber(recipientId, safetyNumber);
@@ -1287,23 +1296,19 @@ public class ManagerImpl implements Manager {
         RecipientId recipientId;
         try {
             recipientId = resolveRecipient(recipient);
-        } catch (UnregisteredUserException e) {
+        } catch (IOException e) {
             return false;
         }
         return identityHelper.trustIdentityAllKeys(recipientId);
     }
 
     private void handleIdentityFailure(
-            final RecipientId recipientId, final SendMessageResult.IdentityFailure identityFailure
+            final RecipientId recipientId,
+            final org.whispersystems.signalservice.api.messages.SendMessageResult.IdentityFailure identityFailure
     ) {
         this.identityHelper.handleIdentityFailure(recipientId, identityFailure);
     }
 
-    @Override
-    public SignalServiceAddress resolveSignalServiceAddress(SignalServiceAddress address) {
-        return resolveSignalServiceAddress(resolveRecipient(address));
-    }
-
     private SignalServiceAddress resolveSignalServiceAddress(RecipientId recipientId) {
         final var address = account.getRecipientStore().resolveRecipientAddress(recipientId);
         if (address.getUuid().isPresent()) {
@@ -1324,7 +1329,7 @@ public class ManagerImpl implements Manager {
         return resolveSignalServiceAddress(account.getRecipientStore().resolveRecipient(uuid));
     }
 
-    private Set<RecipientId> resolveRecipients(Collection<RecipientIdentifier.Single> recipients) throws UnregisteredUserException {
+    private Set<RecipientId> resolveRecipients(Collection<RecipientIdentifier.Single> recipients) throws IOException {
         final var recipientIds = new HashSet<RecipientId>(recipients.size());
         for (var number : recipients) {
             final var recipientId = resolveRecipient(number);
@@ -1333,11 +1338,11 @@ public class ManagerImpl implements Manager {
         return recipientIds;
     }
 
-    private RecipientId resolveRecipient(final RecipientIdentifier.Single recipient) throws UnregisteredUserException {
+    private RecipientId resolveRecipient(final RecipientIdentifier.Single recipient) throws IOException {
         if (recipient instanceof RecipientIdentifier.Uuid) {
-            return account.getRecipientStore().resolveRecipient(((RecipientIdentifier.Uuid) recipient).uuid);
+            return account.getRecipientStore().resolveRecipient(((RecipientIdentifier.Uuid) recipient).uuid());
         } else {
-            final var number = ((RecipientIdentifier.Number) recipient).number;
+            final var number = ((RecipientIdentifier.Number) recipient).number();
             return account.getRecipientStore().resolveRecipient(number, () -> {
                 try {
                     return getRegisteredUser(number);
@@ -1348,6 +1353,10 @@ public class ManagerImpl implements Manager {
         }
     }
 
+    private RecipientId resolveRecipient(RecipientAddress address) {
+        return account.getRecipientStore().resolveRecipient(address);
+    }
+
     private RecipientId resolveRecipient(SignalServiceAddress address) {
         return account.getRecipientStore().resolveRecipient(address);
     }