X-Git-Url: https://git.nmode.ca/signal-cli/blobdiff_plain/b738f5740c94fe7a5df9e322e1345a99ef0c5ce5..de273586b4106171a3940ce5ec2c2b3712430c8c:/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 d19116a4..8cbbbc03 100644 --- a/src/main/java/org/asamk/signal/dbus/DbusSignalImpl.java +++ b/src/main/java/org/asamk/signal/dbus/DbusSignalImpl.java @@ -6,17 +6,16 @@ import org.asamk.signal.manager.Manager; 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.libsignal.util.Pair; +import org.whispersystems.libsignal.util.guava.Optional; import org.whispersystems.signalservice.api.messages.SendMessageResult; import org.whispersystems.signalservice.api.push.SignalServiceAddress; import org.whispersystems.signalservice.api.util.InvalidNumberException; +import java.io.File; import java.io.IOException; import java.util.ArrayList; -import java.util.Collections; import java.util.List; import java.util.stream.Collectors; @@ -40,23 +39,44 @@ public class DbusSignalImpl implements Signal { @Override public long sendMessage(final String message, final List attachments, final String recipient) { - List recipients = new ArrayList<>(1); + var recipients = new ArrayList(1); recipients.add(recipient); return sendMessage(message, attachments, recipients); } + private static void checkSendMessageResult(long timestamp, SendMessageResult result) throws DBusExecutionException { + var error = ErrorUtils.getErrorMessageFromSendMessageResult(result); + + if (error == null) { + return; + } + + final var message = timestamp + "\nFailed to send message:\n" + error + '\n'; + + if (result.getIdentityFailure() != null) { + throw new Error.UntrustedIdentity(message); + } else { + throw new Error.Failure(message); + } + } + private static void checkSendMessageResults( long timestamp, List results ) throws DBusExecutionException { - List errors = ErrorUtils.getErrorMessagesFromSendMessageResults(results); + if (results.size() == 1) { + checkSendMessageResult(timestamp, results.get(0)); + return; + } + + var errors = ErrorUtils.getErrorMessagesFromSendMessageResults(results); if (errors.size() == 0) { return; } - StringBuilder message = new StringBuilder(); + var message = new StringBuilder(); message.append(timestamp).append('\n'); message.append("Failed to send (some) messages:\n"); - for (String error : errors) { + for (var error : errors) { message.append(error).append('\n'); } @@ -66,7 +86,7 @@ public class DbusSignalImpl implements Signal { @Override public long sendMessage(final String message, final List attachments, final List recipients) { try { - final Pair> results = m.sendMessage(message, attachments, recipients); + final var results = m.sendMessage(message, attachments, recipients); checkSendMessageResults(results.first(), results.second()); return results.first(); } catch (InvalidNumberException e) { @@ -78,10 +98,25 @@ public class DbusSignalImpl implements Signal { } } + @Override + public long sendNoteToSelfMessage( + final String message, final List attachments + ) throws Error.AttachmentInvalid, Error.Failure, Error.UntrustedIdentity { + try { + final var results = m.sendSelfMessage(message, attachments); + checkSendMessageResult(results.first(), results.second()); + return results.first(); + } catch (AttachmentInvalidException e) { + throw new Error.AttachmentInvalid(e.getMessage()); + } catch (IOException e) { + throw new Error.Failure(e.getMessage()); + } + } + @Override public void sendEndSessionMessage(final List recipients) { try { - final Pair> results = m.sendEndSessionMessage(recipients); + final var results = m.sendEndSessionMessage(recipients); checkSendMessageResults(results.first(), results.second()); } catch (IOException e) { throw new Error.Failure(e.getMessage()); @@ -93,9 +128,7 @@ public class DbusSignalImpl implements Signal { @Override public long sendGroupMessage(final String message, final List attachments, final byte[] groupId) { try { - Pair> results = m.sendGroupMessage(message, - attachments, - GroupId.unknownVersion(groupId)); + var results = m.sendGroupMessage(message, attachments, GroupId.unknownVersion(groupId)); checkSendMessageResults(results.first(), results.second()); return results.first(); } catch (IOException e) { @@ -145,9 +178,9 @@ public class DbusSignalImpl implements Signal { @Override public List getGroupIds() { - List groups = m.getGroups(); - List ids = new ArrayList<>(groups.size()); - for (GroupInfo group : groups) { + var groups = m.getGroups(); + var ids = new ArrayList(groups.size()); + for (var group : groups) { ids.add(group.getGroupId().serialize()); } return ids; @@ -155,7 +188,7 @@ public class DbusSignalImpl implements Signal { @Override public String getGroupName(final byte[] groupId) { - GroupInfo group = m.getGroup(GroupId.unknownVersion(groupId)); + var group = m.getGroup(GroupId.unknownVersion(groupId)); if (group == null) { return ""; } else { @@ -165,9 +198,9 @@ public class DbusSignalImpl implements Signal { @Override public List getGroupMembers(final byte[] groupId) { - GroupInfo group = m.getGroup(GroupId.unknownVersion(groupId)); + var group = m.getGroup(GroupId.unknownVersion(groupId)); if (group == null) { - return Collections.emptyList(); + return List.of(); } else { return group.getMembers() .stream() @@ -192,9 +225,10 @@ public class DbusSignalImpl implements Signal { if (avatar.isEmpty()) { avatar = null; } - final Pair> results = m.updateGroup(groupId == null - ? null - : GroupId.unknownVersion(groupId), name, members, avatar); + final var results = m.updateGroup(groupId == null ? null : GroupId.unknownVersion(groupId), + name, + members, + avatar == null ? null : new File(avatar)); checkSendMessageResults(0, results.second()); return results.first().serialize(); } catch (IOException e) { @@ -212,4 +246,26 @@ public class DbusSignalImpl implements Signal { public boolean isRegistered() { return true; } + + @Override + public void updateProfile( + final String name, + final String about, + final String aboutEmoji, + String avatarPath, + final boolean removeAvatar + ) { + try { + if (avatarPath.isEmpty()) { + avatarPath = null; + } + Optional avatarFile = removeAvatar + ? Optional.absent() + : avatarPath == null ? null : Optional.of(new File(avatarPath)); + m.setProfile(name, about, aboutEmoji, avatarFile); + } catch (IOException e) { + throw new Error.Failure(e.getMessage()); + } + } + }