Handle send failures as non fatal and return detailed results in json output

Fixes #348
This commit is contained in:
AsamK 2021-12-11 12:44:11 +01:00
parent ceafe96e81
commit 06e93b84da
13 changed files with 191 additions and 138 deletions

View file

@ -578,12 +578,40 @@
"allDeclaredMethods":true, "allDeclaredMethods":true,
"allDeclaredConstructors":true} "allDeclaredConstructors":true}
, ,
{
"name":"org.asamk.signal.json.JsonRecipientAddress",
"allDeclaredFields":true,
"queryAllDeclaredMethods":true,
"queryAllDeclaredConstructors":true,
"methods":[
{"name":"number","parameterTypes":[] },
{"name":"uuid","parameterTypes":[] }
]}
,
{ {
"name":"org.asamk.signal.json.JsonRemoteDelete", "name":"org.asamk.signal.json.JsonRemoteDelete",
"allDeclaredFields":true, "allDeclaredFields":true,
"allDeclaredMethods":true, "allDeclaredMethods":true,
"allDeclaredConstructors":true} "allDeclaredConstructors":true}
, ,
{
"name":"org.asamk.signal.json.JsonSendMessageResult",
"allDeclaredFields":true,
"queryAllDeclaredMethods":true,
"queryAllDeclaredConstructors":true,
"methods":[
{"name":"groupId","parameterTypes":[] },
{"name":"recipientAddress","parameterTypes":[] },
{"name":"retryAfterSeconds","parameterTypes":[] },
{"name":"token","parameterTypes":[] },
{"name":"type","parameterTypes":[] }
]}
,
{
"name":"org.asamk.signal.json.JsonSendMessageResult$Type",
"allDeclaredFields":true,
"queryAllDeclaredMethods":true}
,
{ {
"name":"org.asamk.signal.json.JsonSharedContact", "name":"org.asamk.signal.json.JsonSharedContact",
"allDeclaredFields":true, "allDeclaredFields":true,

View file

@ -13,13 +13,12 @@ import org.asamk.signal.manager.groups.GroupInviteLinkUrl;
import org.asamk.signal.output.JsonWriter; import org.asamk.signal.output.JsonWriter;
import org.asamk.signal.output.OutputWriter; import org.asamk.signal.output.OutputWriter;
import org.asamk.signal.output.PlainTextWriter; import org.asamk.signal.output.PlainTextWriter;
import org.asamk.signal.util.SendMessageResultUtils;
import org.freedesktop.dbus.exceptions.DBusExecutionException; import org.freedesktop.dbus.exceptions.DBusExecutionException;
import java.io.IOException; import java.io.IOException;
import java.util.Map; import java.util.Map;
import static org.asamk.signal.util.ErrorUtils.handleSendMessageResults;
public class JoinGroupCommand implements JsonRpcLocalCommand { public class JoinGroupCommand implements JsonRpcLocalCommand {
@Override @Override
@ -55,10 +54,23 @@ public class JoinGroupCommand implements JsonRpcLocalCommand {
final var results = m.joinGroup(linkUrl); final var results = m.joinGroup(linkUrl);
var newGroupId = results.first(); var newGroupId = results.first();
if (outputWriter instanceof JsonWriter writer) { if (outputWriter instanceof JsonWriter writer) {
var jsonResults = SendMessageResultUtils.getJsonSendMessageResults(results.second().results());
if (!m.getGroup(newGroupId).isMember()) { if (!m.getGroup(newGroupId).isMember()) {
writer.write(Map.of("groupId", newGroupId.toBase64(), "onlyRequested", true)); writer.write(Map.of("timestamp",
results.second().timestamp(),
"results",
jsonResults,
"groupId",
newGroupId.toBase64(),
"onlyRequested",
true));
} else { } else {
writer.write(Map.of("groupId", newGroupId.toBase64())); writer.write(Map.of("timestamp",
results.second().timestamp(),
"results",
jsonResults,
"groupId",
newGroupId.toBase64()));
} }
} else { } else {
final var writer = (PlainTextWriter) outputWriter; final var writer = (PlainTextWriter) outputWriter;
@ -67,8 +79,10 @@ public class JoinGroupCommand implements JsonRpcLocalCommand {
} else { } else {
writer.println("Joined group \"{}\"", newGroupId.toBase64()); writer.println("Joined group \"{}\"", newGroupId.toBase64());
} }
var errors = SendMessageResultUtils.getErrorMessagesFromSendMessageResults(results.second().results());
SendMessageResultUtils.printSendMessageResultErrors(writer, errors);
writer.println("{}", results.second().timestamp());
} }
handleSendMessageResults(results.second().results());
} catch (IOException e) { } catch (IOException e) {
throw new IOErrorException("Failed to send message: " throw new IOErrorException("Failed to send message: "
+ e.getMessage() + e.getMessage()

View file

@ -11,17 +11,14 @@ import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.groups.GroupNotFoundException; import org.asamk.signal.manager.groups.GroupNotFoundException;
import org.asamk.signal.manager.groups.LastGroupAdminException; import org.asamk.signal.manager.groups.LastGroupAdminException;
import org.asamk.signal.manager.groups.NotAGroupMemberException; import org.asamk.signal.manager.groups.NotAGroupMemberException;
import org.asamk.signal.output.JsonWriter;
import org.asamk.signal.output.OutputWriter; import org.asamk.signal.output.OutputWriter;
import org.asamk.signal.output.PlainTextWriter;
import org.asamk.signal.util.CommandUtil; import org.asamk.signal.util.CommandUtil;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.io.IOException; import java.io.IOException;
import java.util.Map;
import static org.asamk.signal.util.ErrorUtils.handleSendMessageResults; import static org.asamk.signal.util.SendMessageResultUtils.outputResult;
public class QuitGroupCommand implements JsonRpcLocalCommand { public class QuitGroupCommand implements JsonRpcLocalCommand {
@ -55,9 +52,7 @@ public class QuitGroupCommand implements JsonRpcLocalCommand {
try { try {
try { try {
final var results = m.quitGroup(groupId, groupAdmins); final var results = m.quitGroup(groupId, groupAdmins);
final var timestamp = results.timestamp(); outputResult(outputWriter, results);
outputResult(outputWriter, timestamp);
handleSendMessageResults(results.results());
} catch (NotAGroupMemberException e) { } catch (NotAGroupMemberException e) {
logger.info("User is not a group member"); logger.info("User is not a group member");
} }
@ -77,13 +72,4 @@ public class QuitGroupCommand implements JsonRpcLocalCommand {
throw new UserErrorException("You need to specify a new admin with --admin: " + e.getMessage()); throw new UserErrorException("You need to specify a new admin with --admin: " + e.getMessage());
} }
} }
private void outputResult(final OutputWriter outputWriter, final long timestamp) {
if (outputWriter instanceof PlainTextWriter writer) {
writer.println("{}", timestamp);
} else {
final var writer = (JsonWriter) outputWriter;
writer.write(Map.of("timestamp", timestamp));
}
}
} }

View file

@ -11,14 +11,12 @@ import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.groups.GroupNotFoundException; import org.asamk.signal.manager.groups.GroupNotFoundException;
import org.asamk.signal.manager.groups.GroupSendingNotAllowedException; import org.asamk.signal.manager.groups.GroupSendingNotAllowedException;
import org.asamk.signal.manager.groups.NotAGroupMemberException; import org.asamk.signal.manager.groups.NotAGroupMemberException;
import org.asamk.signal.output.JsonWriter;
import org.asamk.signal.output.OutputWriter; import org.asamk.signal.output.OutputWriter;
import org.asamk.signal.output.PlainTextWriter;
import org.asamk.signal.util.CommandUtil; import org.asamk.signal.util.CommandUtil;
import org.asamk.signal.util.ErrorUtils;
import java.io.IOException; import java.io.IOException;
import java.util.Map;
import static org.asamk.signal.util.SendMessageResultUtils.outputResult;
public class RemoteDeleteCommand implements JsonRpcLocalCommand { public class RemoteDeleteCommand implements JsonRpcLocalCommand {
@ -56,8 +54,7 @@ public class RemoteDeleteCommand implements JsonRpcLocalCommand {
try { try {
final var results = m.sendRemoteDeleteMessage(targetTimestamp, recipientIdentifiers); final var results = m.sendRemoteDeleteMessage(targetTimestamp, recipientIdentifiers);
outputResult(outputWriter, results.timestamp()); outputResult(outputWriter, results);
ErrorUtils.handleSendMessageResults(results.results());
} catch (GroupNotFoundException | NotAGroupMemberException | GroupSendingNotAllowedException e) { } catch (GroupNotFoundException | NotAGroupMemberException | GroupSendingNotAllowedException e) {
throw new UserErrorException(e.getMessage()); throw new UserErrorException(e.getMessage());
} catch (IOException e) { } catch (IOException e) {
@ -65,13 +62,4 @@ public class RemoteDeleteCommand implements JsonRpcLocalCommand {
.getSimpleName() + ")", e); .getSimpleName() + ")", e);
} }
} }
private void outputResult(final OutputWriter outputWriter, final long timestamp) {
if (outputWriter instanceof PlainTextWriter writer) {
writer.println("{}", timestamp);
} else {
final var writer = (JsonWriter) outputWriter;
writer.write(Map.of("timestamp", timestamp));
}
}
} }

View file

@ -14,11 +14,8 @@ import org.asamk.signal.manager.api.RecipientIdentifier;
import org.asamk.signal.manager.groups.GroupNotFoundException; import org.asamk.signal.manager.groups.GroupNotFoundException;
import org.asamk.signal.manager.groups.GroupSendingNotAllowedException; import org.asamk.signal.manager.groups.GroupSendingNotAllowedException;
import org.asamk.signal.manager.groups.NotAGroupMemberException; import org.asamk.signal.manager.groups.NotAGroupMemberException;
import org.asamk.signal.output.JsonWriter;
import org.asamk.signal.output.OutputWriter; import org.asamk.signal.output.OutputWriter;
import org.asamk.signal.output.PlainTextWriter;
import org.asamk.signal.util.CommandUtil; import org.asamk.signal.util.CommandUtil;
import org.asamk.signal.util.ErrorUtils;
import org.asamk.signal.util.IOUtils; import org.asamk.signal.util.IOUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -27,11 +24,12 @@ import java.io.IOException;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static org.asamk.signal.util.SendMessageResultUtils.outputResult;
public class SendCommand implements JsonRpcLocalCommand { public class SendCommand implements JsonRpcLocalCommand {
private final static Logger logger = LoggerFactory.getLogger(SendCommand.class); private final static Logger logger = LoggerFactory.getLogger(SendCommand.class);
@ -93,8 +91,7 @@ public class SendCommand implements JsonRpcLocalCommand {
try { try {
final var results = m.sendEndSessionMessage(singleRecipients); final var results = m.sendEndSessionMessage(singleRecipients);
outputResult(outputWriter, results.timestamp()); outputResult(outputWriter, results);
ErrorUtils.handleSendMessageResults(results.results());
return; return;
} catch (IOException e) { } catch (IOException e) {
throw new UnexpectedErrorException("Failed to send message: " + e.getMessage() + " (" + e.getClass() throw new UnexpectedErrorException("Failed to send message: " + e.getMessage() + " (" + e.getClass()
@ -140,8 +137,7 @@ public class SendCommand implements JsonRpcLocalCommand {
try { try {
var results = m.sendMessage(new Message(messageText, attachments, mentions, Optional.ofNullable(quote)), var results = m.sendMessage(new Message(messageText, attachments, mentions, Optional.ofNullable(quote)),
recipientIdentifiers); recipientIdentifiers);
outputResult(outputWriter, results.timestamp()); outputResult(outputWriter, results);
ErrorUtils.handleSendMessageResults(results.results());
} catch (AttachmentInvalidException | IOException e) { } catch (AttachmentInvalidException | IOException e) {
throw new UnexpectedErrorException("Failed to send message: " + e.getMessage() + " (" + e.getClass() throw new UnexpectedErrorException("Failed to send message: " + e.getMessage() + " (" + e.getClass()
.getSimpleName() + ")", e); .getSimpleName() + ")", e);
@ -168,13 +164,4 @@ public class SendCommand implements JsonRpcLocalCommand {
} }
return mentions; return mentions;
} }
private void outputResult(final OutputWriter outputWriter, final long timestamp) {
if (outputWriter instanceof PlainTextWriter writer) {
writer.println("{}", timestamp);
} else {
final var writer = (JsonWriter) outputWriter;
writer.write(Map.of("timestamp", timestamp));
}
}
} }

View file

@ -11,14 +11,12 @@ import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.groups.GroupNotFoundException; import org.asamk.signal.manager.groups.GroupNotFoundException;
import org.asamk.signal.manager.groups.GroupSendingNotAllowedException; import org.asamk.signal.manager.groups.GroupSendingNotAllowedException;
import org.asamk.signal.manager.groups.NotAGroupMemberException; import org.asamk.signal.manager.groups.NotAGroupMemberException;
import org.asamk.signal.output.JsonWriter;
import org.asamk.signal.output.OutputWriter; import org.asamk.signal.output.OutputWriter;
import org.asamk.signal.output.PlainTextWriter;
import org.asamk.signal.util.CommandUtil; import org.asamk.signal.util.CommandUtil;
import org.asamk.signal.util.ErrorUtils;
import java.io.IOException; import java.io.IOException;
import java.util.Map;
import static org.asamk.signal.util.SendMessageResultUtils.outputResult;
public class SendReactionCommand implements JsonRpcLocalCommand { public class SendReactionCommand implements JsonRpcLocalCommand {
@ -72,8 +70,7 @@ public class SendReactionCommand implements JsonRpcLocalCommand {
CommandUtil.getSingleRecipientIdentifier(targetAuthor, m.getSelfNumber()), CommandUtil.getSingleRecipientIdentifier(targetAuthor, m.getSelfNumber()),
targetTimestamp, targetTimestamp,
recipientIdentifiers); recipientIdentifiers);
outputResult(outputWriter, results.timestamp()); outputResult(outputWriter, results);
ErrorUtils.handleSendMessageResults(results.results());
} catch (GroupNotFoundException | NotAGroupMemberException | GroupSendingNotAllowedException e) { } catch (GroupNotFoundException | NotAGroupMemberException | GroupSendingNotAllowedException e) {
throw new UserErrorException(e.getMessage()); throw new UserErrorException(e.getMessage());
} catch (IOException e) { } catch (IOException e) {
@ -81,13 +78,4 @@ public class SendReactionCommand implements JsonRpcLocalCommand {
.getSimpleName() + ")", e); .getSimpleName() + ")", e);
} }
} }
private void outputResult(final OutputWriter outputWriter, final long timestamp) {
if (outputWriter instanceof PlainTextWriter writer) {
writer.println("{}", timestamp);
} else {
final var writer = (JsonWriter) outputWriter;
writer.write(Map.of("timestamp", timestamp));
}
}
} }

View file

@ -7,14 +7,12 @@ import org.asamk.signal.commands.exceptions.CommandException;
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.Manager;
import org.asamk.signal.manager.api.SendMessageResults; import org.asamk.signal.manager.api.SendMessageResults;
import org.asamk.signal.output.JsonWriter;
import org.asamk.signal.output.OutputWriter; import org.asamk.signal.output.OutputWriter;
import org.asamk.signal.output.PlainTextWriter;
import org.asamk.signal.util.CommandUtil; import org.asamk.signal.util.CommandUtil;
import org.asamk.signal.util.ErrorUtils;
import java.io.IOException; import java.io.IOException;
import java.util.Map;
import static org.asamk.signal.util.SendMessageResultUtils.outputResult;
public class SendReceiptCommand implements JsonRpcLocalCommand { public class SendReceiptCommand implements JsonRpcLocalCommand {
@ -55,20 +53,10 @@ public class SendReceiptCommand implements JsonRpcLocalCommand {
} else { } else {
throw new UserErrorException("Unknown receipt type: " + type); throw new UserErrorException("Unknown receipt type: " + type);
} }
outputResult(outputWriter, results.timestamp()); outputResult(outputWriter, results);
ErrorUtils.handleSendMessageResults(results.results());
} catch (IOException e) { } catch (IOException e) {
throw new UserErrorException("Failed to send message: " + e.getMessage() + " (" + e.getClass() throw new UserErrorException("Failed to send message: " + e.getMessage() + " (" + e.getClass()
.getSimpleName() + ")"); .getSimpleName() + ")");
} }
} }
private void outputResult(final OutputWriter outputWriter, final long timestamp) {
if (outputWriter instanceof PlainTextWriter writer) {
writer.println("{}", timestamp);
} else {
final var writer = (JsonWriter) outputWriter;
writer.write(Map.of("timestamp", timestamp));
}
}
} }

