getContactList method

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

contact list of a given pubkey, not intended to get followers

Implementation

Future<ContactList?> getContactList(
  String pubKey, {
  /// skips the cache
  bool forceRefresh = false,
  int idleTimeout = Requests.DEFAULT_QUERY_TIMEOUT,
}) async {
  ContactList? contactList = await _cacheManager.loadContactList(pubKey);

  if (contactList != null && !forceRefresh) {
    return contactList;
  }

  ContactList? loadedContactList;
  try {
    await for (final event in _requests.query(
      filters: [
        Filter(kinds: [ContactList.KIND], authors: [pubKey], limit: 1)
      ],
    ).stream) {
      if (loadedContactList == null ||
          loadedContactList.createdAt < event.createdAt) {
        loadedContactList = ContactList.fromEvent(event);
      }
    }
  } catch (e) {
    // probably timeout;
    Logger.log.e(e);
  }
  if (loadedContactList != null &&
      (contactList == null ||
          contactList.createdAt < loadedContactList.createdAt)) {
    loadedContactList.loadedTimestamp =
        DateTime.now().millisecondsSinceEpoch ~/ 1000;
    await _cacheManager.saveContactList(loadedContactList);
    contactList = loadedContactList;
  }

  return contactList;
}