]> nmode's Git Repositories - signal-cli/blobdiff - src/main/java/org/asamk/signal/commands/SendTypingCommand.java
Add RecipientIdentifier as external Manager interface
[signal-cli] / src / main / java / org / asamk / signal / commands / SendTypingCommand.java
index d5a686041c16118c18026d49f9b3a8af86c5d33e..14139885aae426396c3cc82930b52cdf2a7cea69 100644 (file)
@@ -8,14 +8,12 @@ import org.asamk.signal.OutputWriter;
 import org.asamk.signal.commands.exceptions.CommandException;
 import org.asamk.signal.commands.exceptions.UserErrorException;
 import org.asamk.signal.manager.Manager;
+import org.asamk.signal.manager.api.RecipientIdentifier;
 import org.asamk.signal.manager.api.TypingAction;
-import org.asamk.signal.manager.groups.GroupId;
-import org.asamk.signal.manager.groups.GroupIdFormatException;
 import org.asamk.signal.manager.groups.GroupNotFoundException;
 import org.asamk.signal.manager.groups.NotAGroupMemberException;
-import org.asamk.signal.util.Util;
+import org.asamk.signal.util.CommandUtil;
 import org.whispersystems.signalservice.api.crypto.UntrustedIdentityException;
-import org.whispersystems.signalservice.api.util.InvalidNumberException;
 
 import java.io.IOException;
 import java.util.HashSet;
@@ -31,7 +29,7 @@ public class SendTypingCommand implements JsonRpcLocalCommand {
     public void attachToSubparser(final Subparser subparser) {
         subparser.help(
                 "Send typing message to trigger a typing indicator for the recipient. Indicator will be shown for 15seconds unless a typing STOP message is sent first.");
-        subparser.addArgument("-g", "--group-id", "--group").help("Specify the recipient group ID.");
+        subparser.addArgument("-g", "--group-id", "--group").help("Specify the recipient group ID.").nargs("*");
         subparser.addArgument("recipient").help("Specify the recipients' phone number.").nargs("*");
         subparser.addArgument("-s", "--stop").help("Send a typing STOP message.").action(Arguments.storeTrue());
     }
@@ -40,40 +38,29 @@ public class SendTypingCommand implements JsonRpcLocalCommand {
     public void handleCommand(
             final Namespace ns, final Manager m, final OutputWriter outputWriter
     ) throws CommandException {
-        final var recipients = ns.<String>getList("recipient");
-        final var groupIdString = ns.getString("group-id");
+        final var recipientStrings = ns.<String>getList("recipient");
+        final var groupIdStrings = ns.<String>getList("group-id");
+        final var action = ns.getBoolean("stop") ? TypingAction.STOP : TypingAction.START;
 
-        final var noRecipients = recipients == null || recipients.isEmpty();
-        if (noRecipients && groupIdString == null) {
-            throw new UserErrorException("No recipients given");
+        final var recipientIdentifiers = new HashSet<RecipientIdentifier>();
+        if (recipientStrings != null) {
+            final var localNumber = m.getUsername();
+            recipientIdentifiers.addAll(CommandUtil.getSingleRecipientIdentifiers(recipientStrings, localNumber));
         }
-        if (!noRecipients && groupIdString != null) {
-            throw new UserErrorException("You cannot specify recipients by phone number and groups at the same time");
+        if (groupIdStrings != null) {
+            recipientIdentifiers.addAll(CommandUtil.getGroupIdentifiers(groupIdStrings));
         }
 
-        final var action = ns.getBoolean("stop") ? TypingAction.STOP : TypingAction.START;
-
-        GroupId groupId = null;
-        if (groupIdString != null) {
-            try {
-                groupId = Util.decodeGroupId(groupIdString);
-            } catch (GroupIdFormatException e) {
-                throw new UserErrorException("Invalid group id: " + e.getMessage());
-            }
+        if (recipientIdentifiers.isEmpty()) {
+            throw new UserErrorException("No recipients given");
         }
 
         try {
-            if (groupId != null) {
-                m.sendGroupTypingMessage(action, groupId);
-            } else {
-                m.sendTypingMessage(action, new HashSet<>(recipients));
-            }
+            m.sendTypingMessage(action, recipientIdentifiers);
         } catch (IOException | UntrustedIdentityException e) {
             throw new UserErrorException("Failed to send message: " + e.getMessage());
         } catch (GroupNotFoundException | NotAGroupMemberException e) {
             throw new UserErrorException("Failed to send to group: " + e.getMessage());
-        } catch (InvalidNumberException e) {
-            throw new UserErrorException("Invalid number: " + e.getMessage());
         }
     }
 }