View file

@ -12,15 +12,13 @@ import org.asamk.signal.manager.api.TypingAction;
import org.asamk.signal.manager.groups.GroupNotFoundException; import org.asamk.signal.manager.groups.GroupNotFoundException;
import org.asamk.signal.manager.groups.GroupSendingNotAllowedException; import org.asamk.signal.manager.groups.GroupSendingNotAllowedException;
import org.asamk.signal.manager.groups.NotAGroupMemberException; import org.asamk.signal.manager.groups.NotAGroupMemberException;
import org.asamk.signal.output.JsonWriter;
import org.asamk.signal.output.OutputWriter; import org.asamk.signal.output.OutputWriter;
import org.asamk.signal.output.PlainTextWriter;
import org.asamk.signal.util.CommandUtil; import org.asamk.signal.util.CommandUtil;
import org.asamk.signal.util.ErrorUtils;
import java.io.IOException; import java.io.IOException;
import java.util.HashSet; import java.util.HashSet;
import java.util.Map;
import static org.asamk.signal.util.SendMessageResultUtils.outputResult;
public class SendTypingCommand implements JsonRpcLocalCommand { public class SendTypingCommand implements JsonRpcLocalCommand {
@ -61,8 +59,7 @@ public class SendTypingCommand implements JsonRpcLocalCommand {
try { try {
final var results = m.sendTypingMessage(action, recipientIdentifiers); final var results = m.sendTypingMessage(action, recipientIdentifiers);
outputResult(outputWriter, results.timestamp()); outputResult(outputWriter, results);
ErrorUtils.handleSendMessageResults(results.results());
} catch (IOException e) { } catch (IOException e) {
throw new UserErrorException("Failed to send message: " + e.getMessage() + " (" + e.getClass() throw new UserErrorException("Failed to send message: " + e.getMessage() + " (" + e.getClass()
.getSimpleName() + ")"); .getSimpleName() + ")");
@ -70,13 +67,4 @@ public class SendTypingCommand implements JsonRpcLocalCommand {
throw new UserErrorException("Failed to send to group: " + e.getMessage()); throw new UserErrorException("Failed to send to group: " + e.getMessage());
} }
} }
private void outputResult(final OutputWriter outputWriter, final long timestamp) {
if (outputWriter instanceof PlainTextWriter writer) {
writer.println("{}", timestamp);
} else {
final var writer = (JsonWriter) outputWriter;
writer.write(Map.of("timestamp", timestamp));
}
}
} }

