decryptToDeviceEvent method

Future<ToDeviceEvent> decryptToDeviceEvent(
  1. ToDeviceEvent event
)

Implementation

Future<ToDeviceEvent> decryptToDeviceEvent(ToDeviceEvent event) async {
  if (event.type != EventTypes.Encrypted) {
    return event;
  }
  final senderKey = event.parsedRoomEncryptedContent.senderKey;
  Future<bool> loadFromDb() async {
    final sessions = await getOlmSessions(senderKey);
    return sessions.isNotEmpty;
  }

  if (!_olmSessions.containsKey(senderKey)) {
    await loadFromDb();
  }
  try {
    event = await _decryptToDeviceEvent(event);
    if (event.type != EventTypes.Encrypted || !(await loadFromDb())) {
      return event;
    }
    // retry to decrypt!
    return _decryptToDeviceEvent(event);
  } catch (_) {
    // okay, the thing errored while decrypting. It is safe to assume that the olm session is corrupt and we should generate a new one
    runInRoot(() => restoreOlmSession(event.senderId, senderKey));

    rethrow;
  }
}