removeEvents method

  1. @override
Future<void> removeEvents({
  1. List<String>? ids,
  2. List<String>? pubKeys,
  3. List<int>? kinds,
  4. Map<String, List<String>>? tags,
  5. int? since,
  6. int? until,
})
override

Remove events from cache with flexible filtering
ids - list of event ids
pubKeys - list of authors pubKeys
kinds - list of kinds
tags - map of tags (e.g. {'p': 'pubkey1', 'e': 'eventid1'})
since - timestamp
until - timestamp
If all parameters are empty, returns early (doesn't delete everything)

Implementation

@override
Future<void> removeEvents({
  List<String>? ids,
  List<String>? pubKeys,
  List<int>? kinds,
  Map<String, List<String>>? tags,
  int? since,
  int? until,
}) async {
  await dbRdy;

  // If all parameters are empty, return early (don't delete everything)
  if ((ids == null || ids.isEmpty) &&
      (pubKeys == null || pubKeys.isEmpty) &&
      (kinds == null || kinds.isEmpty) &&
      (tags == null || tags.isEmpty) &&
      since == null &&
      until == null) {
    return;
  }

  final eventBox = _objectBox.store.box<DbNip01Event>();

  // Build conditions list
  final conditions = <Condition<DbNip01Event>>[];

  if (ids != null && ids.isNotEmpty) {
    conditions.add(DbNip01Event_.nostrId.oneOf(ids));
  }
  if (pubKeys != null && pubKeys.isNotEmpty) {
    conditions.add(DbNip01Event_.pubKey.oneOf(pubKeys));
  }
  if (kinds != null && kinds.isNotEmpty) {
    conditions.add(DbNip01Event_.kind.oneOf(kinds));
  }
  if (since != null) {
    conditions.add(DbNip01Event_.createdAt.greaterOrEqual(since));
  }
  if (until != null) {
    conditions.add(DbNip01Event_.createdAt.lessOrEqual(until));
  }

  if (tags != null && tags.isNotEmpty) {
    final matchingEventIds = _findEventIdsByTags(eventBox, tags);
    if (matchingEventIds.isEmpty) {
      return;
    }
    conditions.add(DbNip01Event_.dbId.oneOf(matchingEventIds.toList()));
  }

  // Build and execute the query
  final query = conditions.isEmpty
      ? eventBox.query().build()
      : eventBox.query(conditions.reduce((a, b) => a.and(b))).build();
  final results = query.find();

  // Remove matching events
  eventBox.removeMany(results.map((e) => e.dbId).toList());
}