From dc1e4d2544c0f34198bcf09510c7a7a777b1711c Mon Sep 17 00:00:00 2001 From: CommName <17132081+CommName@users.noreply.github.com> Date: Sat, 12 Oct 2019 15:09:12 +0200 Subject: [PATCH] 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 --- src/providers/twitch/TwitchChannel.cpp | 47 ++++++++++++++++++++++++++ src/providers/twitch/TwitchChannel.hpp | 4 +++ src/widgets/splits/SplitHeader.cpp | 16 +++++++++ 3 files changed, 67 insertions(+) diff --git a/src/providers/twitch/TwitchChannel.cpp b/src/providers/twitch/TwitchChannel.cpp index 014552030..23f6a6055 100644 --- a/src/providers/twitch/TwitchChannel.cpp +++ b/src/providers/twitch/TwitchChannel.cpp @@ -89,6 +89,7 @@ TwitchChannel::TwitchChannel(const QString &name, , bttvEmotes_(std::make_shared()) , ffzEmotes_(std::make_shared()) , mod_(false) + , titleRefreshedTime_(QTime::currentTime().addSecs(-titleRefreshPeriod_)) { log("[TwitchChannel:{}] Opened", name); @@ -110,6 +111,7 @@ TwitchChannel::TwitchChannel(const QString &name, // room id loaded -> refresh live status this->roomIdChanged.connect([this]() { this->refreshPubsub(); + this->refreshTitle(); this->refreshLiveStatus(); this->refreshBadges(); 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(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() { auto roomID = this->roomId(); diff --git a/src/providers/twitch/TwitchChannel.hpp b/src/providers/twitch/TwitchChannel.hpp index 49feb8cf3..12f518837 100644 --- a/src/providers/twitch/TwitchChannel.hpp +++ b/src/providers/twitch/TwitchChannel.hpp @@ -69,6 +69,7 @@ public: virtual bool hasHighRateLimit() const override; virtual bool canReconnect() const override; virtual void reconnect() override; + void refreshTitle(); // Data const QString &subscriptionUrl(); @@ -166,6 +167,9 @@ private: QObject lifetimeGuard_; QTimer liveStatusTimer_; QTimer chattersListTimer_; + QTime titleRefreshedTime_; + + const int titleRefreshPeriod_ = 10; friend class TwitchIrcServer; friend class TwitchMessageBuilder; diff --git a/src/widgets/splits/SplitHeader.cpp b/src/widgets/splits/SplitHeader.cpp index 5ba700d87..d86df9b5c 100644 --- a/src/widgets/splits/SplitHeader.cpp +++ b/src/widgets/splits/SplitHeader.cpp @@ -99,6 +99,12 @@ namespace { .arg(s.uptime) .arg(QString::number(s.viewerCount)); } + auto formatOfflineTooltip(const TwitchChannel::StreamStatus &s) + { + return QString(" \ +

Offline
%1

") + .arg(s.title.toHtmlEscaped()); + } auto formatTitle(const TwitchChannel::StreamStatus &s, Settings &settings) { auto title = QString(); @@ -550,6 +556,10 @@ void SplitHeader::updateChannelText() this->tooltipText_ = formatTooltip(*streamStatus); title += formatTitle(*streamStatus, *getSettings()); } + else + { + this->tooltipText_ = formatOfflineTooltip(*streamStatus); + } } this->titleLabel_->setText(title.isEmpty() ? "" : title); @@ -670,6 +680,12 @@ void SplitHeader::enterEvent(QEvent *event) { if (!this->tooltipText_.isEmpty()) { + auto channel = this->split_->getChannel().get(); + if (channel->getType() == Channel::Type::Twitch) + { + dynamic_cast(channel)->refreshTitle(); + } + TooltipPreviewImage::instance().setImage(nullptr); auto tooltip = TooltipWidget::instance();