]> nmode's Git Repositories - signal-cli/commitdiff
Merge multiple SendReceiptActions to same recipient to only send one receipt
authorAsamK <asamk@gmx.de>
Fri, 12 Nov 2021 11:10:46 +0000 (12:10 +0100)
committerAsamK <asamk@gmx.de>
Fri, 12 Nov 2021 11:10:46 +0000 (12:10 +0100)
graalvm-config-dir/resource-config.json
lib/src/main/java/org/asamk/signal/manager/ManagerImpl.java
lib/src/main/java/org/asamk/signal/manager/actions/HandleAction.java
lib/src/main/java/org/asamk/signal/manager/actions/SendReceiptAction.java

index 3957fcad30560aa6a00b2fd09e62de765d2be756..5aabcdf957152966897c13f804ec3e6ff6d6ff5c 100644 (file)
     {
       "pattern":"\\Qcom/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_GR\\E"
     }, 
     {
       "pattern":"\\Qcom/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_GR\\E"
     }, 
+    {
+      "pattern":"\\Qcom/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_IL\\E"
+    }, 
     {
       "pattern":"\\Qcom/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_IN\\E"
     }, 
     {
       "pattern":"\\Qcom/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_IN\\E"
     }, 
+    {
+      "pattern":"\\Qcom/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_IT\\E"
+    }, 
     {
       "pattern":"\\Qcom/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_PA\\E"
     }, 
     {
       "pattern":"\\Qcom/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_PA\\E"
     }, 
index 69ae926992fe0764b55fb461935ceb3acf9a6b49..756c0a9474894ff8ca770c6b7b8b62f727f74fe9 100644 (file)
@@ -1028,7 +1028,8 @@ public class ManagerImpl implements Manager {
     ) throws IOException {
         retryFailedReceivedMessages(handler);
 
     ) throws IOException {
         retryFailedReceivedMessages(handler);
 
-        Set<HandleAction> queuedActions = new HashSet<>();
+        // Use a Map here because java Set doesn't have a get method ...
+        Map<HandleAction, HandleAction> queuedActions = new HashMap<>();
 
         final var signalWebSocket = dependencies.getSignalWebSocket();
         signalWebSocket.connect();
 
         final var signalWebSocket = dependencies.getSignalWebSocket();
         signalWebSocket.connect();
@@ -1057,7 +1058,7 @@ public class ManagerImpl implements Manager {
                     logger.debug("New message received from server");
                 } else {
                     logger.debug("Received indicator that server queue is empty");
                     logger.debug("New message received from server");
                 } else {
                     logger.debug("Received indicator that server queue is empty");
-                    handleQueuedActions(queuedActions);
+                    handleQueuedActions(queuedActions.keySet());
                     queuedActions.clear();
 
                     hasCaughtUpWithOldMessages = true;
                     queuedActions.clear();
 
                     hasCaughtUpWithOldMessages = true;
@@ -1098,11 +1099,18 @@ public class ManagerImpl implements Manager {
             }
 
             final var result = incomingMessageHandler.handleEnvelope(envelope, ignoreAttachments, handler);
             }
 
             final var result = incomingMessageHandler.handleEnvelope(envelope, ignoreAttachments, handler);
-            queuedActions.addAll(result.first());
+            for (final var h : result.first()) {
+                final var existingAction = queuedActions.get(h);
+                if (existingAction == null) {
+                    queuedActions.put(h, h);
+                } else {
+                    existingAction.mergeOther(h);
+                }
+            }
             final var exception = result.second();
 
             if (hasCaughtUpWithOldMessages) {
             final var exception = result.second();
 
             if (hasCaughtUpWithOldMessages) {
-                handleQueuedActions(queuedActions);
+                handleQueuedActions(queuedActions.keySet());
                 queuedActions.clear();
             }
             if (cachedMessage[0] != null) {
                 queuedActions.clear();
             }
             if (cachedMessage[0] != null) {
@@ -1123,7 +1131,7 @@ public class ManagerImpl implements Manager {
                 }
             }
         }
                 }
             }
         }
-        handleQueuedActions(queuedActions);
+        handleQueuedActions(queuedActions.keySet());
         queuedActions.clear();
         dependencies.getSignalWebSocket().disconnect();
     }
         queuedActions.clear();
         dependencies.getSignalWebSocket().disconnect();
     }
index cfe13bce20fa6cd4cbc187fdc6e7d723f2ba75b4..b793b8414b4703d456ab9f1ccb6202b28c35541d 100644 (file)
@@ -5,4 +5,7 @@ import org.asamk.signal.manager.jobs.Context;
 public interface HandleAction {
 
     void execute(Context context) throws Throwable;
 public interface HandleAction {
 
     void execute(Context context) throws Throwable;
+
+    default void mergeOther(HandleAction action) {
+    }
 }
 }
index 8341304c426128b8f76b170698648e596f5d2186..9dc3d3b98bb5f4671f1a7c53a7798c36952a0a72 100644 (file)
@@ -3,34 +3,44 @@ package org.asamk.signal.manager.actions;
 import org.asamk.signal.manager.jobs.Context;
 import org.asamk.signal.manager.storage.recipients.RecipientId;
 
 import org.asamk.signal.manager.jobs.Context;
 import org.asamk.signal.manager.storage.recipients.RecipientId;
 
+import java.util.ArrayList;
 import java.util.List;
 import java.util.Objects;
 
 public class SendReceiptAction implements HandleAction {
 
     private final RecipientId recipientId;
 import java.util.List;
 import java.util.Objects;
 
 public class SendReceiptAction implements HandleAction {
 
     private final RecipientId recipientId;
-    private final long timestamp;
+    private final List<Long> timestamps = new ArrayList<>();
 
     public SendReceiptAction(final RecipientId recipientId, final long timestamp) {
         this.recipientId = recipientId;
 
     public SendReceiptAction(final RecipientId recipientId, final long timestamp) {
         this.recipientId = recipientId;
-        this.timestamp = timestamp;
+        this.timestamps.add(timestamp);
     }
 
     @Override
     public void execute(Context context) throws Throwable {
     }
 
     @Override
     public void execute(Context context) throws Throwable {
-        context.getSendHelper().sendDeliveryReceipt(recipientId, List.of(timestamp));
+        context.getSendHelper().sendDeliveryReceipt(recipientId, timestamps);
+    }
+
+    @Override
+    public void mergeOther(final HandleAction action) {
+        if (action instanceof SendReceiptAction sendReceiptAction) {
+            this.timestamps.addAll(sendReceiptAction.timestamps);
+        }
     }
 
     @Override
     public boolean equals(final Object o) {
         if (this == o) return true;
         if (o == null || getClass() != o.getClass()) return false;
     }
 
     @Override
     public boolean equals(final Object o) {
         if (this == o) return true;
         if (o == null || getClass() != o.getClass()) return false;
-        final var that = (SendReceiptAction) o;
-        return timestamp == that.timestamp && recipientId.equals(that.recipientId);
+        final SendReceiptAction that = (SendReceiptAction) o;
+        // Using only recipientId here on purpose
+        return recipientId.equals(that.recipientId);
     }
 
     @Override
     public int hashCode() {
     }
 
     @Override
     public int hashCode() {
-        return Objects.hash(recipientId, timestamp);
+        // Using only recipientId here on purpose
+        return Objects.hash(recipientId);
     }
 }
     }
 }