Show the title when offline (#1347)

* Channel text for offline chats

* Title refresh timer

* Fixed typo

* Refresh title when room id is loaded

* removed timer

* Typo

* Loaded on demand when hovered over

* removed the extra api request

* Moved refreshTitle function call

* Added thottling for 10s

* Fixed formating style

* reformat

* make status fetcher safer

* Requested changes from fourtf
This commit is contained in:
CommName 2019-10-12 15:09:12 +02:00 committed by fourtf
parent 8153aa7d40
commit dc1e4d2544
3 changed files with 67 additions and 0 deletions

View file

@ -89,6 +89,7 @@ TwitchChannel::TwitchChannel(const QString &name,
, bttvEmotes_(std::make_shared<EmoteMap>()) , bttvEmotes_(std::make_shared<EmoteMap>())
, ffzEmotes_(std::make_shared<EmoteMap>()) , ffzEmotes_(std::make_shared<EmoteMap>())
, mod_(false) , mod_(false)
, titleRefreshedTime_(QTime::currentTime().addSecs(-titleRefreshPeriod_))
{ {
log("[TwitchChannel:{}] Opened", name); log("[TwitchChannel:{}] Opened", name);
@ -110,6 +111,7 @@ TwitchChannel::TwitchChannel(const QString &name,
// room id loaded -> refresh live status // room id loaded -> refresh live status
this->roomIdChanged.connect([this]() { this->roomIdChanged.connect([this]() {
this->refreshPubsub(); this->refreshPubsub();
this->refreshTitle();
this->refreshLiveStatus(); this->refreshLiveStatus();
this->refreshBadges(); this->refreshBadges();
this->refreshCheerEmotes(); this->refreshCheerEmotes();
@ -437,6 +439,51 @@ void TwitchChannel::setLive(bool newLiveStatus)
} }
} }
void TwitchChannel::refreshTitle()
{
auto roomID = this->roomId();
if (roomID.isEmpty())
{
return;
}
if (this->titleRefreshedTime_.elapsed() < this->titleRefreshPeriod_ * 1000)
{
return;
}
this->titleRefreshedTime_ = QTime::currentTime();
QString url("https://api.twitch.tv/kraken/channels/" + roomID);
NetworkRequest::twitchRequest(url)
.onSuccess(
[this, weak = weakOf<Channel>(this)](auto result) -> Outcome {
ChannelPtr shared = weak.lock();
if (!shared)
return Failure;
const auto document = result.parseRapidJson();
auto statusIt = document.FindMember("status");
if (statusIt == document.MemberEnd())
{
return Failure;
}
{
auto status = this->streamStatus_.access();
if (!rj::getSafe(statusIt->value, status->title))
{
return Failure;
}
}
this->liveStatusChanged.invoke();
return Success;
})
.execute();
}
void TwitchChannel::refreshLiveStatus() void TwitchChannel::refreshLiveStatus()
{ {
auto roomID = this->roomId(); auto roomID = this->roomId();

View file

@ -69,6 +69,7 @@ public:
virtual bool hasHighRateLimit() const override; virtual bool hasHighRateLimit() const override;
virtual bool canReconnect() const override; virtual bool canReconnect() const override;
virtual void reconnect() override; virtual void reconnect() override;
void refreshTitle();
// Data // Data
const QString &subscriptionUrl(); const QString &subscriptionUrl();
@ -166,6 +167,9 @@ private:
QObject lifetimeGuard_; QObject lifetimeGuard_;
QTimer liveStatusTimer_; QTimer liveStatusTimer_;
QTimer chattersListTimer_; QTimer chattersListTimer_;
QTime titleRefreshedTime_;
const int titleRefreshPeriod_ = 10;
friend class TwitchIrcServer; friend class TwitchIrcServer;
friend class TwitchMessageBuilder; friend class TwitchMessageBuilder;

View file

@ -99,6 +99,12 @@ namespace {
.arg(s.uptime) .arg(s.uptime)
.arg(QString::number(s.viewerCount)); .arg(QString::number(s.viewerCount));
} }
auto formatOfflineTooltip(const TwitchChannel::StreamStatus &s)
{
return QString("<style>.center { text-align: center; }</style> \
<p class=\"center\">Offline<br>%1</p>")
.arg(s.title.toHtmlEscaped());
}
auto formatTitle(const TwitchChannel::StreamStatus &s, Settings &settings) auto formatTitle(const TwitchChannel::StreamStatus &s, Settings &settings)
{ {
auto title = QString(); auto title = QString();
@ -550,6 +556,10 @@ void SplitHeader::updateChannelText()
this->tooltipText_ = formatTooltip(*streamStatus); this->tooltipText_ = formatTooltip(*streamStatus);
title += formatTitle(*streamStatus, *getSettings()); title += formatTitle(*streamStatus, *getSettings());
} }
else
{
this->tooltipText_ = formatOfflineTooltip(*streamStatus);
}
} }
this->titleLabel_->setText(title.isEmpty() ? "<empty>" : title); this->titleLabel_->setText(title.isEmpty() ? "<empty>" : title);
@ -670,6 +680,12 @@ void SplitHeader::enterEvent(QEvent *event)
{ {
if (!this->tooltipText_.isEmpty()) if (!this->tooltipText_.isEmpty())
{ {
auto channel = this->split_->getChannel().get();
if (channel->getType() == Channel::Type::Twitch)
{
dynamic_cast<TwitchChannel *>(channel)->refreshTitle();
}
TooltipPreviewImage::instance().setImage(nullptr); TooltipPreviewImage::instance().setImage(nullptr);
auto tooltip = TooltipWidget::instance(); auto tooltip = TooltipWidget::instance();