mirror of
https://github.com/AsamK/signal-cli
synced 2025-08-29 10:30:38 +00:00
Refactor set blocked methods to accept multiple recipientIds/groupIds
This commit is contained in:
parent
2d60f98e93
commit
53f47d42fc
6 changed files with 74 additions and 62 deletions
|
@ -38,6 +38,7 @@ import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
@ -151,12 +152,12 @@ public interface Manager extends Closeable {
|
||||||
RecipientIdentifier.Single recipient, String name
|
RecipientIdentifier.Single recipient, String name
|
||||||
) throws NotMasterDeviceException, IOException, UnregisteredRecipientException;
|
) throws NotMasterDeviceException, IOException, UnregisteredRecipientException;
|
||||||
|
|
||||||
void setContactBlocked(
|
void setContactsBlocked(
|
||||||
RecipientIdentifier.Single recipient, boolean blocked
|
Collection<RecipientIdentifier.Single> recipient, boolean blocked
|
||||||
) throws NotMasterDeviceException, IOException, UnregisteredRecipientException;
|
) throws NotMasterDeviceException, IOException, UnregisteredRecipientException;
|
||||||
|
|
||||||
void setGroupBlocked(
|
void setGroupsBlocked(
|
||||||
GroupId groupId, boolean blocked
|
Collection<GroupId> groupId, boolean blocked
|
||||||
) throws GroupNotFoundException, IOException, NotMasterDeviceException;
|
) throws GroupNotFoundException, IOException, NotMasterDeviceException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -79,6 +79,7 @@ import java.io.IOException;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collection;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -683,25 +684,36 @@ class ManagerImpl implements Manager {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setContactBlocked(
|
public void setContactsBlocked(
|
||||||
RecipientIdentifier.Single recipient, boolean blocked
|
Collection<RecipientIdentifier.Single> recipients, boolean blocked
|
||||||
) throws NotMasterDeviceException, IOException, UnregisteredRecipientException {
|
) throws NotMasterDeviceException, IOException, UnregisteredRecipientException {
|
||||||
if (!account.isMasterDevice()) {
|
if (!account.isMasterDevice()) {
|
||||||
throw new NotMasterDeviceException();
|
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
|
// TODO cycle our profile key, if we're not together in a group with recipient
|
||||||
context.getSyncHelper().sendBlockedList();
|
context.getSyncHelper().sendBlockedList();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setGroupBlocked(
|
public void setGroupsBlocked(
|
||||||
final GroupId groupId, final boolean blocked
|
final Collection<GroupId> groupIds, final boolean blocked
|
||||||
) throws GroupNotFoundException, NotMasterDeviceException {
|
) throws GroupNotFoundException, NotMasterDeviceException {
|
||||||
if (!account.isMasterDevice()) {
|
if (!account.isMasterDevice()) {
|
||||||
throw new NotMasterDeviceException();
|
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
|
// TODO cycle our profile key
|
||||||
context.getSyncHelper().sendBlockedList();
|
context.getSyncHelper().sendBlockedList();
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,31 +38,27 @@ public class BlockCommand implements JsonRpcLocalCommand {
|
||||||
final Namespace ns, final Manager m, final OutputWriter outputWriter
|
final Namespace ns, final Manager m, final OutputWriter outputWriter
|
||||||
) throws CommandException {
|
) throws CommandException {
|
||||||
final var contacts = ns.<String>getList("recipient");
|
final var contacts = ns.<String>getList("recipient");
|
||||||
for (var contact : CommandUtil.getSingleRecipientIdentifiers(contacts, m.getSelfNumber())) {
|
final var recipients = CommandUtil.getSingleRecipientIdentifiers(contacts, m.getSelfNumber());
|
||||||
try {
|
try {
|
||||||
m.setContactBlocked(contact, true);
|
m.setContactsBlocked(recipients, true);
|
||||||
} catch (NotMasterDeviceException e) {
|
} catch (NotMasterDeviceException e) {
|
||||||
throw new UserErrorException("This command doesn't work on linked devices.");
|
throw new UserErrorException("This command doesn't work on linked devices.");
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new UnexpectedErrorException("Failed to sync block to linked devices: " + e.getMessage(), e);
|
throw new UnexpectedErrorException("Failed to sync block to linked devices: " + e.getMessage(), e);
|
||||||
} catch (UnregisteredRecipientException e) {
|
} catch (UnregisteredRecipientException e) {
|
||||||
throw new UserErrorException("The user " + e.getSender().getIdentifier() + " is not registered.");
|
throw new UserErrorException("The user " + e.getSender().getIdentifier() + " is not registered.");
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
final var groupIdStrings = ns.<String>getList("group-id");
|
final var groupIdStrings = ns.<String>getList("group-id");
|
||||||
if (groupIdStrings != null) {
|
final var groupIds = CommandUtil.getGroupIds(groupIdStrings);
|
||||||
for (var groupId : CommandUtil.getGroupIds(groupIdStrings)) {
|
try {
|
||||||
try {
|
m.setGroupsBlocked(groupIds, true);
|
||||||
m.setGroupBlocked(groupId, true);
|
} catch (NotMasterDeviceException e) {
|
||||||
} catch (NotMasterDeviceException e) {
|
throw new UserErrorException("This command doesn't work on linked devices.");
|
||||||
throw new UserErrorException("This command doesn't work on linked devices.");
|
} catch (GroupNotFoundException e) {
|
||||||
} catch (GroupNotFoundException e) {
|
logger.warn("Unknown group id: {}", e.getMessage());
|
||||||
logger.warn("Group not found {}: {}", groupId.toBase64(), e.getMessage());
|
} catch (IOException e) {
|
||||||
} catch (IOException e) {
|
throw new UnexpectedErrorException("Failed to sync block to linked devices: " + e.getMessage(), e);
|
||||||
throw new UnexpectedErrorException("Failed to sync block to linked devices: " + e.getMessage(), e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,30 +37,28 @@ public class UnblockCommand implements JsonRpcLocalCommand {
|
||||||
public void handleCommand(
|
public void handleCommand(
|
||||||
final Namespace ns, final Manager m, final OutputWriter outputWriter
|
final Namespace ns, final Manager m, final OutputWriter outputWriter
|
||||||
) throws CommandException {
|
) throws CommandException {
|
||||||
for (var contactNumber : CommandUtil.getSingleRecipientIdentifiers(ns.getList("recipient"),
|
final var contacts = ns.<String>getList("recipient");
|
||||||
m.getSelfNumber())) {
|
final var recipients = CommandUtil.getSingleRecipientIdentifiers(contacts, m.getSelfNumber());
|
||||||
try {
|
try {
|
||||||
m.setContactBlocked(contactNumber, false);
|
m.setContactsBlocked(recipients, false);
|
||||||
} catch (NotMasterDeviceException e) {
|
} catch (NotMasterDeviceException e) {
|
||||||
throw new UserErrorException("This command doesn't work on linked devices.");
|
throw new UserErrorException("This command doesn't work on linked devices.");
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new UnexpectedErrorException("Failed to sync unblock to linked devices: " + e.getMessage(), e);
|
throw new UnexpectedErrorException("Failed to sync unblock to linked devices: " + e.getMessage(), e);
|
||||||
} catch (UnregisteredRecipientException e) {
|
} catch (UnregisteredRecipientException e) {
|
||||||
throw new UserErrorException("The user " + e.getSender().getIdentifier() + " is not registered.");
|
throw new UserErrorException("The user " + e.getSender().getIdentifier() + " is not registered.");
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
final var groupIdStrings = ns.<String>getList("group-id");
|
final var groupIdStrings = ns.<String>getList("group-id");
|
||||||
for (var groupId : CommandUtil.getGroupIds(groupIdStrings)) {
|
final var groupIds = CommandUtil.getGroupIds(groupIdStrings);
|
||||||
try {
|
try {
|
||||||
m.setGroupBlocked(groupId, false);
|
m.setGroupsBlocked(groupIds, false);
|
||||||
} catch (NotMasterDeviceException e) {
|
} catch (NotMasterDeviceException e) {
|
||||||
throw new UserErrorException("This command doesn't work on linked devices.");
|
throw new UserErrorException("This command doesn't work on linked devices.");
|
||||||
} catch (GroupNotFoundException e) {
|
} catch (GroupNotFoundException e) {
|
||||||
logger.warn("Unknown group id: {}", groupId);
|
logger.warn("Unknown group id: {}", e.getMessage());
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new UnexpectedErrorException("Failed to sync unblock to linked devices: " + e.getMessage(), e);
|
throw new UnexpectedErrorException("Failed to sync unblock to linked devices: " + e.getMessage(), e);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -47,6 +47,7 @@ import java.net.URI;
|
||||||
import java.net.URISyntaxException;
|
import java.net.URISyntaxException;
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collection;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -410,17 +411,21 @@ public class DbusManagerImpl implements Manager {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setContactBlocked(
|
public void setContactsBlocked(
|
||||||
final RecipientIdentifier.Single recipient, final boolean blocked
|
final Collection<RecipientIdentifier.Single> recipients, final boolean blocked
|
||||||
) throws NotMasterDeviceException, IOException {
|
) throws NotMasterDeviceException, IOException {
|
||||||
signal.setContactBlocked(recipient.getIdentifier(), blocked);
|
for (final var recipient : recipients) {
|
||||||
|
signal.setContactBlocked(recipient.getIdentifier(), blocked);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setGroupBlocked(
|
public void setGroupsBlocked(
|
||||||
final GroupId groupId, final boolean blocked
|
final Collection<GroupId> groupIds, final boolean blocked
|
||||||
) throws GroupNotFoundException, IOException {
|
) 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) {
|
private void setGroupProperty(final GroupId groupId, final String propertyName, final boolean blocked) {
|
||||||
|
|
|
@ -516,7 +516,7 @@ public class DbusSignalImpl implements Signal {
|
||||||
@Override
|
@Override
|
||||||
public void setContactBlocked(final String number, final boolean blocked) {
|
public void setContactBlocked(final String number, final boolean blocked) {
|
||||||
try {
|
try {
|
||||||
m.setContactBlocked(getSingleRecipientIdentifier(number, m.getSelfNumber()), blocked);
|
m.setContactsBlocked(List.of(getSingleRecipientIdentifier(number, m.getSelfNumber())), blocked);
|
||||||
} catch (NotMasterDeviceException e) {
|
} catch (NotMasterDeviceException e) {
|
||||||
throw new Error.Failure("This command doesn't work on linked devices.");
|
throw new Error.Failure("This command doesn't work on linked devices.");
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
@ -529,7 +529,7 @@ public class DbusSignalImpl implements Signal {
|
||||||
@Override
|
@Override
|
||||||
public void setGroupBlocked(final byte[] groupId, final boolean blocked) {
|
public void setGroupBlocked(final byte[] groupId, final boolean blocked) {
|
||||||
try {
|
try {
|
||||||
m.setGroupBlocked(getGroupId(groupId), blocked);
|
m.setGroupsBlocked(List.of(getGroupId(groupId)), blocked);
|
||||||
} catch (NotMasterDeviceException e) {
|
} catch (NotMasterDeviceException e) {
|
||||||
throw new Error.Failure("This command doesn't work on linked devices.");
|
throw new Error.Failure("This command doesn't work on linked devices.");
|
||||||
} catch (GroupNotFoundException e) {
|
} catch (GroupNotFoundException e) {
|
||||||
|
@ -1287,7 +1287,7 @@ public class DbusSignalImpl implements Signal {
|
||||||
|
|
||||||
private void setIsBlocked(final boolean isBlocked) {
|
private void setIsBlocked(final boolean isBlocked) {
|
||||||
try {
|
try {
|
||||||
m.setGroupBlocked(groupId, isBlocked);
|
m.setGroupsBlocked(List.of(groupId), isBlocked);
|
||||||
} catch (NotMasterDeviceException e) {
|
} catch (NotMasterDeviceException e) {
|
||||||
throw new Error.Failure("This command doesn't work on linked devices.");
|
throw new Error.Failure("This command doesn't work on linked devices.");
|
||||||
} catch (GroupNotFoundException e) {
|
} catch (GroupNotFoundException e) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue