From c40bdf812bb3eccb81eb2465fb6e8fdf4e3fa9a7 Mon Sep 17 00:00:00 2001 From: apa420 <17131426+apa420@users.noreply.github.com> Date: Sat, 8 May 2021 14:14:49 +0200 Subject: [PATCH] Fix automod messages not being parsed/showing up properly (#2742) Co-authored-by: pajlada --- CHANGELOG.md | 1 + src/messages/MessageBuilder.cpp | 2 +- src/providers/twitch/PubsubClient.cpp | 155 ++++++++++++++++++++------ src/providers/twitch/PubsubClient.hpp | 4 + 4 files changed, 129 insertions(+), 33 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0987e51a6..92363681e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unversioned +- Bugfix: Automod messages now work properly again. (#2682) - Bugfix: `Login expired` message no longer highlights all tabs. (#2735) ## 2.3.1 diff --git a/src/messages/MessageBuilder.cpp b/src/messages/MessageBuilder.cpp index 4123ceb28..b49e4413c 100644 --- a/src/messages/MessageBuilder.cpp +++ b/src/messages/MessageBuilder.cpp @@ -336,7 +336,7 @@ MessageBuilder::MessageBuilder(const AutomodUserAction &action) break; case AutomodUserAction::RemovePermitted: { - text = QString("%1 removed %2 as a permitted term term on AutoMod.") + text = QString("%1 removed %2 as a permitted term on AutoMod.") .arg(action.source.name) .arg(action.message); } diff --git a/src/providers/twitch/PubsubClient.cpp b/src/providers/twitch/PubsubClient.cpp index 9ebbd8734..14a10372c 100644 --- a/src/providers/twitch/PubsubClient.cpp +++ b/src/providers/twitch/PubsubClient.cpp @@ -583,7 +583,7 @@ PubSub::PubSub() } }; - this->moderationActionHandlers["add_permitted_term"] = + this->channelTermsActionHandlers["add_permitted_term"] = [this](const auto &data, const auto &roomID) { // This term got a pass through automod AutomodUserAction action(data, roomID); @@ -591,15 +591,13 @@ PubSub::PubSub() try { - const auto &args = getArgs(data); action.type = AutomodUserAction::AddPermitted; - - if (args.Size() < 1) + if (!rj::getSafe(data, "text", action.message)) { return; } - if (!rj::getSafe(args[0], action.message)) + if (!rj::getSafe(data, "requester_login", action.source.name)) { return; } @@ -609,11 +607,11 @@ PubSub::PubSub() catch (const std::runtime_error &ex) { qCDebug(chatterinoPubsub) - << "Error parsing moderation action:" << ex.what(); + << "Error parsing channel terms action:" << ex.what(); } }; - this->moderationActionHandlers["add_blocked_term"] = + this->channelTermsActionHandlers["add_blocked_term"] = [this](const auto &data, const auto &roomID) { // A term has been added AutomodUserAction action(data, roomID); @@ -621,15 +619,13 @@ PubSub::PubSub() try { - const auto &args = getArgs(data); action.type = AutomodUserAction::AddBlocked; - - if (args.Size() < 1) + if (!rj::getSafe(data, "text", action.message)) { return; } - if (!rj::getSafe(args[0], action.message)) + if (!rj::getSafe(data, "requester_login", action.source.name)) { return; } @@ -639,7 +635,7 @@ PubSub::PubSub() catch (const std::runtime_error &ex) { qCDebug(chatterinoPubsub) - << "Error parsing moderation action:" << ex.what(); + << "Error parsing channel terms action:" << ex.what(); } }; @@ -672,6 +668,33 @@ PubSub::PubSub() << "Error parsing moderation action:" << ex.what(); } }; + this->channelTermsActionHandlers["delete_permitted_term"] = + [this](const auto &data, const auto &roomID) { + // This term got deleted + AutomodUserAction action(data, roomID); + getCreatedByUser(data, action.source); + + try + { + action.type = AutomodUserAction::RemovePermitted; + if (!rj::getSafe(data, "text", action.message)) + { + return; + } + + if (!rj::getSafe(data, "requester_login", action.source.name)) + { + return; + } + + this->signals_.moderation.automodUserMessage.invoke(action); + } + catch (const std::runtime_error &ex) + { + qCDebug(chatterinoPubsub) + << "Error parsing channel terms action:" << ex.what(); + } + }; this->moderationActionHandlers["delete_blocked_term"] = [this](const auto &data, const auto &roomID) { @@ -703,16 +726,47 @@ PubSub::PubSub() << "Error parsing moderation action:" << ex.what(); } }; - - this->moderationActionHandlers["modified_automod_properties"] = + this->channelTermsActionHandlers["delete_blocked_term"] = [this](const auto &data, const auto &roomID) { - // The automod settings got modified + // This term got deleted AutomodUserAction action(data, roomID); + getCreatedByUser(data, action.source); - action.type = AutomodUserAction::Properties; - this->signals_.moderation.automodUserMessage.invoke(action); + + try + { + action.type = AutomodUserAction::RemoveBlocked; + if (!rj::getSafe(data, "text", action.message)) + { + return; + } + + if (!rj::getSafe(data, "requester_login", action.source.name)) + { + return; + } + + this->signals_.moderation.automodUserMessage.invoke(action); + } + catch (const std::runtime_error &ex) + { + qCDebug(chatterinoPubsub) + << "Error parsing channel terms action:" << ex.what(); + } }; + // We don't get this one anymore or anything similiar + // We need some new topic so we can listen + // + //this->moderationActionHandlers["modified_automod_properties"] = + // [this](const auto &data, const auto &roomID) { + // // The automod settings got modified + // AutomodUserAction action(data, roomID); + // getCreatedByUser(data, action.source); + // action.type = AutomodUserAction::Properties; + // this->signals_.moderation.automodUserMessage.invoke(action); + // }; + this->moderationActionHandlers["denied_automod_message"] = [](const auto &data, const auto @@ -1059,6 +1113,7 @@ void PubSub::handleListenResponse(const rapidjson::Document &msg) void PubSub::handleMessageResponse(const rapidjson::Value &outerData) { QString topic; + qCDebug(chatterinoPubsub) << rj::stringify(outerData).c_str(); if (!rj::getSafe(outerData, "topic", topic)) { @@ -1122,27 +1177,63 @@ void PubSub::handleMessageResponse(const rapidjson::Value &outerData) assert(topicParts.length() == 3); const auto &data = msg["data"]; - std::string moderationAction; + std::string moderationEventType; - if (!rj::getSafe(data, "moderation_action", moderationAction)) + if (!rj::getSafe(msg, "type", moderationEventType)) { - qCDebug(chatterinoPubsub) << "Missing moderation action in data:" - << rj::stringify(data).c_str(); + qCDebug(chatterinoPubsub) << "Bad moderator event data"; return; } - - auto handlerIt = this->moderationActionHandlers.find(moderationAction); - - if (handlerIt == this->moderationActionHandlers.end()) + if (moderationEventType == "moderation_action") { - qCDebug(chatterinoPubsub) - << "No handler found for moderation action" - << moderationAction.c_str(); - return; - } + std::string moderationAction; - // Invoke handler function - handlerIt->second(data, topicParts[2]); + if (!rj::getSafe(data, "moderation_action", moderationAction)) + { + qCDebug(chatterinoPubsub) + << "Missing moderation action in data:" + << rj::stringify(data).c_str(); + return; + } + + auto handlerIt = + this->moderationActionHandlers.find(moderationAction); + + if (handlerIt == this->moderationActionHandlers.end()) + { + qCDebug(chatterinoPubsub) + << "No handler found for moderation action" + << moderationAction.c_str(); + return; + } + // Invoke handler function + handlerIt->second(data, topicParts[2]); + } + else if (moderationEventType == "channel_terms_action") + { + std::string channelTermsAction; + + if (!rj::getSafe(data, "type", channelTermsAction)) + { + qCDebug(chatterinoPubsub) + << "Missing channel terms action in data:" + << rj::stringify(data).c_str(); + return; + } + + auto handlerIt = + this->channelTermsActionHandlers.find(channelTermsAction); + + if (handlerIt == this->channelTermsActionHandlers.end()) + { + qCDebug(chatterinoPubsub) + << "No handler found for channel terms action" + << channelTermsAction.c_str(); + return; + } + // Invoke handler function + handlerIt->second(data, topicParts[2]); + } } else if (topic.startsWith("community-points-channel-v1.")) { diff --git a/src/providers/twitch/PubsubClient.hpp b/src/providers/twitch/PubsubClient.hpp index 686098c8a..f64dd078d 100644 --- a/src/providers/twitch/PubsubClient.hpp +++ b/src/providers/twitch/PubsubClient.hpp @@ -179,6 +179,10 @@ private: const QString &)>> moderationActionHandlers; + std::unordered_map> + channelTermsActionHandlers; + void onMessage(websocketpp::connection_hdl hdl, WebsocketMessagePtr msg); void onConnectionOpen(websocketpp::connection_hdl hdl); void onConnectionClose(websocketpp::connection_hdl hdl);