Include recipient identifier in send error message

This commit is contained in:
AsamK 2021-12-10 20:21:14 +01:00
parent b8ac75313f
commit ceafe96e81
2 changed files with 26 additions and 16 deletions

View file

@ -11,9 +11,16 @@ import java.util.UUID;
public sealed interface RecipientIdentifier { public sealed interface RecipientIdentifier {
String getIdentifier();
record NoteToSelf() implements RecipientIdentifier { record NoteToSelf() implements RecipientIdentifier {
public static NoteToSelf INSTANCE = new NoteToSelf(); public static NoteToSelf INSTANCE = new NoteToSelf();
@Override
public String getIdentifier() {
return "Note-To-Self";
}
} }
sealed interface Single extends RecipientIdentifier { sealed interface Single extends RecipientIdentifier {
@ -43,8 +50,6 @@ public sealed interface RecipientIdentifier {
} }
throw new AssertionError("RecipientAddress without identifier"); throw new AssertionError("RecipientAddress without identifier");
} }
String getIdentifier();
} }
record Uuid(UUID uuid) implements Single { record Uuid(UUID uuid) implements Single {
@ -63,5 +68,11 @@ public sealed interface RecipientIdentifier {
} }
} }
record Group(GroupId groupId) implements RecipientIdentifier {} record Group(GroupId groupId) implements RecipientIdentifier {
@Override
public String getIdentifier() {
return groupId.toBase64();
}
}
} }

View file

@ -6,10 +6,10 @@ import org.asamk.signal.manager.api.ProofRequiredException;
import org.asamk.signal.manager.api.RecipientIdentifier; import org.asamk.signal.manager.api.RecipientIdentifier;
import org.asamk.signal.manager.api.SendMessageResult; import org.asamk.signal.manager.api.SendMessageResult;
import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class ErrorUtils { public class ErrorUtils {
@ -20,7 +20,7 @@ public class ErrorUtils {
public static void handleSendMessageResults( public static void handleSendMessageResults(
Map<RecipientIdentifier, List<SendMessageResult>> mapResults Map<RecipientIdentifier, List<SendMessageResult>> mapResults
) throws CommandException { ) throws CommandException {
List<String> errors = getErrorMessagesFromSendMessageResults(mapResults); var errors = getErrorMessagesFromSendMessageResults(mapResults);
handleSendMessageResultErrors(errors); handleSendMessageResultErrors(errors);
} }
@ -32,22 +32,21 @@ public class ErrorUtils {
} }
public static List<String> getErrorMessagesFromSendMessageResults(final Map<RecipientIdentifier, List<SendMessageResult>> mapResults) { public static List<String> getErrorMessagesFromSendMessageResults(final Map<RecipientIdentifier, List<SendMessageResult>> mapResults) {
return mapResults.values() return mapResults.entrySet()
.stream() .stream()
.flatMap(results -> getErrorMessagesFromSendMessageResults(results).stream()) .flatMap(entry -> entry.getValue()
.stream()
.map(ErrorUtils::getErrorMessageFromSendMessageResult)
.filter(Objects::nonNull)
.map(error -> entry.getKey().getIdentifier() + ": " + error))
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
public static List<String> getErrorMessagesFromSendMessageResults(Collection<SendMessageResult> results) { public static List<String> getErrorMessagesFromSendMessageResults(Collection<SendMessageResult> results) {
var errors = new ArrayList<String>(); return results.stream()
for (var result : results) { .map(ErrorUtils::getErrorMessageFromSendMessageResult)
var error = getErrorMessageFromSendMessageResult(result); .filter(Objects::nonNull)
if (error != null) { .collect(Collectors.toList());
errors.add(error);
}
}
return errors;
} }
public static String getErrorMessageFromSendMessageResult(SendMessageResult result) { public static String getErrorMessageFromSendMessageResult(SendMessageResult result) {