]> nmode's Git Repositories - signal-cli/blobdiff - src/main/java/org/asamk/signal/dbus/DbusSignalImpl.java
dbus implementation of sendReaction command (#581)
[signal-cli] / src / main / java / org / asamk / signal / dbus / DbusSignalImpl.java
index 552c80ea55f1c755e842d4a51c8e7ff0c542d698..9bc4b67f9a9ba68133c005fff47b457cd5014d89 100644 (file)
@@ -8,8 +8,6 @@ import org.asamk.signal.manager.groups.GroupId;
 import org.asamk.signal.manager.groups.GroupInviteLinkUrl;
 import org.asamk.signal.manager.groups.GroupNotFoundException;
 import org.asamk.signal.manager.groups.NotAGroupMemberException;
-import org.asamk.signal.manager.storage.protocol.IdentityInfo;
-import org.asamk.signal.manager.util.Utils;
 import org.asamk.signal.util.ErrorUtils;
 import org.freedesktop.dbus.exceptions.DBusExecutionException;
 import org.whispersystems.libsignal.util.guava.Optional;
@@ -105,6 +103,30 @@ public class DbusSignalImpl implements Signal {
         }
     }
 
+    @Override
+    public long sendMessageReaction(
+            final String emoji, final boolean remove, final String targetAuthor, final long targetSentTimestamp, final String recipient
+    ) {
+        var recipients = new ArrayList<String>(1);
+        recipients.add(recipient);
+        return sendMessageReaction(emoji, remove, targetAuthor, targetSentTimestamp, recipients);
+    }
+
+    @Override
+    public long sendMessageReaction(
+            final String emoji, final boolean remove, final String targetAuthor, final long targetSentTimestamp, final List<String> recipients
+    ) {
+        try {
+            final var results = m.sendMessageReaction(emoji, remove, targetAuthor, targetSentTimestamp, recipients);
+            checkSendMessageResults(results.first(), results.second());
+            return results.first();
+        } catch (InvalidNumberException e) {
+            throw new Error.InvalidNumber(e.getMessage());
+        } catch (IOException e) {
+            throw new Error.Failure(e.getMessage());
+        }
+    }
+
     @Override
     public long sendNoteToSelfMessage(
             final String message, final List<String> attachments
@@ -147,17 +169,32 @@ public class DbusSignalImpl implements Signal {
         }
     }
 
+    @Override
+    public long sendGroupMessageReaction(
+            final String emoji, final boolean remove, final String targetAuthor, final long targetSentTimestamp, final byte[] groupId
+    ) {
+        try {
+            final var results = m.sendGroupMessageReaction(emoji, remove, targetAuthor, targetSentTimestamp, GroupId.unknownVersion(groupId));
+            checkSendMessageResults(results.first(), results.second());
+            return results.first();
+        } catch (IOException e) {
+            throw new Error.Failure(e.getMessage());
+        } catch (InvalidNumberException e) {
+            throw new Error.InvalidNumber(e.getMessage());
+        } catch (GroupNotFoundException | NotAGroupMemberException e) {
+            throw new Error.GroupNotFound(e.getMessage());
+        }
+    }
+
     // Since contact names might be empty if not defined, also potentially return
     // the profile name
     @Override
     public String getContactName(final String number) {
-        String name = "";
         try {
-            name = m.getContactOrProfileName(number);
+            return m.getContactOrProfileName(number);
         } catch (Exception e) {
             throw new Error.InvalidNumber(e.getMessage());
         }
-        return name;
     }
 
     @Override
@@ -299,8 +336,8 @@ public class DbusSignalImpl implements Signal {
 
     @Override
     public List<String> getContactNumber(final String name) {
-        // Contact names have precendence.
-        List<String> numbers = new ArrayList<>();
+        // Contact names have precedence.
+        var numbers = new ArrayList<String>();
         var contacts = m.getContacts();
         for (var c : contacts) {
             if (c.name != null && c.name.equals(name)) {
@@ -308,20 +345,16 @@ public class DbusSignalImpl implements Signal {
             }
         }
         // Try profiles if no contact name was found
-        for (IdentityInfo identity : m.getIdentities()) {
-            String number = identity.getAddress().getNumber().orNull();
+        for (var identity : m.getIdentities()) {
+            final var address = identity.getAddress();
+            var number = address.getNumber().orNull();
             if (number != null) {
-                var address = Utils.getSignalServiceAddressFromIdentifier(number);
                 var profile = m.getRecipientProfile(address);
-                String profileName = profile.getDisplayName();
-                if (profileName.equals(name)) {
+                if (profile != null && profile.getDisplayName().equals(name)) {
                     numbers.add(number);
                 }
             }
         }
-        if (numbers.size() == 0) {
-            throw new Error.Failure("Contact name not found");
-        }
         return numbers;
     }
 
@@ -339,9 +372,11 @@ public class DbusSignalImpl implements Signal {
 
     @Override
     public void joinGroup(final String groupLink) {
-        final GroupInviteLinkUrl linkUrl;
         try {
-            linkUrl = GroupInviteLinkUrl.fromUri(groupLink);
+            final var linkUrl = GroupInviteLinkUrl.fromUri(groupLink);
+            if (linkUrl == null) {
+                throw new Error.Failure("Group link is invalid:");
+            }
             m.joinGroup(linkUrl);
         } catch (GroupInviteLinkUrl.InvalidGroupLinkException | GroupLinkNotActiveException e) {
             throw new Error.Failure("Group link is invalid: " + e.getMessage());
@@ -382,5 +417,4 @@ public class DbusSignalImpl implements Signal {
             return group.isMember(m.getSelfAddress());
         }
     }
-
 }