Make generic version of batcher function (#3822)

This commit is contained in:
Kasia
2022-06-17 20:52:20 +02:00
committed by GitHub
parent a83c139154
commit f3f340335f
10 changed files with 316 additions and 259 deletions
+2 -26
View File
@@ -17,36 +17,12 @@
#include "providers/twitch/TwitchUser.hpp"
#include "providers/twitch/api/Helix.hpp"
#include "singletons/Emotes.hpp"
#include "util/Helpers.hpp"
#include "util/QStringHash.hpp"
#include "util/RapidjsonHelpers.hpp"
namespace chatterino {
std::vector<QStringList> getEmoteSetBatches(QStringList emoteSetKeys)
{
// splitting emoteSetKeys to batches of 100, because Ivr API endpoint accepts a maximum of 100 emotesets at once
constexpr int batchSize = 100;
int batchCount = (emoteSetKeys.size() / batchSize) + 1;
std::vector<QStringList> batches;
batches.reserve(batchCount);
for (int i = 0; i < batchCount; i++)
{
QStringList batch;
int last = std::min(batchSize, emoteSetKeys.size() - batchSize * i);
for (int j = 0; j < last; j++)
{
batch.push_back(emoteSetKeys.at(j + (batchSize * i)));
}
batches.emplace_back(batch);
}
return batches;
}
TwitchAccount::TwitchAccount(const QString &username, const QString &oauthToken,
const QString &oauthClient, const QString &userID)
: Account(ProviderId::Twitch)
@@ -268,7 +244,7 @@ void TwitchAccount::loadUserstateEmotes(std::weak_ptr<Channel> weakChannel)
}
// requesting emotes
auto batches = getEmoteSetBatches(newEmoteSetKeys);
auto batches = splitListIntoBatches(newEmoteSetKeys);
for (int i = 0; i < batches.size(); i++)
{
qCDebug(chatterinoTwitch)
+2 -30
View File
@@ -15,6 +15,7 @@
#include "providers/twitch/TwitchAccount.hpp"
#include "providers/twitch/TwitchChannel.hpp"
#include "providers/twitch/TwitchHelpers.hpp"
#include "util/Helpers.hpp"
#include "util/PostToThread.hpp"
#include <QMetaEnum>
@@ -26,35 +27,6 @@ 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))
@@ -344,7 +316,7 @@ void TwitchIrcServer::bulkRefreshLiveStatus()
});
// iterate over batches of channel IDs
for (const auto &batch : getChannelsInBatches(twitchChans->keys()))
for (const auto &batch : splitListIntoBatches(twitchChans->keys()))
{
getHelix()->fetchStreams(
batch, {},