View file

@ -9,6 +9,7 @@ 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.AttachmentInvalidException; import org.asamk.signal.manager.AttachmentInvalidException;
import org.asamk.signal.manager.Manager; import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.api.SendGroupMessageResults;
import org.asamk.signal.manager.api.UpdateGroup; import org.asamk.signal.manager.api.UpdateGroup;
import org.asamk.signal.manager.groups.GroupId; import org.asamk.signal.manager.groups.GroupId;
import org.asamk.signal.manager.groups.GroupLinkState; import org.asamk.signal.manager.groups.GroupLinkState;
@ -20,13 +21,15 @@ import org.asamk.signal.output.JsonWriter;
import org.asamk.signal.output.OutputWriter; import org.asamk.signal.output.OutputWriter;
import org.asamk.signal.output.PlainTextWriter; import org.asamk.signal.output.PlainTextWriter;
import org.asamk.signal.util.CommandUtil; import org.asamk.signal.util.CommandUtil;
import org.asamk.signal.util.ErrorUtils; import org.asamk.signal.util.SendMessageResultUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class UpdateGroupCommand implements JsonRpcLocalCommand { public class UpdateGroupCommand implements JsonRpcLocalCommand {
@ -121,14 +124,13 @@ public class UpdateGroupCommand implements JsonRpcLocalCommand {
try { try {
boolean isNewGroup = false; boolean isNewGroup = false;
Long timestamp = null; SendGroupMessageResults groupMessageResults = null;
if (groupId == null) { if (groupId == null) {
isNewGroup = true; isNewGroup = true;
var results = m.createGroup(groupName, var results = m.createGroup(groupName,
groupMembers, groupMembers,
groupAvatar == null ? null : new File(groupAvatar)); groupAvatar == null ? null : new File(groupAvatar));
timestamp = results.second().timestamp(); groupMessageResults = results.second();
ErrorUtils.handleSendMessageResults(results.second().results());
groupId = results.first(); groupId = results.first();
groupName = null; groupName = null;
groupMembers = null; groupMembers = null;
@ -154,10 +156,15 @@ public class UpdateGroupCommand implements JsonRpcLocalCommand {
: groupSendMessagesPermission == GroupPermission.ONLY_ADMINS) : groupSendMessagesPermission == GroupPermission.ONLY_ADMINS)
.build()); .build());
if (results != null) { if (results != null) {
timestamp = results.timestamp(); if (groupMessageResults == null) {
ErrorUtils.handleSendMessageResults(results.results()); groupMessageResults = results;
} else {
groupMessageResults = new SendGroupMessageResults(results.timestamp(),
Stream.concat(groupMessageResults.results().stream(), results.results().stream())
.collect(Collectors.toList()));
}
} }
outputResult(outputWriter, timestamp, isNewGroup ? groupId : null); outputResult(outputWriter, groupMessageResults, isNewGroup ? groupId : null);
} catch (AttachmentInvalidException e) { } catch (AttachmentInvalidException e) {
throw new UserErrorException("Failed to add avatar attachment for group\": " + e.getMessage()); throw new UserErrorException("Failed to add avatar attachment for group\": " + e.getMessage());
} catch (GroupNotFoundException | NotAGroupMemberException | GroupSendingNotAllowedException e) { } catch (GroupNotFoundException | NotAGroupMemberException | GroupSendingNotAllowedException e) {
@ -168,24 +175,30 @@ public class UpdateGroupCommand implements JsonRpcLocalCommand {
} }
} }
private void outputResult(final OutputWriter outputWriter, final Long timestamp, final GroupId groupId) { private void outputResult(
final OutputWriter outputWriter, final SendGroupMessageResults results, final GroupId groupId
) {
if (outputWriter instanceof PlainTextWriter writer) { if (outputWriter instanceof PlainTextWriter writer) {
if (groupId != null) { if (groupId != null) {
writer.println("Created new group: \"{}\"", groupId.toBase64()); writer.println("Created new group: \"{}\"", groupId.toBase64());
} }
if (timestamp != null) { if (results != null) {
writer.println("{}", timestamp); var errors = SendMessageResultUtils.getErrorMessagesFromSendMessageResults(results.results());
SendMessageResultUtils.printSendMessageResultErrors(writer, errors);
writer.println("{}", results.timestamp());
} }
} else { } else {
final var writer = (JsonWriter) outputWriter; final var writer = (JsonWriter) outputWriter;
final var result = new HashMap<>(); final var response = new HashMap<>();
if (timestamp != null) { if (results != null) {
result.put("timestamp", timestamp); response.put("timestamp", results.timestamp());
var jsonResults = SendMessageResultUtils.getJsonSendMessageResults(results.results());
response.put("results", jsonResults);
} }
if (groupId != null) { if (groupId != null) {
result.put("groupId", groupId.toBase64()); response.put("groupId", groupId.toBase64());
} }
writer.write(result); writer.write(response);
} }
} }
} }

