doRelayRequest method

Future<bool> doRelayRequest(
  1. String id,
  2. RelayRequestState request
)

Implementation

Future<bool> doRelayRequest(String id, RelayRequestState request) async {
  if (_globalState.blockedRelays.contains(request.url)) {
    Logger.log.w(
      () => "COULD NOT SEND REQUEST TO ${request.url} since relay is blocked",
    );
    return false;
  }

  // a connection that is still opening is not one that is gone, and
  // _checkNetworkClose cannot tell them apart on its own
  final state = _globalState.inFlightRequests[id];
  if (state != null) {
    _relayManager.beginPendingConnection(state);
  }
  final bool connected;
  try {
    connected = await _relayManager.reconnectConnection(
      request.key,
      connectionSource:
          ConnectionSource.explicit, // TODO improve this connection source
      force: false,
      as: state?.request.auth?.account,
    );
  } finally {
    if (state != null) {
      _relayManager.endPendingConnection(state);
    }
  }
  // a timeout or a closeSubscription may have ended the request while the
  // connection was opening: nothing tracks it anymore, so nothing would ever
  // CLOSE what we would send here
  if (state != null && !_relayManager.isStillInFlight(state)) {
    return false;
  }
  if (connected) {
    RelayConnectivity? relay = _globalState.relays[request.key];
    if (relay == null) {
      // the connection was closed between opening it and looking it up
      Logger.log.w(
        () => "COULD NOT SEND REQUEST TO ${request.url}, ${request.key} "
            "is gone",
      );
      return false;
    }
    try {
      await _relayManager.sendOrThrow(
        relay,
        ClientMsg(ClientMsgType.kReq, id: id, filters: request.filters),
      );
    } catch (e) {
      Logger.log.e(
        () => "COULD NOT SEND REQUEST TO ${request.url}:",
        error: e,
      );
      return false;
    }
    return true;
  } else {
    Logger.log.e(
      () =>
          "COULD NOT SEND REQUEST TO ${request.url} since socket seems to be not open",
    );
    return false;
  }
}