X-Git-Url: https://git.nmode.ca/signal-cli/blobdiff_plain/1e0aa8929d059b0740da496f6f4ac62159e75141..b738f5740c94fe7a5df9e322e1345a99ef0c5ce5:/src/main/java/org/asamk/signal/dbus/DbusSignalImpl.java diff --git a/src/main/java/org/asamk/signal/dbus/DbusSignalImpl.java b/src/main/java/org/asamk/signal/dbus/DbusSignalImpl.java index 17cc2caa..d19116a4 100644 --- a/src/main/java/org/asamk/signal/dbus/DbusSignalImpl.java +++ b/src/main/java/org/asamk/signal/dbus/DbusSignalImpl.java @@ -2,21 +2,23 @@ package org.asamk.signal.dbus; import org.asamk.Signal; import org.asamk.signal.manager.AttachmentInvalidException; -import org.asamk.signal.manager.GroupNotFoundException; import org.asamk.signal.manager.Manager; -import org.asamk.signal.manager.NotAGroupMemberException; -import org.asamk.signal.storage.groups.GroupInfo; +import org.asamk.signal.manager.groups.GroupId; +import org.asamk.signal.manager.groups.GroupNotFoundException; +import org.asamk.signal.manager.groups.NotAGroupMemberException; +import org.asamk.signal.manager.storage.groups.GroupInfo; +import org.asamk.signal.util.ErrorUtils; import org.freedesktop.dbus.exceptions.DBusExecutionException; -import org.whispersystems.signalservice.api.crypto.UntrustedIdentityException; -import org.whispersystems.signalservice.api.push.exceptions.EncapsulatedExceptions; -import org.whispersystems.signalservice.api.push.exceptions.NetworkFailureException; -import org.whispersystems.signalservice.api.push.exceptions.UnregisteredUserException; +import org.whispersystems.libsignal.util.Pair; +import org.whispersystems.signalservice.api.messages.SendMessageResult; +import org.whispersystems.signalservice.api.push.SignalServiceAddress; import org.whispersystems.signalservice.api.util.InvalidNumberException; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.stream.Collectors; public class DbusSignalImpl implements Signal { @@ -43,41 +45,30 @@ public class DbusSignalImpl implements Signal { return sendMessage(message, attachments, recipients); } - private static DBusExecutionException convertEncapsulatedExceptions(EncapsulatedExceptions e) { - if (e.getNetworkExceptions().size() + e.getUnregisteredUserExceptions().size() + e.getUntrustedIdentityExceptions().size() == 1) { - if (e.getNetworkExceptions().size() == 1) { - NetworkFailureException n = e.getNetworkExceptions().get(0); - return new Error.Failure("Network failure for \"" + n.getE164number() + "\": " + n.getMessage()); - } else if (e.getUnregisteredUserExceptions().size() == 1) { - UnregisteredUserException n = e.getUnregisteredUserExceptions().get(0); - return new Error.UnregisteredUser("Unregistered user \"" + n.getE164Number() + "\": " + n.getMessage()); - } else if (e.getUntrustedIdentityExceptions().size() == 1) { - UntrustedIdentityException n = e.getUntrustedIdentityExceptions().get(0); - return new Error.UntrustedIdentity("Untrusted Identity for \"" + n.getIdentifier() + "\": " + n.getMessage()); - } + private static void checkSendMessageResults( + long timestamp, List results + ) throws DBusExecutionException { + List errors = ErrorUtils.getErrorMessagesFromSendMessageResults(results); + if (errors.size() == 0) { + return; } StringBuilder message = new StringBuilder(); - message.append("Failed to send (some) messages:").append('\n'); - for (NetworkFailureException n : e.getNetworkExceptions()) { - message.append("Network failure for \"").append(n.getE164number()).append("\": ").append(n.getMessage()).append('\n'); - } - for (UnregisteredUserException n : e.getUnregisteredUserExceptions()) { - message.append("Unregistered user \"").append(n.getE164Number()).append("\": ").append(n.getMessage()).append('\n'); - } - for (UntrustedIdentityException n : e.getUntrustedIdentityExceptions()) { - message.append("Untrusted Identity for \"").append(n.getIdentifier()).append("\": ").append(n.getMessage()).append('\n'); + message.append(timestamp).append('\n'); + message.append("Failed to send (some) messages:\n"); + for (String error : errors) { + message.append(error).append('\n'); } - return new Error.Failure(message.toString()); + throw new Error.Failure(message.toString()); } @Override public long sendMessage(final String message, final List attachments, final List recipients) { try { - return m.sendMessage(message, attachments, recipients); - } catch (EncapsulatedExceptions e) { - throw convertEncapsulatedExceptions(e); + final Pair> results = m.sendMessage(message, attachments, recipients); + checkSendMessageResults(results.first(), results.second()); + return results.first(); } catch (InvalidNumberException e) { throw new Error.InvalidNumber(e.getMessage()); } catch (AttachmentInvalidException e) { @@ -90,11 +81,10 @@ public class DbusSignalImpl implements Signal { @Override public void sendEndSessionMessage(final List recipients) { try { - m.sendEndSessionMessage(recipients); + final Pair> results = m.sendEndSessionMessage(recipients); + checkSendMessageResults(results.first(), results.second()); } catch (IOException e) { throw new Error.Failure(e.getMessage()); - } catch (EncapsulatedExceptions e) { - throw convertEncapsulatedExceptions(e); } catch (InvalidNumberException e) { throw new Error.InvalidNumber(e.getMessage()); } @@ -103,11 +93,13 @@ public class DbusSignalImpl implements Signal { @Override public long sendGroupMessage(final String message, final List attachments, final byte[] groupId) { try { - return m.sendGroupMessage(message, attachments, groupId); + Pair> results = m.sendGroupMessage(message, + attachments, + GroupId.unknownVersion(groupId)); + checkSendMessageResults(results.first(), results.second()); + return results.first(); } catch (IOException e) { throw new Error.Failure(e.getMessage()); - } catch (EncapsulatedExceptions e) { - throw convertEncapsulatedExceptions(e); } catch (GroupNotFoundException | NotAGroupMemberException e) { throw new Error.GroupNotFound(e.getMessage()); } catch (AttachmentInvalidException e) { @@ -145,7 +137,7 @@ public class DbusSignalImpl implements Signal { @Override public void setGroupBlocked(final byte[] groupId, final boolean blocked) { try { - m.setGroupBlocked(groupId, blocked); + m.setGroupBlocked(GroupId.unknownVersion(groupId), blocked); } catch (GroupNotFoundException e) { throw new Error.GroupNotFound(e.getMessage()); } @@ -156,39 +148,57 @@ public class DbusSignalImpl implements Signal { List groups = m.getGroups(); List ids = new ArrayList<>(groups.size()); for (GroupInfo group : groups) { - ids.add(group.groupId); + ids.add(group.getGroupId().serialize()); } return ids; } @Override public String getGroupName(final byte[] groupId) { - GroupInfo group = m.getGroup(groupId); + GroupInfo group = m.getGroup(GroupId.unknownVersion(groupId)); if (group == null) { return ""; } else { - return group.name; + return group.getTitle(); } } @Override public List getGroupMembers(final byte[] groupId) { - GroupInfo group = m.getGroup(groupId); + GroupInfo group = m.getGroup(GroupId.unknownVersion(groupId)); if (group == null) { return Collections.emptyList(); } else { - return new ArrayList<>(group.getMembersE164()); + return group.getMembers() + .stream() + .map(m::resolveSignalServiceAddress) + .map(SignalServiceAddress::getLegacyIdentifier) + .collect(Collectors.toList()); } } @Override - public byte[] updateGroup(final byte[] groupId, final String name, final List members, final String avatar) { + public byte[] updateGroup(byte[] groupId, String name, List members, String avatar) { try { - return m.updateGroup(groupId, name, members, avatar); + if (groupId.length == 0) { + groupId = null; + } + if (name.isEmpty()) { + name = null; + } + if (members.isEmpty()) { + members = null; + } + if (avatar.isEmpty()) { + avatar = null; + } + final Pair> results = m.updateGroup(groupId == null + ? null + : GroupId.unknownVersion(groupId), name, members, avatar); + checkSendMessageResults(0, results.second()); + return results.first().serialize(); } catch (IOException e) { throw new Error.Failure(e.getMessage()); - } catch (EncapsulatedExceptions e) { - throw convertEncapsulatedExceptions(e); } catch (GroupNotFoundException | NotAGroupMemberException e) { throw new Error.GroupNotFound(e.getMessage()); } catch (InvalidNumberException e) {