loadMetadatas method
Implementation
Future<List<Metadata>> loadMetadatas(List<String> pubKeys, RelaySet? relaySet,
{Function(Metadata)? onLoad}) async {
List<String> missingPubKeys = [];
for (var pubKey in pubKeys) {
Metadata? userMetadata = await _cacheManager.loadMetadata(pubKey);
if (userMetadata == null) {
// TODO check if not too old (time passed since last refreshed timestamp)
missingPubKeys.add(pubKey);
}
}
Map<String, Metadata> metadatas = {};
if (missingPubKeys.isNotEmpty) {
Logger.log.d("loading missing user metadatas ${missingPubKeys.length}");
try {
await for (final event in (_requests.query(
name: "load-metadatas",
filters: [
Filter(authors: missingPubKeys, kinds: [Metadata.KIND])
],
relaySet: relaySet))
.stream
.timeout(const Duration(seconds: 5), onTimeout: (sink) {
Logger.log.w("timeout metadatas.length:${metadatas.length}");
})) {
if (metadatas[event.pubKey] == null ||
metadatas[event.pubKey]!.updatedAt! < event.createdAt) {
metadatas[event.pubKey] = Metadata.fromEvent(event);
metadatas[event.pubKey]!.refreshedTimestamp = Helpers.now;
await _cacheManager.saveMetadata(metadatas[event.pubKey]!);
if (onLoad != null) {
onLoad(metadatas[event.pubKey]!);
}
}
}
} catch (e) {
Logger.log.e(e);
}
Logger.log.d("Loaded ${metadatas.length} user metadatas ");
}
return metadatas.values.toList();
}