dbus implementation of sendReaction command (#581)

This commit is contained in:
Adaptive Garage 2021-04-02 08:53:54 +02:00 committed by GitHub
parent ea035db94f
commit 8f4d89e2f7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 74 additions and 28 deletions

View file

@ -21,6 +21,14 @@ public interface Signal extends DBusInterface {
String message, List<String> attachments, List<String> recipients String message, List<String> attachments, List<String> recipients
) throws Error.AttachmentInvalid, Error.Failure, Error.InvalidNumber, Error.UntrustedIdentity; ) throws Error.AttachmentInvalid, Error.Failure, Error.InvalidNumber, Error.UntrustedIdentity;
long sendMessageReaction(
String emoji, boolean remove, String targetAuthor, long targetSentTimestamp, String recipient
) throws Error.InvalidNumber, Error.Failure;
long sendMessageReaction(
String emoji, boolean remove, String targetAuthor, long targetSentTimestamp, List<String> recipients
) throws Error.InvalidNumber, Error.Failure;
long sendNoteToSelfMessage( long sendNoteToSelfMessage(
String message, List<String> attachments String message, List<String> attachments
) throws Error.AttachmentInvalid, Error.Failure; ) throws Error.AttachmentInvalid, Error.Failure;
@ -31,6 +39,10 @@ public interface Signal extends DBusInterface {
String message, List<String> attachments, byte[] groupId String message, List<String> attachments, byte[] groupId
) throws Error.GroupNotFound, Error.Failure, Error.AttachmentInvalid; ) throws Error.GroupNotFound, Error.Failure, Error.AttachmentInvalid;
long sendGroupMessageReaction(
String emoji, boolean remove, String targetAuthor, long targetSentTimestamp, byte[] groupId
) throws Error.GroupNotFound, Error.Failure, Error.InvalidNumber;
String getContactName(String number) throws Error.InvalidNumber; String getContactName(String number) throws Error.InvalidNumber;
void setContactName(String number, String name) throws Error.InvalidNumber; void setContactName(String number, String name) throws Error.InvalidNumber;

View file

