From d6faf6c815611450d1b61045b53525d7f25ac5c9 Mon Sep 17 00:00:00 2001 From: buttercat1791 Date: Tue, 7 May 2024 09:22:21 -0500 Subject: Remove 'RelayList' type alias --- include/nostr.hpp | 32 +++++++++++++++----------------- src/nostr_service.cpp | 42 +++++++++++++++++++++--------------------- 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 RelayList; - class ISigner; class NostrService; @@ -135,12 +133,12 @@ public: std::shared_ptr appender, std::shared_ptr client, std::shared_ptr signer, - RelayList relays); + std::vector relays); ~NostrService(); - RelayList defaultRelays() const; + std::vector defaultRelays() const; - RelayList activeRelays() const; + std::vector activeRelays() const; std::unordered_map> 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 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 openRelayConnections(std::vector 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 relays); /** * @brief Publishes a Nostr event to all open relay connections. - * @returns A tuple of `RelayList` objects, of the form ``, indicating + * @returns A tuple of `std::vector` 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); + std::tuple, std::vector> publishEvent(std::shared_ptr 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 ``, indicating + * @returns A tuple of `std::vector` objects, of the form ``, indicating * to which relays the message was sent successfully, and which relays failed to receive the * message. */ - std::tuple closeSubscription(std::string subscriptionId); + std::tuple, std::vector> 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 closeSubscriptions(RelayList relays); + std::vector closeSubscriptions(std::vector 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 _defaultRelays; ///< The set of Nostr relays to which the service is currently connected. - RelayList _activeRelays; + std::vector _activeRelays; ///< A map from subscription IDs to the relays on which each subscription is open. std::unordered_map> _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 getConnectedRelays(std::vector 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 getUnconnectedRelays(std::vector 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 appender, shared_ptr client, shared_ptr signer, - RelayList relays) + vector 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 NostrService::defaultRelays() const { return this->_defaultRelays; }; -RelayList NostrService::activeRelays() const { return this->_activeRelays; }; +vector NostrService::activeRelays() const { return this->_activeRelays; }; unordered_map> NostrService::subscriptions() const { return this->_subscriptions; }; -RelayList NostrService::openRelayConnections() +vector NostrService::openRelayConnections() { return this->openRelayConnections(this->_defaultRelays); }; -RelayList NostrService::openRelayConnections(RelayList relays) +vector NostrService::openRelayConnections(vector relays) { PLOG_INFO << "Attempting to connect to Nostr relays."; - RelayList unconnectedRelays = this->getUnconnectedRelays(relays); + vector unconnectedRelays = this->getUnconnectedRelays(relays); vector 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 relays) { PLOG_INFO << "Disconnecting from Nostr relays."; - RelayList connectedRelays = getConnectedRelays(relays); + vector connectedRelays = getConnectedRelays(relays); vector disconnectionThreads; for (string relay : connectedRelays) @@ -102,10 +102,10 @@ void NostrService::closeRelayConnections(RelayList relays) }; // TODO: Make this method return a promise. -tuple NostrService::publishEvent(shared_ptr event) +tuple, vector> NostrService::publishEvent(shared_ptr event) { - RelayList successfulRelays; - RelayList failedRelays; + vector successfulRelays; + vector failedRelays; PLOG_INFO << "Attempting to publish event to Nostr relays."; @@ -127,7 +127,7 @@ tuple NostrService::publishEvent(shared_ptr event) } lock_guard lock(this->_propertyMutex); - RelayList targetRelays = this->_activeRelays; + vector targetRelays = this->_activeRelays; vector>> publishFutures; for (const string& relay : targetRelays) { @@ -283,8 +283,8 @@ string NostrService::queryRelays( function eoseHandler, function closeHandler) { - RelayList successfulRelays; - RelayList failedRelays; + vector successfulRelays; + vector failedRelays; string subscriptionId = this->generateSubscriptionId(); string request = filters->serialize(subscriptionId); @@ -329,10 +329,10 @@ string NostrService::queryRelays( return subscriptionId; }; -tuple NostrService::closeSubscription(string subscriptionId) +tuple, vector> NostrService::closeSubscription(string subscriptionId) { - RelayList successfulRelays; - RelayList failedRelays; + vector successfulRelays; + vector failedRelays; vector subscriptionRelays; size_t subscriptionRelayCount; @@ -452,10 +452,10 @@ vector NostrService::closeSubscriptions() return remainingSubscriptions; }; -RelayList NostrService::getConnectedRelays(RelayList relays) +vector NostrService::getConnectedRelays(vector relays) { PLOG_VERBOSE << "Identifying connected relays."; - RelayList connectedRelays; + vector 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 NostrService::getUnconnectedRelays(vector relays) { PLOG_VERBOSE << "Identifying unconnected relays."; - RelayList unconnectedRelays; + vector 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 #include +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 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 testRelays = { "wss://nos.lol" }; mutex connectionStatusMutex; auto connectionStatus = make_shared>(); @@ -312,7 +297,7 @@ TEST_F(NostrServiceTest, OpenRelayConnections_OpensConnections_ToProvidedRelays) TEST_F(NostrServiceTest, OpenRelayConnections_AddsOpenConnections_ToActiveRelays) { - nostr::RelayList testRelays = { "wss://nos.lol" }; + vector testRelays = { "wss://nos.lol" }; mutex connectionStatusMutex; auto connectionStatus = make_shared>(); @@ -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 testRelays = { "wss://nos.lol" }; + vector allTestRelays = { defaultTestRelays[0], defaultTestRelays[1], testRelays[0] }; mutex connectionStatusMutex; auto connectionStatus = make_shared>(); -- cgit