upsertMessages method

Future<void> upsertMessages(
  1. List<LocalityEvent> messages, {
  2. String? password,
})

Implementation

Future<void> upsertMessages(List<LocalityEvent> messages, {String? password}) async {
  final key = password ?? _defaultPassword;
  final batch = _database.batch();

  for (var message in messages) {
    final encryptedPayload = PayloadCipher.encodePayload(
        message.payload,
        nonce: Nonce.fromString(message.uuid),
        chaCha20: ChaCha20(ChaCha20Key.fromString(key))
    );

    final messageMap = message.toMap();
    messageMap[LocalityEvent.columnPayload] = encryptedPayload;

    batch.insert(
      LocalityEvent.tableName,
      messageMap,
      conflictAlgorithm: ConflictAlgorithm.replace,
    );
  }

  await batch.commit(noResult: true);
}