handleEventBroadcast method

  1. @override
NdkBroadcastResponse handleEventBroadcast({
  1. required Nip01Event nostrEvent,
  2. required EventSigner mySigner,
  3. Iterable<String>? specificRelays,
})
override

Implementation

@override
NdkBroadcastResponse handleEventBroadcast({
  required Nip01Event nostrEvent,
  required EventSigner mySigner,
  Iterable<String>? specificRelays,
}) {
  Future<void> asyncStuff() async {
    // =====================================================================================
    // specific relays
    // =====================================================================================

    if (specificRelays != null) {
      for (final relayUrl in specificRelays) {
        final isConnected = _relayManager.isRelayConnected(relayUrl);
        if (isConnected) {
          continue;
        }
        await _relayManager.connectRelay(relayUrl);
      }
      _relayManager.broadcastEvent(
        nostrEvent,
        specificRelays,
        mySigner,
      );
    }
    // =====================================================================================
    // own outbox
    // =====================================================================================
    final nip65Data =
        await getNip65DataSingle(nostrEvent.pubKey, _cacheManager);
    final writeRelaysUrls = nip65Data.relays.entries
        .where((element) => element.value.isWrite)
        .map((e) => e.key)
        .toList();

    for (final relayUrl in writeRelaysUrls) {
      final isConnected = _relayManager.isRelayConnected(relayUrl);
      if (isConnected) {
        continue;
      }
      await _relayManager.connectRelay(relayUrl);
    }

    _relayManager.broadcastEvent(nostrEvent, writeRelaysUrls, mySigner);

    // =====================================================================================
    // other inbox
    // =====================================================================================
    if (nostrEvent.pTags.isNotEmpty) {
      final nip65Data = await getNip65Data(nostrEvent.pTags, _cacheManager);

      List<String> myWriteRelayUrlsOthers = [];

      /// filter read relays
      for (final userNip65 in nip65Data) {
        final completeList = userNip65.relays.entries
            .where((element) => element.value.isRead)
            .map((e) => e.key)
            .toList();

        // cut list of at a certain threshold
        final maxList = completeList.sublist(
          0,
          BroadcastDefaults.MAX_INBOX_RELAYS_TO_BROADCAST,
        );
        myWriteRelayUrlsOthers.addAll(maxList);
      }

      for (final relayUrl in myWriteRelayUrlsOthers) {
        final isConnected = _relayManager.isRelayConnected(relayUrl);
        if (isConnected) {
          continue;
        }
        await _relayManager.connectRelay(relayUrl);
      }
      _relayManager.broadcastEvent(
          nostrEvent, myWriteRelayUrlsOthers, mySigner);
    }
  }

  asyncStuff();

  return NdkBroadcastResponse(publishedEvent: nostrEvent);
}