mirror of
https://github.com/AsamK/signal-cli
synced 2025-08-30 02:50:39 +00:00
Handle send failures as non fatal and return detailed results in json output
Fixes #348
This commit is contained in:
parent
ceafe96e81
commit
06e93b84da
13 changed files with 191 additions and 138 deletions
119
src/main/java/org/asamk/signal/util/SendMessageResultUtils.java
Normal file
119
src/main/java/org/asamk/signal/util/SendMessageResultUtils.java
Normal file
|
@ -0,0 +1,119 @@
|
|||
package org.asamk.signal.util;
|
||||
|
||||
import org.asamk.signal.json.JsonSendMessageResult;
|
||||
import org.asamk.signal.manager.api.ProofRequiredException;
|
||||
import org.asamk.signal.manager.api.RecipientIdentifier;
|
||||
import org.asamk.signal.manager.api.SendGroupMessageResults;
|
||||
import org.asamk.signal.manager.api.SendMessageResult;
|
||||
import org.asamk.signal.manager.api.SendMessageResults;
|
||||
import org.asamk.signal.output.JsonWriter;
|
||||
import org.asamk.signal.output.OutputWriter;
|
||||
import org.asamk.signal.output.PlainTextWriter;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class SendMessageResultUtils {
|
||||
|
||||
private SendMessageResultUtils() {
|
||||
}
|
||||
|
||||
public static void outputResult(final OutputWriter outputWriter, final SendGroupMessageResults sendMessageResults) {
|
||||
if (outputWriter instanceof PlainTextWriter writer) {
|
||||
var errors = getErrorMessagesFromSendMessageResults(sendMessageResults.results());
|
||||
printSendMessageResultErrors(writer, errors);
|
||||
writer.println("{}", sendMessageResults.timestamp());
|
||||
} else {
|
||||
final var writer = (JsonWriter) outputWriter;
|
||||
var results = getJsonSendMessageResults(sendMessageResults.results());
|
||||
writer.write(Map.of("timestamp", sendMessageResults.timestamp(), "results", results));
|
||||
}
|
||||
}
|
||||
|
||||
public static void outputResult(final OutputWriter outputWriter, final SendMessageResults sendMessageResults) {
|
||||
if (outputWriter instanceof PlainTextWriter writer) {
|
||||
var errors = getErrorMessagesFromSendMessageResults(sendMessageResults.results());
|
||||
printSendMessageResultErrors(writer, errors);
|
||||
writer.println("{}", sendMessageResults.timestamp());
|
||||
} else {
|
||||
final var writer = (JsonWriter) outputWriter;
|
||||
var results = getJsonSendMessageResults(sendMessageResults.results());
|
||||
writer.write(Map.of("timestamp", sendMessageResults.timestamp(), "results", results));
|
||||
}
|
||||
}
|
||||
|
||||
public static List<String> getErrorMessagesFromSendMessageResults(final Map<RecipientIdentifier, List<SendMessageResult>> mapResults) {
|
||||
return mapResults.entrySet()
|
||||
.stream()
|
||||
.flatMap(entry -> entry.getValue()
|
||||
.stream()
|
||||
.map(SendMessageResultUtils::getErrorMessageFromSendMessageResult)
|
||||
.filter(Objects::nonNull)
|
||||
.map(error -> entry.getKey().getIdentifier() + ": " + error))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public static List<String> getErrorMessagesFromSendMessageResults(Collection<SendMessageResult> results) {
|
||||
return results.stream()
|
||||
.map(SendMessageResultUtils::getErrorMessageFromSendMessageResult)
|
||||
.filter(Objects::nonNull)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public static String getErrorMessageFromSendMessageResult(SendMessageResult result) {
|
||||
var identifier = result.address().getLegacyIdentifier();
|
||||
if (result.proofRequiredFailure() != null) {
|
||||
final var failure = result.proofRequiredFailure();
|
||||
return String.format(
|
||||
"CAPTCHA proof required for sending to \"%s\", available options \"%s\" with challenge token \"%s\", or wait \"%d\" seconds.\n"
|
||||
+ (
|
||||
failure.getOptions().contains(ProofRequiredException.Option.RECAPTCHA)
|
||||
? """
|
||||
To get the captcha token, go to https://signalcaptchas.org/challenge/generate.html
|
||||
Check the developer tools (F12) console for a failed redirect to signalcaptcha://
|
||||
Everything after signalcaptcha:// is the captcha token.
|
||||
Use the following command to submit the captcha token:
|
||||
signal-cli submitRateLimitChallenge --challenge CHALLENGE_TOKEN --captcha CAPTCHA_TOKEN"""
|
||||
: ""
|
||||
),
|
||||
identifier,
|
||||
failure.getOptions()
|
||||
.stream()
|
||||
.map(ProofRequiredException.Option::toString)
|
||||
.collect(Collectors.joining(", ")),
|
||||
failure.getToken(),
|
||||
failure.getRetryAfterSeconds());
|
||||
} else if (result.isNetworkFailure()) {
|
||||
return String.format("Network failure for \"%s\"", identifier);
|
||||
} else if (result.isUnregisteredFailure()) {
|
||||
return String.format("Unregistered user \"%s\"", identifier);
|
||||
} else if (result.isIdentityFailure()) {
|
||||
return String.format("Untrusted Identity for \"%s\"", identifier);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void printSendMessageResultErrors(PlainTextWriter writer, List<String> errors) {
|
||||
if (errors.size() == 0) {
|
||||
return;
|
||||
}
|
||||
writer.println("Failed to send (some) messages:");
|
||||
for (var error : errors) {
|
||||
writer.println(error);
|
||||
}
|
||||
}
|
||||
|
||||
public 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));
|
||||
}).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public static List<JsonSendMessageResult> getJsonSendMessageResults(Collection<SendMessageResult> results) {
|
||||
return results.stream().map(JsonSendMessageResult::from).collect(Collectors.toList());
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue