mirror-chatterino2/src/twitch/twitchchannel.cpp

175 lines
5.2 KiB
C++
Raw Normal View History

#include "twitchchannel.hpp"
#include "debug/log.hpp"
2017-12-31 00:50:07 +01:00
#include "singletons/emotemanager.hpp"
#include "util/urlfetch.hpp"
#include <QThread>
#include <QTimer>
namespace chatterino {
namespace twitch {
2017-12-31 00:50:07 +01:00
TwitchChannel::TwitchChannel(const QString &channelName)
: Channel(channelName)
2017-12-31 22:58:35 +01:00
, bttvChannelEmotes(new util::EmoteMap)
, ffzChannelEmotes(new util::EmoteMap)
2017-12-28 17:50:28 +01:00
, subscriptionURL("https://www.twitch.tv/subs/" + name)
, channelURL("https://twitch.tv/" + name)
, popoutPlayerURL("https://player.twitch.tv/?channel=" + name)
, isLive(false)
{
debug::Log("[TwitchChannel:{}] Opened", this->name);
this->dontAddMessages = true;
2017-12-31 00:50:07 +01:00
this->reloadChannelEmotes();
this->liveStatusTimer = new QTimer;
QObject::connect(this->liveStatusTimer, &QTimer::timeout, [this]() {
this->refreshLiveStatus(); //
});
this->liveStatusTimer->start(60000);
this->roomIDchanged.connect([this]() {
this->refreshLiveStatus(); //
});
this->fetchMessages.connect([this] {
this->fetchRecentMessages(); //
});
}
TwitchChannel::~TwitchChannel()
{
this->liveStatusTimer->stop();
this->liveStatusTimer->deleteLater();
}
bool TwitchChannel::isEmpty() const
{
return this->name.isEmpty();
}
bool TwitchChannel::canSendMessage() const
{
2017-12-31 00:50:07 +01:00
return !this->isEmpty();
}
void TwitchChannel::setRoomID(const QString &_roomID)
{
this->roomID = _roomID;
this->roomIDchanged();
this->fetchMessages.invoke();
}
void TwitchChannel::reloadChannelEmotes()
{
2017-12-31 22:58:35 +01:00
auto &emoteManager = singletons::EmoteManager::getInstance();
2017-12-17 02:18:13 +01:00
debug::Log("[TwitchChannel:{}] Reloading channel emotes", this->name);
2017-12-17 02:18:13 +01:00
emoteManager.reloadBTTVChannelEmotes(this->name, this->bttvChannelEmotes);
emoteManager.reloadFFZChannelEmotes(this->name, this->ffzChannelEmotes);
}
void TwitchChannel::sendMessage(const QString &message)
{
2017-12-31 22:58:35 +01:00
auto &emoteManager = singletons::EmoteManager::getInstance();
2017-12-17 02:18:13 +01:00
debug::Log("[TwitchChannel:{}] Send message: {}", this->name, message);
// Do last message processing
2017-12-17 02:18:13 +01:00
QString parsedMessage = emoteManager.replaceShortCodes(message);
2017-12-31 22:58:35 +01:00
singletons::IrcManager::getInstance().sendMessage(this->name, parsedMessage);
}
void TwitchChannel::setLive(bool newLiveStatus)
{
if (this->isLive == newLiveStatus) {
return;
}
this->isLive = newLiveStatus;
this->onlineStatusChanged();
}
void TwitchChannel::refreshLiveStatus()
{
if (this->roomID.isEmpty()) {
this->setLive(false);
return;
}
debug::Log("[TwitchChannel:{}] Refreshing live status", this->name);
QString url("https://api.twitch.tv/kraken/streams/" + this->roomID);
util::twitch::get2(url, QThread::currentThread(), [this](rapidjson::Document &d) {
if (!d.IsObject()) {
debug::Log("[TwitchChannel:refreshLiveStatus] root is not an object");
return;
}
if (!d.HasMember("stream")) {
debug::Log("[TwitchChannel:refreshLiveStatus] Missing stream in root");
return;
}
const auto &stream = d["stream"];
if (!stream.IsObject()) {
// Stream is offline (stream is most likely null)
this->setLive(false);
return;
}
if (!stream.HasMember("viewers") || !stream.HasMember("game") ||
!stream.HasMember("channel") || !stream.HasMember("created_at")) {
debug::Log("[TwitchChannel:refreshLiveStatus] Missing members in stream");
this->setLive(false);
return;
}
const rapidjson::Value &streamChannel = stream["channel"];
if (!streamChannel.IsObject() || !streamChannel.HasMember("status")) {
debug::Log("[TwitchChannel:refreshLiveStatus] Missing member \"status\" in channel");
return;
}
// Stream is live
this->streamViewerCount = QString::number(stream["viewers"].GetInt());
this->streamGame = stream["game"].GetString();
this->streamStatus = streamChannel["status"].GetString();
QDateTime since = QDateTime::fromString(stream["created_at"].GetString(), Qt::ISODate);
auto diff = since.secsTo(QDateTime::currentDateTime());
this->streamUptime =
QString::number(diff / 3600) + "h " + QString::number(diff % 3600 / 60) + "m";
this->setLive(true);
});
}
void TwitchChannel::fetchRecentMessages()
{
static QString genericURL =
"https://tmi.twitch.tv/api/rooms/%1/recent_messages?client_id=" + getDefaultClientID();
2017-12-31 22:58:35 +01:00
static auto readConnection = singletons::IrcManager::getInstance().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);
2017-12-31 22:58:35 +01:00
singletons::IrcManager::getInstance().privateMessageReceived(privMsg);
}
});
}
} // namespace twitch
} // namespace chatterino