Fixed messages getting loaded multiple times. (#170)

* Implemented loading recent messages.

* Fixed messages getting loaded multiple times.
This commit is contained in:
Cranken 2017-12-28 00:03:52 +01:00 committed by fourtf
parent 2cdc404eb4
commit cb75c2a725
6 changed files with 41 additions and 25 deletions

View file

@ -36,6 +36,9 @@ messages::LimitedQueueSnapshot<messages::SharedMessage> Channel::getMessageSnaps
void Channel::addMessage(std::shared_ptr<Message> message)
{
if (dontAddMessages) {
return;
}
std::shared_ptr<Message> deleted;
const QString &username = message->loginName;

View file

@ -51,6 +51,8 @@ public:
virtual bool canSendMessage() const;
virtual void sendMessage(const QString &message);
bool dontAddMessages;
private:
messages::LimitedQueue<messages::SharedMessage> messages;

View file

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

View file

@ -46,11 +46,13 @@ public:
void setUser(std::shared_ptr<twitch::TwitchUser> newAccount);
pajlada::Signals::Signal<Communi::IrcPrivateMessage *> onPrivateMessage;
void privateMessageReceived(Communi::IrcPrivateMessage *message);
ChannelManager &channelManager;
Resources &resources;
WindowManager &windowManager;
Communi::IrcConnection* getReadConnection();
private:
// variables
std::shared_ptr<twitch::TwitchUser> 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<QString> dontParse;
private:
QByteArray messageSuffix;
};

View file

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

View file

@ -33,6 +33,8 @@ public:
boost::signals2::signal<void()> roomIDchanged;
boost::signals2::signal<void()> 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;