View file

@ -26,7 +26,7 @@ import org.asamk.signal.manager.groups.LastGroupAdminException;
import org.asamk.signal.manager.groups.NotAGroupMemberException; import org.asamk.signal.manager.groups.NotAGroupMemberException;
import org.asamk.signal.manager.storage.recipients.Profile; import org.asamk.signal.manager.storage.recipients.Profile;
import org.asamk.signal.manager.storage.recipients.RecipientAddress; import org.asamk.signal.manager.storage.recipients.RecipientAddress;
import org.asamk.signal.util.ErrorUtils; import org.asamk.signal.util.SendMessageResultUtils;
import org.freedesktop.dbus.DBusPath; import org.freedesktop.dbus.DBusPath;
import org.freedesktop.dbus.connections.impl.DBusConnection; import org.freedesktop.dbus.connections.impl.DBusConnection;
import org.freedesktop.dbus.exceptions.DBusException; import org.freedesktop.dbus.exceptions.DBusException;
@ -781,7 +781,7 @@ public class DbusSignalImpl implements Signal {
} }
private static void checkSendMessageResult(long timestamp, SendMessageResult result) throws DBusExecutionException { private static void checkSendMessageResult(long timestamp, SendMessageResult result) throws DBusExecutionException {
var error = ErrorUtils.getErrorMessageFromSendMessageResult(result); var error = SendMessageResultUtils.getErrorMessageFromSendMessageResult(result);
if (error == null) { if (error == null) {
return; return;
@ -805,7 +805,7 @@ public class DbusSignalImpl implements Signal {
return; return;
} }
var errors = ErrorUtils.getErrorMessagesFromSendMessageResults(results); var errors = SendMessageResultUtils.getErrorMessagesFromSendMessageResults(results);
if (errors.size() == 0) { if (errors.size() == 0) {
return; return;
} }
@ -828,7 +828,7 @@ public class DbusSignalImpl implements Signal {
return; return;
} }
var errors = ErrorUtils.getErrorMessagesFromSendMessageResults(results); var errors = SendMessageResultUtils.getErrorMessagesFromSendMessageResults(results);
if (errors.size() == 0) { if (errors.size() == 0) {
return; return;
} }

View file

@ -0,0 +1,12 @@
package org.asamk.signal.json;
import org.asamk.signal.manager.storage.recipients.RecipientAddress;
import java.util.UUID;
public record JsonRecipientAddress(String uuid, String number) {
public static JsonRecipientAddress from(RecipientAddress address) {
return new JsonRecipientAddress(address.uuid().map(UUID::toString).orElse(null), address.number().orElse(null));
}
}

View file

@ -0,0 +1,38 @@
package org.asamk.signal.json;
import com.fasterxml.jackson.annotation.JsonInclude;
import org.asamk.signal.manager.api.SendMessageResult;
import org.asamk.signal.manager.groups.GroupId;
public record JsonSendMessageResult(
JsonRecipientAddress recipientAddress,
@JsonInclude(JsonInclude.Include.NON_NULL) String groupId,
Type type,
@JsonInclude(JsonInclude.Include.NON_NULL) String token,
@JsonInclude(JsonInclude.Include.NON_NULL) Long retryAfterSeconds
) {
public static JsonSendMessageResult from(SendMessageResult result) {
return from(result, null);
}
public static JsonSendMessageResult from(SendMessageResult result, GroupId groupId) {
return new JsonSendMessageResult(JsonRecipientAddress.from(result.address()),
groupId != null ? groupId.toBase64() : null,
result.isSuccess()
? Type.SUCCESS
: result.isNetworkFailure()
? Type.NETWORK_FAILURE
: result.isUnregisteredFailure() ? Type.UNREGISTERED_FAILURE : Type.IDENTITY_FAILURE,
result.proofRequiredFailure() != null ? result.proofRequiredFailure().getToken() : null,
result.proofRequiredFailure() != null ? result.proofRequiredFailure().getRetryAfterSeconds() : null);
}
public enum Type {
SUCCESS,
NETWORK_FAILURE,
UNREGISTERED_FAILURE,
IDENTITY_FAILURE,
}
}

View file

@ -1,10 +1,14 @@
package org.asamk.signal.util; package org.asamk.signal.util;
import org.asamk.signal.commands.exceptions.CommandException; import org.asamk.signal.json.JsonSendMessageResult;
import org.asamk.signal.commands.exceptions.IOErrorException;
import org.asamk.signal.manager.api.ProofRequiredException; 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.SendGroupMessageResults;
import org.asamk.signal.manager.api.SendMessageResult; 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.Collection;
import java.util.List; import java.util.List;
@ -12,23 +16,33 @@ import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class ErrorUtils { public class SendMessageResultUtils {
private ErrorUtils() { private SendMessageResultUtils() {
} }
public static void handleSendMessageResults( public static void outputResult(final OutputWriter outputWriter, final SendGroupMessageResults sendMessageResults) {
Map<RecipientIdentifier, List<SendMessageResult>> mapResults if (outputWriter instanceof PlainTextWriter writer) {
) throws CommandException { var errors = getErrorMessagesFromSendMessageResults(sendMessageResults.results());
var errors = getErrorMessagesFromSendMessageResults(mapResults); printSendMessageResultErrors(writer, errors);
handleSendMessageResultErrors(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 handleSendMessageResults( public static void outputResult(final OutputWriter outputWriter, final SendMessageResults sendMessageResults) {
Collection<SendMessageResult> results if (outputWriter instanceof PlainTextWriter writer) {
) throws CommandException { var errors = getErrorMessagesFromSendMessageResults(sendMessageResults.results());
var errors = getErrorMessagesFromSendMessageResults(results); printSendMessageResultErrors(writer, errors);
handleSendMessageResultErrors(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) { public static List<String> getErrorMessagesFromSendMessageResults(final Map<RecipientIdentifier, List<SendMessageResult>> mapResults) {
@ -36,7 +50,7 @@ public class ErrorUtils {
.stream() .stream()
.flatMap(entry -> entry.getValue() .flatMap(entry -> entry.getValue()
.stream() .stream()
.map(ErrorUtils::getErrorMessageFromSendMessageResult) .map(SendMessageResultUtils::getErrorMessageFromSendMessageResult)
.filter(Objects::nonNull) .filter(Objects::nonNull)
.map(error -> entry.getKey().getIdentifier() + ": " + error)) .map(error -> entry.getKey().getIdentifier() + ": " + error))
.collect(Collectors.toList()); .collect(Collectors.toList());
@ -44,7 +58,7 @@ public class ErrorUtils {
public static List<String> getErrorMessagesFromSendMessageResults(Collection<SendMessageResult> results) { public static List<String> getErrorMessagesFromSendMessageResults(Collection<SendMessageResult> results) {
return results.stream() return results.stream()
.map(ErrorUtils::getErrorMessageFromSendMessageResult) .map(SendMessageResultUtils::getErrorMessageFromSendMessageResult)
.filter(Objects::nonNull) .filter(Objects::nonNull)
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
@ -82,15 +96,24 @@ public class ErrorUtils {
return null; return null;
} }
private static void handleSendMessageResultErrors(List<String> errors) throws CommandException { public static void printSendMessageResultErrors(PlainTextWriter writer, List<String> errors) {
if (errors.size() == 0) { if (errors.size() == 0) {
return; return;
} }
var message = new StringBuilder(); writer.println("Failed to send (some) messages:");
message.append("Failed to send (some) messages:\n");
for (var error : errors) { for (var error : errors) {
message.append(error).append("\n"); writer.println(error);
} }
throw new IOErrorException(message.toString(), null); }
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());
} }
} }