import net.sourceforge.argparse4j.inf.Namespace;
import net.sourceforge.argparse4j.inf.Subparser;
-import org.asamk.Signal;
-import org.asamk.signal.PlainTextWriterImpl;
import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.UnexpectedErrorException;
import org.asamk.signal.commands.exceptions.UserErrorException;
-import org.asamk.signal.manager.groups.GroupIdFormatException;
-import org.asamk.signal.util.Util;
-import org.freedesktop.dbus.errors.UnknownObject;
-import org.freedesktop.dbus.exceptions.DBusExecutionException;
+import org.asamk.signal.manager.Manager;
+import org.asamk.signal.manager.api.GroupNotFoundException;
+import org.asamk.signal.manager.api.GroupSendingNotAllowedException;
+import org.asamk.signal.manager.api.NotAGroupMemberException;
+import org.asamk.signal.manager.api.UnregisteredRecipientException;
+import org.asamk.signal.output.OutputWriter;
+import org.asamk.signal.util.CommandUtil;
-import java.util.List;
+import java.io.IOException;
-public class SendReactionCommand implements DbusCommand {
+import static org.asamk.signal.util.SendMessageResultUtils.outputResult;
+
+public class SendReactionCommand implements JsonRpcLocalCommand {
+
+ @Override
+ public String getName() {
+ return "sendReaction";
+ }
@Override
public void attachToSubparser(final Subparser subparser) {
subparser.help("Send reaction to a previously received or sent message.");
- subparser.addArgument("-g", "--group").help("Specify the recipient group ID.");
+ subparser.addArgument("-g", "--group-id", "--group").help("Specify the recipient group ID.").nargs("*");
subparser.addArgument("recipient").help("Specify the recipients' phone number.").nargs("*");
+ subparser.addArgument("-u", "--username").help("Specify the recipient username or username link.").nargs("*");
+ subparser.addArgument("--note-to-self")
+ .help("Send the reaction to self without notification.")
+ .action(Arguments.storeTrue());
subparser.addArgument("-e", "--emoji")
.required(true)
.help("Specify the emoji, should be a single unicode grapheme cluster.");
.type(long.class)
.help("Specify the timestamp of the message to which to react.");
subparser.addArgument("-r", "--remove").help("Remove a reaction.").action(Arguments.storeTrue());
+ subparser.addArgument("--story")
+ .help("React to a story instead of a normal message")
+ .action(Arguments.storeTrue());
}
@Override
- public void handleCommand(final Namespace ns, final Signal signal) throws CommandException {
- final List<String> recipients = ns.getList("recipient");
- final var groupIdString = ns.getString("group");
+ public void handleCommand(
+ final Namespace ns, final Manager m, final OutputWriter outputWriter
+ ) throws CommandException {
+ final var isNoteToSelf = Boolean.TRUE.equals(ns.getBoolean("note-to-self"));
+ final var recipientStrings = ns.<String>getList("recipient");
+ final var groupIdStrings = ns.<String>getList("group-id");
+ final var usernameStrings = ns.<String>getList("username");
- final var noRecipients = recipients == null || recipients.isEmpty();
- if (noRecipients && groupIdString == null) {
- throw new UserErrorException("No recipients given");
- }
- if (!noRecipients && groupIdString != null) {
- throw new UserErrorException("You cannot specify recipients by phone number and groups at the same time");
- }
+ final var recipientIdentifiers = CommandUtil.getRecipientIdentifiers(m,
+ isNoteToSelf,
+ recipientStrings,
+ groupIdStrings,
+ usernameStrings);
final var emoji = ns.getString("emoji");
- final boolean isRemove = ns.getBoolean("remove");
+ final var isRemove = Boolean.TRUE.equals(ns.getBoolean("remove"));
final var targetAuthor = ns.getString("target-author");
- final long targetTimestamp = ns.getLong("target-timestamp");
-
- final var writer = new PlainTextWriterImpl(System.out);
-
- byte[] groupId = null;
- if (groupIdString != null) {
- try {
- groupId = Util.decodeGroupId(groupIdString).serialize();
- } catch (GroupIdFormatException e) {
- throw new UserErrorException("Invalid group id: " + e.getMessage());
- }
- }
+ final var targetTimestamp = ns.getLong("target-timestamp");
+ final var isStory = Boolean.TRUE.equals(ns.getBoolean("story"));
try {
- long timestamp;
- if (groupId != null) {
- timestamp = signal.sendGroupMessageReaction(emoji, isRemove, targetAuthor, targetTimestamp, groupId);
- } else {
- timestamp = signal.sendMessageReaction(emoji, isRemove, targetAuthor, targetTimestamp, recipients);
- }
- writer.println("{}", 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());
+ final var results = m.sendMessageReaction(emoji,
+ isRemove,
+ CommandUtil.getSingleRecipientIdentifier(targetAuthor, m.getSelfNumber()),
+ targetTimestamp,
+ recipientIdentifiers,
+ isStory);
+ outputResult(outputWriter, results);
+ } catch (GroupNotFoundException | NotAGroupMemberException | GroupSendingNotAllowedException e) {
+ throw new UserErrorException(e.getMessage());
+ } catch (IOException e) {
+ throw new UnexpectedErrorException("Failed to send message: " + e.getMessage() + " (" + e.getClass()
+ .getSimpleName() + ")", e);
+ } catch (UnregisteredRecipientException e) {
+ throw new UserErrorException("The user " + e.getSender().getIdentifier() + " is not registered.");
}
}
}