From e31dc09e91ea13dfec4fcc03946069f61b4b8b43 Mon Sep 17 00:00:00 2001 From: Rasmus Karlsson Date: Sun, 29 Apr 2018 13:24:37 +0200 Subject: [PATCH] Add remaining pubsub messages to chat --- src/application.cpp | 52 ++++++++++++++++++---- src/providers/twitch/ircmessagehandler.cpp | 1 + src/providers/twitch/pubsub.cpp | 32 ++++++++++++- src/providers/twitch/pubsubactions.hpp | 20 +++++++-- 4 files changed, 93 insertions(+), 12 deletions(-) diff --git a/src/application.cpp b/src/application.cpp index 79cbb8774..fd6e155f5 100644 --- a/src/application.cpp +++ b/src/application.cpp @@ -114,18 +114,54 @@ void Application::initialize() debug::Log("WHISPER RECEIVED LOL"); // }); - this->twitch.pubsub->sig.moderation.chatCleared.connect([&](const auto &action) { - debug::Log("Chat cleared by {}", action.source.name); // + this->twitch.pubsub->sig.moderation.chatCleared.connect([this](const auto &action) { + auto chan = this->twitch.server->getChannelOrEmptyByID(action.roomID); + if (chan->isEmpty()) { + return; + } + + QString text = QString("%1 cleared the chat").arg(action.source.name); + + auto msg = messages::Message::createSystemMessage(text); + util::postToThread([chan, msg] { chan->addMessage(msg); }); }); - this->twitch.pubsub->sig.moderation.modeChanged.connect([&](const auto &action) { - debug::Log("Mode {} was turned {} by {} (duration {})", (int &)action.mode, - (bool &)action.state, action.source.name, action.args.duration); + this->twitch.pubsub->sig.moderation.modeChanged.connect([this](const auto &action) { + auto chan = this->twitch.server->getChannelOrEmptyByID(action.roomID); + if (chan->isEmpty()) { + return; + } + + QString text = + QString("%1 turned %2 %3 mode") // + .arg(action.source.name) + .arg(action.state == providers::twitch::ModeChangedAction::State::On ? "on" : "off") + .arg(action.getModeName()); + + if (action.duration > 0) { + text.append(" (" + QString::number(action.duration) + " seconds)"); + } + + auto msg = messages::Message::createSystemMessage(text); + util::postToThread([chan, msg] { chan->addMessage(msg); }); }); - this->twitch.pubsub->sig.moderation.moderationStateChanged.connect([&](const auto &action) { - debug::Log("User {} was {} by {}", action.target.id, action.modded ? "modded" : "unmodded", - action.source.name); + this->twitch.pubsub->sig.moderation.moderationStateChanged.connect([this](const auto &action) { + auto chan = this->twitch.server->getChannelOrEmptyByID(action.roomID); + if (chan->isEmpty()) { + return; + } + + QString text; + + if (action.modded) { + text = QString("%1 modded %2").arg(action.source.name, action.target.name); + } else { + text = QString("%1 unmodded %2").arg(action.source.name, action.target.name); + } + + auto msg = messages::Message::createSystemMessage(text); + util::postToThread([chan, msg] { chan->addMessage(msg); }); }); this->twitch.pubsub->sig.moderation.userBanned.connect([&](const auto &action) { diff --git a/src/providers/twitch/ircmessagehandler.cpp b/src/providers/twitch/ircmessagehandler.cpp index 865a31a44..ccbf3464d 100644 --- a/src/providers/twitch/ircmessagehandler.cpp +++ b/src/providers/twitch/ircmessagehandler.cpp @@ -212,6 +212,7 @@ void IrcMessageHandler::handleModeMessage(Communi::IrcMessage *message) void IrcMessageHandler::handleNoticeMessage(Communi::IrcNoticeMessage *message) { + return; auto app = getApp(); MessagePtr msg = Message::createSystemMessage(message->content()); diff --git a/src/providers/twitch/pubsub.cpp b/src/providers/twitch/pubsub.cpp index 1856e6d33..bd8914fad 100644 --- a/src/providers/twitch/pubsub.cpp +++ b/src/providers/twitch/pubsub.cpp @@ -226,7 +226,7 @@ PubSub::PubSub() bool ok; - action.args.duration = QString(durationArg.GetString()).toUInt(&ok, 10); + action.duration = QString(durationArg.GetString()).toUInt(&ok, 10); this->sig.moderation.modeChanged.invoke(action); }; @@ -290,6 +290,21 @@ PubSub::PubSub() ModerationStateAction action(data, roomID); getTargetUser(data, action.target); + + try { + const auto &args = getArgs(data); + + if (args.Size() < 1) { + return; + } + + if (!rj::getSafe(args[0], action.target.name)) { + return; + } + } catch (const std::runtime_error &ex) { + debug::Log("Error parsing moderation action: {}", ex.what()); + } + action.modded = false; this->sig.moderation.moderationStateChanged.invoke(action); @@ -299,6 +314,21 @@ PubSub::PubSub() ModerationStateAction action(data, roomID); getTargetUser(data, action.target); + + try { + const auto &args = getArgs(data); + + if (args.Size() < 1) { + return; + } + + if (!rj::getSafe(args[0], action.target.name)) { + return; + } + } catch (const std::runtime_error &ex) { + debug::Log("Error parsing moderation action: {}", ex.what()); + } + action.modded = true; this->sig.moderation.moderationStateChanged.invoke(action); diff --git a/src/providers/twitch/pubsubactions.hpp b/src/providers/twitch/pubsubactions.hpp index 68c026273..f108c92b2 100644 --- a/src/providers/twitch/pubsubactions.hpp +++ b/src/providers/twitch/pubsubactions.hpp @@ -41,9 +41,23 @@ struct ModeChangedAction : PubSubAction { On, } state; - union { - uint32_t duration; - } args; + uint32_t duration = 0; + + const char *getModeName() const + { + switch (this->mode) { + case Mode::Slow: + return "slow"; + case Mode::R9K: + return "r9k"; + case Mode::SubscribersOnly: + return "subscribers-only"; + case Mode::EmoteOnly: + return "emote-only"; + default: + return "unknown"; + } + } }; struct BanAction : PubSubAction {