broadcastAddNip51SetRelay method

Future<Nip51Set> broadcastAddNip51SetRelay(
  1. String relayUrl,
  2. String name,
  3. Iterable<String> broadcastRelays,
  4. EventSigner eventSigner, {
  5. bool private = false,
})

Implementation

Future<Nip51Set> broadcastAddNip51SetRelay(String relayUrl, String name,
    Iterable<String> broadcastRelays, EventSigner eventSigner,
    {bool private = false}) async {
  if (private && !eventSigner.canSign()) {
    throw Exception(
        "cannot broadcast private nip51 list without a signer that can sign");
  }
  Nip51Set? list =
      await getSingleNip51RelaySet(name, eventSigner, forceRefresh: true);
  list ??= Nip51Set(
      name: name,
      pubKey: eventSigner.getPublicKey(),
      createdAt: Helpers.now,
      elements: []);
  list.addRelay(relayUrl, private);
  list.createdAt = Helpers.now;
  Nip01Event event = await list.toEvent(eventSigner);
  //print(event);
  await Future.wait([
    _relayManager.broadcastEvent(event, broadcastRelays, eventSigner),
  ]);
  List<Nip01Event>? events = await _cacheManager.loadEvents(
      pubKeys: [eventSigner.getPublicKey()], kinds: [Nip51List.RELAY_SET]);
  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 list;
}