]> nmode's Git Repositories - signal-cli/commitdiff
Refactor set blocked methods to accept multiple recipientIds/groupIds
authorAsamK <asamk@gmx.de>
Wed, 18 May 2022 09:09:05 +0000 (11:09 +0200)
committerAsamK <asamk@gmx.de>
Wed, 18 May 2022 09:09:05 +0000 (11:09 +0200)
lib/src/main/java/org/asamk/signal/manager/Manager.java
lib/src/main/java/org/asamk/signal/manager/ManagerImpl.java
src/main/java/org/asamk/signal/commands/BlockCommand.java
src/main/java/org/asamk/signal/commands/UnblockCommand.java
src/main/java/org/asamk/signal/dbus/DbusManagerImpl.java
src/main/java/org/asamk/signal/dbus/DbusSignalImpl.java

index d14b0f44752b4df8b8f9e994ec8c7a0f4b54772f..8f49126fc724c7754cef07e9736bed6702371581 100644 (file)
@@ -38,6 +38,7 @@ import java.io.File;
 import java.io.IOException;
 import java.net.URI;
 import java.time.Duration;
+import java.util.Collection;
 import java.util.List;
 import java.util.Map;
 import java.util.Optional;
@@ -151,12 +152,12 @@ public interface Manager extends Closeable {
             RecipientIdentifier.Single recipient, String name
     ) throws NotMasterDeviceException, IOException, UnregisteredRecipientException;
 
-    void setContactBlocked(
-            RecipientIdentifier.Single recipient, boolean blocked
+    void setContactsBlocked(
+            Collection<RecipientIdentifier.Single> recipient, boolean blocked
     ) throws NotMasterDeviceException, IOException, UnregisteredRecipientException;
 
-    void setGroupBlocked(
-            GroupId groupId, boolean blocked
+    void setGroupsBlocked(
+            Collection<GroupId> groupId, boolean blocked
     ) throws GroupNotFoundException, IOException, NotMasterDeviceException;
 
     /**
index db8744cd3b5b89711bc4906fe57c8eb6cf49a154..f6205dba7c7c59b0fb3ae83644a5f48c01f35bbb 100644 (file)
@@ -79,6 +79,7 @@ import java.io.IOException;
 import java.net.URI;
 import java.time.Duration;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
@@ -683,25 +684,36 @@ class ManagerImpl implements Manager {
     }
 
     @Override
-    public void setContactBlocked(
-            RecipientIdentifier.Single recipient, boolean blocked
+    public void setContactsBlocked(
+            Collection<RecipientIdentifier.Single> recipients, boolean blocked
     ) throws NotMasterDeviceException, IOException, UnregisteredRecipientException {
         if (!account.isMasterDevice()) {
             throw new NotMasterDeviceException();
         }
-        context.getContactHelper().setContactBlocked(context.getRecipientHelper().resolveRecipient(recipient), blocked);
+        if (recipients.size() == 0) {
+            return;
+        }
+        final var recipientIds = context.getRecipientHelper().resolveRecipients(recipients);
+        for (final var recipientId : recipientIds) {
+            context.getContactHelper().setContactBlocked(recipientId, blocked);
+        }
         // TODO cycle our profile key, if we're not together in a group with recipient
         context.getSyncHelper().sendBlockedList();
     }
 
     @Override
-    public void setGroupBlocked(
-            final GroupId groupId, final boolean blocked
+    public void setGroupsBlocked(
+            final Collection<GroupId> groupIds, final boolean blocked
     ) throws GroupNotFoundException, NotMasterDeviceException {
         if (!account.isMasterDevice()) {
             throw new NotMasterDeviceException();
         }
-        context.getGroupHelper().setGroupBlocked(groupId, blocked);
+        if (groupIds.size() == 0) {
+            return;
+        }
+        for (final var groupId : groupIds) {
+            context.getGroupHelper().setGroupBlocked(groupId, blocked);
+        }
         // TODO cycle our profile key
         context.getSyncHelper().sendBlockedList();
     }
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);
         }
     }
 }
index af49236cdbd7fae84142165f4ed13e16e565572b..4cfe46478000861cb2ba32f7d6eed88d3ebaad4b 100644 (file)
@@ -37,30 +37,28 @@ public class UnblockCommand implements JsonRpcLocalCommand {
     public void handleCommand(
             final Namespace ns, final Manager m, final OutputWriter outputWriter
     ) throws CommandException {
-        for (var contactNumber : CommandUtil.getSingleRecipientIdentifiers(ns.getList("recipient"),
-                m.getSelfNumber())) {
-            try {
-                m.setContactBlocked(contactNumber, false);
-            } catch (NotMasterDeviceException e) {
-                throw new UserErrorException("This command doesn't work on linked devices.");
-            } catch (IOException e) {
-                throw new UnexpectedErrorException("Failed to sync unblock to linked devices: " + e.getMessage(), e);
-            } catch (UnregisteredRecipientException e) {
-                throw new UserErrorException("The user " + e.getSender().getIdentifier() + " is not registered.");
-            }
+        final var contacts = ns.<String>getList("recipient");
+        final var recipients = CommandUtil.getSingleRecipientIdentifiers(contacts, m.getSelfNumber());
+        try {
+            m.setContactsBlocked(recipients, false);
+        } catch (NotMasterDeviceException e) {
+            throw new UserErrorException("This command doesn't work on linked devices.");
+        } catch (IOException e) {
+            throw new UnexpectedErrorException("Failed to sync unblock 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");
-        for (var groupId : CommandUtil.getGroupIds(groupIdStrings)) {
-            try {
-                m.setGroupBlocked(groupId, false);
-            } catch (NotMasterDeviceException e) {
-                throw new UserErrorException("This command doesn't work on linked devices.");
-            } catch (GroupNotFoundException e) {
-                logger.warn("Unknown group id: {}", groupId);
-            } catch (IOException e) {
-                throw new UnexpectedErrorException("Failed to sync unblock to linked devices: " + e.getMessage(), e);
-            }
+        final var groupIds = CommandUtil.getGroupIds(groupIdStrings);
+        try {
+            m.setGroupsBlocked(groupIds, false);
+        } 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 unblock to linked devices: " + e.getMessage(), e);
         }
     }
 }
index 05ba6b9983d943a2f3d5bf3df496b849e8683483..29889c3d32fc5106a96b38629fda21e716bc5aaf 100644 (file)
@@ -47,6 +47,7 @@ import java.net.URI;
 import java.net.URISyntaxException;
 import java.time.Duration;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
@@ -410,17 +411,21 @@ public class DbusManagerImpl implements Manager {
     }
 
     @Override
-    public void setContactBlocked(
-            final RecipientIdentifier.Single recipient, final boolean blocked
+    public void setContactsBlocked(
+            final Collection<RecipientIdentifier.Single> recipients, final boolean blocked
     ) throws NotMasterDeviceException, IOException {
-        signal.setContactBlocked(recipient.getIdentifier(), blocked);
+        for (final var recipient : recipients) {
+            signal.setContactBlocked(recipient.getIdentifier(), blocked);
+        }
     }
 
     @Override
-    public void setGroupBlocked(
-            final GroupId groupId, final boolean blocked
+    public void setGroupsBlocked(
+            final Collection<GroupId> groupIds, final boolean blocked
     ) throws GroupNotFoundException, IOException {
-        setGroupProperty(groupId, "IsBlocked", blocked);
+        for (final var groupId : groupIds) {
+            setGroupProperty(groupId, "IsBlocked", blocked);
+        }
     }
 
     private void setGroupProperty(final GroupId groupId, final String propertyName, final boolean blocked) {
index 746f8f52fe32a7b254d6b1524a2c8065558d76b7..b4485e3293bfa7523fbcc181d713e4df8917f851 100644 (file)
@@ -516,7 +516,7 @@ public class DbusSignalImpl implements Signal {
     @Override
     public void setContactBlocked(final String number, final boolean blocked) {
         try {
-            m.setContactBlocked(getSingleRecipientIdentifier(number, m.getSelfNumber()), blocked);
+            m.setContactsBlocked(List.of(getSingleRecipientIdentifier(number, m.getSelfNumber())), blocked);
         } catch (NotMasterDeviceException e) {
             throw new Error.Failure("This command doesn't work on linked devices.");
         } catch (IOException e) {
@@ -529,7 +529,7 @@ public class DbusSignalImpl implements Signal {
     @Override
     public void setGroupBlocked(final byte[] groupId, final boolean blocked) {
         try {
-            m.setGroupBlocked(getGroupId(groupId), blocked);
+            m.setGroupsBlocked(List.of(getGroupId(groupId)), blocked);
         } catch (NotMasterDeviceException e) {
             throw new Error.Failure("This command doesn't work on linked devices.");
         } catch (GroupNotFoundException e) {
@@ -1287,7 +1287,7 @@ public class DbusSignalImpl implements Signal {
 
         private void setIsBlocked(final boolean isBlocked) {
             try {
-                m.setGroupBlocked(groupId, isBlocked);
+                m.setGroupsBlocked(List.of(groupId), isBlocked);
             } catch (NotMasterDeviceException e) {
                 throw new Error.Failure("This command doesn't work on linked devices.");
             } catch (GroupNotFoundException e) {