diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/workers/UnlinkDeviceCommand.java b/service/src/main/java/org/whispersystems/textsecuregcm/workers/UnlinkDeviceCommand.java index 288df0905553a751e2143e59d1ea89bd63d947e0..045465cb1587849c66102c64b327b64298d812ef 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/workers/UnlinkDeviceCommand.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/workers/UnlinkDeviceCommand.java @@ -9,7 +9,10 @@ import com.fasterxml.jackson.databind.DeserializationFeature; import io.dropwizard.Application; import io.dropwizard.cli.EnvironmentCommand; import io.dropwizard.setup.Environment; + +import java.util.List; import java.util.UUID; +import net.sourceforge.argparse4j.impl.Arguments; import net.sourceforge.argparse4j.inf.Namespace; import net.sourceforge.argparse4j.inf.Subparser; import org.whispersystems.textsecuregcm.WhisperServerConfiguration; @@ -32,8 +35,9 @@ public class UnlinkDeviceCommand extends EnvironmentCommand<WhisperServerConfigu super.configure(subparser); subparser.addArgument("-d", "--deviceId") - .dest("deviceId") + .dest("deviceIds") .type(Long.class) + .action(Arguments.append()) .required(true); subparser.addArgument("-u", "--uuid") @@ -48,33 +52,40 @@ public class UnlinkDeviceCommand extends EnvironmentCommand<WhisperServerConfigu final WhisperServerConfiguration configuration) throws Exception { environment.getObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); - final UUID aci = UUID.fromString(namespace.getString("uuid").trim()); - final long deviceId = namespace.getLong("deviceId"); - - final CommandDependencies deps = CommandDependencies.build("unlink-device", environment, configuration); + final CommandStopListener commandStopListener = new CommandStopListener(configuration.getCommandStopListener()); + try { + commandStopListener.start(); - Account account = deps.accountsManager().getByAccountIdentifier(aci) - .orElseThrow(() -> new IllegalArgumentException("account id " + aci +" does not exist")); + final UUID aci = UUID.fromString(namespace.getString("uuid").trim()); + final List<Long> deviceIds = namespace.getList("deviceIds"); - if (deviceId == Device.MASTER_ID) { - throw new IllegalArgumentException("cannot delete primary device"); - } + final CommandDependencies deps = CommandDependencies.build("unlink-device", environment, configuration); - /** see {@link org.whispersystems.textsecuregcm.controllers.DeviceController#removeDevice} */ - System.out.format("Removing device %s::%d\n", aci, deviceId); - account = deps.accountsManager().update(account, a -> a.removeDevice(deviceId)); + Account account = deps.accountsManager().getByAccountIdentifier(aci) + .orElseThrow(() -> new IllegalArgumentException("account id " + aci + " does not exist")); - System.out.format("Removing keys for device %s::%d\n", aci, deviceId); - deps.keysManager().delete(account.getUuid(), deviceId).join(); + if (deviceIds.contains(Device.MASTER_ID)) { + throw new IllegalArgumentException("cannot delete primary device"); + } - System.out.format("Clearing additional messages for %s::%d\n", aci, deviceId); - deps.messagesManager().clear(account.getUuid(), deviceId).join(); + for (long deviceId : deviceIds) { + /** see {@link org.whispersystems.textsecuregcm.controllers.DeviceController#removeDevice} */ + System.out.format("Removing device %s::%d\n", aci, deviceId); + account = deps.accountsManager().update(account, a -> a.removeDevice(deviceId)); - System.out.format("Clearing presence state for %s::%d\n", aci, deviceId); - deps.clientPresenceManager().disconnectPresence(aci, deviceId); + System.out.format("Removing keys for device %s::%d\n", aci, deviceId); + deps.keysManager().delete(account.getUuid(), deviceId).join(); - System.out.format("Device %s::%d successfully removed\n", aci, deviceId); + System.out.format("Clearing additional messages for %s::%d\n", aci, deviceId); + deps.messagesManager().clear(account.getUuid(), deviceId).join(); + System.out.format("Clearing presence state for %s::%d\n", aci, deviceId); + deps.clientPresenceManager().disconnectPresence(aci, deviceId); + System.out.format("Device %s::%d successfully removed\n", aci, deviceId); + } + } finally { + commandStopListener.stop(); + } } }