From 5427fa0132d04a23f459d490f8e9a0ed33730d2e Mon Sep 17 00:00:00 2001 From: AsamK Date: Tue, 4 May 2021 20:38:00 +0200 Subject: [PATCH] Archive recipient's sessions after identity key switch --- .../java/org/asamk/signal/manager/Manager.java | 16 +++++++++++----- .../manager/storage/sessions/SessionStore.java | 8 ++++++++ 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/lib/src/main/java/org/asamk/signal/manager/Manager.java b/lib/src/main/java/org/asamk/signal/manager/Manager.java index 99f71563..f9d86ce6 100644 --- a/lib/src/main/java/org/asamk/signal/manager/Manager.java +++ b/lib/src/main/java/org/asamk/signal/manager/Manager.java @@ -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); + } } } diff --git a/lib/src/main/java/org/asamk/signal/manager/storage/sessions/SessionStore.java b/lib/src/main/java/org/asamk/signal/manager/storage/sessions/SessionStore.java index 30df6900..3e5555f4 100644 --- a/lib/src/main/java/org/asamk/signal/manager/storage/sessions/SessionStore.java +++ b/lib/src/main/java/org/asamk/signal/manager/storage/sessions/SessionStore.java @@ -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;