feat: Replace isSuccessful with canSend
This commit is contained in:
		
							parent
							
								
									65c0975a77
								
							
						
					
					
						commit
						fe2b090ea0
					
				@ -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<String, List<EncryptToJidError>> 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;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -630,6 +630,9 @@ class OmemoManager {
 | 
			
		||||
      ciphertext = null;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    final successfulEncryptions = Map<String, int>.fromEntries(
 | 
			
		||||
      stanza.recipientJids.map((jid) => MapEntry(jid, 0)),
 | 
			
		||||
    );
 | 
			
		||||
    final encryptionErrors = <String, List<EncryptToJidError>>{};
 | 
			
		||||
    final addedRatchetKeys = List<RatchetMapKey>.empty(growable: true);
 | 
			
		||||
    final kex = <RatchetMapKey, OMEMOKeyExchange>{};
 | 
			
		||||
@ -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),
 | 
			
		||||
    );
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -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<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]!.first.error,
 | 
			
		||||
@ -1023,7 +1023,7 @@ void main() {
 | 
			
		||||
          messageText,
 | 
			
		||||
        ),
 | 
			
		||||
      );
 | 
			
		||||
      expect(bobResponseMessage.isSuccess(1), isTrue);
 | 
			
		||||
      expect(bobResponseMessage.canSend, isTrue);
 | 
			
		||||
 | 
			
		||||
      final aliceReceivedMessage = await aliceManager.onIncomingStanza(
 | 
			
		||||
        OmemoIncomingStanza(
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user