deleteAttempts method

  1. @override
Future<int> deleteAttempts(
  1. Session session, {
  2. T? nonce,
  3. Duration? olderThan,
  4. Transaction? transaction,
})
override

Deletes all attempts that match the given filters.

If nonce is provided, only attempts for the given nonce will be deleted. If olderThan is provided, only attempts older than the given duration will be deleted.

If both are provided, only attempts for the given nonce and older than the given duration will be deleted.

Returns the number of attempts deleted.

Implementation

@override
Future<int> deleteAttempts(
  final Session session, {
  final T? nonce,
  final Duration? olderThan,
  final Transaction? transaction,
}) async {
  final timeframe = olderThan ?? config.timeframe ?? Duration.zero;
  final removeBefore = clock.now().subtract(timeframe);

  final deletedAttempts = await RateLimitedRequestAttempt.db.deleteWhere(
    session,
    where: (final t) {
      var expression =
          t.domain.equals(config.domain) &
          t.source.equals(config.source) &
          (t.attemptedAt < removeBefore);
      if (nonce != null) {
        expression &= t.nonce.equals(config.nonceToString(nonce));
      }
      return expression;
    },
    transaction: transaction,
  );

  return deletedAttempts.length;
}