]> nmode's Git Repositories - signal-cli/commitdiff
Archive recipient's sessions after identity key switch
authorAsamK <asamk@gmx.de>
Tue, 4 May 2021 18:38:00 +0000 (20:38 +0200)
committerAsamK <asamk@gmx.de>
Tue, 4 May 2021 18:38:00 +0000 (20:38 +0200)
lib/src/main/java/org/asamk/signal/manager/Manager.java
lib/src/main/java/org/asamk/signal/manager/storage/sessions/SessionStore.java

index 99f71563a7978e494bc8f8d467e95a013174d62f..f9d86ce6686d23c885fca6453aad2348769f2a28 100644 (file)
@@ -601,10 +601,14 @@ public class Manager implements Closeable {
         final var profile = profileAndCredential.getProfile();
 
         try {
-            account.getIdentityKeyStore()
+            var newIdentity = account.getIdentityKeyStore()
                     .saveIdentity(recipientId,
                             new IdentityKey(Base64.getDecoder().decode(profile.getIdentityKey())),
                             new Date());
+
+            if (newIdentity) {
+                account.getSessionStore().archiveSessions(recipientId);
+            }
         } catch (InvalidKeyException ignored) {
             logger.warn("Got invalid identity key in profile for {}",
                     resolveSignalServiceAddress(recipientId).getLegacyIdentifier());
@@ -1363,10 +1367,12 @@ public class Manager implements Closeable {
 
                     for (var r : result) {
                         if (r.getIdentityFailure() != null) {
-                            account.getIdentityKeyStore().
-                                    saveIdentity(resolveRecipient(r.getAddress()),
-                                            r.getIdentityFailure().getIdentityKey(),
-                                            new Date());
+                            final var recipientId = resolveRecipient(r.getAddress());
+                            final var newIdentity = account.getIdentityKeyStore()
+                                    .saveIdentity(recipientId, r.getIdentityFailure().getIdentityKey(), new Date());
+                            if (newIdentity) {
+                                account.getSessionStore().archiveSessions(recipientId);
+                            }
                         }
                     }
 
index 30df690045e99ebc2afff68346dce108c576725f..3e5555f49afd2bb381c269b6b3fbe5e9bc569077 100644 (file)
@@ -133,6 +133,14 @@ public class SessionStore implements SignalServiceSessionStore {
         }
     }
 
+    public void archiveSessions(final RecipientId recipientId) {
+        synchronized (cachedSessions) {
+            getKeysLocked().stream()
+                    .filter(key -> key.recipientId.equals(recipientId))
+                    .forEach(this::archiveSessionLocked);
+        }
+    }
+
     public void mergeRecipients(RecipientId recipientId, RecipientId toBeMergedRecipientId) {
         synchronized (cachedSessions) {
             final var otherHasSession = getKeysLocked(toBeMergedRecipientId).size() > 0;