From 6d56148ed260f1e78ec98d13bb7efe6ca45a35f1 Mon Sep 17 00:00:00 2001 From: Rasmus Karlsson Date: Sat, 16 Dec 2017 19:08:32 +0100 Subject: [PATCH] Implement basic ClearChat handling Fixes #56 --- src/ircmanager.cpp | 44 +++++++++++++++++++++++++++++++++++++++- src/messages/message.cpp | 30 +++++++++++++++++++++++++++ src/messages/message.hpp | 3 +++ 3 files changed, 76 insertions(+), 1 deletion(-) diff --git a/src/ircmanager.cpp b/src/ircmanager.cpp index f145dc01e..8723ea82e 100644 --- a/src/ircmanager.cpp +++ b/src/ircmanager.cpp @@ -263,7 +263,49 @@ void IrcManager::handleRoomStateMessage(Communi::IrcMessage *message) void IrcManager::handleClearChatMessage(Communi::IrcMessage *message) { - // TODO: Implement + assert(message->parameters().length() >= 1); + + auto rawChannelName = message->parameter(0); + + assert(rawChannelName.length() >= 2); + + auto trimmedChannelName = rawChannelName.mid(1); + + auto c = this->channelManager.getTwitchChannel(trimmedChannelName); + + if (!c) { + debug::Log("[IrcManager:handleClearChatMessage] Channel {} not found in channel manager", + trimmedChannelName); + return; + } + + if (message->parameters().length() == 1) { + std::shared_ptr msg( + Message::createSystemMessage("Chat has been cleared by a moderator.")); + + c->addMessage(msg); + + return; + } + + assert(message->parameters().length() >= 2); + + QString username = message->parameter(1); + QString durationInSeconds, reason; + QVariant v = message->tag("ban-duration"); + if (v.isValid()) { + durationInSeconds = v.toString(); + } + + v = message->tag("ban-reason"); + if (v.isValid()) { + reason = v.toString(); + } + + std::shared_ptr msg( + Message::createTimeoutMessage(username, durationInSeconds, reason)); + + c->addMessage(msg); } void IrcManager::handleUserStateMessage(Communi::IrcMessage *message) diff --git a/src/messages/message.cpp b/src/messages/message.cpp index e0469b9f3..dc4567280 100644 --- a/src/messages/message.cpp +++ b/src/messages/message.cpp @@ -107,6 +107,36 @@ Message *Message::createSystemMessage(const QString &text) return message; } +Message *Message::createTimeoutMessage(const QString &username, const QString &durationInSeconds, + const QString &reason) +{ + Message *message = new Message; + + AddCurrentTimestamp(message); + + QString text; + + text.append(username); + text.append(" has been timed out"); + + // TODO: Implement who timed the user out + + text.append(" for "); + text.append(durationInSeconds); + bool ok = true; + int timeoutDuration = durationInSeconds.toInt(&ok); + text.append(" second"); + if (ok && timeoutDuration > 1) { + text.append("s"); + } + + if (reason.length() > 0) { + text.append(": \""); + text.append(reason); + text.append("\""); + } + text.append("."); + Word word(text, Word::Type::Default, MessageColor(MessageColor::Type::System), text, text); message->getWords().push_back(word); diff --git a/src/messages/message.hpp b/src/messages/message.hpp index fded9131d..cb49366f3 100644 --- a/src/messages/message.hpp +++ b/src/messages/message.hpp @@ -35,6 +35,9 @@ public: static Message *createSystemMessage(const QString &text); + static Message *createTimeoutMessage(const QString &username, const QString &durationInSeconds, + const QString &reason); + private: static LazyLoadedImage *badgeStaff; static LazyLoadedImage *badgeAdmin;