]> nmode's Git Repositories - signal-cli/blobdiff - src/main/java/org/asamk/signal/manager/Manager.java
Canonicalize number before getting/setting contact info
[signal-cli] / src / main / java / org / asamk / signal / manager / Manager.java
index 5c6a210b1dc05ce227ac3b5ca345d5a22c4930f3..50da5f23f748c725e6764d5b7e5357eb6273b5da 100644 (file)
@@ -647,7 +647,20 @@ public class Manager implements Signal {
             throws IOException, EncapsulatedExceptions, AttachmentInvalidException {
         final SignalServiceDataMessage.Builder messageBuilder = SignalServiceDataMessage.newBuilder().withBody(messageText);
         if (attachments != null) {
-            messageBuilder.withAttachments(Utils.getSignalServiceAttachments(attachments));
+            List<SignalServiceAttachment> attachmentStreams = Utils.getSignalServiceAttachments(attachments);
+
+            // Upload attachments here, so we only upload once even for multiple recipients
+            SignalServiceMessageSender messageSender = getMessageSender();
+            List<SignalServiceAttachment> attachmentPointers = new ArrayList<>(attachmentStreams.size());
+            for (SignalServiceAttachment attachment : attachmentStreams) {
+                if (attachment.isStream()) {
+                    attachmentPointers.add(messageSender.uploadAttachment(attachment.asStream()));
+                } else if (attachment.isPointer()) {
+                    attachmentPointers.add(attachment.asPointer());
+                }
+            }
+
+            messageBuilder.withAttachments(attachmentPointers);
         }
         messageBuilder.withProfileKey(account.getProfileKey());
         sendMessageLegacy(messageBuilder, recipients);
@@ -662,8 +675,9 @@ public class Manager implements Signal {
     }
 
     @Override
-    public String getContactName(String number) {
-        ContactInfo contact = account.getContactStore().getContact(number);
+    public String getContactName(String number) throws InvalidNumberException {
+        String canonicalizedNumber = Utils.canonicalizeNumber(number, username);
+        ContactInfo contact = account.getContactStore().getContact(canonicalizedNumber);
         if (contact == null) {
             return "";
         } else {
@@ -672,14 +686,15 @@ public class Manager implements Signal {
     }
 
     @Override
-    public void setContactName(String number, String name) {
-        ContactInfo contact = account.getContactStore().getContact(number);
+    public void setContactName(String number, String name) throws InvalidNumberException {
+        String canonicalizedNumber = Utils.canonicalizeNumber(number, username);
+        ContactInfo contact = account.getContactStore().getContact(canonicalizedNumber);
         if (contact == null) {
             contact = new ContactInfo();
-            contact.number = number;
-            System.err.println("Add contact " + number + " named " + name);
+            contact.number = canonicalizedNumber;
+            System.err.println("Add contact " + canonicalizedNumber + " named " + name);
         } else {
-            System.err.println("Updating contact " + number + " name " + contact.name + " -> " + name);
+            System.err.println("Updating contact " + canonicalizedNumber + " name " + contact.name + " -> " + name);
         }
         contact.name = name;
         account.getContactStore().updateContact(contact);