]> nmode's Git Repositories - signal-cli/blob - src/main/java/org/asamk/signal/commands/SendReactionCommand.java
Implement JsonRpc command for remoteDelete and sendReaction
[signal-cli] / src / main / java / org / asamk / signal / commands / SendReactionCommand.java
1 package org.asamk.signal.commands;
2
3 import net.sourceforge.argparse4j.impl.Arguments;
4 import net.sourceforge.argparse4j.inf.Namespace;
5 import net.sourceforge.argparse4j.inf.Subparser;
6
7 import org.asamk.Signal;
8 import org.asamk.signal.JsonWriter;
9 import org.asamk.signal.OutputWriter;
10 import org.asamk.signal.PlainTextWriter;
11 import org.asamk.signal.commands.exceptions.CommandException;
12 import org.asamk.signal.commands.exceptions.UnexpectedErrorException;
13 import org.asamk.signal.commands.exceptions.UserErrorException;
14 import org.asamk.signal.dbus.DbusSignalImpl;
15 import org.asamk.signal.manager.Manager;
16 import org.asamk.signal.manager.groups.GroupIdFormatException;
17 import org.asamk.signal.util.Util;
18 import org.freedesktop.dbus.errors.UnknownObject;
19 import org.freedesktop.dbus.exceptions.DBusExecutionException;
20
21 import java.util.List;
22 import java.util.Map;
23
24 public class SendReactionCommand implements DbusCommand, JsonRpcLocalCommand {
25
26 private final OutputWriter outputWriter;
27
28 public SendReactionCommand(final OutputWriter outputWriter) {
29 this.outputWriter = outputWriter;
30 }
31
32 public static void attachToSubparser(final Subparser subparser) {
33 subparser.help("Send reaction to a previously received or sent message.");
34 subparser.addArgument("-g", "--group-id", "--group").help("Specify the recipient group ID.");
35 subparser.addArgument("recipient").help("Specify the recipients' phone number.").nargs("*");
36 subparser.addArgument("-e", "--emoji")
37 .required(true)
38 .help("Specify the emoji, should be a single unicode grapheme cluster.");
39 subparser.addArgument("-a", "--target-author")
40 .required(true)
41 .help("Specify the number of the author of the message to which to react.");
42 subparser.addArgument("-t", "--target-timestamp")
43 .required(true)
44 .type(long.class)
45 .help("Specify the timestamp of the message to which to react.");
46 subparser.addArgument("-r", "--remove").help("Remove a reaction.").action(Arguments.storeTrue());
47 }
48
49 @Override
50 public void handleCommand(final Namespace ns, final Signal signal) throws CommandException {
51 final List<String> recipients = ns.getList("recipient");
52 final var groupIdString = ns.getString("group-id");
53
54 final var noRecipients = recipients == null || recipients.isEmpty();
55 if (noRecipients && groupIdString == null) {
56 throw new UserErrorException("No recipients given");
57 }
58 if (!noRecipients && groupIdString != null) {
59 throw new UserErrorException("You cannot specify recipients by phone number and groups at the same time");
60 }
61
62 final var emoji = ns.getString("emoji");
63 final boolean isRemove = ns.getBoolean("remove");
64 final var targetAuthor = ns.getString("target-author");
65 final long targetTimestamp = ns.getLong("target-timestamp");
66
67 byte[] groupId = null;
68 if (groupIdString != null) {
69 try {
70 groupId = Util.decodeGroupId(groupIdString).serialize();
71 } catch (GroupIdFormatException e) {
72 throw new UserErrorException("Invalid group id: " + e.getMessage());
73 }
74 }
75
76 try {
77 long timestamp;
78 if (groupId != null) {
79 timestamp = signal.sendGroupMessageReaction(emoji, isRemove, targetAuthor, targetTimestamp, groupId);
80 } else {
81 timestamp = signal.sendMessageReaction(emoji, isRemove, targetAuthor, targetTimestamp, recipients);
82 }
83 outputResult(timestamp);
84 } catch (UnknownObject e) {
85 throw new UserErrorException("Failed to find dbus object, maybe missing the -u flag: " + e.getMessage());
86 } catch (Signal.Error.InvalidNumber e) {
87 throw new UserErrorException("Invalid number: " + e.getMessage());
88 } catch (Signal.Error.GroupNotFound e) {
89 throw new UserErrorException("Failed to send to group: " + e.getMessage());
90 } catch (DBusExecutionException e) {
91 throw new UnexpectedErrorException("Failed to send message: " + e.getMessage());
92 }
93 }
94
95 @Override
96 public void handleCommand(final Namespace ns, final Manager m) throws CommandException {
97 handleCommand(ns, new DbusSignalImpl(m, null));
98 }
99
100 private void outputResult(final long timestamp) {
101 if (outputWriter instanceof PlainTextWriter) {
102 final var writer = (PlainTextWriter) outputWriter;
103 writer.println("{}", timestamp);
104 } else {
105 final var writer = (JsonWriter) outputWriter;
106 writer.write(Map.of("timestamp", timestamp));
107 }
108 }
109 }