broadcastRemoveNip51SetRelay method

Future<Nip51Set?> broadcastRemoveNip51SetRelay(
  1. String relayUrl,
  2. String name,
  3. Iterable<String>? broadcastRelays, {
  4. List<String>? defaultRelaysIfEmpty,
  5. bool private = false,
})

Implementation

Future<Nip51Set?> broadcastRemoveNip51SetRelay(
  String relayUrl,
  String name,
  Iterable<String>? broadcastRelays, {
  List<String>? defaultRelaysIfEmpty,
  bool private = false,
}) async {
  if (_eventSigner == null) {
    throw Exception(
        "cannot broadcast private nip51 list without a signer that can sign");
  }
  Nip51Set? relaySet = await getSingleNip51RelaySet(
    name,
    _eventSigner,
    forceRefresh: true,
  );
  if ((relaySet == null || relaySet.allRelays.isEmpty) &&
      defaultRelaysIfEmpty != null &&
      defaultRelaysIfEmpty.isNotEmpty) {
    relaySet = Nip51Set(
        name: name,
        kind: Nip51List.kRelaySet,
        pubKey: _eventSigner.getPublicKey(),
        createdAt: Helpers.now,
        elements: []);
    relaySet.privateRelays = defaultRelaysIfEmpty;
  }
  if (relaySet != null) {
    relaySet.removeRelay(relayUrl);
    relaySet.createdAt = Helpers.now;
    Nip01Event event = await relaySet.toEvent(_eventSigner);

    final broadcastResponse = _broadcast.broadcast(
      nostrEvent: event,
      specificRelays: broadcastRelays,
      customSigner: _eventSigner,
    );
    await broadcastResponse.broadcastDoneFuture;

    List<Nip01Event>? events = await _cacheManager.loadEvents(
        pubKeys: [_eventSigner.getPublicKey()], kinds: [Nip51List.kRelaySet]);
    events = events.where((event) {
      if (event.getDtag() != null && event.getDtag() == name) {
        return true;
      }
      return false;
    }).toList();
    for (var event in events) {
      _cacheManager.removeEvent(event.id);
    }
    await _cacheManager.saveEvent(event);
  }
  return relaySet;
}