startOutgoingOlmSessions method

Future<void> startOutgoingOlmSessions(
  1. List<DeviceKeys> deviceKeys
)

Implementation

Future<void> startOutgoingOlmSessions(List<DeviceKeys> deviceKeys) async {
  Logs().v(
      '[OlmManager] Starting session with ${deviceKeys.length} devices...');
  final requestingKeysFrom = <String, Map<String, String>>{};
  for (final device in deviceKeys) {
    if (requestingKeysFrom[device.userId] == null) {
      requestingKeysFrom[device.userId] = {};
    }
    requestingKeysFrom[device.userId]![device.deviceId!] =
        'signed_curve25519';
  }

  final response = await client.claimKeys(requestingKeysFrom, timeout: 10000);

  for (final userKeysEntry in response.oneTimeKeys.entries) {
    final userId = userKeysEntry.key;
    for (final deviceKeysEntry in userKeysEntry.value.entries) {
      final deviceId = deviceKeysEntry.key;
      final fingerprintKey =
          client.userDeviceKeys[userId]!.deviceKeys[deviceId]!.ed25519Key;
      final identityKey =
          client.userDeviceKeys[userId]!.deviceKeys[deviceId]!.curve25519Key;
      for (final deviceKey in deviceKeysEntry.value.values) {
        if (fingerprintKey == null ||
            identityKey == null ||
            deviceKey is! Map<String, Object?> ||
            !deviceKey.checkJsonSignature(fingerprintKey, userId, deviceId) ||
            deviceKey['key'] is! String) {
          Logs().w(
            'Skipping invalid device key from $userId:$deviceId',
            deviceKey,
          );
          continue;
        }
        Logs().v('[OlmManager] Starting session with $userId:$deviceId');
        final session = olm.Session();
        try {
          session.create_outbound(
              _olmAccount!, identityKey, deviceKey.tryGet<String>('key')!);
          await storeOlmSession(OlmSession(
            key: client.userID!,
            identityKey: identityKey,
            sessionId: session.session_id(),
            session: session,
            lastReceived:
                DateTime.now(), // we want to use a newly created session
          ));
        } catch (e, s) {
          session.free();
          Logs()
              .e('[LibOlm] Could not create new outbound olm session', e, s);
        }
      }
    }
  }
}