diff --git a/src/ircmanager.cpp b/src/ircmanager.cpp index 8e7472ba0..ac25e81ac 100644 --- a/src/ircmanager.cpp +++ b/src/ircmanager.cpp @@ -204,6 +204,7 @@ void IrcManager::joinChannel(const QString &channelName) this->readConnection->sendRaw("JOIN #" + channelName); this->writeConnection->sendRaw("JOIN #" + channelName); } + this->dontParse.append(channelName); this->connectionMutex.unlock(); } @@ -223,9 +224,10 @@ void IrcManager::partChannel(const QString &channelName) void IrcManager::privateMessageReceived(Communi::IrcPrivateMessage *message) { this->onPrivateMessage.invoke(message); - auto c = this->channelManager.getTwitchChannel(message->target().mid(1)); + QString channel = message->target().mid(1); + auto c = this->channelManager.getTwitchChannel(channel); - if (!c) { + if (!c || this->dontParse.contains(channel)) { return; } @@ -286,6 +288,7 @@ void IrcManager::handleRoomStateMessage(Communi::IrcMessage *message) channelManager.getTwitchChannel(channel)->setRoomID(roomID); this->resources.loadChannelData(roomID); + this->fetchRecentMessages(roomID, channel); } } @@ -520,4 +523,23 @@ void IrcManager::onDisconnected() }); } +void IrcManager::fetchRecentMessages(QString &roomID, QString &channel) +{ + 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); + } + }); + +} + } // namespace chatterino diff --git a/src/ircmanager.hpp b/src/ircmanager.hpp index 01f952660..25fd3ae8a 100644 --- a/src/ircmanager.hpp +++ b/src/ircmanager.hpp @@ -90,6 +90,9 @@ private: void onConnected(); void onDisconnected(); + void fetchRecentMessages(QString &roomID, QString &channel); + QVector dontParse; + private: QByteArray messageSuffix; }; diff --git a/src/messages/messagebuilder.cpp b/src/messages/messagebuilder.cpp index 37b9aab05..2a0014964 100644 --- a/src/messages/messagebuilder.cpp +++ b/src/messages/messagebuilder.cpp @@ -3,6 +3,8 @@ #include "emotemanager.hpp" #include "resources.hpp" +#include + namespace chatterino { namespace messages { @@ -24,8 +26,7 @@ void MessageBuilder::appendWord(const Word &&word) void MessageBuilder::appendTimestamp() { - std::time_t t; - time(&t); + QDateTime t = QDateTime::currentDateTime(); this->appendTimestamp(t); } @@ -34,20 +35,16 @@ void MessageBuilder::setHighlight(bool value) this->message->setHighlight(value); } -void MessageBuilder::appendTimestamp(time_t time) +void MessageBuilder::appendTimestamp(QDateTime &time) { - char timeStampBuffer[69]; - // Add word for timestamp with no seconds - strftime(timeStampBuffer, 69, "%H:%M", localtime(&time)); - QString timestampNoSeconds(timeStampBuffer); + QString timestampNoSeconds(time.toString("hh:mm")); this->appendWord(Word(timestampNoSeconds, Word::TimestampNoSeconds, MessageColor(MessageColor::System), FontManager::Medium, QString(), QString())); // Add word for timestamp with seconds - strftime(timeStampBuffer, 69, "%H:%M:%S", localtime(&time)); - QString timestampWithSeconds(timeStampBuffer); + QString timestampWithSeconds(time.toString("hh:mm:ss")); this->appendWord(Word(timestampWithSeconds, Word::TimestampWithSeconds, MessageColor(MessageColor::System), FontManager::Medium, QString(), QString())); diff --git a/src/messages/messagebuilder.hpp b/src/messages/messagebuilder.hpp index ec61994f2..128bc07da 100644 --- a/src/messages/messagebuilder.hpp +++ b/src/messages/messagebuilder.hpp @@ -18,7 +18,7 @@ public: void appendWord(const Word &&word); void appendTimestamp(); - void appendTimestamp(std::time_t time); + void appendTimestamp(QDateTime &time); void setHighlight(bool value); QString matchLink(const QString &string); diff --git a/src/twitch/twitchmessagebuilder.cpp b/src/twitch/twitchmessagebuilder.cpp index 027774096..30b11e611 100644 --- a/src/twitch/twitchmessagebuilder.cpp +++ b/src/twitch/twitchmessagebuilder.cpp @@ -47,7 +47,17 @@ SharedMessage TwitchMessageBuilder::parse() // The timestamp is always appended to the builder // Whether or not will be rendered is decided/checked later - this->appendTimestamp(); + + // Appends the correct timestamp if the message is a past message + bool isPastMsg = this->tags.contains("historical"); + if(isPastMsg) { + // This may be architecture dependent(datatype) + qint64 ts = this->tags.value("tmi-sent-ts").toLongLong(); + QDateTime time = QDateTime::fromMSecsSinceEpoch(ts); + this->appendTimestamp(time); + } else { + this->appendTimestamp(); + } this->parseMessageID(); @@ -66,7 +76,7 @@ SharedMessage TwitchMessageBuilder::parse() this->appendUsername(); // highlights - if (settings.enableHighlights) { + if (settings.enableHighlights && !isPastMsg) { this->parseHighlights(); }