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 viewer list not closing after pressing escape key. (#3734)
- Bugfix: Fixed links with no thumbnail having previous link's thumbnail. (#3720) - 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: 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 ## 2.3.5

View file

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

View file

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

View file

@ -26,6 +26,35 @@ using namespace std::chrono_literals;
namespace chatterino { 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() TwitchIrcServer::TwitchIrcServer()
: whispersChannel(new Channel("/whispers", Channel::Type::TwitchWhispers)) : whispersChannel(new Channel("/whispers", Channel::Type::TwitchWhispers))
, mentionsChannel(new Channel("/mentions", Channel::Type::TwitchMentions)) , mentionsChannel(new Channel("/mentions", Channel::Type::TwitchMentions))
@ -302,60 +331,55 @@ std::shared_ptr<Channel> TwitchIrcServer::getChannelOrEmptyByID(
return Channel::getEmpty(); 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() void TwitchIrcServer::bulkRefreshLiveStatus()
{ {
QStringList userIDs; auto twitchChans = std::make_shared<QHash<QString, TwitchChannel *>>();
this->forEachChannel([&userIDs](ChannelPtr chan) {
auto twitchChan = dynamic_cast<TwitchChannel *>(chan.get()); this->forEachChannel([twitchChans](ChannelPtr chan) {
if (!twitchChan->roomId().isEmpty()) auto tc = dynamic_cast<TwitchChannel *>(chan.get());
userIDs.push_back(twitchChan->roomId()); 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( getHelix()->fetchStreams(
batch, QStringList(), batch, {},
[this](std::vector<HelixStream> streams) { [twitchChans](std::vector<HelixStream> streams) {
for (const auto &stream : streams) for (const auto &stream : streams)
{ {
auto chan = this->getChannelOrEmpty(stream.userLogin); // remaining channels will be used later to set their stream status as offline
if (chan->getType() != Channel::Type::Twitch) // so we use take(id) to remove it
auto tc = twitchChans->take(stream.userId);
if (tc == nullptr)
{
continue; continue;
}
auto twitchChan = dynamic_cast<TwitchChannel *>(chan.get()); tc->parseLiveStatus(true, stream);
twitchChan->parseLiveStatus(true, stream);
} }
}, },
[]() { []() {
// failure // 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( void Helix::fetchStreams(
QStringList userIds, QStringList userLogins, QStringList userIds, QStringList userLogins,
ResultCallback<std::vector<HelixStream>> successCallback, ResultCallback<std::vector<HelixStream>> successCallback,
HelixFailureCallback failureCallback) HelixFailureCallback failureCallback, std::function<void()> finallyCallback)
{ {
QUrlQuery urlQuery; QUrlQuery urlQuery;
@ -186,19 +186,21 @@ void Helix::fetchStreams(
// TODO: make better xd // TODO: make better xd
failureCallback(); failureCallback();
}) })
.finally(finallyCallback)
.execute(); .execute();
} }
void Helix::getStreamById(QString userId, void Helix::getStreamById(QString userId,
ResultCallback<bool, HelixStream> successCallback, ResultCallback<bool, HelixStream> successCallback,
HelixFailureCallback failureCallback) HelixFailureCallback failureCallback,
std::function<void()> finallyCallback)
{ {
QStringList userIds{std::move(userId)}; QStringList userIds{std::move(userId)};
QStringList userLogins; QStringList userLogins;
this->fetchStreams( this->fetchStreams(
userIds, userLogins, userIds, userLogins,
[successCallback, failureCallback](const auto &streams) { [successCallback](const auto &streams) {
if (streams.empty()) if (streams.empty())
{ {
successCallback(false, HelixStream()); successCallback(false, HelixStream());
@ -206,12 +208,13 @@ void Helix::getStreamById(QString userId,
} }
successCallback(true, streams[0]); successCallback(true, streams[0]);
}, },
failureCallback); failureCallback, finallyCallback);
} }
void Helix::getStreamByName(QString userName, void Helix::getStreamByName(QString userName,
ResultCallback<bool, HelixStream> successCallback, ResultCallback<bool, HelixStream> successCallback,
HelixFailureCallback failureCallback) HelixFailureCallback failureCallback,
std::function<void()> finallyCallback)
{ {
QStringList userIds; QStringList userIds;
QStringList userLogins{std::move(userName)}; QStringList userLogins{std::move(userName)};
@ -226,7 +229,7 @@ void Helix::getStreamByName(QString userName,
} }
successCallback(true, streams[0]); successCallback(true, streams[0]);
}, },
failureCallback); failureCallback, finallyCallback);
} }
/// ///

View file

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