]> nmode's Git Repositories - signal-cli/blobdiff - lib/src/main/java/org/asamk/signal/manager/Manager.java
Implement jsonRpc command
[signal-cli] / lib / src / main / java / org / asamk / signal / manager / Manager.java
index 37b4aa860e6ead15b1562923bc9ca323acf76276..98b02c7f4e3cd45af04eaa06642e7bb85e06f415 100644 (file)
@@ -618,21 +618,27 @@ public class Manager implements Closeable {
             return null;
         }
 
+        profile = decryptProfileIfKeyKnown(recipientId, encryptedProfile);
+        account.getProfileStore().storeProfile(recipientId, profile);
+
+        return profile;
+    }
+
+    private Profile decryptProfileIfKeyKnown(
+            final RecipientId recipientId, final SignalServiceProfile encryptedProfile
+    ) {
         var profileKey = account.getProfileStore().getProfileKey(recipientId);
         if (profileKey == null) {
-            profile = new Profile(System.currentTimeMillis(),
+            return new Profile(System.currentTimeMillis(),
                     null,
                     null,
                     null,
                     null,
                     ProfileUtils.getUnidentifiedAccessMode(encryptedProfile, null),
                     ProfileUtils.getCapabilities(encryptedProfile));
-        } else {
-            profile = decryptProfileAndDownloadAvatar(recipientId, profileKey, encryptedProfile);
         }
-        account.getProfileStore().storeProfile(recipientId, profile);
 
-        return profile;
+        return decryptProfileAndDownloadAvatar(recipientId, profileKey, encryptedProfile);
     }
 
     private SignalServiceProfile retrieveEncryptedProfile(RecipientId recipientId) {
@@ -2022,6 +2028,9 @@ public class Manager implements Closeable {
             try {
                 action.execute(this);
             } catch (Throwable e) {
+                if (e instanceof AssertionError && e.getCause() instanceof InterruptedException) {
+                    Thread.currentThread().interrupt();
+                }
                 logger.warn("Message action failed.", e);
             }
         }
@@ -2068,7 +2077,7 @@ public class Manager implements Closeable {
             boolean returnOnTimeout,
             boolean ignoreAttachments,
             ReceiveMessageHandler handler
-    ) throws IOException {
+    ) throws IOException, InterruptedException {
         retryFailedReceivedMessages(handler, ignoreAttachments);
 
         Set<HandleAction> queuedActions = null;
@@ -2077,7 +2086,7 @@ public class Manager implements Closeable {
 
         var hasCaughtUpWithOldMessages = false;
 
-        while (true) {
+        while (!Thread.interrupted()) {
             SignalServiceEnvelope envelope;
             SignalServiceContent content = null;
             Exception exception = null;
@@ -2104,6 +2113,9 @@ public class Manager implements Closeable {
                             try {
                                 action.execute(this);
                             } catch (Throwable e) {
+                                if (e instanceof AssertionError && e.getCause() instanceof InterruptedException) {
+                                    Thread.currentThread().interrupt();
+                                }
                                 logger.warn("Message action failed.", e);
                             }
                         }
@@ -2114,6 +2126,12 @@ public class Manager implements Closeable {
                     // Continue to wait another timeout for new messages
                     continue;
                 }
+            } catch (AssertionError e) {
+                if (e.getCause() instanceof InterruptedException) {
+                    throw (InterruptedException) e.getCause();
+                } else {
+                    throw e;
+                }
             } catch (TimeoutException e) {
                 if (returnOnTimeout) return;
                 continue;
@@ -2147,6 +2165,9 @@ public class Manager implements Closeable {
                         try {
                             action.execute(this);
                         } catch (Throwable e) {
+                            if (e instanceof AssertionError && e.getCause() instanceof InterruptedException) {
+                                Thread.currentThread().interrupt();
+                            }
                             logger.warn("Message action failed.", e);
                         }
                     }
@@ -2543,6 +2564,9 @@ public class Manager implements Closeable {
             avatarStore.storeProfileAvatar(address,
                     outputStream -> retrieveProfileAvatar(avatarPath, profileKey, outputStream));
         } catch (Throwable e) {
+            if (e instanceof AssertionError && e.getCause() instanceof InterruptedException) {
+                Thread.currentThread().interrupt();
+            }
             logger.warn("Failed to download profile avatar, ignoring: {}", e.getMessage());
         }
     }