X-Git-Url: https://git.nmode.ca/signal-cli/blobdiff_plain/abebffb2cd99678bd17498fea2d1f0749b602c8b..2e4d346bc826da5c35a17841772cf81ba4f98101:/src/main/java/org/asamk/signal/dbus/DbusManagerImpl.java diff --git a/src/main/java/org/asamk/signal/dbus/DbusManagerImpl.java b/src/main/java/org/asamk/signal/dbus/DbusManagerImpl.java index 90153147..c0dc1cf8 100644 --- a/src/main/java/org/asamk/signal/dbus/DbusManagerImpl.java +++ b/src/main/java/org/asamk/signal/dbus/DbusManagerImpl.java @@ -15,6 +15,8 @@ import org.asamk.signal.manager.api.MessageEnvelope; import org.asamk.signal.manager.api.NotPrimaryDeviceException; import org.asamk.signal.manager.api.Pair; import org.asamk.signal.manager.api.ReceiveConfig; +import org.asamk.signal.manager.api.Recipient; +import org.asamk.signal.manager.api.RecipientAddress; import org.asamk.signal.manager.api.RecipientIdentifier; import org.asamk.signal.manager.api.SendGroupMessageResults; import org.asamk.signal.manager.api.SendMessageResults; @@ -34,8 +36,6 @@ import org.asamk.signal.manager.groups.LastGroupAdminException; import org.asamk.signal.manager.groups.NotAGroupMemberException; import org.asamk.signal.manager.storage.recipients.Contact; import org.asamk.signal.manager.storage.recipients.Profile; -import org.asamk.signal.manager.storage.recipients.Recipient; -import org.asamk.signal.manager.storage.recipients.RecipientAddress; import org.freedesktop.dbus.DBusMap; import org.freedesktop.dbus.DBusPath; import org.freedesktop.dbus.connections.impl.DBusConnection; @@ -46,6 +46,7 @@ import org.freedesktop.dbus.types.Variant; import java.io.File; import java.io.IOException; +import java.io.InputStream; import java.net.URI; import java.net.URISyntaxException; import java.time.Duration; @@ -58,6 +59,7 @@ import java.util.Map; import java.util.Objects; import java.util.Optional; import java.util.Set; +import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicLong; import java.util.function.Function; import java.util.function.Supplier; @@ -364,7 +366,8 @@ public class DbusManagerImpl implements Manager { final boolean remove, final RecipientIdentifier.Single targetAuthor, final long targetSentTimestamp, - final Set recipients + final Set recipients, + final boolean isStory ) throws IOException, NotAGroupMemberException, GroupNotFoundException, GroupSendingNotAllowedException { return handleMessage(recipients, numbers -> signal.sendMessageReaction(emoji, @@ -388,7 +391,8 @@ public class DbusManagerImpl implements Manager { public SendMessageResults sendPaymentNotificationMessage( final byte[] receipt, final String note, final RecipientIdentifier.Single recipient ) throws IOException { - throw new UnsupportedOperationException(); + final var timestamp = signal.sendPaymentNotification(receipt, note, recipient.getIdentifier()); + return new SendMessageResults(timestamp, Map.of()); } @Override @@ -495,39 +499,46 @@ public class DbusManagerImpl implements Manager { } } - @Override - public void receiveMessages(final ReceiveMessageHandler handler) throws IOException { - addReceiveHandler(handler); - try { - synchronized (this) { - this.wait(); - } - } catch (InterruptedException ignored) { - } - removeReceiveHandler(handler); - } - @Override public void receiveMessages( - final Duration timeout, final ReceiveMessageHandler handler + Optional timeout, Optional maxMessages, ReceiveMessageHandler handler ) throws IOException { + final var remainingMessages = new AtomicInteger(maxMessages.orElse(-1)); final var lastMessage = new AtomicLong(System.currentTimeMillis()); + final var thread = Thread.currentThread(); final ReceiveMessageHandler receiveHandler = (envelope, e) -> { lastMessage.set(System.currentTimeMillis()); handler.handleMessage(envelope, e); + if (remainingMessages.get() > 0) { + if (remainingMessages.decrementAndGet() <= 0) { + remainingMessages.set(0); + thread.interrupt(); + } + } }; addReceiveHandler(receiveHandler); - while (true) { + if (timeout.isPresent()) { + while (remainingMessages.get() != 0) { + try { + final var passedTime = System.currentTimeMillis() - lastMessage.get(); + final var sleepTimeRemaining = timeout.get().toMillis() - passedTime; + if (sleepTimeRemaining < 0) { + break; + } + Thread.sleep(sleepTimeRemaining); + } catch (InterruptedException ignored) { + } + } + } else { try { - final var sleepTimeRemaining = timeout.toMillis() - (System.currentTimeMillis() - lastMessage.get()); - if (sleepTimeRemaining < 0) { - break; + synchronized (this) { + this.wait(); } - Thread.sleep(sleepTimeRemaining); } catch (InterruptedException ignored) { } } + removeReceiveHandler(receiveHandler); } @@ -751,6 +762,7 @@ public class DbusManagerImpl implements Manager { messageReceived.getGroupId()), false, 0)) : Optional.empty(), Optional.empty(), + Optional.empty(), Optional.of(messageReceived.getMessage()), 0, false, @@ -768,6 +780,7 @@ public class DbusManagerImpl implements Manager { List.of(), List.of())), Optional.empty(), + Optional.empty(), Optional.empty()); notifyMessageHandlers(envelope); }; @@ -793,6 +806,7 @@ public class DbusManagerImpl implements Manager { Optional.empty(), Optional.empty(), Optional.empty(), + Optional.empty(), Optional.empty()); notifyMessageHandlers(envelope); }; @@ -822,6 +836,7 @@ public class DbusManagerImpl implements Manager { syncReceived.getGroupId()), false, 0)) : Optional.empty(), Optional.empty(), + Optional.empty(), Optional.of(syncReceived.getMessage()), 0, false, @@ -837,7 +852,8 @@ public class DbusManagerImpl implements Manager { Optional.empty(), List.of(), List.of(), - List.of())))), + List.of())), + Optional.empty())), Optional.empty(), List.of(), List.of(), @@ -845,6 +861,7 @@ public class DbusManagerImpl implements Manager { Optional.empty(), Optional.empty(), Optional.empty())), + Optional.empty(), Optional.empty()); notifyMessageHandlers(envelope); }; @@ -900,6 +917,11 @@ public class DbusManagerImpl implements Manager { }).toList(); } + @Override + public InputStream retrieveAttachment(final String id) throws IOException { + throw new UnsupportedOperationException(); + } + @SuppressWarnings("unchecked") private T getValue( final Map> stringVariantMap, final String field