From c8bb6c8f56e0c6d93c8623722ab932c04de882b5 Mon Sep 17 00:00:00 2001 From: Michael Jurkoic Date: Wed, 10 Apr 2024 21:33:45 -0500 Subject: Handle relay response messages These changes do not yet have unit tests. --- include/client/web_socket_client.hpp | 13 +++++++++++ include/nostr.hpp | 44 ++++++++++++++++++++++++++++++++---- 2 files changed, 52 insertions(+), 5 deletions(-) (limited to 'include') diff --git a/include/client/web_socket_client.hpp b/include/client/web_socket_client.hpp index 3ef2b86..6fbede6 100644 --- a/include/client/web_socket_client.hpp +++ b/include/client/web_socket_client.hpp @@ -42,6 +42,19 @@ public: */ virtual std::tuple send(std::string message, std::string uri) = 0; + /** + * @brief Sends the given message to the given server and sets up a message handler for + * messages received from the server. + * @returns A tuple indicating the server URI and whether the message was successfully + * sent. + * @remark Use this method to send a message and set up a message handler for responses in the + * same call. + */ + virtual std::tuple send( + std::string message, + std::string uri, + std::function messageHandler) = 0; + /** * @brief Sets up a message handler for the given server. * @param uri The URI of the server to which the message handler should be attached. diff --git a/include/nostr.hpp b/include/nostr.hpp index e450505..62eceff 100644 --- a/include/nostr.hpp +++ b/include/nostr.hpp @@ -157,9 +157,23 @@ public: * @returns A tuple of `RelayList` objects, of the form ``, indicating * to which relays the event was published successfully, and to which relays the event failed * to publish. - */ + */ std::tuple publishEvent(std::shared_ptr event); + /** + * @brief Queries all open relay connections for events matching the given set of filters, and + * returns all stored matching events returned by the relays. + * @param filters The filters to use for the query. + * @returns A vector of all events matching the filters from all open relay connections. + * @remark This method runs until the relays send an EOSE message, indicating they have no more + * stored events matching the given filters. When the EOSE message is received, the method + * will close the subscription for each relay and return the received events. + * @remark Use this method to fetch a batch of events from the relays. A `limit` value must be + * set on the filters in the range 1-64, inclusive. If no valid limit is given, it will be + * defaulted to 16. + */ + std::vector> queryRelays(std::shared_ptr filters); + /** * @brief Queries all open relay connections for events matching the given set of filters. * @param filters The filters to use for the query. @@ -172,7 +186,9 @@ public: */ std::string queryRelays( std::shared_ptr filters, - std::function)> responseHandler); + std::function)> eventHandler, + std::function eoseHandler, + std::function closeHandler); /** * @brief Closes the subscription with the given ID on all open relay connections. @@ -269,11 +285,29 @@ private: bool hasSubscription(std::string relay, std::string subscriptionId); /** - * @brief Parses messages received from the relay and invokes the appropriate message handler. + * @brief Parses EVENT messages received from the relay and invokes the given event handler. + * @param message The raw message received from the relay. + * @param eventHandler A callable object that will be invoked with the subscription ID and the + * payload of the event. + * @param eoseHandler A callable object that will be invoked with the subscription ID when the + * relay sends an EOSE message, indicating it has reached the end of stored events for the + * given query. + * @param closeHandler A callable object that will be invoked with the subscription ID and the + * message sent by the relay if the subscription is ended by the relay. */ - void onMessage( + void onSubscriptionMessage( std::string message, - std::function)> eventHandler); + std::function)> eventHandler, + std::function eoseHandler, + std::function closeHandler); + + /** + * @brief Parses OK messages received from the relay and invokes the given acceptance handler. + * @remark The OK message type is sent to indicate whether the relay has accepted an event sent + * by the client. Note that this is distinct from whether the message was successfully sent to + * the relay over the WebSocket connection. + */ + void onAcceptance(std::string message, std::function acceptanceHandler); }; class ISigner -- cgit