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);
}
// 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();
// For tag filtering, we need to do it in memory since ObjectBox doesn't support
// complex JSON querying within arrays
List<DbNip01Event> filteredResults = results;
// Apply tag filters in memory if needed
if (tags != null && tags.isNotEmpty) {
filteredResults = results.where((event) {
// Check if the event matches all tag filters
return tags.entries.every((tagEntry) {
String tagKey = tagEntry.key;
List<String> tagValues = tagEntry.value;
// Handle the special case where tag key starts with '#'
if (tagKey.startsWith('#') && tagKey.length > 1) {
tagKey = tagKey.substring(1); // Remove the '#' prefix
}
// Get all tags with this key
List<DbTag> eventTags =
event.tags.where((t) => t.key == tagKey).toList();
// Check if any of the event's tags with this key have a value in the requested values
return eventTags.any((tag) =>
tagValues.contains(tag.value) ||
tagValues.contains(tag.value.toLowerCase()));
});
}).toList();
}
// Apply limit after filtering if tags were applied
final limitedResults = (limit != null && limit > 0 && tags != null)
? filteredResults.take(limit).toList()
: filteredResults;
return limitedResults.map((dbEvent) => dbEvent.toNdk()).toList();
}