loadContactList method

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

Implementation

Future<ContactList?> loadContactList(String pubKey,
    {bool forceRefresh = false,
      int idleTimeout = DEFAULT_STREAM_IDLE_TIMEOUT}) async {
  ContactList? contactList = cacheManager.loadContactList(pubKey);
  if (contactList == null || forceRefresh) {
    ContactList? loadedContactList;
    try {
      await for (final event in (await requestRelays(
          bootstrapRelays,
          timeout: idleTimeout,
          Filter(kinds: [ContactList.KIND], authors: [pubKey], limit: 1))).stream) {
        if (loadedContactList == null ||
            loadedContactList.createdAt < event.createdAt) {
          loadedContactList = ContactList.fromEvent(event);
        }
      }
    } catch (e) {
      print(e);
      // probably timeout;
    }
    if (loadedContactList != null &&
        (contactList == null ||
            contactList.createdAt < loadedContactList.createdAt)) {
      loadedContactList.loadedTimestamp =
          DateTime
              .now()
              .millisecondsSinceEpoch ~/ 1000;
      await cacheManager.saveContactList(loadedContactList);
      contactList = loadedContactList;
    }
  }
  return contactList;
}