mirror of
https://github.com/AsamK/signal-cli
synced 2025-08-29 18:40: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
|
@ -13,13 +13,12 @@ import org.asamk.signal.manager.groups.GroupInviteLinkUrl;
|
|||
import org.asamk.signal.output.JsonWriter;
|
||||
import org.asamk.signal.output.OutputWriter;
|
||||
import org.asamk.signal.output.PlainTextWriter;
|
||||
import org.asamk.signal.util.SendMessageResultUtils;
|
||||
import org.freedesktop.dbus.exceptions.DBusExecutionException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.asamk.signal.util.ErrorUtils.handleSendMessageResults;
|
||||
|
||||
public class JoinGroupCommand implements JsonRpcLocalCommand {
|
||||
|
||||
@Override
|
||||
|
@ -55,10 +54,23 @@ public class JoinGroupCommand implements JsonRpcLocalCommand {
|
|||
final var results = m.joinGroup(linkUrl);
|
||||
var newGroupId = results.first();
|
||||
if (outputWriter instanceof JsonWriter writer) {
|
||||
var jsonResults = SendMessageResultUtils.getJsonSendMessageResults(results.second().results());
|
||||
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 {
|
||||
writer.write(Map.of("groupId", newGroupId.toBase64()));
|
||||
writer.write(Map.of("timestamp",
|
||||
results.second().timestamp(),
|
||||
"results",
|
||||
jsonResults,
|
||||
"groupId",
|
||||
newGroupId.toBase64()));
|
||||
}
|
||||
} else {
|
||||
final var writer = (PlainTextWriter) outputWriter;
|
||||
|
@ -67,8 +79,10 @@ public class JoinGroupCommand implements JsonRpcLocalCommand {
|
|||
} else {
|
||||
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) {
|
||||
throw new IOErrorException("Failed to send message: "
|
||||
+ e.getMessage()
|
||||
|
|
|
@ -11,17 +11,14 @@ import org.asamk.signal.manager.Manager;
|
|||
import org.asamk.signal.manager.groups.GroupNotFoundException;
|
||||
import org.asamk.signal.manager.groups.LastGroupAdminException;
|
||||
import org.asamk.signal.manager.groups.NotAGroupMemberException;
|
||||
import org.asamk.signal.output.JsonWriter;
|
||||
import org.asamk.signal.output.OutputWriter;
|
||||
import org.asamk.signal.output.PlainTextWriter;
|
||||
import org.asamk.signal.util.CommandUtil;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
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 {
|
||||
|
||||
|
@ -55,9 +52,7 @@ public class QuitGroupCommand implements JsonRpcLocalCommand {
|
|||
try {
|
||||
try {
|
||||
final var results = m.quitGroup(groupId, groupAdmins);
|
||||
final var timestamp = results.timestamp();
|
||||
outputResult(outputWriter, timestamp);
|
||||
handleSendMessageResults(results.results());
|
||||
outputResult(outputWriter, results);
|
||||
} catch (NotAGroupMemberException e) {
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,14 +11,12 @@ import org.asamk.signal.manager.Manager;
|
|||
import org.asamk.signal.manager.groups.GroupNotFoundException;
|
||||
import org.asamk.signal.manager.groups.GroupSendingNotAllowedException;
|
||||
import org.asamk.signal.manager.groups.NotAGroupMemberException;
|
||||
import org.asamk.signal.output.JsonWriter;
|
||||
import org.asamk.signal.output.OutputWriter;
|
||||
import org.asamk.signal.output.PlainTextWriter;
|
||||
import org.asamk.signal.util.CommandUtil;
|
||||
import org.asamk.signal.util.ErrorUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.asamk.signal.util.SendMessageResultUtils.outputResult;
|
||||
|
||||
public class RemoteDeleteCommand implements JsonRpcLocalCommand {
|
||||
|
||||
|
@ -56,8 +54,7 @@ public class RemoteDeleteCommand implements JsonRpcLocalCommand {
|
|||
|
||||
try {
|
||||
final var results = m.sendRemoteDeleteMessage(targetTimestamp, recipientIdentifiers);
|
||||
outputResult(outputWriter, results.timestamp());
|
||||
ErrorUtils.handleSendMessageResults(results.results());
|
||||
outputResult(outputWriter, results);
|
||||
} catch (GroupNotFoundException | NotAGroupMemberException | GroupSendingNotAllowedException e) {
|
||||
throw new UserErrorException(e.getMessage());
|
||||
} catch (IOException e) {
|
||||
|
@ -65,13 +62,4 @@ public class RemoteDeleteCommand implements JsonRpcLocalCommand {
|
|||
.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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,11 +14,8 @@ import org.asamk.signal.manager.api.RecipientIdentifier;
|
|||
import org.asamk.signal.manager.groups.GroupNotFoundException;
|
||||
import org.asamk.signal.manager.groups.GroupSendingNotAllowedException;
|
||||
import org.asamk.signal.manager.groups.NotAGroupMemberException;
|
||||
import org.asamk.signal.output.JsonWriter;
|
||||
import org.asamk.signal.output.OutputWriter;
|
||||
import org.asamk.signal.output.PlainTextWriter;
|
||||
import org.asamk.signal.util.CommandUtil;
|
||||
import org.asamk.signal.util.ErrorUtils;
|
||||
import org.asamk.signal.util.IOUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
@ -27,11 +24,12 @@ import java.io.IOException;
|
|||
import java.nio.charset.Charset;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static org.asamk.signal.util.SendMessageResultUtils.outputResult;
|
||||
|
||||
public class SendCommand implements JsonRpcLocalCommand {
|
||||
|
||||
private final static Logger logger = LoggerFactory.getLogger(SendCommand.class);
|
||||
|
@ -93,8 +91,7 @@ public class SendCommand implements JsonRpcLocalCommand {
|
|||
|
||||
try {
|
||||
final var results = m.sendEndSessionMessage(singleRecipients);
|
||||
outputResult(outputWriter, results.timestamp());
|
||||
ErrorUtils.handleSendMessageResults(results.results());
|
||||
outputResult(outputWriter, results);
|
||||
return;
|
||||
} catch (IOException e) {
|
||||
throw new UnexpectedErrorException("Failed to send message: " + e.getMessage() + " (" + e.getClass()
|
||||
|
@ -140,8 +137,7 @@ public class SendCommand implements JsonRpcLocalCommand {
|
|||
try {
|
||||
var results = m.sendMessage(new Message(messageText, attachments, mentions, Optional.ofNullable(quote)),
|
||||
recipientIdentifiers);
|
||||
outputResult(outputWriter, results.timestamp());
|
||||
ErrorUtils.handleSendMessageResults(results.results());
|
||||
outputResult(outputWriter, results);
|
||||
} catch (AttachmentInvalidException | IOException e) {
|
||||
throw new UnexpectedErrorException("Failed to send message: " + e.getMessage() + " (" + e.getClass()
|
||||
.getSimpleName() + ")", e);
|
||||
|
@ -168,13 +164,4 @@ public class SendCommand implements JsonRpcLocalCommand {
|
|||
}
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,14 +11,12 @@ import org.asamk.signal.manager.Manager;
|
|||
import org.asamk.signal.manager.groups.GroupNotFoundException;
|
||||
import org.asamk.signal.manager.groups.GroupSendingNotAllowedException;
|
||||
import org.asamk.signal.manager.groups.NotAGroupMemberException;
|
||||
import org.asamk.signal.output.JsonWriter;
|
||||
import org.asamk.signal.output.OutputWriter;
|
||||
import org.asamk.signal.output.PlainTextWriter;
|
||||
import org.asamk.signal.util.CommandUtil;
|
||||
import org.asamk.signal.util.ErrorUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.asamk.signal.util.SendMessageResultUtils.outputResult;
|
||||
|
||||
public class SendReactionCommand implements JsonRpcLocalCommand {
|
||||
|
||||
|
@ -72,8 +70,7 @@ public class SendReactionCommand implements JsonRpcLocalCommand {
|
|||
CommandUtil.getSingleRecipientIdentifier(targetAuthor, m.getSelfNumber()),
|
||||
targetTimestamp,
|
||||
recipientIdentifiers);
|
||||
outputResult(outputWriter, results.timestamp());
|
||||
ErrorUtils.handleSendMessageResults(results.results());
|
||||
outputResult(outputWriter, results);
|
||||
} catch (GroupNotFoundException | NotAGroupMemberException | GroupSendingNotAllowedException e) {
|
||||
throw new UserErrorException(e.getMessage());
|
||||
} catch (IOException e) {
|
||||
|
@ -81,13 +78,4 @@ public class SendReactionCommand implements JsonRpcLocalCommand {
|
|||
.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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,14 +7,12 @@ import org.asamk.signal.commands.exceptions.CommandException;
|
|||
import org.asamk.signal.commands.exceptions.UserErrorException;
|
||||
import org.asamk.signal.manager.Manager;
|
||||
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 org.asamk.signal.util.CommandUtil;
|
||||
import org.asamk.signal.util.ErrorUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.asamk.signal.util.SendMessageResultUtils.outputResult;
|
||||
|
||||
public class SendReceiptCommand implements JsonRpcLocalCommand {
|
||||
|
||||
|
@ -55,20 +53,10 @@ public class SendReceiptCommand implements JsonRpcLocalCommand {
|
|||
} else {
|
||||
throw new UserErrorException("Unknown receipt type: " + type);
|
||||
}
|
||||
outputResult(outputWriter, results.timestamp());
|
||||
ErrorUtils.handleSendMessageResults(results.results());
|
||||
outputResult(outputWriter, results);
|
||||
} catch (IOException e) {
|
||||
throw new UserErrorException("Failed to send message: " + e.getMessage() + " (" + e.getClass()
|
||||
.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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,15 +12,13 @@ import org.asamk.signal.manager.api.TypingAction;
|
|||
import org.asamk.signal.manager.groups.GroupNotFoundException;
|
||||
import org.asamk.signal.manager.groups.GroupSendingNotAllowedException;
|
||||
import org.asamk.signal.manager.groups.NotAGroupMemberException;
|
||||
import org.asamk.signal.output.JsonWriter;
|
||||
import org.asamk.signal.output.OutputWriter;
|
||||
import org.asamk.signal.output.PlainTextWriter;
|
||||
import org.asamk.signal.util.CommandUtil;
|
||||
import org.asamk.signal.util.ErrorUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.asamk.signal.util.SendMessageResultUtils.outputResult;
|
||||
|
||||
public class SendTypingCommand implements JsonRpcLocalCommand {
|
||||
|
||||
|
@ -61,8 +59,7 @@ public class SendTypingCommand implements JsonRpcLocalCommand {
|
|||
|
||||
try {
|
||||
final var results = m.sendTypingMessage(action, recipientIdentifiers);
|
||||
outputResult(outputWriter, results.timestamp());
|
||||
ErrorUtils.handleSendMessageResults(results.results());
|
||||
outputResult(outputWriter, results);
|
||||
} catch (IOException e) {
|
||||
throw new UserErrorException("Failed to send message: " + e.getMessage() + " (" + e.getClass()
|
||||
.getSimpleName() + ")");
|
||||
|
@ -70,13 +67,4 @@ public class SendTypingCommand implements JsonRpcLocalCommand {
|
|||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,6 +9,7 @@ import org.asamk.signal.commands.exceptions.UnexpectedErrorException;
|
|||
import org.asamk.signal.commands.exceptions.UserErrorException;
|
||||
import org.asamk.signal.manager.AttachmentInvalidException;
|
||||
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.groups.GroupId;
|
||||
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.PlainTextWriter;
|
||||
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.LoggerFactory;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class UpdateGroupCommand implements JsonRpcLocalCommand {
|
||||
|
||||
|
@ -121,14 +124,13 @@ public class UpdateGroupCommand implements JsonRpcLocalCommand {
|
|||
|
||||
try {
|
||||
boolean isNewGroup = false;
|
||||
Long timestamp = null;
|
||||
SendGroupMessageResults groupMessageResults = null;
|
||||
if (groupId == null) {
|
||||
isNewGroup = true;
|
||||
var results = m.createGroup(groupName,
|
||||
groupMembers,
|
||||
groupAvatar == null ? null : new File(groupAvatar));
|
||||
timestamp = results.second().timestamp();
|
||||
ErrorUtils.handleSendMessageResults(results.second().results());
|
||||
groupMessageResults = results.second();
|
||||
groupId = results.first();
|
||||
groupName = null;
|
||||
groupMembers = null;
|
||||
|
@ -154,10 +156,15 @@ public class UpdateGroupCommand implements JsonRpcLocalCommand {
|
|||
: groupSendMessagesPermission == GroupPermission.ONLY_ADMINS)
|
||||
.build());
|
||||
if (results != null) {
|
||||
timestamp = results.timestamp();
|
||||
ErrorUtils.handleSendMessageResults(results.results());
|
||||
if (groupMessageResults == null) {
|
||||
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) {
|
||||
throw new UserErrorException("Failed to add avatar attachment for group\": " + e.getMessage());
|
||||
} 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 (groupId != null) {
|
||||
writer.println("Created new group: \"{}\"", groupId.toBase64());
|
||||
}
|
||||
if (timestamp != null) {
|
||||
writer.println("{}", timestamp);
|
||||
if (results != null) {
|
||||
var errors = SendMessageResultUtils.getErrorMessagesFromSendMessageResults(results.results());
|
||||
SendMessageResultUtils.printSendMessageResultErrors(writer, errors);
|
||||
writer.println("{}", results.timestamp());
|
||||
}
|
||||
} else {
|
||||
final var writer = (JsonWriter) outputWriter;
|
||||
final var result = new HashMap<>();
|
||||
if (timestamp != null) {
|
||||
result.put("timestamp", timestamp);
|
||||
final var response = new HashMap<>();
|
||||
if (results != null) {
|
||||
response.put("timestamp", results.timestamp());
|
||||
var jsonResults = SendMessageResultUtils.getJsonSendMessageResults(results.results());
|
||||
response.put("results", jsonResults);
|
||||
}
|
||||
if (groupId != null) {
|
||||
result.put("groupId", groupId.toBase64());
|
||||
response.put("groupId", groupId.toBase64());
|
||||
}
|
||||
writer.write(result);
|
||||
writer.write(response);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue