Add command sendReaction for emoji reactions

This commit is contained in:
AsamK 2020-02-22 11:29:17 +01:00
parent f3b8df789d
commit 063fb95dca
4 changed files with 148 additions and 0 deletions

View file

@ -22,6 +22,7 @@ public class Commands {
addCommand("removeDevice", new RemoveDeviceCommand());
addCommand("removePin", new RemovePinCommand());
addCommand("send", new SendCommand());
addCommand("sendReaction", new SendReactionCommand());
addCommand("sendContacts", new SendContactsCommand());
addCommand("updateContact", new UpdateContactCommand());
addCommand("setPin", new SetPinCommand());

View file

@ -0,0 +1,95 @@
package org.asamk.signal.commands;
import net.sourceforge.argparse4j.impl.Arguments;
import net.sourceforge.argparse4j.inf.Namespace;
import net.sourceforge.argparse4j.inf.Subparser;
import org.asamk.signal.GroupIdFormatException;
import org.asamk.signal.GroupNotFoundException;
import org.asamk.signal.NotAGroupMemberException;
import org.asamk.signal.manager.Manager;
import org.asamk.signal.util.Util;
import org.whispersystems.signalservice.api.push.SignalServiceAddress;
import org.whispersystems.signalservice.api.push.exceptions.EncapsulatedExceptions;
import java.io.IOException;
import static org.asamk.signal.util.ErrorUtils.handleAssertionError;
import static org.asamk.signal.util.ErrorUtils.handleEncapsulatedExceptions;
import static org.asamk.signal.util.ErrorUtils.handleGroupIdFormatException;
import static org.asamk.signal.util.ErrorUtils.handleGroupNotFoundException;
import static org.asamk.signal.util.ErrorUtils.handleIOException;
import static org.asamk.signal.util.ErrorUtils.handleNotAGroupMemberException;
public class SendReactionCommand implements LocalCommand {
@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("recipient")
.help("Specify the recipients' phone number.")
.nargs("*");
subparser.addArgument("-e", "--emoji")
.required(true)
.help("Specify the emoji, should be a single unicode grapheme cluster.");
subparser.addArgument("-a", "--target-author")
.required(true)
.help("Specify the number of the author of the message to which to react.");
subparser.addArgument("-t", "--target-timestamp")
.required(true)
.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());
}
@Override
public int handleCommand(final Namespace ns, final Manager m) {
if (!m.isRegistered()) {
System.err.println("User is not registered.");
return 1;
}
if ((ns.getList("recipient") == null || ns.getList("recipient").size() == 0) && ns.getString("group") == null) {
System.err.println("No recipients given");
System.err.println("Aborting sending.");
return 1;
}
String emoji = ns.getString("emoji");
boolean isRemove = ns.getBoolean("remove");
SignalServiceAddress targetAuthor = new SignalServiceAddress(null, ns.getString("target_author"));
long targetTimestamp = ns.getLong("target_timestamp");
try {
if (ns.getString("group") != null) {
byte[] groupId = Util.decodeGroupId(ns.getString("group"));
m.sendGroupMessageReaction(emoji, isRemove, targetAuthor, targetTimestamp, groupId);
} else {
m.sendMessageReaction(emoji, isRemove, targetAuthor, targetTimestamp, ns.<String>getList("recipient"));
}
return 0;
} catch (IOException e) {
handleIOException(e);
return 3;
} catch (EncapsulatedExceptions e) {
handleEncapsulatedExceptions(e);
return 3;
} catch (AssertionError e) {
handleAssertionError(e);
return 1;
} catch (GroupNotFoundException e) {
handleGroupNotFoundException(e);
return 1;
} catch (NotAGroupMemberException e) {
handleNotAGroupMemberException(e);
return 1;
} catch (GroupIdFormatException e) {
handleGroupIdFormatException(e);
return 1;
}
}
}

View file

@ -496,6 +496,26 @@ public class Manager implements Signal {
sendMessageLegacy(messageBuilder, membersSend);
}
public void sendGroupMessageReaction(String emoji, boolean remove, SignalServiceAddress targetAuthor,
long targetSentTimestamp, byte[] groupId)
throws IOException, EncapsulatedExceptions, AttachmentInvalidException {
SignalServiceDataMessage.Reaction reaction = new SignalServiceDataMessage.Reaction(emoji, remove, targetAuthor, targetSentTimestamp);
final SignalServiceDataMessage.Builder messageBuilder = SignalServiceDataMessage.newBuilder()
.withReaction(reaction)
.withProfileKey(account.getProfileKey());
if (groupId != null) {
SignalServiceGroup group = SignalServiceGroup.newBuilder(SignalServiceGroup.Type.DELIVER)
.withId(groupId)
.build();
messageBuilder.asGroupMessage(group);
}
final GroupInfo g = getGroupForSending(groupId);
// Don't send group message to ourself
final List<String> membersSend = new ArrayList<>(g.members);
membersSend.remove(this.username);
sendMessageLegacy(messageBuilder, membersSend);
}
public void sendQuitGroupMessage(byte[] groupId) throws GroupNotFoundException, IOException, EncapsulatedExceptions {
SignalServiceGroup group = SignalServiceGroup.newBuilder(SignalServiceGroup.Type.QUIT)
.withId(groupId)
@ -669,6 +689,16 @@ public class Manager implements Signal {
sendMessageLegacy(messageBuilder, recipients);
}
public void sendMessageReaction(String emoji, boolean remove, SignalServiceAddress targetAuthor,
long targetSentTimestamp, List<String> recipients)
throws IOException, EncapsulatedExceptions, AttachmentInvalidException {
SignalServiceDataMessage.Reaction reaction = new SignalServiceDataMessage.Reaction(emoji, remove, targetAuthor, targetSentTimestamp);
final SignalServiceDataMessage.Builder messageBuilder = SignalServiceDataMessage.newBuilder()
.withReaction(reaction)
.withProfileKey(account.getProfileKey());
sendMessageLegacy(messageBuilder, recipients);
}
@Override
public void sendEndSessionMessage(List<String> recipients) throws IOException, EncapsulatedExceptions {
SignalServiceDataMessage.Builder messageBuilder = SignalServiceDataMessage.newBuilder()