diff --git a/src/channel.cpp b/src/channel.cpp index 75afbd0f6..7b63a2a81 100644 --- a/src/channel.cpp +++ b/src/channel.cpp @@ -36,6 +36,9 @@ messages::LimitedQueueSnapshot Channel::getMessageSnaps void Channel::addMessage(std::shared_ptr message) { + if (dontAddMessages) { + return; + } std::shared_ptr deleted; const QString &username = message->loginName; diff --git a/src/channel.hpp b/src/channel.hpp index 193e95ff2..6fe0d4e5a 100644 --- a/src/channel.hpp +++ b/src/channel.hpp @@ -51,6 +51,8 @@ public: virtual bool canSendMessage() const; virtual void sendMessage(const QString &message); + bool dontAddMessages; + private: messages::LimitedQueue messages; diff --git a/src/ircmanager.cpp b/src/ircmanager.cpp index ac25e81ac..ab2e6d11a 100644 --- a/src/ircmanager.cpp +++ b/src/ircmanager.cpp @@ -204,7 +204,6 @@ void IrcManager::joinChannel(const QString &channelName) this->readConnection->sendRaw("JOIN #" + channelName); this->writeConnection->sendRaw("JOIN #" + channelName); } - this->dontParse.append(channelName); this->connectionMutex.unlock(); } @@ -224,10 +223,9 @@ void IrcManager::partChannel(const QString &channelName) void IrcManager::privateMessageReceived(Communi::IrcPrivateMessage *message) { this->onPrivateMessage.invoke(message); - QString channel = message->target().mid(1); - auto c = this->channelManager.getTwitchChannel(channel); + auto c = this->channelManager.getTwitchChannel(message->target().mid(1)); - if (!c || this->dontParse.contains(channel)) { + if (!c) { return; } @@ -288,7 +286,6 @@ void IrcManager::handleRoomStateMessage(Communi::IrcMessage *message) channelManager.getTwitchChannel(channel)->setRoomID(roomID); this->resources.loadChannelData(roomID); - this->fetchRecentMessages(roomID, channel); } } @@ -523,23 +520,9 @@ void IrcManager::onDisconnected() }); } -void IrcManager::fetchRecentMessages(QString &roomID, QString &channel) +Communi::IrcConnection *IrcManager::getReadConnection() { - static QString genericURL = - "https://tmi.twitch.tv/api/rooms/%1/recent_messages?client_id=" + getDefaultClientID(); - - util::twitch::get(genericURL.arg(roomID), this, [=](QJsonObject obj) { - this->dontParse.removeAll(channel); - auto msgArray = obj.value("messages").toArray(); - if (msgArray.size()) - for (int i = 0; i < msgArray.size(); i++) { - QByteArray content = msgArray[i].toString().toUtf8(); - auto msg = Communi::IrcMessage::fromData(content, this->readConnection.get()); - auto privMsg = static_cast(msg); - privateMessageReceived(privMsg); - } - }); - + return this->readConnection.get(); } } // namespace chatterino diff --git a/src/ircmanager.hpp b/src/ircmanager.hpp index 25fd3ae8a..e56e052d5 100644 --- a/src/ircmanager.hpp +++ b/src/ircmanager.hpp @@ -46,11 +46,13 @@ public: void setUser(std::shared_ptr newAccount); pajlada::Signals::Signal onPrivateMessage; + void privateMessageReceived(Communi::IrcPrivateMessage *message); ChannelManager &channelManager; Resources &resources; WindowManager &windowManager; + Communi::IrcConnection* getReadConnection(); private: // variables std::shared_ptr account = nullptr; @@ -73,7 +75,6 @@ private: void beginConnecting(); - void privateMessageReceived(Communi::IrcPrivateMessage *message); void messageReceived(Communi::IrcMessage *message); void writeConnectionMessageReceived(Communi::IrcMessage *message); @@ -90,9 +91,6 @@ private: void onConnected(); void onDisconnected(); - void fetchRecentMessages(QString &roomID, QString &channel); - QVector dontParse; - private: QByteArray messageSuffix; }; diff --git a/src/twitch/twitchchannel.cpp b/src/twitch/twitchchannel.cpp index 78dc72166..c65b95636 100644 --- a/src/twitch/twitchchannel.cpp +++ b/src/twitch/twitchchannel.cpp @@ -22,6 +22,8 @@ TwitchChannel::TwitchChannel(IrcManager &ircManager, const QString &channelName, { debug::Log("[TwitchChannel:{}] Opened", this->name); + this->dontAddMessages = true; + if (!this->isSpecial) { this->reloadChannelEmotes(); } @@ -35,6 +37,10 @@ TwitchChannel::TwitchChannel(IrcManager &ircManager, const QString &channelName, this->roomIDchanged.connect([this]() { this->refreshLiveStatus(); // }); + + this->fetchMessages.connect([this] { + this->fetchRecentMessages(); + }); } TwitchChannel::~TwitchChannel() @@ -57,6 +63,7 @@ void TwitchChannel::setRoomID(const QString &_roomID) { this->roomID = _roomID; this->roomIDchanged(); + this->fetchMessages.invoke(); } void TwitchChannel::reloadChannelEmotes() @@ -121,5 +128,24 @@ void TwitchChannel::refreshLiveStatus() }); } +void TwitchChannel::fetchRecentMessages() +{ + static QString genericURL = + "https://tmi.twitch.tv/api/rooms/%1/recent_messages?client_id=" + getDefaultClientID(); + static auto readConnection = this->ircManager.getReadConnection(); + + util::twitch::get(genericURL.arg(roomID), QThread::currentThread(), [=](QJsonObject obj) { + this->dontAddMessages = false; + auto msgArray = obj.value("messages").toArray(); + if (msgArray.size()) + for (int i = 0; i < msgArray.size(); i++) { + QByteArray content = msgArray[i].toString().toUtf8(); + auto msg = Communi::IrcMessage::fromData(content, readConnection); + auto privMsg = static_cast(msg); + this->ircManager.privateMessageReceived(privMsg); + } + }); +} + } // namespace twitch } // namespace chatterino diff --git a/src/twitch/twitchchannel.hpp b/src/twitch/twitchchannel.hpp index cba47fd4e..de450c2fb 100644 --- a/src/twitch/twitchchannel.hpp +++ b/src/twitch/twitchchannel.hpp @@ -33,6 +33,8 @@ public: boost::signals2::signal roomIDchanged; boost::signals2::signal onlineStatusChanged; + pajlada::Signals::NoArgBoltSignal fetchMessages; + QString roomID; bool isLive; QString streamViewerCount; @@ -44,6 +46,8 @@ private: void setLive(bool newLiveStatus); void refreshLiveStatus(); + void fetchRecentMessages(); + IrcManager &ircManager; bool isSpecial;