Handle changed identity key correctly when sending message

Fixes #686
This commit is contained in:
AsamK 2021-08-20 20:03:59 +02:00
parent 19f7b5d78d
commit b77d820661

View file

@ -1594,14 +1594,7 @@ public class Manager implements Closeable {
() -> false); () -> false);
for (var r : result) { for (var r : result) {
if (r.getIdentityFailure() != null) { handlePossibleIdentityFailure(r);
final var recipientId = resolveRecipient(r.getAddress());
final var newIdentity = account.getIdentityKeyStore()
.saveIdentity(recipientId, r.getIdentityFailure().getIdentityKey(), new Date());
if (newIdentity) {
account.getSessionStore().archiveSessions(recipientId);
}
}
} }
return new Pair<>(timestamp, result); return new Pair<>(timestamp, result);
@ -1617,7 +1610,9 @@ public class Manager implements Closeable {
final var expirationTime = contact != null ? contact.getMessageExpirationTime() : 0; final var expirationTime = contact != null ? contact.getMessageExpirationTime() : 0;
messageBuilder.withExpiration(expirationTime); messageBuilder.withExpiration(expirationTime);
message = messageBuilder.build(); message = messageBuilder.build();
results.add(sendMessage(recipientId, message)); final var result = sendMessage(recipientId, message);
handlePossibleIdentityFailure(result);
results.add(result);
} }
return new Pair<>(timestamp, results); return new Pair<>(timestamp, results);
} }
@ -1630,6 +1625,23 @@ public class Manager implements Closeable {
} }
} }
private void handlePossibleIdentityFailure(final SendMessageResult r) {
if (r.getIdentityFailure() != null) {
final var recipientId = resolveRecipient(r.getAddress());
final var identityKey = r.getIdentityFailure().getIdentityKey();
if (identityKey != null) {
final var newIdentity = account.getIdentityKeyStore()
.saveIdentity(recipientId, identityKey, new Date());
if (newIdentity) {
account.getSessionStore().archiveSessions(recipientId);
}
} else {
// Retrieve profile to get the current identity key from the server
retrieveEncryptedProfile(recipientId);
}
}
}
private Pair<Long, SendMessageResult> sendSelfMessage( private Pair<Long, SendMessageResult> sendSelfMessage(
SignalServiceDataMessage.Builder messageBuilder SignalServiceDataMessage.Builder messageBuilder
) throws IOException { ) throws IOException {