Implement JsonRpc command for remoteDelete and sendReaction

This commit is contained in:
AsamK 2021-08-10 18:27:54 +02:00
parent b24fa98e3c
commit 0bbd75c469
2 changed files with 42 additions and 8 deletions

View file

@ -4,19 +4,23 @@ import net.sourceforge.argparse4j.inf.Namespace;
import net.sourceforge.argparse4j.inf.Subparser; import net.sourceforge.argparse4j.inf.Subparser;
import org.asamk.Signal; import org.asamk.Signal;
import org.asamk.signal.JsonWriter;
import org.asamk.signal.OutputWriter; import org.asamk.signal.OutputWriter;
import org.asamk.signal.PlainTextWriter; import org.asamk.signal.PlainTextWriter;
import org.asamk.signal.commands.exceptions.CommandException; import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.UnexpectedErrorException; import org.asamk.signal.commands.exceptions.UnexpectedErrorException;
import org.asamk.signal.commands.exceptions.UserErrorException; import org.asamk.signal.commands.exceptions.UserErrorException;
import org.asamk.signal.dbus.DbusSignalImpl;
import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.groups.GroupIdFormatException; import org.asamk.signal.manager.groups.GroupIdFormatException;
import org.asamk.signal.util.Util; import org.asamk.signal.util.Util;
import org.freedesktop.dbus.errors.UnknownObject; import org.freedesktop.dbus.errors.UnknownObject;
import org.freedesktop.dbus.exceptions.DBusExecutionException; import org.freedesktop.dbus.exceptions.DBusExecutionException;
import java.util.List; import java.util.List;
import java.util.Map;
public class RemoteDeleteCommand implements DbusCommand { public class RemoteDeleteCommand implements DbusCommand, JsonRpcLocalCommand {
private final OutputWriter outputWriter; private final OutputWriter outputWriter;
@ -49,8 +53,6 @@ public class RemoteDeleteCommand implements DbusCommand {
final long targetTimestamp = ns.getLong("target-timestamp"); final long targetTimestamp = ns.getLong("target-timestamp");
final var writer = (PlainTextWriter) outputWriter;
byte[] groupId = null; byte[] groupId = null;
if (groupIdString != null) { if (groupIdString != null) {
try { try {
@ -67,7 +69,7 @@ public class RemoteDeleteCommand implements DbusCommand {
} else { } else {
timestamp = signal.sendRemoteDeleteMessage(targetTimestamp, recipients); timestamp = signal.sendRemoteDeleteMessage(targetTimestamp, recipients);
} }
writer.println("{}", timestamp); outputResult(timestamp);
} catch (UnknownObject e) { } catch (UnknownObject e) {
throw new UserErrorException("Failed to find dbus object, maybe missing the -u flag: " + e.getMessage()); throw new UserErrorException("Failed to find dbus object, maybe missing the -u flag: " + e.getMessage());
} catch (Signal.Error.InvalidNumber e) { } catch (Signal.Error.InvalidNumber e) {
@ -78,4 +80,19 @@ public class RemoteDeleteCommand implements DbusCommand {
throw new UnexpectedErrorException("Failed to send message: " + e.getMessage()); throw new UnexpectedErrorException("Failed to send message: " + e.getMessage());
} }
} }
@Override
public void handleCommand(final Namespace ns, final Manager m) throws CommandException {
handleCommand(ns, new DbusSignalImpl(m, null));
}
private void outputResult(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));
}
}
} }

View file

@ -5,19 +5,23 @@ import net.sourceforge.argparse4j.inf.Namespace;
import net.sourceforge.argparse4j.inf.Subparser; import net.sourceforge.argparse4j.inf.Subparser;
import org.asamk.Signal; import org.asamk.Signal;
import org.asamk.signal.JsonWriter;
import org.asamk.signal.OutputWriter; import org.asamk.signal.OutputWriter;
import org.asamk.signal.PlainTextWriter; import org.asamk.signal.PlainTextWriter;
import org.asamk.signal.commands.exceptions.CommandException; import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.UnexpectedErrorException; import org.asamk.signal.commands.exceptions.UnexpectedErrorException;
import org.asamk.signal.commands.exceptions.UserErrorException; import org.asamk.signal.commands.exceptions.UserErrorException;
import org.asamk.signal.dbus.DbusSignalImpl;
import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.groups.GroupIdFormatException; import org.asamk.signal.manager.groups.GroupIdFormatException;
import org.asamk.signal.util.Util; import org.asamk.signal.util.Util;
import org.freedesktop.dbus.errors.UnknownObject; import org.freedesktop.dbus.errors.UnknownObject;
import org.freedesktop.dbus.exceptions.DBusExecutionException; import org.freedesktop.dbus.exceptions.DBusExecutionException;
import java.util.List; import java.util.List;
import java.util.Map;
public class SendReactionCommand implements DbusCommand { public class SendReactionCommand implements DbusCommand, JsonRpcLocalCommand {
private final OutputWriter outputWriter; private final OutputWriter outputWriter;
@ -60,8 +64,6 @@ public class SendReactionCommand implements DbusCommand {
final var targetAuthor = ns.getString("target-author"); final var targetAuthor = ns.getString("target-author");
final long targetTimestamp = ns.getLong("target-timestamp"); final long targetTimestamp = ns.getLong("target-timestamp");
final var writer = (PlainTextWriter) outputWriter;
byte[] groupId = null; byte[] groupId = null;
if (groupIdString != null) { if (groupIdString != null) {
try { try {
@ -78,7 +80,7 @@ public class SendReactionCommand implements DbusCommand {
} else { } else {
timestamp = signal.sendMessageReaction(emoji, isRemove, targetAuthor, targetTimestamp, recipients); timestamp = signal.sendMessageReaction(emoji, isRemove, targetAuthor, targetTimestamp, recipients);
} }
writer.println("{}", timestamp); outputResult(timestamp);
} catch (UnknownObject e) { } catch (UnknownObject e) {
throw new UserErrorException("Failed to find dbus object, maybe missing the -u flag: " + e.getMessage()); throw new UserErrorException("Failed to find dbus object, maybe missing the -u flag: " + e.getMessage());
} catch (Signal.Error.InvalidNumber e) { } catch (Signal.Error.InvalidNumber e) {
@ -89,4 +91,19 @@ public class SendReactionCommand implements DbusCommand {
throw new UnexpectedErrorException("Failed to send message: " + e.getMessage()); throw new UnexpectedErrorException("Failed to send message: " + e.getMessage());
} }
} }
@Override
public void handleCommand(final Namespace ns, final Manager m) throws CommandException {
handleCommand(ns, new DbusSignalImpl(m, null));
}
private void outputResult(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));
}
}
} }