mirror of
https://github.com/AsamK/signal-cli
synced 2025-09-01 20:10:39 +00:00
Improve exit code for message sending
Exit with 0 status code if the message was sent successfully to at least one recipient, otherwise exit with status code 2 or 4 (for untrusted). Fixes #828
This commit is contained in:
parent
4aa8e3f2f2
commit
9a72733c4f
4 changed files with 64 additions and 35 deletions
|
@ -1,5 +1,8 @@
|
|||
package org.asamk.signal.util;
|
||||
|
||||
import org.asamk.signal.commands.exceptions.CommandException;
|
||||
import org.asamk.signal.commands.exceptions.UntrustedKeyErrorException;
|
||||
import org.asamk.signal.commands.exceptions.UserErrorException;
|
||||
import org.asamk.signal.json.JsonSendMessageResult;
|
||||
import org.asamk.signal.manager.api.ProofRequiredException;
|
||||
import org.asamk.signal.manager.api.RecipientIdentifier;
|
||||
|
@ -33,7 +36,9 @@ public class SendMessageResultUtils {
|
|||
}
|
||||
}
|
||||
|
||||
public static void outputResult(final OutputWriter outputWriter, final SendMessageResults sendMessageResults) {
|
||||
public static void outputResult(
|
||||
final OutputWriter outputWriter, final SendMessageResults sendMessageResults
|
||||
) throws CommandException {
|
||||
if (outputWriter instanceof PlainTextWriter writer) {
|
||||
var errors = getErrorMessagesFromSendMessageResults(sendMessageResults.results());
|
||||
printSendMessageResultErrors(writer, errors);
|
||||
|
@ -43,6 +48,13 @@ public class SendMessageResultUtils {
|
|||
var results = getJsonSendMessageResults(sendMessageResults.results());
|
||||
writer.write(Map.of("timestamp", sendMessageResults.timestamp(), "results", results));
|
||||
}
|
||||
if (!sendMessageResults.hasSuccess()) {
|
||||
if (sendMessageResults.hasOnlyUntrustedIdentity()) {
|
||||
throw new UntrustedKeyErrorException("Failed to send message due to untrusted identities");
|
||||
} else {
|
||||
throw new UserErrorException("Failed to send message");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static List<String> getErrorMessagesFromSendMessageResults(final Map<RecipientIdentifier, List<SendMessageResult>> mapResults) {
|
||||
|
@ -106,7 +118,7 @@ public class SendMessageResultUtils {
|
|||
}
|
||||
}
|
||||
|
||||
public static List<JsonSendMessageResult> getJsonSendMessageResults(final Map<RecipientIdentifier, List<SendMessageResult>> mapResults) {
|
||||
private static List<JsonSendMessageResult> getJsonSendMessageResults(final Map<RecipientIdentifier, List<SendMessageResult>> mapResults) {
|
||||
return mapResults.entrySet().stream().flatMap(entry -> {
|
||||
final var groupId = entry.getKey() instanceof RecipientIdentifier.Group g ? g.groupId() : null;
|
||||
return entry.getValue().stream().map(r -> JsonSendMessageResult.from(r, groupId));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue