Implemented loading recent messages. (#162)

This commit is contained in:
Cranken 2017-12-27 01:22:12 +01:00 committed by fourtf
parent 2fc546cada
commit 223f4af55a
5 changed files with 46 additions and 14 deletions

View file

@ -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<Communi::IrcPrivateMessage *>(msg);
privateMessageReceived(privMsg);
}
});
}
} // namespace chatterino

View file

@ -90,6 +90,9 @@ private:
void onConnected();
void onDisconnected();
void fetchRecentMessages(QString &roomID, QString &channel);
QVector<QString> dontParse;
private:
QByteArray messageSuffix;
};

View file

@ -3,6 +3,8 @@
#include "emotemanager.hpp"
#include "resources.hpp"
#include <QDateTime>
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()));

View file

@ -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);

View file

@ -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();
}