handleEventBroadcast method

  1. @override
NdkBroadcastResponse handleEventBroadcast({
  1. required Nip01Event nostrEvent,
  2. required EventSigner? signer,
  3. required Stream<List<RelayBroadcastResponse>> doneStream,
  4. Iterable<String>? specificRelays,
})
override

Implementation

@override
NdkBroadcastResponse handleEventBroadcast({
  required Nip01Event nostrEvent,
  required EventSigner? signer,
  required Stream<List<RelayBroadcastResponse>> doneStream,
  Iterable<String>? specificRelays,
}) {
  Future<void> asyncStuff() async {
    // =====================================================================================
    // specific relays
    // =====================================================================================
    if (signer != null) {
      await signer.sign(nostrEvent);
    }

    if (specificRelays != null) {
      // check connectivity
      for (final relayUrl in specificRelays) {
        if (_relayManager.isRelayConnected(relayUrl)) {
          continue;
        }
        await _relayManager.connectRelay(
            dirtyUrl: relayUrl,
            connectionSource: ConnectionSource.BROADCAST_SPECIFIC);
      }
      // send out request
      for (final relayUrl in specificRelays) {
        _relayManager.registerRelayBroadcast(
          eventToPublish: nostrEvent,
          relayUrl: relayUrl,
        );

        final relayConnectivity =
            _relayManager.getRelayConnectivity(relayUrl);
        if (relayConnectivity != null) {
          _relayManager.send(
              relayConnectivity,
              ClientMsg(
                ClientMsgType.EVENT,
                event: nostrEvent,
              ));
        }
      }
      return;
    }
    // =====================================================================================
    // own outbox
    // =====================================================================================
    final nip65Data = (await UserRelayLists.getUserRelayListCacheLatest(
      pubkeys: [nostrEvent.pubKey],
      cacheManager: _cacheManager,
    ))
        .first;
    final writeRelaysUrls = nip65Data.relays.entries
        .where((element) => element.value.isWrite)
        .map((e) => e.key)
        .toList();

    for (final relayUrl in writeRelaysUrls) {
      final isConnected =
          _globalState.relays[relayUrl]?.relayTransport?.isOpen() ?? false;
      if (isConnected) {
        continue;
      }

      await _relayManager.connectRelay(
        dirtyUrl: relayUrl,
        connectionSource: ConnectionSource.BROADCAST_OWN,
      );
    }

    for (final relayUrl in writeRelaysUrls) {
      final relay = _globalState.relays[relayUrl];
      if (relay == null) {
        Logger.log.w("relay $relayUrl not found");
        continue;
      }

      _relayManager.registerRelayBroadcast(
        eventToPublish: nostrEvent,
        relayUrl: relayUrl,
      );

      _relayManager.send(
          relay,
          ClientMsg(
            ClientMsgType.EVENT,
            event: nostrEvent,
          ));
    }

    // =====================================================================================
    // other inbox
    // =====================================================================================
    if (nostrEvent.pTags.isNotEmpty) {
      final nip65Data = await UserRelayLists.getUserRelayListCacheLatest(
        pubkeys: nostrEvent.pTags,
        cacheManager: _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 =
            _globalState.relays[relayUrl]?.relayTransport?.isOpen() ?? false;
        if (isConnected) {
          continue;
        }
        await _relayManager.connectRelay(
            dirtyUrl: relayUrl,
            connectionSource: ConnectionSource.BROADCAST_OTHER);
      }
      for (final relayUrl in myWriteRelayUrlsOthers) {
        final relay = _globalState.relays[relayUrl];

        if (relay == null) {
          Logger.log.w("relay $relayUrl not found");
          continue;
        }

        _relayManager.registerRelayBroadcast(
          eventToPublish: nostrEvent,
          relayUrl: relayUrl,
        );

        _relayManager.send(
            relay,
            ClientMsg(
              ClientMsgType.EVENT,
              event: nostrEvent,
            ));
      }
    }
  }

  asyncStuff();

  return NdkBroadcastResponse(
    publishEvent: nostrEvent,
    broadcastDoneStream: doneStream,
  );
}