Fixed channels not being set as offline (#3767)

This commit is contained in:
Kasia
2022-05-28 20:10:10 +02:00
committed by GitHub
parent 57f92f5eaa
commit 74ec310228
6 changed files with 92 additions and 53 deletions
+64 -40
View File
@@ -26,6 +26,35 @@ using namespace std::chrono_literals;
namespace chatterino {
namespace {
// TODO: combine this with getEmoteSetBatches in TwitchAccount.cpp, maybe some templated thing
template <class T>
std::vector<T> getChannelsInBatches(T channels)
{
constexpr int batchSize = 100;
int batchCount = (channels.size() / batchSize) + 1;
std::vector<T> batches;
batches.reserve(batchCount);
for (int i = 0; i < batchCount; i++)
{
T 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
TwitchIrcServer::TwitchIrcServer()
: whispersChannel(new Channel("/whispers", Channel::Type::TwitchWhispers))
, mentionsChannel(new Channel("/mentions", Channel::Type::TwitchMentions))
@@ -302,60 +331,55 @@ std::shared_ptr<Channel> TwitchIrcServer::getChannelOrEmptyByID(
return Channel::getEmpty();
}
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 TwitchIrcServer::bulkRefreshLiveStatus()
{
QStringList userIDs;
this->forEachChannel([&userIDs](ChannelPtr chan) {
auto twitchChan = dynamic_cast<TwitchChannel *>(chan.get());
if (!twitchChan->roomId().isEmpty())
userIDs.push_back(twitchChan->roomId());
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);
}
});
for (const auto &batch : getChannelsInBatches(userIDs))
// iterate over batches of channel IDs
for (const auto &batch : getChannelsInBatches(twitchChans->keys()))
{
getHelix()->fetchStreams(
batch, QStringList(),
[this](std::vector<HelixStream> streams) {
batch, {},
[twitchChans](std::vector<HelixStream> streams) {
for (const auto &stream : streams)
{
auto chan = this->getChannelOrEmpty(stream.userLogin);
if (chan->getType() != Channel::Type::Twitch)
// 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;
}
auto twitchChan = dynamic_cast<TwitchChannel *>(chan.get());
twitchChan->parseLiveStatus(true, stream);
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, {});
}
});
}
}