encryptToDeviceMessage method

Future<Map<String, Map<String, Map<String, dynamic>>>> encryptToDeviceMessage(
  1. List<DeviceKeys> deviceKeys,
  2. String type,
  3. Map<String, dynamic> payload
)

Implementation

Future<Map<String, Map<String, Map<String, dynamic>>>> encryptToDeviceMessage(
    List<DeviceKeys> deviceKeys,
    String type,
    Map<String, dynamic> payload) async {
  final data = <String, Map<String, Map<String, dynamic>>>{};
  // first check if any of our sessions we want to encrypt for are in the database
  if (encryption.olmDatabase != null) {
    await getOlmSessionsForDevicesFromDatabase(
        deviceKeys.map((d) => d.curve25519Key!).toList());
  }
  final deviceKeysWithoutSession = List<DeviceKeys>.from(deviceKeys);
  deviceKeysWithoutSession.removeWhere((DeviceKeys deviceKeys) =>
      olmSessions[deviceKeys.curve25519Key]?.isNotEmpty ?? false);
  if (deviceKeysWithoutSession.isNotEmpty) {
    await startOutgoingOlmSessions(deviceKeysWithoutSession);
  }
  for (final device in deviceKeys) {
    final userData = data[device.userId] ??= {};
    try {
      userData[device.deviceId!] = await encryptToDeviceMessagePayload(
          device, type, payload,
          getFromDb: false);
    } on NoOlmSessionFoundException catch (e) {
      Logs().d('[LibOlm] Error encrypting to-device event', e);
      continue;
    } catch (e, s) {
      Logs().wtf('[LibOlm] Error encrypting to-device event', e, s);
      continue;
    }
  }
  return data;
}