loadEvents method
Future<List<Nip01Event> >
loadEvents({
- List<
String> ? ids, - List<
String> ? pubKeys, - List<
int> ? kinds, - Map<
String, List< ? tags,String> > - int? since,
- int? until,
- String? search,
- int? limit,
override
Load 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
search - search string to match against content
limit - limit of results
returns list of events
Implementation
@override
Future<List<Nip01Event>> loadEvents({
List<String>? ids,
List<String>? pubKeys,
List<int>? kinds,
Map<String, List<String>>? tags,
int? since,
int? until,
String? search,
int? limit,
}) async {
await dbRdy;
final eventBox = _objectBox.store.box<DbNip01Event>();
// Build conditions
Condition<DbNip01Event>? condition;
// Add search condition if provided (NIP-50)
if (search != null && search.isNotEmpty) {
condition = DbNip01Event_.content.contains(search, caseSensitive: false);
}
// Add ids filter
if (ids != null && ids.isNotEmpty) {
Condition<DbNip01Event> idsCondition = DbNip01Event_.nostrId.oneOf(ids);
condition =
(condition == null) ? idsCondition : condition.and(idsCondition);
}
// Add pubKeys filter
if (pubKeys != null && pubKeys.isNotEmpty) {
Condition<DbNip01Event> pubKeysCondition =
DbNip01Event_.pubKey.oneOf(pubKeys);
condition = (condition == null)
? pubKeysCondition
: condition.and(pubKeysCondition);
}
// Add kinds filter
if (kinds != null && kinds.isNotEmpty) {
Condition<DbNip01Event> kindsCondition = DbNip01Event_.kind.oneOf(kinds);
condition =
(condition == null) ? kindsCondition : condition.and(kindsCondition);
}
// Add since filter
if (since != null) {
Condition<DbNip01Event> sinceCondition =
DbNip01Event_.createdAt.greaterOrEqual(since);
condition =
(condition == null) ? sinceCondition : condition.and(sinceCondition);
}
// Add until filter
if (until != null) {
Condition<DbNip01Event> untilCondition =
DbNip01Event_.createdAt.lessOrEqual(until);
condition =
(condition == null) ? untilCondition : condition.and(untilCondition);
}
if (tags != null && tags.isNotEmpty) {
final matchingEventIds = _findEventIdsByTags(eventBox, tags);
if (matchingEventIds.isEmpty) {
return [];
}
final tagCondition = DbNip01Event_.dbId.oneOf(matchingEventIds.toList());
condition =
(condition == null) ? tagCondition : condition.and(tagCondition);
}
// Create and build the query
QueryBuilder<DbNip01Event> queryBuilder;
if (condition != null) {
queryBuilder = eventBox.query(condition);
} else {
queryBuilder = eventBox.query();
}
// Apply sorting
queryBuilder.order(DbNip01Event_.createdAt, flags: Order.descending);
// Build and execute the query
final query = queryBuilder.build();
if (limit != null && limit > 0) {
query.limit = limit;
}
final results = query.find();
return results.map((dbEvent) => dbEvent.toNdk()).toList();
}