From 01b2230bcfd49f859ac6e4ab01578ff0dbfec37e Mon Sep 17 00:00:00 2001 From: Rasmus Karlsson Date: Sat, 28 Apr 2018 16:07:18 +0200 Subject: [PATCH] Modify pubsub functions to follow the Chatterino function style --- src/application.cpp | 6 +- src/providers/twitch/pubsub.cpp | 100 ++++++++++++------------- src/providers/twitch/pubsub.hpp | 44 +++++------ src/providers/twitch/pubsubhelpers.cpp | 6 +- src/providers/twitch/pubsubhelpers.hpp | 14 ++-- src/providers/twitch/twitchchannel.cpp | 2 +- 6 files changed, 86 insertions(+), 86 deletions(-) diff --git a/src/application.cpp b/src/application.cpp index 2e1231f48..79cbb8774 100644 --- a/src/application.cpp +++ b/src/application.cpp @@ -152,14 +152,14 @@ void Application::initialize() util::postToThread([chan, msg] { chan->addMessage(msg); }); }); - this->twitch.pubsub->Start(); + this->twitch.pubsub->start(); auto RequestModerationActions = [=]() { - this->twitch.pubsub->UnlistenAllModerationActions(); + this->twitch.pubsub->unlistenAllModerationActions(); // TODO(pajlada): Unlisten to all authed topics instead of only moderation topics // this->twitch.pubsub->UnlistenAllAuthedTopics(); - this->twitch.pubsub->ListenToWhispers(this->accounts->Twitch.getCurrent()); // + this->twitch.pubsub->listenToWhispers(this->accounts->Twitch.getCurrent()); // }; this->accounts->Twitch.userChanged.connect(RequestModerationActions); diff --git a/src/providers/twitch/pubsub.cpp b/src/providers/twitch/pubsub.cpp index 22a48bd58..1856e6d33 100644 --- a/src/providers/twitch/pubsub.cpp +++ b/src/providers/twitch/pubsub.cpp @@ -33,23 +33,23 @@ PubSubClient::PubSubClient(WebsocketClient &_websocketClient, WebsocketHandle _h { } -void PubSubClient::Start() +void PubSubClient::start() { assert(!this->started); this->started = true; - this->Ping(); + this->ping(); } -void PubSubClient::Stop() +void PubSubClient::stop() { assert(this->started); this->started = false; } -bool PubSubClient::Listen(rapidjson::Document &message) +bool PubSubClient::listen(rapidjson::Document &message) { int numRequestedListens = message["data"]["topics"].Size(); @@ -68,15 +68,15 @@ bool PubSubClient::Listen(rapidjson::Document &message) rj::set(message, "nonce", uuid); - std::string payload = Stringify(message); + std::string payload = stringify(message); sentMessages[uuid.toStdString()] = payload; - this->Send(payload.c_str()); + this->send(payload.c_str()); return true; } -void PubSubClient::UnlistenPrefix(const std::string &prefix) +void PubSubClient::unlistenPrefix(const std::string &prefix) { std::vector topics; @@ -94,19 +94,19 @@ void PubSubClient::UnlistenPrefix(const std::string &prefix) return; } - auto message = CreateUnlistenMessage(topics); + auto message = createUnlistenMessage(topics); auto uuid = CreateUUID(); rj::set(message, "nonce", CreateUUID()); - std::string payload = Stringify(message); + std::string payload = stringify(message); sentMessages[uuid.toStdString()] = payload; - this->Send(payload.c_str()); + this->send(payload.c_str()); } -void PubSubClient::HandlePong() +void PubSubClient::handlePong() { assert(this->awaitingPong); @@ -126,11 +126,11 @@ bool PubSubClient::isListeningToTopic(const std::string &payload) return false; } -void PubSubClient::Ping() +void PubSubClient::ping() { assert(this->started); - if (!this->Send(pingPayload)) { + if (!this->send(pingPayload)) { return; } @@ -138,7 +138,7 @@ void PubSubClient::Ping() auto self = this->shared_from_this(); - RunAfter(this->websocketClient.get_io_service(), std::chrono::seconds(15), [self](auto timer) { + runAfter(this->websocketClient.get_io_service(), std::chrono::seconds(15), [self](auto timer) { if (!self->started) { return; } @@ -149,16 +149,16 @@ void PubSubClient::Ping() } }); - RunAfter(this->websocketClient.get_io_service(), std::chrono::minutes(5), [self](auto timer) { + runAfter(this->websocketClient.get_io_service(), std::chrono::minutes(5), [self](auto timer) { if (!self->started) { return; } - self->Ping(); // + self->ping(); // }); } -bool PubSubClient::Send(const char *payload) +bool PubSubClient::send(const char *payload) { WebsocketErrorCode ec; this->websocketClient.send(this->handle, payload, websocketpp::frame::opcode::text, ec); @@ -425,17 +425,17 @@ PubSub::PubSub() this->websocketClient.init_asio(); // SSL Handshake - this->websocketClient.set_tls_init_handler(bind(&PubSub::OnTLSInit, this, ::_1)); + this->websocketClient.set_tls_init_handler(bind(&PubSub::onTLSInit, this, ::_1)); - this->websocketClient.set_message_handler(bind(&PubSub::OnMessage, this, ::_1, ::_2)); - this->websocketClient.set_open_handler(bind(&PubSub::OnConnectionOpen, this, ::_1)); - this->websocketClient.set_close_handler(bind(&PubSub::OnConnectionClose, this, ::_1)); + this->websocketClient.set_message_handler(bind(&PubSub::onMessage, this, ::_1, ::_2)); + this->websocketClient.set_open_handler(bind(&PubSub::onConnectionOpen, this, ::_1)); + this->websocketClient.set_close_handler(bind(&PubSub::onConnectionClose, this, ::_1)); // Add an initial client - this->AddClient(); + this->addClient(); } -void PubSub::AddClient() +void PubSub::addClient() { websocketpp::lib::error_code ec; auto con = this->websocketClient.get_connection(TWITCH_PUBSUB_URL, ec); @@ -448,12 +448,12 @@ void PubSub::AddClient() this->websocketClient.connect(con); } -void PubSub::Start() +void PubSub::start() { - this->mainThread.reset(new std::thread(std::bind(&PubSub::RunThread, this))); + this->mainThread.reset(new std::thread(std::bind(&PubSub::runThread, this))); } -void PubSub::ListenToWhispers(std::shared_ptr account) +void PubSub::listenToWhispers(std::shared_ptr account) { assert(account != nullptr); @@ -464,7 +464,7 @@ void PubSub::ListenToWhispers(std::shared_ptr std::vector topics({"whispers." + userID}); - this->Listen(std::move(CreateListenMessage(topics, account))); + this->listen(std::move(createListenMessage(topics, account))); if (ec) { debug::Log("Unable to send message to websocket server: {}", ec.message()); @@ -472,15 +472,15 @@ void PubSub::ListenToWhispers(std::shared_ptr } } -void PubSub::UnlistenAllModerationActions() +void PubSub::unlistenAllModerationActions() { for (const auto &p : this->clients) { const auto &client = p.second; - client->UnlistenPrefix("chat_moderator_actions."); + client->unlistenPrefix("chat_moderator_actions."); } } -void PubSub::ListenToChannelModerationActions( +void PubSub::listenToChannelModerationActions( const QString &channelID, std::shared_ptr account) { assert(!channelID.isEmpty()); @@ -503,14 +503,14 @@ void PubSub::ListenToChannelModerationActions( void PubSub::listenToTopic(const std::string &topic, std::shared_ptr account) { - auto message = CreateListenMessage({topic}, account); + auto message = createListenMessage({topic}, account); - this->Listen(std::move(message)); + this->listen(std::move(message)); } -void PubSub::Listen(rapidjson::Document &&msg) +void PubSub::listen(rapidjson::Document &&msg) { - if (this->TryListen(msg)) { + if (this->tryListen(msg)) { debug::Log("Successfully listened!"); return; } @@ -519,12 +519,12 @@ void PubSub::Listen(rapidjson::Document &&msg) this->requests.emplace_back(std::make_unique(std::move(msg))); } -bool PubSub::TryListen(rapidjson::Document &msg) +bool PubSub::tryListen(rapidjson::Document &msg) { - debug::Log("TryListen with {} clients", this->clients.size()); + debug::Log("tryListen with {} clients", this->clients.size()); for (const auto &p : this->clients) { const auto &client = p.second; - if (client->Listen(msg)) { + if (client->listen(msg)) { return true; } } @@ -544,7 +544,7 @@ bool PubSub::isListeningToTopic(const std::string &topic) return false; } -void PubSub::OnMessage(websocketpp::connection_hdl hdl, WebsocketMessagePtr websocketMessage) +void PubSub::onMessage(websocketpp::connection_hdl hdl, WebsocketMessagePtr websocketMessage) { const std::string &payload = websocketMessage->get_payload(); @@ -571,7 +571,7 @@ void PubSub::OnMessage(websocketpp::connection_hdl hdl, WebsocketMessagePtr webs } if (type == "RESPONSE") { - this->HandleListenResponse(msg); + this->handleListenResponse(msg); } else if (type == "MESSAGE") { if (!msg.HasMember("data")) { debug::Log("Missing required object member `data` in message root"); @@ -585,7 +585,7 @@ void PubSub::OnMessage(websocketpp::connection_hdl hdl, WebsocketMessagePtr webs return; } - this->HandleMessageResponse(data); + this->handleMessageResponse(data); } else if (type == "PONG") { auto clientIt = this->clients.find(hdl); @@ -595,25 +595,25 @@ void PubSub::OnMessage(websocketpp::connection_hdl hdl, WebsocketMessagePtr webs auto &client = *clientIt; - client.second->HandlePong(); + client.second->handlePong(); } else { debug::Log("Unknown message type: {}", type); } } -void PubSub::OnConnectionOpen(WebsocketHandle hdl) +void PubSub::onConnectionOpen(WebsocketHandle hdl) { auto client = std::make_shared(this->websocketClient, hdl); // We separate the starting from the constructor because we will want to use shared_from_this - client->Start(); + client->start(); this->clients.emplace(hdl, client); this->connected.invoke(); } -void PubSub::OnConnectionClose(WebsocketHandle hdl) +void PubSub::onConnectionClose(WebsocketHandle hdl) { auto clientIt = this->clients.find(hdl); @@ -623,14 +623,14 @@ void PubSub::OnConnectionClose(WebsocketHandle hdl) auto &client = clientIt->second; - client->Stop(); + client->stop(); this->clients.erase(clientIt); this->connected.invoke(); } -PubSub::WebsocketContextPtr PubSub::OnTLSInit(websocketpp::connection_hdl hdl) +PubSub::WebsocketContextPtr PubSub::onTLSInit(websocketpp::connection_hdl hdl) { WebsocketContextPtr ctx(new boost::asio::ssl::context(boost::asio::ssl::context::tlsv1)); @@ -645,7 +645,7 @@ PubSub::WebsocketContextPtr PubSub::OnTLSInit(websocketpp::connection_hdl hdl) return ctx; } -void PubSub::HandleListenResponse(const rapidjson::Document &msg) +void PubSub::handleListenResponse(const rapidjson::Document &msg) { std::string error; @@ -666,7 +666,7 @@ void PubSub::HandleListenResponse(const rapidjson::Document &msg) } } -void PubSub::HandleMessageResponse(const rapidjson::Value &outerData) +void PubSub::handleMessageResponse(const rapidjson::Value &outerData) { QString topic; @@ -719,7 +719,7 @@ void PubSub::HandleMessageResponse(const rapidjson::Value &outerData) std::string moderationAction; if (!rj::getSafe(data, "moderation_action", moderationAction)) { - debug::Log("Missing moderation action in data: {}", Stringify(data)); + debug::Log("Missing moderation action in data: {}", stringify(data)); return; } @@ -738,7 +738,7 @@ void PubSub::HandleMessageResponse(const rapidjson::Value &outerData) } } -void PubSub::RunThread() +void PubSub::runThread() { debug::Log("Start pubsub manager thread"); this->websocketClient.run(); diff --git a/src/providers/twitch/pubsub.hpp b/src/providers/twitch/pubsub.hpp index a21867f9f..9eba6c29d 100644 --- a/src/providers/twitch/pubsub.hpp +++ b/src/providers/twitch/pubsub.hpp @@ -54,19 +54,19 @@ class PubSubClient : public std::enable_shared_from_this public: PubSubClient(WebsocketClient &_websocketClient, WebsocketHandle _handle); - void Start(); - void Stop(); + void start(); + void stop(); - bool Listen(rapidjson::Document &message); - void UnlistenPrefix(const std::string &prefix); + bool listen(rapidjson::Document &message); + void unlistenPrefix(const std::string &prefix); - void HandlePong(); + void handlePong(); bool isListeningToTopic(const std::string &topic); private: - void Ping(); - bool Send(const char *payload); + void ping(); + bool send(const char *payload); }; } // namespace detail @@ -92,9 +92,9 @@ public: Disconnected, }; - void Start(); + void start(); - bool IsConnected() const + bool isConnected() const { return this->state == State::Connected; } @@ -119,11 +119,11 @@ public: } whisper; } sig; - void ListenToWhispers(std::shared_ptr account); + void listenToWhispers(std::shared_ptr account); - void UnlistenAllModerationActions(); + void unlistenAllModerationActions(); - void ListenToChannelModerationActions( + void listenToChannelModerationActions( const QString &channelID, std::shared_ptr account); std::vector> requests; @@ -132,12 +132,12 @@ private: void listenToTopic(const std::string &topic, std::shared_ptr account); - void Listen(rapidjson::Document &&msg); - bool TryListen(rapidjson::Document &msg); + void listen(rapidjson::Document &&msg); + bool tryListen(rapidjson::Document &msg); bool isListeningToTopic(const std::string &topic); - void AddClient(); + void addClient(); State state = State::Connected; @@ -148,15 +148,15 @@ private: std::unordered_map> moderationActionHandlers; - void OnMessage(websocketpp::connection_hdl hdl, WebsocketMessagePtr msg); - void OnConnectionOpen(websocketpp::connection_hdl hdl); - void OnConnectionClose(websocketpp::connection_hdl hdl); - WebsocketContextPtr OnTLSInit(websocketpp::connection_hdl hdl); + void onMessage(websocketpp::connection_hdl hdl, WebsocketMessagePtr msg); + void onConnectionOpen(websocketpp::connection_hdl hdl); + void onConnectionClose(websocketpp::connection_hdl hdl); + WebsocketContextPtr onTLSInit(websocketpp::connection_hdl hdl); - void HandleListenResponse(const rapidjson::Document &msg); - void HandleMessageResponse(const rapidjson::Value &data); + void handleListenResponse(const rapidjson::Document &msg); + void handleMessageResponse(const rapidjson::Value &data); - void RunThread(); + void runThread(); }; } // namespace twitch diff --git a/src/providers/twitch/pubsubhelpers.cpp b/src/providers/twitch/pubsubhelpers.cpp index 0c88a5950..658123ff0 100644 --- a/src/providers/twitch/pubsubhelpers.cpp +++ b/src/providers/twitch/pubsubhelpers.cpp @@ -34,12 +34,12 @@ bool getTargetUser(const rapidjson::Value &data, ActionUser &user) return rj::getSafe(data, "target_user_id", user.id); } -std::string Stringify(const rapidjson::Value &v) +std::string stringify(const rapidjson::Value &v) { return pajlada::Settings::SettingManager::stringify(v); } -rapidjson::Document CreateListenMessage(const std::vector &topicsVec, +rapidjson::Document createListenMessage(const std::vector &topicsVec, std::shared_ptr account) { rapidjson::Document msg(rapidjson::kObjectType); @@ -65,7 +65,7 @@ rapidjson::Document CreateListenMessage(const std::vector &topicsVe return msg; } -rapidjson::Document CreateUnlistenMessage(const std::vector &topicsVec) +rapidjson::Document createUnlistenMessage(const std::vector &topicsVec) { rapidjson::Document msg(rapidjson::kObjectType); auto &a = msg.GetAllocator(); diff --git a/src/providers/twitch/pubsubhelpers.hpp b/src/providers/twitch/pubsubhelpers.hpp index 49a0b9da2..4d3d838c9 100644 --- a/src/providers/twitch/pubsubhelpers.hpp +++ b/src/providers/twitch/pubsubhelpers.hpp @@ -21,22 +21,22 @@ bool getCreatedByUser(const rapidjson::Value &data, ActionUser &user); bool getTargetUser(const rapidjson::Value &data, ActionUser &user); -std::string Stringify(const rapidjson::Value &v); +std::string stringify(const rapidjson::Value &v); -rapidjson::Document CreateListenMessage(const std::vector &topicsVec, +rapidjson::Document createListenMessage(const std::vector &topicsVec, std::shared_ptr account); -rapidjson::Document CreateUnlistenMessage(const std::vector &topicsVec); +rapidjson::Document createUnlistenMessage(const std::vector &topicsVec); // Create timer using given ioService template -void RunAfter(boost::asio::io_service &ioService, Duration duration, Callback cb) +void runAfter(boost::asio::io_service &ioService, Duration duration, Callback cb) { auto timer = std::make_shared(ioService); timer->expires_from_now(duration); timer->async_wait([timer, cb](const boost::system::error_code &ec) { if (ec) { - debug::Log("Error in RunAfter: {}", ec.message()); + debug::Log("Error in runAfter: {}", ec.message()); return; } @@ -46,13 +46,13 @@ void RunAfter(boost::asio::io_service &ioService, Duration duration, Callback cb // Use provided timer template -void RunAfter(std::shared_ptr timer, Duration duration, Callback cb) +void runAfter(std::shared_ptr timer, Duration duration, Callback cb) { timer->expires_from_now(duration); timer->async_wait([timer, cb](const boost::system::error_code &ec) { if (ec) { - debug::Log("Error in RunAfter: {}", ec.message()); + debug::Log("Error in runAfter: {}", ec.message()); return; } diff --git a/src/providers/twitch/twitchchannel.cpp b/src/providers/twitch/twitchchannel.cpp index 525184db5..59abd0e71 100644 --- a/src/providers/twitch/twitchchannel.cpp +++ b/src/providers/twitch/twitchchannel.cpp @@ -58,7 +58,7 @@ TwitchChannel::TwitchChannel(const QString &channelName, Communi::IrcConnection auto account = app->accounts->Twitch.getCurrent(); if (account && !account->getUserId().isEmpty()) { - app->twitch.pubsub->ListenToChannelModerationActions(this->roomID, account); + app->twitch.pubsub->listenToChannelModerationActions(this->roomID, account); } };