Ensure live status requests are always batched (#4713)

This commit is contained in:
pajlada
2023-07-02 15:52:15 +02:00
committed by GitHub
parent f915eab1a2
commit 76527073cf
20 changed files with 582 additions and 282 deletions
-59
View File
@@ -80,12 +80,6 @@ void TwitchIrcServer::initialize(Settings &settings, Paths &paths)
this->reloadBTTVGlobalEmotes();
this->reloadFFZGlobalEmotes();
this->reloadSevenTVGlobalEmotes();
/* Refresh all twitch channel's live status in bulk every 30 seconds after starting chatterino */
QObject::connect(&this->bulkLiveStatusTimer_, &QTimer::timeout, [this] {
this->bulkRefreshLiveStatus();
});
this->bulkLiveStatusTimer_.start(30 * 1000);
}
void TwitchIrcServer::initializeConnection(IrcConnection *connection,
@@ -412,59 +406,6 @@ std::shared_ptr<Channel> TwitchIrcServer::getChannelOrEmptyByID(
return Channel::getEmpty();
}
void TwitchIrcServer::bulkRefreshLiveStatus()
{
auto twitchChans = std::make_shared<QHash<QString, TwitchChannel *>>();
this->forEachChannel([twitchChans](ChannelPtr chan) {
auto tc = dynamic_cast<TwitchChannel *>(chan.get());
if (tc && !tc->roomId().isEmpty())
{
twitchChans->insert(tc->roomId(), tc);
}
});
// iterate over batches of channel IDs
for (const auto &batch : splitListIntoBatches(twitchChans->keys()))
{
getHelix()->fetchStreams(
batch, {},
[twitchChans](std::vector<HelixStream> streams) {
for (const auto &stream : streams)
{
// remaining channels will be used later to set their stream status as offline
// so we use take(id) to remove it
auto tc = twitchChans->take(stream.userId);
if (tc == nullptr)
{
continue;
}
tc->parseLiveStatus(true, stream);
}
},
[]() {
// failure
},
[batch, twitchChans] {
// All the channels that were not present in fetchStreams response should be assumed to be offline
// It is necessary to update their stream status in case they've gone live -> offline
// Otherwise some of them will be marked as live forever
for (const auto &chID : batch)
{
auto tc = twitchChans->value(chID);
// early out in case channel does not exist anymore
if (tc == nullptr)
{
continue;
}
tc->parseLiveStatus(false, {});
}
});
}
}
QString TwitchIrcServer::cleanChannelName(const QString &dirtyChannelName)
{
if (dirtyChannelName.startsWith('#'))