- throw new UnexpectedErrorException("Failed to send message: " + e.getMessage());
- }
- }
-
- @Override
- public void handleCommand(
- final Namespace ns, final Signal signal, final OutputWriter outputWriter
- ) throws CommandException {
- final var recipients = ns.<String>getList("recipient");
- final var groupIdStrings = ns.<String>getList("group-id");
-
- final var noRecipients = recipients == null || recipients.isEmpty();
- final var noGroups = groupIdStrings == null || groupIdStrings.isEmpty();
- if (noRecipients && noGroups) {
- throw new UserErrorException("No recipients given");
- }
- if (!noRecipients && !noGroups) {
- throw new UserErrorException("You cannot specify recipients by phone number and groups at the same time");
- }
-
- final long targetTimestamp = ns.getLong("target-timestamp");
-
- try {
- long timestamp = 0;
- if (!noGroups) {
- final var groupIds = CommandUtil.getGroupIds(groupIdStrings);
- for (final var groupId : groupIds) {
- timestamp = signal.sendGroupRemoteDeleteMessage(targetTimestamp, groupId.serialize());
- }
- } else {
- timestamp = signal.sendRemoteDeleteMessage(targetTimestamp, recipients);
- }
- outputResult(outputWriter, timestamp);
- } catch (UnknownObject e) {
- throw new UserErrorException("Failed to find dbus object, maybe missing the -u flag: " + e.getMessage());
- } catch (Signal.Error.InvalidNumber e) {
- throw new UserErrorException("Invalid number: " + e.getMessage());
- } catch (Signal.Error.GroupNotFound e) {
- throw new UserErrorException("Failed to send to group: " + e.getMessage());
- } catch (DBusExecutionException e) {
- throw new UnexpectedErrorException("Failed to send message: " + e.getMessage());
- }
- }
-
- private void outputResult(final OutputWriter outputWriter, final long timestamp) {
- if (outputWriter instanceof PlainTextWriter) {
- final var writer = (PlainTextWriter) outputWriter;
- writer.println("{}", timestamp);
- } else {
- final var writer = (JsonWriter) outputWriter;
- writer.write(Map.of("timestamp", timestamp));