Moved online status checking logic from ChatWidgetHeader to TwitchChannel

- Channel now needs to be initialized with a name. Special cases like the emote window just sends an empty string.
 - ChatWidget now has a signal which is called whenever the widgets channel is changed
 - Changed roomID from an std::string to a QString
This commit is contained in:
Rasmus Karlsson
2017-11-04 14:57:29 +01:00
parent 064daaa77a
commit 7b2e3a94a6
14 changed files with 132 additions and 105 deletions
+74 -31
View File
@@ -1,31 +1,48 @@
#include "twitchchannel.hpp"
#include "debug/log.hpp"
#include "emotemanager.hpp"
#include "util/urlfetch.hpp"
#include <QDebug>
#include <QThread>
#include <QTimer>
namespace chatterino {
namespace twitch {
TwitchChannel::TwitchChannel(EmoteManager &emoteManager, IrcManager &ircManager,
const QString &channelName, bool isSpecial)
: emoteManager(emoteManager)
const QString &channelName, bool _isSpecial)
: Channel(channelName)
, emoteManager(emoteManager)
, ircManager(ircManager)
// , name(channelName)
, bttvChannelEmotes(new EmoteMap)
, ffzChannelEmotes(new EmoteMap)
, subLink("https://www.twitch.tv/" + name + "/subscribe?ref=in_chat_subscriber_link")
, channelLink("https://twitch.tv/" + name)
, popoutPlayerLink("https://player.twitch.tv/?channel=" + name)
, subscriptionURL("https://www.twitch.tv/" + name + "/subscribe?ref=in_chat_subscriber_link")
, channelURL("https://twitch.tv/" + name)
, popoutPlayerURL("https://player.twitch.tv/?channel=" + name)
, isLive(false)
, isSpecial(isSpecial)
, isSpecial(_isSpecial)
{
this->name = channelName;
debug::Log("[TwitchChannel:{}] Opened", this->name);
qDebug() << "Open twitch channel:" << this->name;
if (!isSpecial) {
if (!this->isSpecial) {
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(); //
});
}
TwitchChannel::~TwitchChannel()
{
this->liveStatusTimer->stop();
this->liveStatusTimer->deleteLater();
}
bool TwitchChannel::isEmpty() const
@@ -33,35 +50,20 @@ bool TwitchChannel::isEmpty() const
return this->name.isEmpty();
}
const QString &TwitchChannel::getSubLink() const
{
return this->subLink;
}
bool TwitchChannel::canSendMessage() const
{
return !this->isEmpty() && !this->isSpecial;
}
const QString &TwitchChannel::getChannelLink() const
void TwitchChannel::setRoomID(const QString &_roomID)
{
return this->channelLink;
}
const QString &TwitchChannel::getPopoutPlayerLink() const
{
return this->popoutPlayerLink;
}
void TwitchChannel::setRoomID(std::string id)
{
this->roomID = id;
this->roomID = _roomID;
this->roomIDchanged();
}
void TwitchChannel::reloadChannelEmotes()
{
printf("[TwitchChannel:%s] Reloading channel emotes\n", qPrintable(this->name));
debug::Log("[TwitchChannel:{}] Reloading channel emotes", this->name);
this->emoteManager.reloadBTTVChannelEmotes(this->name, this->bttvChannelEmotes);
this->emoteManager.reloadFFZChannelEmotes(this->name, this->ffzChannelEmotes);
@@ -69,12 +71,53 @@ void TwitchChannel::reloadChannelEmotes()
void TwitchChannel::sendMessage(const QString &message)
{
qDebug() << "TwitchChannel send message: " << message;
debug::Log("[TwitchChannel:{}] Send message: {}", this->name, message);
// Do last message processing
QString parsedMessage = this->emoteManager.replaceShortCodes(message);
this->ircManager.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::get(url, QThread::currentThread(), [this](QJsonObject obj) {
if (obj.value("stream").isNull()) {
this->setLive(false);
} else {
auto stream = obj.value("stream").toObject();
this->streamViewerCount = QString::number(stream.value("viewers").toDouble());
this->streamGame = stream.value("game").toString();
this->streamStatus = stream.value("channel").toObject().value("status").toString();
QDateTime since =
QDateTime::fromString(stream.value("created_at").toString(), Qt::ISODate);
auto diff = since.secsTo(QDateTime::currentDateTime());
this->streamUptime =
QString::number(diff / 3600) + "h " + QString::number(diff % 3600 / 60) + "m";
this->setLive(true);
}
});
}
} // namespace twitch
} // namespace chatterino
+16 -11
View File
@@ -9,9 +9,12 @@ namespace twitch {
class TwitchChannel : public Channel
{
QTimer *liveStatusTimer;
public:
explicit TwitchChannel(EmoteManager &emoteManager, IrcManager &ircManager,
const QString &channelName, bool isSpecial = false);
const QString &channelName, bool _isSpecial = false);
~TwitchChannel();
void reloadChannelEmotes();
@@ -19,14 +22,15 @@ public:
bool canSendMessage() const override;
void sendMessage(const QString &message) override;
const QString &getSubLink() const;
const QString &getChannelLink() const;
const QString &getPopoutPlayerLink() const;
const QString subscriptionURL;
const QString channelURL;
const QString popoutPlayerURL;
void setRoomID(std::string id);
void setRoomID(const QString &_roomID);
boost::signals2::signal<void()> roomIDchanged;
boost::signals2::signal<void()> onlineStatusChanged;
std::string roomID;
QString roomID;
bool isLive;
QString streamViewerCount;
QString streamStatus;
@@ -37,13 +41,14 @@ public:
const std::shared_ptr<EmoteMap> ffzChannelEmotes;
private:
void setLive(bool newLiveStatus);
void refreshLiveStatus();
EmoteManager &emoteManager;
IrcManager &ircManager;
QString subLink;
QString channelLink;
QString popoutPlayerLink;
bool isSpecial;
};
}
}
} // namespace twitch
} // namespace chatterino
+2 -2
View File
@@ -263,9 +263,9 @@ void TwitchMessageBuilder::parseRoomID()
auto iterator = this->tags.find("room-id");
if (iterator != std::end(this->tags)) {
this->roomID = iterator.value().toString().toStdString();
this->roomID = iterator.value().toString();
if (this->twitchChannel->roomID.empty()) {
if (this->twitchChannel->roomID.isEmpty()) {
this->twitchChannel->roomID = this->roomID;
}
}
+1 -1
View File
@@ -53,7 +53,7 @@ public:
// const std::pair<long int, messages::LazyLoadedImage *> &b);
private:
std::string roomID;
QString roomID;
QColor usernameColor;