diff --git a/CHANGELOG.md b/CHANGELOG.md index 9dc821bc6..5b79a62c7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,6 +27,7 @@ - Bugfix: Fixed viewer list not closing after pressing escape key. (#3734) - Bugfix: Fixed links with no thumbnail having previous link's thumbnail. (#3720) - Dev: Use Game Name returned by Get Streams instead of querying it from the Get Games API. (#3662) +- Dev: Batch checking live status for all channels after startup. (#3757) ## 2.3.5 diff --git a/src/providers/twitch/TwitchChannel.cpp b/src/providers/twitch/TwitchChannel.cpp index ce8bc10d1..0f6feba57 100644 --- a/src/providers/twitch/TwitchChannel.cpp +++ b/src/providers/twitch/TwitchChannel.cpp @@ -188,11 +188,6 @@ TwitchChannel::TwitchChannel(const QString &name) }); this->chattersListTimer_.start(5 * 60 * 1000); - QObject::connect(&this->liveStatusTimer_, &QTimer::timeout, [=] { - this->refreshLiveStatus(); - }); - this->liveStatusTimer_.start(60 * 1000); - // debugging #if 0 for (int i = 0; i < 1000; i++) { diff --git a/src/providers/twitch/TwitchChannel.hpp b/src/providers/twitch/TwitchChannel.hpp index c6264d3c8..12f74374a 100644 --- a/src/providers/twitch/TwitchChannel.hpp +++ b/src/providers/twitch/TwitchChannel.hpp @@ -193,7 +193,6 @@ private: // -- QString lastSentMessage_; QObject lifetimeGuard_; - QTimer liveStatusTimer_; QTimer chattersListTimer_; QElapsedTimer titleRefreshedTimer_; QElapsedTimer clipCreationTimer_; diff --git a/src/providers/twitch/TwitchIrcServer.cpp b/src/providers/twitch/TwitchIrcServer.cpp index 5c4da60a0..2869a1850 100644 --- a/src/providers/twitch/TwitchIrcServer.cpp +++ b/src/providers/twitch/TwitchIrcServer.cpp @@ -53,6 +53,12 @@ void TwitchIrcServer::initialize(Settings &settings, Paths &paths) this->bttv.loadEmotes(); this->ffz.loadEmotes(); + + /* Refresh all twitch channel's live status in bulk every 30 seconds after starting chatterino */ + QObject::connect(&this->bulkLiveStatusTimer_, &QTimer::timeout, [=] { + this->bulkRefreshLiveStatus(); + }); + this->bulkLiveStatusTimer_.start(30 * 1000); } void TwitchIrcServer::initializeConnection(IrcConnection *connection, @@ -296,6 +302,64 @@ std::shared_ptr TwitchIrcServer::getChannelOrEmptyByID( return Channel::getEmpty(); } +namespace { + // TODO: combine this with getEmoteSetBatches in TwitchAccount.cpp, maybe some templated thing + std::vector getChannelsInBatches(QStringList channels) + { + constexpr int batchSize = 100; + + int batchCount = (channels.size() / batchSize) + 1; + + std::vector batches; + batches.reserve(batchCount); + + for (int i = 0; i < batchCount; i++) + { + QStringList batch; + + // I hate you, msvc + int last = (std::min)(batchSize, channels.size() - batchSize * i); + for (int j = 0; j < last; j++) + { + batch.push_back(channels.at(j + (batchSize * i))); + } + batches.emplace_back(batch); + } + + return batches; + } +} // namespace + +void TwitchIrcServer::bulkRefreshLiveStatus() +{ + QStringList userIDs; + this->forEachChannel([&userIDs](ChannelPtr chan) { + auto twitchChan = dynamic_cast(chan.get()); + if (!twitchChan->roomId().isEmpty()) + userIDs.push_back(twitchChan->roomId()); + }); + + for (const auto &batch : getChannelsInBatches(userIDs)) + { + getHelix()->fetchStreams( + batch, QStringList(), + [this](std::vector streams) { + for (const auto &stream : streams) + { + auto chan = this->getChannelOrEmptyByID(stream.userId); + if (chan->getType() != Channel::Type::Twitch) + continue; + + auto twitchChan = dynamic_cast(chan.get()); + twitchChan->parseLiveStatus(true, stream); + } + }, + []() { + // failure + }); + } +} + QString TwitchIrcServer::cleanChannelName(const QString &dirtyChannelName) { if (dirtyChannelName.startsWith('#')) diff --git a/src/providers/twitch/TwitchIrcServer.hpp b/src/providers/twitch/TwitchIrcServer.hpp index 8f519d4aa..fd693a206 100644 --- a/src/providers/twitch/TwitchIrcServer.hpp +++ b/src/providers/twitch/TwitchIrcServer.hpp @@ -31,6 +31,8 @@ public: std::shared_ptr getChannelOrEmptyByID(const QString &channelID); + void bulkRefreshLiveStatus(); + Atomic lastUserThatWhisperedMe; const ChannelPtr whispersChannel; @@ -74,6 +76,7 @@ private: BttvEmotes bttv; FfzEmotes ffz; + QTimer bulkLiveStatusTimer_; pajlada::Signals::SignalHolder signalHolder_; };