X-Git-Url: https://git.nmode.ca/signal-cli/blobdiff_plain/ccb37c00f6fed576664427a907fcecc21d2dbc67..8a31b7f2c153e89532010b9ab58eb045ddfe43fe:/lib/src/main/java/org/asamk/signal/manager/ManagerImpl.java diff --git a/lib/src/main/java/org/asamk/signal/manager/ManagerImpl.java b/lib/src/main/java/org/asamk/signal/manager/ManagerImpl.java index 652b4e76..1cc0629b 100644 --- a/lib/src/main/java/org/asamk/signal/manager/ManagerImpl.java +++ b/lib/src/main/java/org/asamk/signal/manager/ManagerImpl.java @@ -25,6 +25,7 @@ import org.asamk.signal.manager.api.Identity; import org.asamk.signal.manager.api.InactiveGroupLinkException; import org.asamk.signal.manager.api.InvalidDeviceLinkException; import org.asamk.signal.manager.api.InvalidStickerException; +import org.asamk.signal.manager.api.InvalidUsernameException; import org.asamk.signal.manager.api.Message; import org.asamk.signal.manager.api.MessageEnvelope; import org.asamk.signal.manager.api.NotPrimaryDeviceException; @@ -65,6 +66,7 @@ import org.asamk.signal.manager.util.AttachmentUtils; import org.asamk.signal.manager.util.KeyUtils; import org.asamk.signal.manager.util.MimeUtils; import org.asamk.signal.manager.util.StickerUtils; +import org.signal.libsignal.usernames.BaseUsernameException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.whispersystems.signalservice.api.SignalSessionLock; @@ -290,6 +292,24 @@ class ManagerImpl implements Manager { context.getSyncHelper().sendSyncFetchProfileMessage(); } + void refreshCurrentUsername() throws IOException, BaseUsernameException { + context.getAccountHelper().refreshCurrentUsername(); + } + + @Override + public String setUsername(final String username) throws IOException, InvalidUsernameException { + try { + return context.getAccountHelper().reserveUsername(username); + } catch (BaseUsernameException e) { + throw new InvalidUsernameException(e.getMessage() + " (" + e.getClass().getSimpleName() + ")", e); + } + } + + @Override + public void deleteUsername() throws IOException { + context.getAccountHelper().deleteUsername(); + } + @Override public void unregister() throws IOException { context.getAccountHelper().unregister(); @@ -394,7 +414,7 @@ class ManagerImpl implements Manager { @Override public Pair createGroup( - String name, Set members, File avatarFile + String name, Set members, String avatarFile ) throws IOException, AttachmentInvalidException, UnregisteredRecipientException { return context.getGroupHelper() .createGroup(name, @@ -446,6 +466,14 @@ class ManagerImpl implements Manager { private SendMessageResults sendMessage( SignalServiceDataMessage.Builder messageBuilder, Set recipients + ) throws IOException, NotAGroupMemberException, GroupNotFoundException, GroupSendingNotAllowedException { + return sendMessage(messageBuilder, recipients, Optional.empty()); + } + + private SendMessageResults sendMessage( + SignalServiceDataMessage.Builder messageBuilder, + Set recipients, + Optional editTargetTimestamp ) throws IOException, NotAGroupMemberException, GroupNotFoundException, GroupSendingNotAllowedException { var results = new HashMap>(); long timestamp = System.currentTimeMillis(); @@ -454,17 +482,19 @@ class ManagerImpl implements Manager { if (recipient instanceof RecipientIdentifier.Single single) { try { final var recipientId = context.getRecipientHelper().resolveRecipient(single); - final var result = context.getSendHelper().sendMessage(messageBuilder, recipientId); + final var result = context.getSendHelper() + .sendMessage(messageBuilder, recipientId, editTargetTimestamp); results.put(recipient, List.of(toSendMessageResult(result))); } catch (UnregisteredRecipientException e) { results.put(recipient, List.of(SendMessageResult.unregisteredFailure(single.toPartialRecipientAddress()))); } } else if (recipient instanceof RecipientIdentifier.NoteToSelf) { - final var result = context.getSendHelper().sendSelfMessage(messageBuilder); + final var result = context.getSendHelper().sendSelfMessage(messageBuilder, editTargetTimestamp); results.put(recipient, List.of(toSendMessageResult(result))); } else if (recipient instanceof RecipientIdentifier.Group group) { - final var result = context.getSendHelper().sendAsGroupMessage(messageBuilder, group.groupId()); + final var result = context.getSendHelper() + .sendAsGroupMessage(messageBuilder, group.groupId(), editTargetTimestamp); results.put(recipient, result.stream().map(this::toSendMessageResult).toList()); } } @@ -561,6 +591,15 @@ class ManagerImpl implements Manager { return sendMessage(messageBuilder, recipients); } + @Override + public SendMessageResults sendEditMessage( + Message message, Set recipients, long editTargetTimestamp + ) throws IOException, AttachmentInvalidException, NotAGroupMemberException, GroupNotFoundException, GroupSendingNotAllowedException, UnregisteredRecipientException, InvalidStickerException { + final var messageBuilder = SignalServiceDataMessage.newBuilder(); + applyMessage(messageBuilder, message); + return sendMessage(messageBuilder, recipients, Optional.of(editTargetTimestamp)); + } + private void applyMessage( final SignalServiceDataMessage.Builder messageBuilder, final Message message ) throws AttachmentInvalidException, IOException, UnregisteredRecipientException, InvalidStickerException { @@ -569,7 +608,7 @@ class ManagerImpl implements Manager { final var textAttachment = AttachmentUtils.createAttachmentStream(new StreamDetails(new ByteArrayInputStream( messageBytes), MimeUtils.LONG_TEXT, messageBytes.length), Optional.empty()); messageBuilder.withBody(message.messageText().substring(0, 2000)); - messageBuilder.withAttachment(textAttachment); + messageBuilder.withAttachment(context.getAttachmentHelper().uploadAttachment(textAttachment)); } else { messageBuilder.withBody(message.messageText()); } @@ -588,7 +627,8 @@ class ManagerImpl implements Manager { quote.message(), List.of(), resolveMentions(quote.mentions()), - SignalServiceDataMessage.Quote.Type.NORMAL)); + SignalServiceDataMessage.Quote.Type.NORMAL, + List.of())); } if (message.sticker().isPresent()) { final var sticker = message.sticker().get(); @@ -655,14 +695,17 @@ class ManagerImpl implements Manager { var delete = new SignalServiceDataMessage.RemoteDelete(targetSentTimestamp); final var messageBuilder = SignalServiceDataMessage.newBuilder().withRemoteDelete(delete); for (final var recipient : recipients) { - if (recipient instanceof RecipientIdentifier.Single r) { + if (recipient instanceof RecipientIdentifier.Uuid u) { + account.getMessageSendLogStore() + .deleteEntryForRecipientNonGroup(targetSentTimestamp, ServiceId.from(u.uuid())); + } else if (recipient instanceof RecipientIdentifier.Single r) { try { final var recipientId = context.getRecipientHelper().resolveRecipient(r); - account.getMessageSendLogStore() - .deleteEntryForRecipientNonGroup(targetSentTimestamp, - account.getRecipientAddressResolver() - .resolveRecipientAddress(recipientId) - .getServiceId()); + final var address = account.getRecipientAddressResolver().resolveRecipientAddress(recipientId); + if (address.serviceId().isPresent()) { + account.getMessageSendLogStore() + .deleteEntryForRecipientNonGroup(targetSentTimestamp, address.serviceId().get()); + } } catch (UnregisteredRecipientException ignored) { } } else if (recipient instanceof RecipientIdentifier.Group r) { @@ -728,21 +771,28 @@ class ManagerImpl implements Manager { final var serviceId = context.getAccount() .getRecipientAddressResolver() .resolveRecipientAddress(recipientId) - .getServiceId(); - account.getAciSessionStore().deleteAllSessions(serviceId); + .serviceId(); + if (serviceId.isPresent()) { + account.getAciSessionStore().deleteAllSessions(serviceId.get()); + } } } } @Override public void deleteRecipient(final RecipientIdentifier.Single recipient) { - account.removeRecipient(account.getRecipientResolver().resolveRecipient(recipient.getIdentifier())); + final var recipientIdOptional = context.getRecipientHelper().resolveRecipientOptional(recipient); + if (recipientIdOptional.isPresent()) { + account.removeRecipient(recipientIdOptional.get()); + } } @Override public void deleteContact(final RecipientIdentifier.Single recipient) { - account.getContactStore() - .deleteContact(account.getRecipientResolver().resolveRecipient(recipient.getIdentifier())); + final var recipientIdOptional = context.getRecipientHelper().resolveRecipientOptional(recipient); + if (recipientIdOptional.isPresent()) { + account.getContactStore().deleteContact(recipientIdOptional.get()); + } } @Override @@ -1105,9 +1155,12 @@ class ManagerImpl implements Manager { public List getIdentities(RecipientIdentifier.Single recipient) { ServiceId serviceId; try { - serviceId = account.getRecipientAddressResolver() - .resolveRecipientAddress(context.getRecipientHelper().resolveRecipient(recipient)) - .getServiceId(); + final var address = account.getRecipientAddressResolver() + .resolveRecipientAddress(context.getRecipientHelper().resolveRecipient(recipient)); + if (address.serviceId().isEmpty()) { + return List.of(); + } + serviceId = address.serviceId().get(); } catch (UnregisteredRecipientException e) { return List.of(); }