From 47198a54894bbc3a16a1762f728cca842d5849e0 Mon Sep 17 00:00:00 2001 From: fourtf Date: Sun, 24 Jun 2018 12:59:47 +0200 Subject: [PATCH] Fixed #501 --- src/providers/twitch/twitchserver.cpp | 103 ++++++++++++++------------ src/providers/twitch/twitchserver.hpp | 11 ++- 2 files changed, 63 insertions(+), 51 deletions(-) diff --git a/src/providers/twitch/twitchserver.cpp b/src/providers/twitch/twitchserver.cpp index af393d227..58a969cff 100644 --- a/src/providers/twitch/twitchserver.cpp +++ b/src/providers/twitch/twitchserver.cpp @@ -14,6 +14,7 @@ // using namespace Communi; using namespace chatterino::singletons; +using namespace std::chrono_literals; namespace chatterino { namespace providers { @@ -71,53 +72,8 @@ std::shared_ptr TwitchServer::createChannel(const QString &channelName) { TwitchChannel *channel = new TwitchChannel(channelName, this->getReadConnection()); - channel->sendMessageSignal.connect([this, channel](auto chan, auto msg, bool &sent) { - { - std::lock_guard guard(this->lastMessageMutex); - - std::queue &lastMessage = - channel->hasModRights() ? this->lastMessageMod : this->lastMessagePleb; - size_t maxMessageCount = channel->hasModRights() ? 99 : 19; - - QTime now = QTime::currentTime(); - - // check if you are sending messages too fast - if (lastMessage.size() > 0 && - lastMessage.back().addMSecs(channel->hasModRights() ? 100 : 1100) > now) { - if (lastErrorTimeSpeed.addSecs(30) < now) { - auto errorMessage = - messages::Message::createSystemMessage("sending messages too fast"); - - channel->addMessage(errorMessage); - - lastErrorTimeSpeed = now; - } - return; - } - - // remove messages older than 30 seconds - while (lastMessage.size() > 0 && lastMessage.front().addSecs(32) < now) { - lastMessage.pop(); - } - - // check if you are sending too many messages - if (lastMessage.size() >= maxMessageCount) { - if (lastErrorTimeAmount.addSecs(30) < now) { - auto errorMessage = - messages::Message::createSystemMessage("sending too many messages"); - - channel->addMessage(errorMessage); - - lastErrorTimeAmount = now; - } - return; - } - - lastMessage.push(now); - } - - this->sendMessage(chan, msg); - sent = true; + channel->sendMessageSignal.connect([this, channel](auto &chan, auto &msg, bool &sent) { + this->onMessageSendRequested(channel, msg, sent); }); return std::shared_ptr(channel); @@ -222,6 +178,59 @@ QString TwitchServer::cleanChannelName(const QString &dirtyChannelName) return dirtyChannelName.toLower(); } +void TwitchServer::onMessageSendRequested(TwitchChannel *channel, const QString &message, + bool &sent) +{ + sent = false; + + { + std::lock_guard guard(this->lastMessageMutex); + + // std::queue + auto &lastMessage = channel->hasModRights() ? this->lastMessageMod : this->lastMessagePleb; + size_t maxMessageCount = channel->hasModRights() ? 99 : 19; + auto minMessageOffset = (channel->hasModRights() ? 100ms : 1100ms); + + auto now = std::chrono::steady_clock::now(); + + // check if you are sending messages too fast + if (!lastMessage.empty() && lastMessage.back() + minMessageOffset > now) { + if (lastErrorTimeSpeed + 30s < now) { + auto errorMessage = + messages::Message::createSystemMessage("sending messages too fast"); + + channel->addMessage(errorMessage); + + lastErrorTimeSpeed = now; + } + return; + } + + // remove messages older than 30 seconds + while (!lastMessage.empty() && lastMessage.front() + 32s < now) { + lastMessage.pop(); + } + + // check if you are sending too many messages + if (lastMessage.size() >= maxMessageCount) { + if (lastErrorTimeAmount + 30s < now) { + auto errorMessage = + messages::Message::createSystemMessage("sending too many messages"); + + channel->addMessage(errorMessage); + + lastErrorTimeAmount = now; + } + return; + } + + lastMessage.push(now); + } + + this->sendMessage(channel->name, message); + sent = true; +} + } // namespace twitch } // namespace providers } // namespace chatterino diff --git a/src/providers/twitch/twitchserver.hpp b/src/providers/twitch/twitchserver.hpp index ee4a44bdc..3d10c8527 100644 --- a/src/providers/twitch/twitchserver.hpp +++ b/src/providers/twitch/twitchserver.hpp @@ -5,6 +5,7 @@ #include "providers/twitch/twitchchannel.hpp" #include "util/mutexvalue.hpp" +#include #include #include @@ -45,10 +46,12 @@ protected: private: std::mutex lastMessageMutex; - std::queue lastMessagePleb; - std::queue lastMessageMod; - QTime lastErrorTimeSpeed; - QTime lastErrorTimeAmount; + std::queue lastMessagePleb; + std::queue lastMessageMod; + std::chrono::steady_clock::time_point lastErrorTimeSpeed; + std::chrono::steady_clock::time_point lastErrorTimeAmount; + + void onMessageSendRequested(TwitchChannel *channel, const QString &message, bool &sent); }; } // namespace twitch