X-Git-Url: https://git.nmode.ca/signal-cli/blobdiff_plain/53f47d42fc30a86a9bb6cd08f4678a756f4a4aaf..e5a67d6ce1312fe118e99b8bc8fb2f55ed1dbcf2:/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 b4485e32..db7253bd 100644 --- a/src/main/java/org/asamk/signal/dbus/DbusSignalImpl.java +++ b/src/main/java/org/asamk/signal/dbus/DbusSignalImpl.java @@ -4,14 +4,22 @@ import org.asamk.Signal; import org.asamk.signal.BaseConfig; import org.asamk.signal.manager.Manager; import org.asamk.signal.manager.api.AttachmentInvalidException; -import org.asamk.signal.manager.api.Identity; +import org.asamk.signal.manager.api.GroupId; +import org.asamk.signal.manager.api.GroupInviteLinkUrl; +import org.asamk.signal.manager.api.GroupLinkState; +import org.asamk.signal.manager.api.GroupNotFoundException; +import org.asamk.signal.manager.api.GroupPermission; +import org.asamk.signal.manager.api.GroupSendingNotAllowedException; import org.asamk.signal.manager.api.InactiveGroupLinkException; import org.asamk.signal.manager.api.InvalidDeviceLinkException; import org.asamk.signal.manager.api.InvalidNumberException; import org.asamk.signal.manager.api.InvalidStickerException; +import org.asamk.signal.manager.api.LastGroupAdminException; import org.asamk.signal.manager.api.Message; -import org.asamk.signal.manager.api.NotMasterDeviceException; -import org.asamk.signal.manager.api.Pair; +import org.asamk.signal.manager.api.NotAGroupMemberException; +import org.asamk.signal.manager.api.NotPrimaryDeviceException; +import org.asamk.signal.manager.api.PendingAdminApprovalException; +import org.asamk.signal.manager.api.RecipientAddress; import org.asamk.signal.manager.api.RecipientIdentifier; import org.asamk.signal.manager.api.SendMessageResult; import org.asamk.signal.manager.api.SendMessageResults; @@ -19,17 +27,8 @@ import org.asamk.signal.manager.api.StickerPackInvalidException; import org.asamk.signal.manager.api.TypingAction; import org.asamk.signal.manager.api.UnregisteredRecipientException; import org.asamk.signal.manager.api.UpdateGroup; +import org.asamk.signal.manager.api.UpdateProfile; import org.asamk.signal.manager.api.UserStatus; -import org.asamk.signal.manager.groups.GroupId; -import org.asamk.signal.manager.groups.GroupInviteLinkUrl; -import org.asamk.signal.manager.groups.GroupLinkState; -import org.asamk.signal.manager.groups.GroupNotFoundException; -import org.asamk.signal.manager.groups.GroupPermission; -import org.asamk.signal.manager.groups.GroupSendingNotAllowedException; -import org.asamk.signal.manager.groups.LastGroupAdminException; -import org.asamk.signal.manager.groups.NotAGroupMemberException; -import org.asamk.signal.manager.storage.recipients.Profile; -import org.asamk.signal.manager.storage.recipients.RecipientAddress; import org.asamk.signal.util.SendMessageResultUtils; import org.freedesktop.dbus.DBusPath; import org.freedesktop.dbus.connections.impl.DBusConnection; @@ -55,7 +54,6 @@ import java.util.Objects; import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; -import java.util.stream.Stream; public class DbusSignalImpl implements Signal { @@ -219,7 +217,10 @@ public class DbusSignalImpl implements Signal { attachments, List.of(), Optional.empty(), - Optional.empty()), + Optional.empty(), + List.of(), + Optional.empty(), + List.of()), getSingleRecipientIdentifiers(recipients, m.getSelfNumber()).stream() .map(RecipientIdentifier.class::cast) .collect(Collectors.toSet())); @@ -288,7 +289,8 @@ public class DbusSignalImpl implements Signal { targetSentTimestamp, getSingleRecipientIdentifiers(recipients, m.getSelfNumber()).stream() .map(RecipientIdentifier.class::cast) - .collect(Collectors.toSet())); + .collect(Collectors.toSet()), + false); checkSendMessageResults(results); return results.timestamp(); } catch (IOException e) { @@ -300,6 +302,21 @@ public class DbusSignalImpl implements Signal { } } + @Override + public long sendPaymentNotification( + final byte[] receipt, final String note, final String recipient + ) throws Error.Failure { + try { + final var results = m.sendPaymentNotificationMessage(receipt, + note, + getSingleRecipientIdentifier(recipient, m.getSelfNumber())); + checkSendMessageResults(results); + return results.timestamp(); + } catch (IOException e) { + throw new Error.Failure(e.getMessage()); + } + } + @Override public void sendTyping( final String recipient, final boolean stop @@ -370,7 +387,10 @@ public class DbusSignalImpl implements Signal { attachments, List.of(), Optional.empty(), - Optional.empty()), Set.of(RecipientIdentifier.NoteToSelf.INSTANCE)); + Optional.empty(), + List.of(), + Optional.empty(), + List.of()), Set.of(RecipientIdentifier.NoteToSelf.INSTANCE)); checkSendMessageResults(results); return results.timestamp(); } catch (AttachmentInvalidException e) { @@ -411,7 +431,10 @@ public class DbusSignalImpl implements Signal { attachments, List.of(), Optional.empty(), - Optional.empty()), Set.of(getGroupRecipientIdentifier(groupId))); + Optional.empty(), + List.of(), + Optional.empty(), + List.of()), Set.of(getGroupRecipientIdentifier(groupId))); checkSendMessageResults(results); return results.timestamp(); } catch (IOException | InvalidStickerException e) { @@ -469,7 +492,8 @@ public class DbusSignalImpl implements Signal { remove, getSingleRecipientIdentifier(targetAuthor, m.getSelfNumber()), targetSentTimestamp, - Set.of(getGroupRecipientIdentifier(groupId))); + Set.of(getGroupRecipientIdentifier(groupId)), + false); checkSendMessageResults(results); return results.timestamp(); } catch (IOException e) { @@ -492,8 +516,8 @@ public class DbusSignalImpl implements Signal { @Override public void setContactName(final String number, final String name) { try { - m.setContactName(getSingleRecipientIdentifier(number, m.getSelfNumber()), name); - } catch (NotMasterDeviceException e) { + m.setContactName(getSingleRecipientIdentifier(number, m.getSelfNumber()), name, ""); + } catch (NotPrimaryDeviceException e) { throw new Error.Failure("This command doesn't work on linked devices."); } catch (IOException e) { throw new Error.Failure("Contact is not registered."); @@ -517,7 +541,7 @@ public class DbusSignalImpl implements Signal { public void setContactBlocked(final String number, final boolean blocked) { try { m.setContactsBlocked(List.of(getSingleRecipientIdentifier(number, m.getSelfNumber())), blocked); - } catch (NotMasterDeviceException e) { + } catch (NotPrimaryDeviceException e) { throw new Error.Failure("This command doesn't work on linked devices."); } catch (IOException e) { throw new Error.Failure(e.getMessage()); @@ -530,7 +554,7 @@ public class DbusSignalImpl implements Signal { public void setGroupBlocked(final byte[] groupId, final boolean blocked) { try { m.setGroupsBlocked(List.of(getGroupId(groupId)), blocked); - } catch (NotMasterDeviceException e) { + } catch (NotPrimaryDeviceException e) { throw new Error.Failure("This command doesn't work on linked devices."); } catch (GroupNotFoundException e) { throw new Error.GroupNotFound(e.getMessage()); @@ -597,7 +621,7 @@ public class DbusSignalImpl implements Signal { avatar = nullIfEmpty(avatar); final var memberIdentifiers = getSingleRecipientIdentifiers(members, m.getSelfNumber()); if (groupId == null) { - final var results = m.createGroup(name, memberIdentifiers, avatar == null ? null : new File(avatar)); + final var results = m.createGroup(name, memberIdentifiers, avatar); updateGroups(); checkGroupSendMessageResults(results.second().timestamp(), results.second().results()); return results.first().serialize(); @@ -606,7 +630,7 @@ public class DbusSignalImpl implements Signal { UpdateGroup.newBuilder() .withName(name) .withMembers(memberIdentifiers) - .withAvatarFile(avatar == null ? null : new File(avatar)) + .withAvatarFile(avatar) .build()); if (results != null) { checkGroupSendMessageResults(results.timestamp(), results.results()); @@ -666,10 +690,15 @@ public class DbusSignalImpl implements Signal { about = nullIfEmpty(about); aboutEmoji = nullIfEmpty(aboutEmoji); avatarPath = nullIfEmpty(avatarPath); - Optional avatarFile = removeAvatar - ? Optional.empty() - : avatarPath == null ? null : Optional.of(new File(avatarPath)); - m.setProfile(givenName, familyName, about, aboutEmoji, avatarFile); + final var avatarFile = removeAvatar || avatarPath == null ? null : avatarPath; + m.updateProfile(UpdateProfile.newBuilder() + .withGivenName(givenName) + .withFamilyName(familyName) + .withAbout(about) + .withAboutEmoji(aboutEmoji) + .withAvatar(avatarFile) + .withDeleteAvatar(removeAvatar) + .build()); } catch (IOException e) { throw new Error.Failure(e.getMessage()); } @@ -692,7 +721,7 @@ public class DbusSignalImpl implements Signal { m.setRegistrationLockPin(Optional.empty()); } catch (IOException e) { throw new Error.Failure("Remove pin error: " + e.getMessage()); - } catch (NotMasterDeviceException e) { + } catch (NotPrimaryDeviceException e) { throw new Error.Failure("This command doesn't work on linked devices."); } } @@ -703,7 +732,7 @@ public class DbusSignalImpl implements Signal { m.setRegistrationLockPin(Optional.of(registrationLockPin)); } catch (IOException e) { throw new Error.Failure("Set pin error: " + e.getMessage()); - } catch (NotMasterDeviceException e) { + } catch (NotPrimaryDeviceException e) { throw new Error.Failure("This command doesn't work on linked devices."); } } @@ -719,9 +748,9 @@ public class DbusSignalImpl implements Signal { // all numbers the system knows @Override public List listNumbers() { - return Stream.concat(m.getIdentities().stream().map(Identity::recipient), - m.getContacts().stream().map(Pair::first)) - .map(a -> a.number().orElse(null)) + return m.getRecipients(false, Optional.empty(), Set.of(), Optional.empty()) + .stream() + .map(r -> r.getAddress().number().orElse(null)) .filter(Objects::nonNull) .distinct() .toList(); @@ -729,30 +758,10 @@ public class DbusSignalImpl implements Signal { @Override public List getContactNumber(final String name) { - // Contact names have precedence. - var numbers = new ArrayList(); - var contacts = m.getContacts(); - for (var c : contacts) { - if (name.equals(c.second().getName())) { - numbers.add(c.first().getLegacyIdentifier()); - } - } - // Try profiles if no contact name was found - for (var identity : m.getIdentities()) { - final var address = identity.recipient(); - var number = address.number().orElse(null); - if (number != null) { - Profile profile = null; - try { - profile = m.getRecipientProfile(RecipientIdentifier.Single.fromAddress(address)); - } catch (IOException | UnregisteredRecipientException ignored) { - } - if (profile != null && profile.getDisplayName().equals(name)) { - numbers.add(number); - } - } - } - return numbers; + return m.getRecipients(false, Optional.empty(), Set.of(), Optional.of(name)) + .stream() + .map(r -> r.getAddress().getLegacyIdentifier()) + .toList(); } @Override @@ -778,6 +787,8 @@ public class DbusSignalImpl implements Signal { } final var result = m.joinGroup(linkUrl); return result.first().serialize(); + } catch (PendingAdminApprovalException e) { + throw new Error.Failure("Pending admin approval: " + e.getMessage()); } catch (GroupInviteLinkUrl.InvalidGroupLinkException | InactiveGroupLinkException e) { throw new Error.Failure("Group link is invalid: " + e.getMessage()); } catch (GroupInviteLinkUrl.UnknownGroupLinkVersionException e) { @@ -1110,7 +1121,7 @@ public class DbusSignalImpl implements Signal { Optional.ofNullable(linkPreviews))); } catch (IOException e) { throw new Error.Failure("UpdateAccount error: " + e.getMessage()); - } catch (NotMasterDeviceException e) { + } catch (NotPrimaryDeviceException e) { throw new Error.Failure("This command doesn't work on linked devices."); } } @@ -1185,8 +1196,10 @@ public class DbusSignalImpl implements Signal { public void quitGroup() throws Error.Failure { try { m.quitGroup(groupId, Set.of()); - } catch (GroupNotFoundException | NotAGroupMemberException e) { + } catch (GroupNotFoundException e) { throw new Error.GroupNotFound(e.getMessage()); + } catch (NotAGroupMemberException e) { + throw new Error.NotAGroupMember(e.getMessage()); } catch (IOException e) { throw new Error.Failure(e.getMessage()); } catch (LastGroupAdminException e) { @@ -1262,7 +1275,7 @@ public class DbusSignalImpl implements Signal { } private void setGroupAvatar(final String avatar) { - updateGroup(UpdateGroup.newBuilder().withAvatarFile(new File(avatar)).build()); + updateGroup(UpdateGroup.newBuilder().withAvatarFile(avatar).build()); } private void setMessageExpirationTime(final int expirationTime) { @@ -1288,7 +1301,7 @@ public class DbusSignalImpl implements Signal { private void setIsBlocked(final boolean isBlocked) { try { m.setGroupsBlocked(List.of(groupId), isBlocked); - } catch (NotMasterDeviceException e) { + } catch (NotPrimaryDeviceException e) { throw new Error.Failure("This command doesn't work on linked devices."); } catch (GroupNotFoundException e) { throw new Error.GroupNotFound(e.getMessage());