broadcastRemoveNip51Relay method

Future<Nip51List?> broadcastRemoveNip51Relay(
  1. int kind,
  2. String relayUrl,
  3. Iterable<String> broadcastRelays,
  4. EventSigner eventSigner, {
  5. List<String>? defaultRelaysIfEmpty,
})

Implementation

Future<Nip51List?> broadcastRemoveNip51Relay(int kind, String relayUrl,
    Iterable<String> broadcastRelays, EventSigner eventSigner,
    {List<String>? defaultRelaysIfEmpty}) async {
  if (!eventSigner.canSign()) {
    throw Exception(
        "cannot broadcast private nip51 list without a signer that can sign");
  }
  Nip51List? list = await getSingleNip51List(
    kind,
    eventSigner,
    forceRefresh: true,
  );
  if ((list == null || list.allRelays.isEmpty) &&
      defaultRelaysIfEmpty != null &&
      defaultRelaysIfEmpty.isNotEmpty) {
    list = Nip51List(
        kind: kind,
        pubKey: eventSigner.getPublicKey(),
        createdAt: Helpers.now,
        elements: []);
    list.privateRelays = defaultRelaysIfEmpty;
  }
  if (list != null && list.allRelays.isNotEmpty) {
    list.removeRelay(relayUrl);
    list.createdAt = Helpers.now;
    Nip01Event event = await list.toEvent(eventSigner);
    await Future.wait([
      _relayManager.broadcastEvent(event, broadcastRelays, eventSigner),
    ]);
    List<Nip01Event>? events = await _cacheManager
        .loadEvents(pubKeys: [eventSigner.getPublicKey()], kinds: [kind]);
    for (var event in events) {
      _cacheManager.removeEvent(event.id);
    }
    await _cacheManager.saveEvent(event);
  }
  return list;
}