fixed chatterino badges (#698)

* fixed chatterino badges
This commit is contained in:
apa420
2018-09-16 17:27:51 +02:00
committed by pajlada
parent e332564e01
commit 56aac47fde
5 changed files with 53 additions and 41 deletions
+34 -24
View File
@@ -5,8 +5,18 @@
#include <QJsonValue>
#include <QThread>
#include "common/NetworkRequest.hpp"
#include "common/Outcome.hpp"
#include "messages/Emote.hpp"
#include <QUrl>
#include <map>
namespace chatterino {
void ChatterinoBadges::initialize(Settings &settings, Paths &paths)
{
this->loadChatterinoBadges();
}
ChatterinoBadges::ChatterinoBadges()
{
@@ -14,41 +24,41 @@ ChatterinoBadges::ChatterinoBadges()
boost::optional<EmotePtr> ChatterinoBadges::getBadge(const UserName &username)
{
auto it = badgeMap.find(username.string);
if (it != badgeMap.end()) {
return emotes[it->second];
}
return boost::none;
// return this->badges.access()->get(username);
}
void ChatterinoBadges::loadChatterinoBadges()
{
// static QString url("https://fourtf.com/chatterino/badges.json");
static QUrl url("https://fourtf.com/chatterino/badges.json");
// NetworkRequest req(url);
// req.setCaller(QThread::currentThread());
NetworkRequest req(url);
req.setCaller(QThread::currentThread());
// req.onSuccess([this](auto result) {
// auto jsonRoot = result.parseJson();
// auto badges = this->badges.access();
// auto replacement = badges->makeReplacment();
req.onSuccess([this](auto result) -> Outcome {
auto jsonRoot = result.parseJson();
int index = 0;
for (const auto &jsonBadge_ : jsonRoot.value("badges").toArray()) {
auto jsonBadge = jsonBadge_.toObject();
auto emote = Emote{
EmoteName{}, ImageSet{Url{jsonBadge.value("image").toString()}},
Tooltip{jsonBadge.value("tooltip").toString()}, Url{}};
// for (auto jsonBadge_ : jsonRoot.value("badges").toArray()) {
// auto jsonBadge = jsonBadge_.toObject();
emotes.push_back(std::make_shared<const Emote>(std::move(emote)));
// auto emote = Emote{
// EmoteName{},
// ImageSet{Url{jsonBadge.value("image").toString()}},
// Tooltip{jsonBadge.value("tooltip").toString()}, Url{}};
for (const auto &user : jsonBadge.value("users").toArray()) {
badgeMap[user.toString()] = index;
}
++index;
}
// for (auto jsonUser : jsonBadge.value("users").toArray()) {
// replacement.add(UserName{jsonUser.toString()},
// std::move(emote));
// }
// }
return Success;
});
// replacement.apply();
// return Success;
//});
// req.execute();
req.execute();
}
} // namespace chatterino