loadMetadata method

Future<Metadata?> loadMetadata(
  1. String pubKey, {
  2. bool forceRefresh = false,
  3. int idleTimeout = RelayManager.DEFAULT_STREAM_IDLE_TIMEOUT,
})

Implementation

Future<Metadata?> loadMetadata(
  String pubKey, {
  bool forceRefresh = false,
  int idleTimeout = RelayManager.DEFAULT_STREAM_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;
}