handleToDeviceEvent method

Future<void> handleToDeviceEvent(
  1. ToDeviceEvent event
)

Implementation

Future<void> handleToDeviceEvent(ToDeviceEvent event) async {
  if (!event.type.startsWith('m.key.verification.') ||
      client.verificationMethods.isEmpty) {
    return;
  }
  // we have key verification going on!
  final transactionId = KeyVerification.getTransactionId(event.content);
  if (transactionId == null) {
    return; // TODO: send cancel with unknown transaction id
  }
  final request = _requests[transactionId];
  if (request != null) {
    // make sure that new requests can't come from ourself
    if (!{EventTypes.KeyVerificationRequest}.contains(event.type)) {
      await request.handlePayload(event.type, event.content);
    }
  } else {
    if (!{EventTypes.KeyVerificationRequest, EventTypes.KeyVerificationStart}
        .contains(event.type)) {
      return; // we can only start on these
    }
    final newKeyRequest =
        KeyVerification(encryption: encryption, userId: event.sender);
    await newKeyRequest.handlePayload(event.type, event.content);
    if (newKeyRequest.state != KeyVerificationState.askAccept) {
      // okay, something went wrong (unknown transaction id?), just dispose it
      newKeyRequest.dispose();
    } else {
      _requests[transactionId] = newKeyRequest;
      client.onKeyVerificationRequest.add(newKeyRequest);
    }
  }
}