broadcastAddNip51SetRelay method

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

Implementation

Future<Nip51Set> broadcastAddNip51SetRelay(
  String relayUrl,
  String name,
  Iterable<String>? broadcastRelays, {
  bool private = false,
}) async {
  if (_eventSigner == null) {
    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(),
      kind: Nip51List.kRelaySet,
      createdAt: Helpers.now,
      elements: []);
  list.addRelay(relayUrl, private);
  list.createdAt = Helpers.now;
  Nip01Event event = await list.toEvent(_eventSigner);
  //print(event);

  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 list;
}