@ -4,27 +4,21 @@ import net.sourceforge.argparse4j.impl.Arguments;
import net.sourceforge.argparse4j.inf.Namespace; 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.PlainTextWriterImpl; import org.asamk.signal.PlainTextWriterImpl;
import org.asamk.signal.commands.exceptions.CommandException; import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.IOErrorException; 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.manager.Manager;
import org.asamk.signal.manager.groups.GroupId;
import org.asamk.signal.manager.groups.GroupIdFormatException; import org.asamk.signal.manager.groups.GroupIdFormatException;
import org.asamk.signal.manager.groups.GroupNotFoundException;
import org.asamk.signal.manager.groups.NotAGroupMemberException;
import org.asamk.signal.util.Util; import org.asamk.signal.util.Util;
import org.whispersystems.libsignal.util.Pair; import org.freedesktop.dbus.errors.UnknownObject;
import org.whispersystems.signalservice.api.messages.SendMessageResult; import org.freedesktop.dbus.exceptions.DBusExecutionException;
import org.whispersystems.signalservice.api.util.InvalidNumberException;
import java.io.IOException;
import java.util.List; import java.util.List;
import static org.asamk.signal.util.ErrorUtils.handleAssertionError; import static org.asamk.signal.util.ErrorUtils.handleAssertionError;
import static org.asamk.signal.util.ErrorUtils.handleTimestampAndSendMessageResults;
public class SendReactionCommand implements LocalCommand { public class SendReactionCommand implements DbusCommand {
@Override @Override
public void attachToSubparser(final Subparser subparser) { public void attachToSubparser(final Subparser subparser) {
@ -45,7 +39,7 @@ public class SendReactionCommand implements LocalCommand {
} }
@Override @Override
public void handleCommand(final Namespace ns, final Manager m) throws CommandException { public void handleCommand(final Namespace ns, final Signal signal) throws CommandException {
final List<String> recipients = ns.getList("recipient"); final List<String> recipients = ns.getList("recipient");
final var groupIdString = ns.getString("group"); final var groupIdString = ns.getString("group");
@ -64,35 +58,34 @@ public class SendReactionCommand implements LocalCommand {
final var writer = new PlainTextWriterImpl(System.out); final var writer = new PlainTextWriterImpl(System.out);
final Pair<Long, List<SendMessageResult>> results; byte[] groupId = null;
GroupId groupId = null;
if (groupIdString != null) { if (groupIdString != null) {
try { try {
groupId = Util.decodeGroupId(groupIdString); groupId = Util.decodeGroupId(groupIdString).serialize();
} catch (GroupIdFormatException e) { } catch (GroupIdFormatException e) {
throw new UserErrorException("Invalid group id: " + e.getMessage()); throw new UserErrorException("Invalid group id: " + e.getMessage());
} }
} }
try { try {
long timestamp;
if (groupId != null) { if (groupId != null) {
results = m.sendGroupMessageReaction(emoji, isRemove, targetAuthor, targetTimestamp, groupId); timestamp = signal.sendGroupMessageReaction(emoji, isRemove, targetAuthor, targetTimestamp, groupId);
} else { } else {
results = m.sendMessageReaction(emoji, isRemove, targetAuthor, targetTimestamp, recipients); timestamp = signal.sendMessageReaction(emoji, isRemove, targetAuthor, targetTimestamp, recipients);
} }
handleTimestampAndSendMessageResults(writer, results.first(), results.second()); writer.println("{}", timestamp);
} catch (IOException e) {
throw new IOErrorException("Failed to send message: " + e.getMessage());
} catch (AssertionError e) { } catch (AssertionError e) {
handleAssertionError(e); handleAssertionError(e);
throw e; throw e;
} catch (GroupNotFoundException e) { } catch (UnknownObject e) {
throw new UserErrorException("Failed to send to group: " + e.getMessage()); throw new UserErrorException("Failed to find dbus object, maybe missing the -u flag: " + e.getMessage());
} catch (NotAGroupMemberException e) { } catch (Signal.Error.InvalidNumber e) {
throw new UserErrorException("Failed to send to group: " + e.getMessage());
} catch (InvalidNumberException e) {
throw new UserErrorException("Invalid number: " + e.getMessage()); 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());
} }
} }
} }

View file

@ -103,6 +103,30 @@ public class DbusSignalImpl implements Signal {
} }
} }
@Override
public long sendMessageReaction(
final String emoji, final boolean remove, final String targetAuthor, final long targetSentTimestamp, final String recipient
) {
var recipients = new ArrayList<String>(1);
recipients.add(recipient);
return sendMessageReaction(emoji, remove, targetAuthor, targetSentTimestamp, recipients);
}
@Override
public long sendMessageReaction(
final String emoji, final boolean remove, final String targetAuthor, final long targetSentTimestamp, final List<String> recipients
) {
try {
final var results = m.sendMessageReaction(emoji, remove, targetAuthor, targetSentTimestamp, recipients);
checkSendMessageResults(results.first(), results.second());
return results.first();
} catch (InvalidNumberException e) {
throw new Error.InvalidNumber(e.getMessage());
} catch (IOException e) {
throw new Error.Failure(e.getMessage());
}
}
@Override @Override
public long sendNoteToSelfMessage( public long sendNoteToSelfMessage(
final String message, final List<String> attachments final String message, final List<String> attachments
@ -145,6 +169,23 @@ public class DbusSignalImpl implements Signal {
} }
} }
@Override
public long sendGroupMessageReaction(
final String emoji, final boolean remove, final String targetAuthor, final long targetSentTimestamp, final byte[] groupId
) {
try {
final var results = m.sendGroupMessageReaction(emoji, remove, targetAuthor, targetSentTimestamp, GroupId.unknownVersion(groupId));
checkSendMessageResults(results.first(), results.second());
return results.first();
} catch (IOException e) {
throw new Error.Failure(e.getMessage());
} catch (InvalidNumberException e) {
throw new Error.InvalidNumber(e.getMessage());
} catch (GroupNotFoundException | NotAGroupMemberException e) {
throw new Error.GroupNotFound(e.getMessage());
}
}
// Since contact names might be empty if not defined, also potentially return // Since contact names might be empty if not defined, also potentially return
// the profile name // the profile name
@Override @Override