diff --git a/lib/src/omemo/encryption_result.dart b/lib/src/omemo/encryption_result.dart index 36a06ad..639b757 100644 --- a/lib/src/omemo/encryption_result.dart +++ b/lib/src/omemo/encryption_result.dart @@ -8,6 +8,7 @@ class EncryptionResult { this.ciphertext, this.encryptedKeys, this.deviceEncryptionErrors, + this.canSend, ); /// The actual message that was encrypted. @@ -20,9 +21,7 @@ class EncryptionResult { /// Mapping of a JID to final Map> deviceEncryptionErrors; - // TODO: Turn this into a property that is computed in [onOutgoingStanza]. - /// True if the encryption was a success. This means that we could encrypt for - /// at least one ratchet per recipient. [recipients] is the number of recipients - /// that the message should've been encrypted for. - bool isSuccess(int recipients) => encryptedKeys.length == recipients; + /// A flag indicating that the message could be sent like that, i.e. we were able + /// to encrypt to at-least one device per recipient. + final bool canSend; } diff --git a/lib/src/omemo/omemo.dart b/lib/src/omemo/omemo.dart index 89c2148..ea1e7c7 100644 --- a/lib/src/omemo/omemo.dart +++ b/lib/src/omemo/omemo.dart @@ -630,6 +630,9 @@ class OmemoManager { ciphertext = null; } + final successfulEncryptions = Map.fromEntries( + stanza.recipientJids.map((jid) => MapEntry(jid, 0)), + ); final encryptionErrors = >{}; final addedRatchetKeys = List.empty(growable: true); final kex = {}; @@ -770,6 +773,7 @@ class OmemoManager { true, ), ); + successfulEncryptions[jid] = successfulEncryptions[jid]! + 1; } else if (!ratchet.acknowledged) { // The ratchet as not yet been acked. // Keep sending the old KEX @@ -789,6 +793,7 @@ class OmemoManager { true, ), ); + successfulEncryptions[jid] = successfulEncryptions[jid]! + 1; } else { // The ratchet exists and is acked encryptedKeys.appendOrCreate( @@ -799,6 +804,7 @@ class OmemoManager { false, ), ); + successfulEncryptions[jid] = successfulEncryptions[jid]! + 1; } } } @@ -807,6 +813,7 @@ class OmemoManager { ciphertext, encryptedKeys, encryptionErrors, + successfulEncryptions.values.every((n) => n > 0), ); } diff --git a/test/omemo_test.dart b/test/omemo_test.dart index 8d37052..56327d4 100644 --- a/test/omemo_test.dart +++ b/test/omemo_test.dart @@ -881,7 +881,7 @@ void main() { ), ); - expect(aliceResult.isSuccess(1), isFalse); + expect(aliceResult.canSend, isFalse); expect(aliceResult.deviceEncryptionErrors[bobJid]!.length, 1); final error = aliceResult.deviceEncryptionErrors[bobJid]!.first; expect(error.error, const TypeMatcher()); @@ -933,7 +933,7 @@ void main() { ), ); - expect(aliceResult.isSuccess(2), isFalse); + expect(aliceResult.canSend, isFalse); expect(aliceResult.deviceEncryptionErrors[cocoJid]!.length, 1); expect( aliceResult.deviceEncryptionErrors[cocoJid]!.first.error, @@ -1023,7 +1023,7 @@ void main() { messageText, ), ); - expect(bobResponseMessage.isSuccess(1), isTrue); + expect(bobResponseMessage.canSend, isTrue); final aliceReceivedMessage = await aliceManager.onIncomingStanza( OmemoIncomingStanza(