handleToDeviceEvent method

Future<void> handleToDeviceEvent(
  1. ToDeviceEvent event
)

Implementation

Future<void> handleToDeviceEvent(ToDeviceEvent event) async {
  if (event.type == EventTypes.Dummy) {
    // We receive dan encrypted m.dummy. This means that the other end was not able to
    // decrypt our last message. So, we re-send it.
    final encryptedContent = event.encryptedContent;
    if (encryptedContent == null || encryption.olmDatabase == null) {
      return;
    }
    final device = client.getUserDeviceKeysByCurve25519Key(
        encryptedContent.tryGet<String>('sender_key') ?? '');
    if (device == null) {
      return; // device not found
    }
    Logs().v(
        '[OlmManager] Device ${device.userId}:${device.deviceId} generated a new olm session, replaying last sent message...');
    final lastSentMessageRes = await encryption.olmDatabase
        ?.getLastSentMessageUserDeviceKey(device.userId, device.deviceId!);
    if (lastSentMessageRes == null ||
        lastSentMessageRes.isEmpty ||
        lastSentMessageRes.first.isEmpty) {
      return;
    }
    final lastSentMessage = json.decode(lastSentMessageRes.first);
    // We do *not* want to re-play m.dummy events, as they hold no value except of saying
    // what olm session is the most recent one. In fact, if we *do* replay them, then
    // we can easily land in an infinite ping-pong trap!
    if (lastSentMessage['type'] != EventTypes.Dummy) {
      // okay, time to send the message!
      await client.sendToDeviceEncrypted(
          [device], lastSentMessage['type'], lastSentMessage['content']);
    }
  }
}