loadMetadata method
load metadata for a pubkey
if forceRefresh
is true, it will use the network to refresh the metadata
Implementation
Future<Metadata?> loadMetadata(
String pubKey, {
bool forceRefresh = false,
int idleTimeout = METADATA_IDLE_TIMEOUT,
}) async {
Metadata? metadata = await _cacheManager.loadMetadata(pubKey);
if (metadata == null || forceRefresh) {
Metadata? loadedMetadata;
try {
await for (final event in _requests.query(
name: 'metadata',
timeout: idleTimeout,
filters: [
Filter(kinds: [Metadata.KIND], authors: [pubKey], limit: 1)
],
).stream) {
if (loadedMetadata == null ||
loadedMetadata.updatedAt == null ||
loadedMetadata.updatedAt! < event.createdAt) {
loadedMetadata = Metadata.fromEvent(event);
}
}
} catch (e) {
// probably timeout;
}
if (loadedMetadata != null &&
(metadata == null ||
loadedMetadata.updatedAt == null ||
metadata.updatedAt == null ||
loadedMetadata.updatedAt! < metadata.updatedAt! ||
forceRefresh)) {
loadedMetadata.refreshedTimestamp = Helpers.now;
await _cacheManager.saveMetadata(loadedMetadata);
metadata = loadedMetadata;
}
}
return metadata;
}