feat: Replace isSuccessful with canSend

This commit is contained in:
PapaTutuWawa 2023-06-17 20:58:33 +02:00
parent 65c0975a77
commit fe2b090ea0
3 changed files with 14 additions and 8 deletions

View File

@ -8,6 +8,7 @@ class EncryptionResult {
this.ciphertext, this.ciphertext,
this.encryptedKeys, this.encryptedKeys,
this.deviceEncryptionErrors, this.deviceEncryptionErrors,
this.canSend,
); );
/// The actual message that was encrypted. /// The actual message that was encrypted.
@ -20,9 +21,7 @@ class EncryptionResult {
/// Mapping of a JID to /// Mapping of a JID to
final Map<String, List<EncryptToJidError>> deviceEncryptionErrors; final Map<String, List<EncryptToJidError>> deviceEncryptionErrors;
// TODO: Turn this into a property that is computed in [onOutgoingStanza]. /// A flag indicating that the message could be sent like that, i.e. we were able
/// True if the encryption was a success. This means that we could encrypt for /// to encrypt to at-least one device per recipient.
/// at least one ratchet per recipient. [recipients] is the number of recipients final bool canSend;
/// that the message should've been encrypted for.
bool isSuccess(int recipients) => encryptedKeys.length == recipients;
} }

View File

@ -630,6 +630,9 @@ class OmemoManager {
ciphertext = null; ciphertext = null;
} }
final successfulEncryptions = Map<String, int>.fromEntries(
stanza.recipientJids.map((jid) => MapEntry(jid, 0)),
);
final encryptionErrors = <String, List<EncryptToJidError>>{}; final encryptionErrors = <String, List<EncryptToJidError>>{};
final addedRatchetKeys = List<RatchetMapKey>.empty(growable: true); final addedRatchetKeys = List<RatchetMapKey>.empty(growable: true);
final kex = <RatchetMapKey, OMEMOKeyExchange>{}; final kex = <RatchetMapKey, OMEMOKeyExchange>{};
@ -770,6 +773,7 @@ class OmemoManager {
true, true,
), ),
); );
successfulEncryptions[jid] = successfulEncryptions[jid]! + 1;
} else if (!ratchet.acknowledged) { } else if (!ratchet.acknowledged) {
// The ratchet as not yet been acked. // The ratchet as not yet been acked.
// Keep sending the old KEX // Keep sending the old KEX
@ -789,6 +793,7 @@ class OmemoManager {
true, true,
), ),
); );
successfulEncryptions[jid] = successfulEncryptions[jid]! + 1;
} else { } else {
// The ratchet exists and is acked // The ratchet exists and is acked
encryptedKeys.appendOrCreate( encryptedKeys.appendOrCreate(
@ -799,6 +804,7 @@ class OmemoManager {
false, false,
), ),
); );
successfulEncryptions[jid] = successfulEncryptions[jid]! + 1;
} }
} }
} }
@ -807,6 +813,7 @@ class OmemoManager {
ciphertext, ciphertext,
encryptedKeys, encryptedKeys,
encryptionErrors, encryptionErrors,
successfulEncryptions.values.every((n) => n > 0),
); );
} }

View File

@ -881,7 +881,7 @@ void main() {
), ),
); );
expect(aliceResult.isSuccess(1), isFalse); expect(aliceResult.canSend, isFalse);
expect(aliceResult.deviceEncryptionErrors[bobJid]!.length, 1); expect(aliceResult.deviceEncryptionErrors[bobJid]!.length, 1);
final error = aliceResult.deviceEncryptionErrors[bobJid]!.first; final error = aliceResult.deviceEncryptionErrors[bobJid]!.first;
expect(error.error, const TypeMatcher<NoKeyMaterialAvailableError>()); expect(error.error, const TypeMatcher<NoKeyMaterialAvailableError>());
@ -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]!.length, 1);
expect( expect(
aliceResult.deviceEncryptionErrors[cocoJid]!.first.error, aliceResult.deviceEncryptionErrors[cocoJid]!.first.error,
@ -1023,7 +1023,7 @@ void main() {
messageText, messageText,
), ),
); );
expect(bobResponseMessage.isSuccess(1), isTrue); expect(bobResponseMessage.canSend, isTrue);
final aliceReceivedMessage = await aliceManager.onIncomingStanza( final aliceReceivedMessage = await aliceManager.onIncomingStanza(
OmemoIncomingStanza( OmemoIncomingStanza(