]> nmode's Git Repositories - signal-cli/blobdiff - lib/src/main/java/org/asamk/signal/manager/Manager.java
Only handle jsonRpc requests, after receive thread has caught up with old messages
[signal-cli] / lib / src / main / java / org / asamk / signal / manager / Manager.java
index 6d91ba11330d58bec80443c5952a6fd024db3571..c4c77b3453c4a2764a9ecc03e17da4afd1b674ec 100644 (file)
@@ -141,6 +141,7 @@ public class Manager implements Closeable {
     private final IncomingMessageHandler incomingMessageHandler;
 
     private final Context context;
+    private boolean hasCaughtUpWithOldMessages = false;
 
     Manager(
             SignalAccount account,
@@ -164,8 +165,7 @@ public class Manager implements Closeable {
                 return LEGACY_LOCK::unlock;
             }
         };
-        this.dependencies = new SignalDependencies(account.getSelfAddress(),
-                serviceEnvironmentConfig,
+        this.dependencies = new SignalDependencies(serviceEnvironmentConfig,
                 userAgent,
                 credentialsProvider,
                 account.getSignalProtocolStore(),
@@ -186,8 +186,6 @@ public class Manager implements Closeable {
                 avatarStore,
                 account.getProfileStore()::getProfileKey,
                 unidentifiedAccessHelper::getAccessFor,
-                dependencies::getProfileService,
-                dependencies::getMessageReceiver,
                 this::resolveSignalServiceAddress);
         final GroupV2Helper groupV2Helper = new GroupV2Helper(profileHelper::getRecipientProfileKeyCredential,
                 this::getRecipientProfile,
@@ -220,8 +218,7 @@ public class Manager implements Closeable {
                 this::resolveSignalServiceAddress);
 
         this.context = new Context(account,
-                dependencies.getAccountManager(),
-                dependencies.getMessageReceiver(),
+                dependencies,
                 stickerPackStore,
                 sendHelper,
                 groupHelper,
@@ -869,7 +866,7 @@ public class Manager implements Closeable {
         final var signalWebSocket = dependencies.getSignalWebSocket();
         signalWebSocket.connect();
 
-        var hasCaughtUpWithOldMessages = false;
+        hasCaughtUpWithOldMessages = false;
 
         while (!Thread.interrupted()) {
             SignalServiceEnvelope envelope;
@@ -889,11 +886,14 @@ public class Manager implements Closeable {
                     envelope = result.get();
                 } else {
                     // Received indicator that server queue is empty
-                    hasCaughtUpWithOldMessages = true;
-
                     handleQueuedActions(queuedActions);
                     queuedActions.clear();
 
+                    hasCaughtUpWithOldMessages = true;
+                    synchronized (this) {
+                        this.notifyAll();
+                    }
+
                     // Continue to wait another timeout for new messages
                     continue;
                 }
@@ -940,17 +940,27 @@ public class Manager implements Closeable {
         handleQueuedActions(queuedActions);
     }
 
+    public boolean hasCaughtUpWithOldMessages() {
+        return hasCaughtUpWithOldMessages;
+    }
+
     private void handleQueuedActions(final Collection<HandleAction> queuedActions) {
+        var interrupted = false;
         for (var action : queuedActions) {
             try {
                 action.execute(context);
             } catch (Throwable e) {
-                if (e instanceof AssertionError && e.getCause() instanceof InterruptedException) {
-                    Thread.currentThread().interrupt();
+                if ((e instanceof AssertionError || e instanceof RuntimeException)
+                        && e.getCause() instanceof InterruptedException) {
+                    interrupted = true;
+                    continue;
                 }
                 logger.warn("Message action failed.", e);
             }
         }
+        if (interrupted) {
+            Thread.currentThread().interrupt();
+        }
     }
 
     public boolean isContactBlocked(final RecipientIdentifier.Single recipient) {
@@ -1149,10 +1159,6 @@ public class Manager implements Closeable {
     }
 
     public SignalServiceAddress resolveSignalServiceAddress(SignalServiceAddress address) {
-        if (address.matches(account.getSelfAddress())) {
-            return account.getSelfAddress();
-        }
-
         return resolveSignalServiceAddress(resolveRecipient(address));
     }