handleRequest method

  1. @override
Future<void> handleRequest(
  1. RequestState state
)
override

Implementation

@override
Future<void> handleRequest(RequestState state) async {
  await _relayManager.seedRelaysConnected;
  state.request.onTimeout = (state) {
    Logger.log.w(
        "request ${state.request.id} : ${state.request.filters} timed out after ${state.request.timeout}");
    for (var url in state.requests.keys) {
      _relayManager.sendCloseToRelay(url, state.id);
    }
    _relayManager.removeInFlightRequestById(state.id);
  };

  if (state.request.relaySet != null) {
    return await doNostrRequestWithRelaySet(state);
  }
  if (state.request.explicitRelays != null &&
      state.request.explicitRelays!.isNotEmpty) {
    for (final url in state.request.explicitRelays!) {
      await _relayManager.connectRelay(
          dirtyUrl: url, connectionSource: ConnectionSource.EXPLICIT);
      state.addRequest(
          url, RelaySet.sliceFilterAuthors(state.request.filters.first));
    }
  } else {
    for (final url in _bootstrapRelays) {
      state.addRequest(
          url, RelaySet.sliceFilterAuthors(state.request.filters.first));
    }
  }
  _globalState.inFlightRequests[state.id] = state;

  List<String> notSent = [];
  for (MapEntry<String, RelayRequestState> entry in state.requests.entries) {
    if (!doRelayRequest(state.id, entry.value)) {
      notSent.add(entry.key);
    }
  }
  for (var url in notSent) {
    state.requests.remove(url);
  }
}