mirror of
https://github.com/AsamK/signal-cli
synced 2025-08-29 18:40:39 +00:00
dbus implementation of sendReaction command (#581)
This commit is contained in:
parent
ea035db94f
commit
8f4d89e2f7
4 changed files with 74 additions and 28 deletions
|
@ -21,6 +21,14 @@ public interface Signal extends DBusInterface {
|
|||
String message, List<String> attachments, List<String> recipients
|
||||
) 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(
|
||||
String message, List<String> attachments
|
||||
) throws Error.AttachmentInvalid, Error.Failure;
|
||||
|
@ -31,6 +39,10 @@ public interface Signal extends DBusInterface {
|
|||
String message, List<String> attachments, byte[] groupId
|
||||
) 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;
|
||||
|
||||
void setContactName(String number, String name) throws Error.InvalidNumber;
|
||||
|
|
|
@ -4,27 +4,21 @@ import net.sourceforge.argparse4j.impl.Arguments;
|
|||
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.IOErrorException;
|
||||
import org.asamk.signal.commands.exceptions.UnexpectedErrorException;
|
||||
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.GroupNotFoundException;
|
||||
import org.asamk.signal.manager.groups.NotAGroupMemberException;
|
||||
import org.asamk.signal.util.Util;
|
||||
import org.whispersystems.libsignal.util.Pair;
|
||||
import org.whispersystems.signalservice.api.messages.SendMessageResult;
|
||||
import org.whispersystems.signalservice.api.util.InvalidNumberException;
|
||||
import org.freedesktop.dbus.errors.UnknownObject;
|
||||
import org.freedesktop.dbus.exceptions.DBusExecutionException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
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
|
||||
public void attachToSubparser(final Subparser subparser) {
|
||||
|
@ -45,7 +39,7 @@ public class SendReactionCommand implements LocalCommand {
|
|||
}
|
||||
|
||||
@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 var groupIdString = ns.getString("group");
|
||||
|
||||
|
@ -64,35 +58,34 @@ public class SendReactionCommand implements LocalCommand {
|
|||
|
||||
final var writer = new PlainTextWriterImpl(System.out);
|
||||
|
||||
final Pair<Long, List<SendMessageResult>> results;
|
||||
|
||||
GroupId groupId = null;
|
||||
byte[] groupId = null;
|
||||
if (groupIdString != null) {
|
||||
try {
|
||||
groupId = Util.decodeGroupId(groupIdString);
|
||||
groupId = Util.decodeGroupId(groupIdString).serialize();
|
||||
} catch (GroupIdFormatException e) {
|
||||
throw new UserErrorException("Invalid group id: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
long timestamp;
|
||||
if (groupId != null) {
|
||||
results = m.sendGroupMessageReaction(emoji, isRemove, targetAuthor, targetTimestamp, groupId);
|
||||
timestamp = signal.sendGroupMessageReaction(emoji, isRemove, targetAuthor, targetTimestamp, groupId);
|
||||
} else {
|
||||
results = m.sendMessageReaction(emoji, isRemove, targetAuthor, targetTimestamp, recipients);
|
||||
timestamp = signal.sendMessageReaction(emoji, isRemove, targetAuthor, targetTimestamp, recipients);
|
||||
}
|
||||
handleTimestampAndSendMessageResults(writer, results.first(), results.second());
|
||||
} catch (IOException e) {
|
||||
throw new IOErrorException("Failed to send message: " + e.getMessage());
|
||||
writer.println("{}", timestamp);
|
||||
} catch (AssertionError e) {
|
||||
handleAssertionError(e);
|
||||
throw e;
|
||||
} catch (GroupNotFoundException e) {
|
||||
throw new UserErrorException("Failed to send to group: " + e.getMessage());
|
||||
} catch (NotAGroupMemberException e) {
|
||||
throw new UserErrorException("Failed to send to group: " + e.getMessage());
|
||||
} catch (InvalidNumberException e) {
|
||||
} 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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
public long sendNoteToSelfMessage(
|
||||
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
|
||||
// the profile name
|
||||
@Override
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue