Check live status for all closed channels with notifications at once (#3442)

Use a single api call for fetching live status of fake channels; batch by 100s

Co-authored-by: Felanbird <41973452+Felanbird@users.noreply.github.com>
Co-authored-by: zneix <zneix@zneix.eu>
This commit is contained in:
Mm2PL 2022-01-01 17:06:54 +00:00 committed by GitHub
parent c26fb01df5
commit ea462f94e4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 97 additions and 55 deletions

View file

@ -76,6 +76,7 @@
- Bugfix: Fixed using special chars in Windows username breaking the storage of custom commands (#3397) - Bugfix: Fixed using special chars in Windows username breaking the storage of custom commands (#3397)
- Bugfix: Fixed character counter changing fonts after going over the limit. (#3422) - Bugfix: Fixed character counter changing fonts after going over the limit. (#3422)
- Bugfix: Fixed crash that could occur if the user opens/closes ChannelViews (e.g. EmotePopup, or Splits) then modifies the showLastMessageIndicator setting. (#3444) - Bugfix: Fixed crash that could occur if the user opens/closes ChannelViews (e.g. EmotePopup, or Splits) then modifies the showLastMessageIndicator setting. (#3444)
- Dev: Batch checking live status for channels with live notifications that aren't connected. (#3442)
- Dev: Add GitHub action to test builds without precompiled headers enabled. (#3327) - Dev: Add GitHub action to test builds without precompiled headers enabled. (#3327)
- Dev: Renamed CMake's build option `USE_SYSTEM_QT5KEYCHAIN` to `USE_SYSTEM_QTKEYCHAIN`. (#3103) - Dev: Renamed CMake's build option `USE_SYSTEM_QT5KEYCHAIN` to `USE_SYSTEM_QTKEYCHAIN`. (#3103)
- Dev: Add benchmarks that can be compiled with the `BUILD_BENCHMARKS` CMake flag. Off by default. (#3038) - Dev: Add benchmarks that can be compiled with the `BUILD_BENCHMARKS` CMake flag. Off by default. (#3038)

View file

@ -20,6 +20,7 @@
#include <QDir> #include <QDir>
#include <QMediaPlayer> #include <QMediaPlayer>
#include <QUrl> #include <QUrl>
#include <unordered_set>
namespace chatterino { namespace chatterino {
@ -127,8 +128,38 @@ NotificationModel *NotificationController::createModel(QObject *parent,
return model; return model;
} }
namespace {
// TODO: combine this with getEmoteSetBatches in TwitchAccount.cpp, maybe some templated thing
std::vector<QStringList> getChannelsInBatches(QStringList channels)
{
constexpr int batchSize = 100;
int batchCount = (channels.size() / batchSize) + 1;
std::vector<QStringList> 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 NotificationController::fetchFakeChannels() void NotificationController::fetchFakeChannels()
{ {
qCDebug(chatterinoNotification) << "fetching fake channels";
QStringList channels;
for (std::vector<int>::size_type i = 0; for (std::vector<int>::size_type i = 0;
i != channelMap[Platform::Twitch].raw().size(); i++) i != channelMap[Platform::Twitch].raw().size(); i++)
{ {
@ -136,20 +167,37 @@ void NotificationController::fetchFakeChannels()
channelMap[Platform::Twitch].raw()[i]); channelMap[Platform::Twitch].raw()[i]);
if (chan->isEmpty()) if (chan->isEmpty())
{ {
getFakeTwitchChannelLiveStatus( channels.push_back(channelMap[Platform::Twitch].raw()[i]);
channelMap[Platform::Twitch].raw()[i]);
} }
} }
for (const auto &batch : getChannelsInBatches(channels))
{
getHelix()->fetchStreams(
QStringList(), batch,
[batch, this](std::vector<HelixStream> streams) {
std::unordered_set<QString> liveStreams;
for (const auto &stream : streams)
{
liveStreams.insert(stream.userName);
} }
void NotificationController::getFakeTwitchChannelLiveStatus( for (const auto &name : batch)
const QString &channelName)
{ {
getHelix()->getStreamByName( auto it = liveStreams.find(name);
channelName, this->checkStream(it != liveStreams.end(), name);
[channelName, this](bool live, const auto &stream) { }
qCDebug(chatterinoNotification) << "[TwitchChannel" << channelName },
<< "] Refreshing live status"; [batch]() {
// we done fucked up.
qCWarning(chatterinoNotification)
<< "Failed to fetch live status for " << batch;
});
}
}
void NotificationController::checkStream(bool live, QString channelName)
{
qCDebug(chatterinoNotification)
<< "[TwitchChannel" << channelName << "] Refreshing live status";
if (!live) if (!live)
{ {
@ -159,8 +207,8 @@ void NotificationController::getFakeTwitchChannelLiveStatus(
} }
// Stream is online // Stream is online
auto i = std::find(fakeTwitchChannels.begin(), auto i = std::find(fakeTwitchChannels.begin(), fakeTwitchChannels.end(),
fakeTwitchChannels.end(), channelName); channelName);
if (i != fakeTwitchChannels.end()) if (i != fakeTwitchChannels.end())
{ {
@ -192,13 +240,6 @@ void NotificationController::getFakeTwitchChannelLiveStatus(
// Indicate that we have pushed notifications for this stream // Indicate that we have pushed notifications for this stream
fakeTwitchChannels.push_back(channelName); fakeTwitchChannels.push_back(channelName);
},
[channelName, this] {
qCDebug(chatterinoNotification)
<< "[TwitchChannel" << channelName
<< "] Refreshing live status (Missing ID)";
this->removeFakeChannel(channelName);
});
} }
void NotificationController::removeFakeChannel(const QString channelName) void NotificationController::removeFakeChannel(const QString channelName)

View file

@ -41,7 +41,7 @@ private:
void fetchFakeChannels(); void fetchFakeChannels();
void removeFakeChannel(const QString channelName); void removeFakeChannel(const QString channelName);
void getFakeTwitchChannelLiveStatus(const QString &channelName); void checkStream(bool live, QString channelName);
// fakeTwitchChannels is a list of streams who are live that we have already sent out a notification for // fakeTwitchChannels is a list of streams who are live that we have already sent out a notification for
std::vector<QString> fakeTwitchChannels; std::vector<QString> fakeTwitchChannels;