]> nmode's Git Repositories - signal-cli/blobdiff - src/main/java/org/asamk/signal/commands/BlockCommand.java
Refactor set blocked methods to accept multiple recipientIds/groupIds
[signal-cli] / src / main / java / org / asamk / signal / commands / BlockCommand.java
index 7d72b1c51ac740500e562f50bf8219731c31228b..8d7f1a8b869a810ed437a01175e726e35bff07cb 100644 (file)
@@ -38,31 +38,27 @@ public class BlockCommand implements JsonRpcLocalCommand {
             final Namespace ns, final Manager m, final OutputWriter outputWriter
     ) throws CommandException {
         final var contacts = ns.<String>getList("recipient");
-        for (var contact : CommandUtil.getSingleRecipientIdentifiers(contacts, m.getSelfNumber())) {
-            try {
-                m.setContactBlocked(contact, true);
-            } catch (NotMasterDeviceException e) {
-                throw new UserErrorException("This command doesn't work on linked devices.");
-            } catch (IOException e) {
-                throw new UnexpectedErrorException("Failed to sync block to linked devices: " + e.getMessage(), e);
-            } catch (UnregisteredRecipientException e) {
-                throw new UserErrorException("The user " + e.getSender().getIdentifier() + " is not registered.");
-            }
+        final var recipients = CommandUtil.getSingleRecipientIdentifiers(contacts, m.getSelfNumber());
+        try {
+            m.setContactsBlocked(recipients, true);
+        } catch (NotMasterDeviceException e) {
+            throw new UserErrorException("This command doesn't work on linked devices.");
+        } catch (IOException e) {
+            throw new UnexpectedErrorException("Failed to sync block to linked devices: " + e.getMessage(), e);
+        } catch (UnregisteredRecipientException e) {
+            throw new UserErrorException("The user " + e.getSender().getIdentifier() + " is not registered.");
         }
 
         final var groupIdStrings = ns.<String>getList("group-id");
-        if (groupIdStrings != null) {
-            for (var groupId : CommandUtil.getGroupIds(groupIdStrings)) {
-                try {
-                    m.setGroupBlocked(groupId, true);
-                } catch (NotMasterDeviceException e) {
-                    throw new UserErrorException("This command doesn't work on linked devices.");
-                } catch (GroupNotFoundException e) {
-                    logger.warn("Group not found {}: {}", groupId.toBase64(), e.getMessage());
-                } catch (IOException e) {
-                    throw new UnexpectedErrorException("Failed to sync block to linked devices: " + e.getMessage(), e);
-                }
-            }
+        final var groupIds = CommandUtil.getGroupIds(groupIdStrings);
+        try {
+            m.setGroupsBlocked(groupIds, true);
+        } catch (NotMasterDeviceException e) {
+            throw new UserErrorException("This command doesn't work on linked devices.");
+        } catch (GroupNotFoundException e) {
+            logger.warn("Unknown group id: {}", e.getMessage());
+        } catch (IOException e) {
+            throw new UnexpectedErrorException("Failed to sync block to linked devices: " + e.getMessage(), e);
         }
     }
 }