diff options
-rw-r--r-- | include/nostr.hpp | 32 | ||||
-rw-r--r-- | src/nostr_service.cpp | 42 | ||||
-rw-r--r-- | test/nostr_service_test.cpp | 31 |
3 files changed, 44 insertions, 61 deletions
diff --git a/include/nostr.hpp b/include/nostr.hpp index e76d1e5..e5b29c7 100644 --- a/include/nostr.hpp +++ b/include/nostr.hpp @@ -21,8 +21,6 @@ namespace nostr { -typedef std::vector<std::string> RelayList; - class ISigner; class NostrService; @@ -135,12 +133,12 @@ public: std::shared_ptr<plog::IAppender> appender, std::shared_ptr<client::IWebSocketClient> client, std::shared_ptr<ISigner> signer, - RelayList relays); + std::vector<std::string> relays); ~NostrService(); - RelayList defaultRelays() const; + std::vector<std::string> defaultRelays() const; - RelayList activeRelays() const; + std::vector<std::string> activeRelays() const; std::unordered_map<std::string, std::vector<std::string>> subscriptions() const; @@ -149,13 +147,13 @@ public: * the constructor. * @return A list of the relay URLs to which connections were successfully opened. */ - RelayList openRelayConnections(); + std::vector<std::string> openRelayConnections(); /** * @brief Opens connections to the specified Nostr relays. * @returns A list of the relay URLs to which connections were successfully opened. */ - RelayList openRelayConnections(RelayList relays); + std::vector<std::string> openRelayConnections(std::vector<std::string> relays); /** * @brief Closes all open relay connections. @@ -165,15 +163,15 @@ public: /** * @brief Closes any open connections to the specified Nostr relays. */ - void closeRelayConnections(RelayList relays); + void closeRelayConnections(std::vector<std::string> relays); /** * @brief Publishes a Nostr event to all open relay connections. - * @returns A tuple of `RelayList` objects, of the form `<successes, failures>`, indicating + * @returns A tuple of `std::vector<std::string>` objects, of the form `<successes, failures>`, indicating * to which relays the event was published successfully, and to which relays the event failed * to publish. */ - std::tuple<RelayList, RelayList> publishEvent(std::shared_ptr<Event> event); + std::tuple<std::vector<std::string>, std::vector<std::string>> publishEvent(std::shared_ptr<Event> event); /** * @brief Queries all open relay connections for events matching the given set of filters, and @@ -211,11 +209,11 @@ public: /** * @brief Closes the subscription with the given ID on all open relay connections. - * @returns A tuple of `RelayList` objects, of the form `<successes, failures>`, indicating + * @returns A tuple of `std::vector<std::string>` objects, of the form `<successes, failures>`, indicating * to which relays the message was sent successfully, and which relays failed to receive the * message. */ - std::tuple<RelayList, RelayList> closeSubscription(std::string subscriptionId); + std::tuple<std::vector<std::string>, std::vector<std::string>> closeSubscription(std::string subscriptionId); /** * @brief Closes the subscription with the given ID on the given relay. @@ -235,7 +233,7 @@ public: * @brief Closes all open subscriptions on the given relays. * @returns A list of any subscription IDs that failed to close. */ - std::vector<std::string> closeSubscriptions(RelayList relays); + std::vector<std::string> closeSubscriptions(std::vector<std::string> relays); private: ///< The maximum number of events the service will store for each subscription. @@ -249,9 +247,9 @@ private: ///< A mutex to protect the instance properties. std::mutex _propertyMutex; ///< The default set of Nostr relays to which the service will attempt to connect. - RelayList _defaultRelays; + std::vector<std::string> _defaultRelays; ///< The set of Nostr relays to which the service is currently connected. - RelayList _activeRelays; + std::vector<std::string> _activeRelays; ///< A map from subscription IDs to the relays on which each subscription is open. std::unordered_map<std::string, std::vector<std::string>> _subscriptions; @@ -259,13 +257,13 @@ private: * @brief Determines which of the given relays are currently connected. * @returns A list of the URIs of currently-open relay connections from the given list. */ - RelayList getConnectedRelays(RelayList relays); + std::vector<std::string> getConnectedRelays(std::vector<std::string> relays); /** * @brief Determines which of the given relays are not currently connected. * @returns A list of the URIs of currently-unconnected relays from the given list. */ - RelayList getUnconnectedRelays(RelayList relays); + std::vector<std::string> getUnconnectedRelays(std::vector<std::string> relays); /** * @brief Determines whether the given relay is currently connected. diff --git a/src/nostr_service.cpp b/src/nostr_service.cpp index 5443aac..664243f 100644 --- a/src/nostr_service.cpp +++ b/src/nostr_service.cpp @@ -16,7 +16,7 @@ NostrService::NostrService( shared_ptr<plog::IAppender> appender, shared_ptr<client::IWebSocketClient> client, shared_ptr<ISigner> signer, - RelayList relays) + vector<string> relays) : _defaultRelays(relays), _client(client), _signer(signer) { plog::init(plog::debug, appender.get()); @@ -28,21 +28,21 @@ NostrService::~NostrService() this->_client->stop(); }; -RelayList NostrService::defaultRelays() const { return this->_defaultRelays; }; +vector<string> NostrService::defaultRelays() const { return this->_defaultRelays; }; -RelayList NostrService::activeRelays() const { return this->_activeRelays; }; +vector<string> NostrService::activeRelays() const { return this->_activeRelays; }; unordered_map<string, vector<string>> NostrService::subscriptions() const { return this->_subscriptions; }; -RelayList NostrService::openRelayConnections() +vector<string> NostrService::openRelayConnections() { return this->openRelayConnections(this->_defaultRelays); }; -RelayList NostrService::openRelayConnections(RelayList relays) +vector<string> NostrService::openRelayConnections(vector<string> relays) { PLOG_INFO << "Attempting to connect to Nostr relays."; - RelayList unconnectedRelays = this->getUnconnectedRelays(relays); + vector<string> unconnectedRelays = this->getUnconnectedRelays(relays); vector<thread> connectionThreads; for (string relay : unconnectedRelays) @@ -77,10 +77,10 @@ void NostrService::closeRelayConnections() this->closeRelayConnections(this->_activeRelays); }; -void NostrService::closeRelayConnections(RelayList relays) +void NostrService::closeRelayConnections(vector<string> relays) { PLOG_INFO << "Disconnecting from Nostr relays."; - RelayList connectedRelays = getConnectedRelays(relays); + vector<string> connectedRelays = getConnectedRelays(relays); vector<thread> disconnectionThreads; for (string relay : connectedRelays) @@ -102,10 +102,10 @@ void NostrService::closeRelayConnections(RelayList relays) }; // TODO: Make this method return a promise. -tuple<RelayList, RelayList> NostrService::publishEvent(shared_ptr<Event> event) +tuple<vector<string>, vector<string>> NostrService::publishEvent(shared_ptr<Event> event) { - RelayList successfulRelays; - RelayList failedRelays; + vector<string> successfulRelays; + vector<string> failedRelays; PLOG_INFO << "Attempting to publish event to Nostr relays."; @@ -127,7 +127,7 @@ tuple<RelayList, RelayList> NostrService::publishEvent(shared_ptr<Event> event) } lock_guard<mutex> lock(this->_propertyMutex); - RelayList targetRelays = this->_activeRelays; + vector<string> targetRelays = this->_activeRelays; vector<future<tuple<string, bool>>> publishFutures; for (const string& relay : targetRelays) { @@ -283,8 +283,8 @@ string NostrService::queryRelays( function<void(const string&)> eoseHandler, function<void(const string&, const string&)> closeHandler) { - RelayList successfulRelays; - RelayList failedRelays; + vector<string> successfulRelays; + vector<string> failedRelays; string subscriptionId = this->generateSubscriptionId(); string request = filters->serialize(subscriptionId); @@ -329,10 +329,10 @@ string NostrService::queryRelays( return subscriptionId; }; -tuple<RelayList, RelayList> NostrService::closeSubscription(string subscriptionId) +tuple<vector<string>, vector<string>> NostrService::closeSubscription(string subscriptionId) { - RelayList successfulRelays; - RelayList failedRelays; + vector<string> successfulRelays; + vector<string> failedRelays; vector<string> subscriptionRelays; size_t subscriptionRelayCount; @@ -452,10 +452,10 @@ vector<string> NostrService::closeSubscriptions() return remainingSubscriptions; }; -RelayList NostrService::getConnectedRelays(RelayList relays) +vector<string> NostrService::getConnectedRelays(vector<string> relays) { PLOG_VERBOSE << "Identifying connected relays."; - RelayList connectedRelays; + vector<string> connectedRelays; for (string relay : relays) { bool isActive = find(this->_activeRelays.begin(), this->_activeRelays.end(), relay) @@ -480,10 +480,10 @@ RelayList NostrService::getConnectedRelays(RelayList relays) return connectedRelays; }; -RelayList NostrService::getUnconnectedRelays(RelayList relays) +vector<string> NostrService::getUnconnectedRelays(vector<string> relays) { PLOG_VERBOSE << "Identifying unconnected relays."; - RelayList unconnectedRelays; + vector<string> unconnectedRelays; for (string relay : relays) { bool isActive = find(this->_activeRelays.begin(), this->_activeRelays.end(), relay) diff --git a/test/nostr_service_test.cpp b/test/nostr_service_test.cpp index 0f0d439..b3b9b28 100644 --- a/test/nostr_service_test.cpp +++ b/test/nostr_service_test.cpp @@ -10,25 +10,10 @@ #include <client/web_socket_client.hpp> #include <nostr.hpp> +using namespace std; +using namespace ::testing; + using nlohmann::json; -using std::function; -using std::lock_guard; -using std::make_shared; -using std::make_unique; -using std::mutex; -using std::promise; -using std::shared_ptr; -using std::string; -using std::thread; -using std::tuple; -using std::unordered_map; -using std::vector; -using ::testing::_; -using ::testing::Args; -using ::testing::HasSubstr; -using ::testing::Invoke; -using ::testing::Return; -using ::testing::Truly; namespace nostr_test { @@ -56,7 +41,7 @@ public: class NostrServiceTest : public testing::Test { public: - inline static const nostr::RelayList defaultTestRelays = + inline static const vector<string> defaultTestRelays = { "wss://relay.damus.io", "wss://nostr.thesamecat.io" @@ -273,7 +258,7 @@ TEST_F(NostrServiceTest, OpenRelayConnections_OpensConnections_ToDefaultRelays) TEST_F(NostrServiceTest, OpenRelayConnections_OpensConnections_ToProvidedRelays) { - nostr::RelayList testRelays = { "wss://nos.lol" }; + vector<string> testRelays = { "wss://nos.lol" }; mutex connectionStatusMutex; auto connectionStatus = make_shared<unordered_map<string, bool>>(); @@ -312,7 +297,7 @@ TEST_F(NostrServiceTest, OpenRelayConnections_OpensConnections_ToProvidedRelays) TEST_F(NostrServiceTest, OpenRelayConnections_AddsOpenConnections_ToActiveRelays) { - nostr::RelayList testRelays = { "wss://nos.lol" }; + vector<string> testRelays = { "wss://nos.lol" }; mutex connectionStatusMutex; auto connectionStatus = make_shared<unordered_map<string, bool>>(); @@ -401,8 +386,8 @@ TEST_F(NostrServiceTest, CloseRelayConnections_ClosesConnections_ToActiveRelays) TEST_F(NostrServiceTest, CloseRelayConnections_RemovesClosedConnections_FromActiveRelays) { - nostr::RelayList testRelays = { "wss://nos.lol" }; - nostr::RelayList allTestRelays = { defaultTestRelays[0], defaultTestRelays[1], testRelays[0] }; + vector<string> testRelays = { "wss://nos.lol" }; + vector<string> allTestRelays = { defaultTestRelays[0], defaultTestRelays[1], testRelays[0] }; mutex connectionStatusMutex; auto connectionStatus = make_shared<unordered_map<string, bool>>(); |