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
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 92 additions and 53 deletions

View file

@ -31,7 +31,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, #3762)
- Dev: Batch checking live status for all channels after startup. (#3757, #3762, #3767)
## 2.3.5

View file

@ -191,6 +191,9 @@ void NotificationController::fetchFakeChannels()
// we done fucked up.
qCWarning(chatterinoNotification)
<< "Failed to fetch live status for " << batch;
},
[]() {
// finally
});
}
}

View file

@ -708,6 +708,9 @@ void TwitchChannel::refreshLiveStatus()
},
[] {
// failure
},
[] {
// finally
});
}

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, {});
}
});
}
}

View file

@ -145,7 +145,7 @@ void Helix::getUserFollowers(
void Helix::fetchStreams(
QStringList userIds, QStringList userLogins,
ResultCallback<std::vector<HelixStream>> successCallback,
HelixFailureCallback failureCallback)
HelixFailureCallback failureCallback, std::function<void()> finallyCallback)
{
QUrlQuery urlQuery;
@ -186,19 +186,21 @@ void Helix::fetchStreams(
// TODO: make better xd
failureCallback();
})
.finally(finallyCallback)
.execute();
}
void Helix::getStreamById(QString userId,
ResultCallback<bool, HelixStream> successCallback,
HelixFailureCallback failureCallback)
HelixFailureCallback failureCallback,
std::function<void()> finallyCallback)
{
QStringList userIds{std::move(userId)};
QStringList userLogins;
this->fetchStreams(
userIds, userLogins,
[successCallback, failureCallback](const auto &streams) {
[successCallback](const auto &streams) {
if (streams.empty())
{
successCallback(false, HelixStream());
@ -206,12 +208,13 @@ void Helix::getStreamById(QString userId,
}
successCallback(true, streams[0]);
},
failureCallback);
failureCallback, finallyCallback);
}
void Helix::getStreamByName(QString userName,
ResultCallback<bool, HelixStream> successCallback,
HelixFailureCallback failureCallback)
HelixFailureCallback failureCallback,
std::function<void()> finallyCallback)
{
QStringList userIds;
QStringList userLogins{std::move(userName)};
@ -226,7 +229,7 @@ void Helix::getStreamByName(QString userName,
}
successCallback(true, streams[0]);
},
failureCallback);
failureCallback, finallyCallback);
}
///

View file

@ -352,15 +352,18 @@ public:
virtual void fetchStreams(
QStringList userIds, QStringList userLogins,
ResultCallback<std::vector<HelixStream>> successCallback,
HelixFailureCallback failureCallback) = 0;
HelixFailureCallback failureCallback,
std::function<void()> finallyCallback) = 0;
virtual void getStreamById(
QString userId, ResultCallback<bool, HelixStream> successCallback,
HelixFailureCallback failureCallback) = 0;
HelixFailureCallback failureCallback,
std::function<void()> finallyCallback) = 0;
virtual void getStreamByName(
QString userName, ResultCallback<bool, HelixStream> successCallback,
HelixFailureCallback failureCallback) = 0;
HelixFailureCallback failureCallback,
std::function<void()> finallyCallback) = 0;
// https://dev.twitch.tv/docs/api/reference#get-games
virtual void fetchGames(
@ -469,15 +472,18 @@ public:
// https://dev.twitch.tv/docs/api/reference#get-streams
void fetchStreams(QStringList userIds, QStringList userLogins,
ResultCallback<std::vector<HelixStream>> successCallback,
HelixFailureCallback failureCallback) final;
HelixFailureCallback failureCallback,
std::function<void()> finallyCallback) final;
void getStreamById(QString userId,
ResultCallback<bool, HelixStream> successCallback,
HelixFailureCallback failureCallback) final;
HelixFailureCallback failureCallback,
std::function<void()> finallyCallback) final;
void getStreamByName(QString userName,
ResultCallback<bool, HelixStream> successCallback,
HelixFailureCallback failureCallback) final;
HelixFailureCallback failureCallback,
std::function<void()> finallyCallback) final;
// https://dev.twitch.tv/docs/api/reference#get-games
void fetchGames(QStringList gameIds, QStringList gameNames,