Implement subscription badges

Fix #61
This commit is contained in:
Rasmus Karlsson 2017-07-02 15:11:33 +02:00
parent 323cdc5561
commit 3e91340788
2 changed files with 28 additions and 11 deletions

View file

@ -86,13 +86,15 @@ void Resources::loadChannelData(const std::string &roomID, bool bypassCache)
QString url = "https://badges.twitch.tv/v1/badges/channels/" + QString::fromStdString(roomID) +
"/display?language=en";
util::urlFetchJSON(url, [this](QJsonObject &root) {
util::urlFetchJSON(url, [this, roomID](QJsonObject &root) {
QJsonObject sets = root.value("badge_sets").toObject();
Resources::Channel &ch = this->channels[roomID];
for (QJsonObject::iterator it = sets.begin(); it != sets.end(); ++it) {
QJsonObject versions = it.value().toObject().value("versions").toObject();
auto &badgeSet = this->badgeSets[it.key().toStdString()];
auto &badgeSet = ch.badgeSets[it.key().toStdString()];
auto &versionsMap = badgeSet.versions;
for (auto versionIt = std::begin(versions); versionIt != std::end(versions);
@ -104,7 +106,7 @@ void Resources::loadChannelData(const std::string &roomID, bool bypassCache)
}
}
this->dynamicBadgesLoaded = true;
ch.loaded = true;
});
}

View file

@ -425,15 +425,30 @@ void TwitchMessageBuilder::appendTwitchBadges(const QStringList &badges, Resourc
} break;
}
} else if (badge.startsWith("subscriber/")) {
qDebug() << "Subscriber badge:" << badge;
int index = badge.midRef(11).toInt();
// TODO: Implement subscriber badges here
switch (index) {
default: {
// printf("[TwitchMessageBuilder] Unhandled subscriber badge index: %d\n",
// index);
} break;
if (channelResources.loaded == false) {
qDebug() << "Channel resources are not loaded, can't add the subscriber badge";
continue;
}
try {
const auto &badgeSet = channelResources.badgeSets.at("subscriber");
std::string versionKey = badge.mid(11).toStdString();
try {
auto &badgeVersion = badgeSet.versions.at(versionKey);
appendWord(
Word(badgeVersion.badgeImage1x, Word::Type::BadgeSubscription, QString(),
QString("Twitch " + QString::fromStdString(badgeVersion.title))));
} catch (const std::exception &e) {
qDebug() << "Exception caught:" << e.what()
<< "when trying to fetch badge version " << versionKey.c_str();
}
} catch (const std::exception &e) {
qDebug() << "No channel badge set with key `subscriber`. Exception: " << e.what();
}
} else {
if (!resources.dynamicBadgesLoaded) {
// Do nothing