From 92a19d61cf3ac1aa05611b3457c4aba2ab5e130c Mon Sep 17 00:00:00 2001 From: Rasmus Karlsson Date: Sun, 22 Apr 2018 15:37:02 +0200 Subject: [PATCH] Add room ID to pubsub moderator actions --- src/singletons/helper/pubsubactions.cpp | 3 +- src/singletons/helper/pubsubactions.hpp | 33 +++--------- src/singletons/pubsubmanager.cpp | 71 +++++++++++++------------ src/singletons/pubsubmanager.hpp | 2 +- 4 files changed, 48 insertions(+), 61 deletions(-) diff --git a/src/singletons/helper/pubsubactions.cpp b/src/singletons/helper/pubsubactions.cpp index a9b736178..02fddc550 100644 --- a/src/singletons/helper/pubsubactions.cpp +++ b/src/singletons/helper/pubsubactions.cpp @@ -5,8 +5,9 @@ namespace chatterino { namespace singletons { -PubSubAction::PubSubAction(const rapidjson::Value &data) +PubSubAction::PubSubAction(const rapidjson::Value &data, const QString &_roomID) : timestamp(std::chrono::steady_clock::now()) + , roomID(_roomID) { getCreatedByUser(data, this->source); } diff --git a/src/singletons/helper/pubsubactions.hpp b/src/singletons/helper/pubsubactions.hpp index cb0ca3692..ea769747d 100644 --- a/src/singletons/helper/pubsubactions.hpp +++ b/src/singletons/helper/pubsubactions.hpp @@ -16,18 +16,16 @@ struct ActionUser { }; struct PubSubAction { - PubSubAction(const rapidjson::Value &data); + PubSubAction(const rapidjson::Value &data, const QString &_roomID); ActionUser source; std::chrono::steady_clock::time_point timestamp; + QString roomID; }; // Used when a chat mode (i.e. slowmode, subscribers only mode) is enabled or disabled struct ModeChangedAction : PubSubAction { - ModeChangedAction(const rapidjson::Value &data) - : PubSubAction(data) - { - } + using PubSubAction::PubSubAction; enum Mode { Unknown, @@ -49,10 +47,7 @@ struct ModeChangedAction : PubSubAction { }; struct TimeoutAction : PubSubAction { - TimeoutAction(const rapidjson::Value &data) - : PubSubAction(data) - { - } + using PubSubAction::PubSubAction; ActionUser target; @@ -61,10 +56,7 @@ struct TimeoutAction : PubSubAction { }; struct BanAction : PubSubAction { - BanAction(const rapidjson::Value &data) - : PubSubAction(data) - { - } + using PubSubAction::PubSubAction; ActionUser target; @@ -72,10 +64,7 @@ struct BanAction : PubSubAction { }; struct UnbanAction : PubSubAction { - UnbanAction(const rapidjson::Value &data) - : PubSubAction(data) - { - } + using PubSubAction::PubSubAction; ActionUser target; @@ -86,17 +75,11 @@ struct UnbanAction : PubSubAction { }; struct ClearChatAction : PubSubAction { - ClearChatAction(const rapidjson::Value &data) - : PubSubAction(data) - { - } + using PubSubAction::PubSubAction; }; struct ModerationStateAction : PubSubAction { - ModerationStateAction(const rapidjson::Value &data) - : PubSubAction(data) - { - } + using PubSubAction::PubSubAction; ActionUser target; diff --git a/src/singletons/pubsubmanager.cpp b/src/singletons/pubsubmanager.cpp index 4fff583e1..044c205b9 100644 --- a/src/singletons/pubsubmanager.cpp +++ b/src/singletons/pubsubmanager.cpp @@ -172,14 +172,14 @@ bool PubSubClient::Send(const char *payload) PubSubManager::PubSubManager() { - this->moderationActionHandlers["clear"] = [this](const auto &data) { - ClearChatAction action(data); + this->moderationActionHandlers["clear"] = [this](const auto &data, const auto &roomID) { + ClearChatAction action(data, roomID); this->sig.moderation.chatCleared.invoke(action); }; - this->moderationActionHandlers["slowoff"] = [this](const auto &data) { - ModeChangedAction action(data); + this->moderationActionHandlers["slowoff"] = [this](const auto &data, const auto &roomID) { + ModeChangedAction action(data, roomID); action.mode = ModeChangedAction::Mode::Slow; action.state = ModeChangedAction::State::Off; @@ -187,8 +187,8 @@ PubSubManager::PubSubManager() this->sig.moderation.modeChanged.invoke(action); }; - this->moderationActionHandlers["slow"] = [this](const auto &data) { - ModeChangedAction action(data); + this->moderationActionHandlers["slow"] = [this](const auto &data, const auto &roomID) { + ModeChangedAction action(data, roomID); action.mode = ModeChangedAction::Mode::Slow; action.state = ModeChangedAction::State::On; @@ -224,8 +224,8 @@ PubSubManager::PubSubManager() this->sig.moderation.modeChanged.invoke(action); }; - this->moderationActionHandlers["r9kbetaoff"] = [this](const auto &data) { - ModeChangedAction action(data); + this->moderationActionHandlers["r9kbetaoff"] = [this](const auto &data, const auto &roomID) { + ModeChangedAction action(data, roomID); action.mode = ModeChangedAction::Mode::R9K; action.state = ModeChangedAction::State::Off; @@ -233,8 +233,8 @@ PubSubManager::PubSubManager() this->sig.moderation.modeChanged.invoke(action); }; - this->moderationActionHandlers["r9kbeta"] = [this](const auto &data) { - ModeChangedAction action(data); + this->moderationActionHandlers["r9kbeta"] = [this](const auto &data, const auto &roomID) { + ModeChangedAction action(data, roomID); action.mode = ModeChangedAction::Mode::R9K; action.state = ModeChangedAction::State::On; @@ -242,8 +242,9 @@ PubSubManager::PubSubManager() this->sig.moderation.modeChanged.invoke(action); }; - this->moderationActionHandlers["subscribersoff"] = [this](const auto &data) { - ModeChangedAction action(data); + this->moderationActionHandlers["subscribersoff"] = [this](const auto &data, + const auto &roomID) { + ModeChangedAction action(data, roomID); action.mode = ModeChangedAction::Mode::SubscribersOnly; action.state = ModeChangedAction::State::Off; @@ -251,8 +252,8 @@ PubSubManager::PubSubManager() this->sig.moderation.modeChanged.invoke(action); }; - this->moderationActionHandlers["subscribers"] = [this](const auto &data) { - ModeChangedAction action(data); + this->moderationActionHandlers["subscribers"] = [this](const auto &data, const auto &roomID) { + ModeChangedAction action(data, roomID); action.mode = ModeChangedAction::Mode::SubscribersOnly; action.state = ModeChangedAction::State::On; @@ -260,8 +261,8 @@ PubSubManager::PubSubManager() this->sig.moderation.modeChanged.invoke(action); }; - this->moderationActionHandlers["emoteonlyoff"] = [this](const auto &data) { - ModeChangedAction action(data); + this->moderationActionHandlers["emoteonlyoff"] = [this](const auto &data, const auto &roomID) { + ModeChangedAction action(data, roomID); action.mode = ModeChangedAction::Mode::EmoteOnly; action.state = ModeChangedAction::State::Off; @@ -269,8 +270,8 @@ PubSubManager::PubSubManager() this->sig.moderation.modeChanged.invoke(action); }; - this->moderationActionHandlers["emoteonly"] = [this](const auto &data) { - ModeChangedAction action(data); + this->moderationActionHandlers["emoteonly"] = [this](const auto &data, const auto &roomID) { + ModeChangedAction action(data, roomID); action.mode = ModeChangedAction::Mode::EmoteOnly; action.state = ModeChangedAction::State::On; @@ -278,8 +279,8 @@ PubSubManager::PubSubManager() this->sig.moderation.modeChanged.invoke(action); }; - this->moderationActionHandlers["unmod"] = [this](const auto &data) { - ModerationStateAction action(data); + this->moderationActionHandlers["unmod"] = [this](const auto &data, const auto &roomID) { + ModerationStateAction action(data, roomID); getTargetUser(data, action.target); action.modded = false; @@ -287,8 +288,8 @@ PubSubManager::PubSubManager() this->sig.moderation.moderationStateChanged.invoke(action); }; - this->moderationActionHandlers["mod"] = [this](const auto &data) { - ModerationStateAction action(data); + this->moderationActionHandlers["mod"] = [this](const auto &data, const auto &roomID) { + ModerationStateAction action(data, roomID); getTargetUser(data, action.target); action.modded = true; @@ -296,8 +297,8 @@ PubSubManager::PubSubManager() this->sig.moderation.moderationStateChanged.invoke(action); }; - this->moderationActionHandlers["timeout"] = [this](const auto &data) { - TimeoutAction action(data); + this->moderationActionHandlers["timeout"] = [this](const auto &data, const auto &roomID) { + TimeoutAction action(data, roomID); getCreatedByUser(data, action.source); getTargetUser(data, action.target); @@ -332,8 +333,8 @@ PubSubManager::PubSubManager() } }; - this->moderationActionHandlers["ban"] = [this](const auto &data) { - BanAction action(data); + this->moderationActionHandlers["ban"] = [this](const auto &data, const auto &roomID) { + BanAction action(data, roomID); getCreatedByUser(data, action.source); getTargetUser(data, action.target); @@ -361,8 +362,8 @@ PubSubManager::PubSubManager() } }; - this->moderationActionHandlers["unban"] = [this](const auto &data) { - UnbanAction action(data); + this->moderationActionHandlers["unban"] = [this](const auto &data, const auto &roomID) { + UnbanAction action(data, roomID); getCreatedByUser(data, action.source); getTargetUser(data, action.target); @@ -386,8 +387,8 @@ PubSubManager::PubSubManager() } }; - this->moderationActionHandlers["untimeout"] = [this](const auto &data) { - UnbanAction action(data); + this->moderationActionHandlers["untimeout"] = [this](const auto &data, const auto &roomID) { + UnbanAction action(data, roomID); getCreatedByUser(data, action.source); getTargetUser(data, action.target); @@ -667,7 +668,7 @@ void PubSubManager::HandleListenResponse(const rapidjson::Document &msg) void PubSubManager::HandleMessageResponse(const rapidjson::Value &outerData) { - std::string topic; + QString topic; if (!rj::getSafe(outerData, "topic", topic)) { debug::Log("Missing required string member `topic` in outerData"); @@ -691,7 +692,7 @@ void PubSubManager::HandleMessageResponse(const rapidjson::Value &outerData) return; } - if (topic.find("whispers.") == 0) { + if (topic.startsWith("whispers.")) { std::string whisperType; if (!rj::getSafe(msg, "type", whisperType)) { @@ -710,7 +711,9 @@ void PubSubManager::HandleMessageResponse(const rapidjson::Value &outerData) assert(false); return; } - } else if (topic.find("chat_moderator_actions.") == 0) { + } else if (topic.startsWith("chat_moderator_actions.")) { + auto topicParts = topic.split("."); + assert(topicParts.length() == 3); const auto &data = msg["data"]; std::string moderationAction; @@ -728,7 +731,7 @@ void PubSubManager::HandleMessageResponse(const rapidjson::Value &outerData) } // Invoke handler function - handlerIt->second(data); + handlerIt->second(data, topicParts[2]); } else { debug::Log("Unknown topic: {}", topic); return; diff --git a/src/singletons/pubsubmanager.hpp b/src/singletons/pubsubmanager.hpp index 8997e8df5..e9a5bf00b 100644 --- a/src/singletons/pubsubmanager.hpp +++ b/src/singletons/pubsubmanager.hpp @@ -140,7 +140,7 @@ private: std::map, std::owner_less> clients; - std::unordered_map> + std::unordered_map> moderationActionHandlers; void OnMessage(websocketpp::connection_hdl hdl, WebsocketMessagePtr msg);