From 56aac47fdee369b188011ffaa33525e6a36d5483 Mon Sep 17 00:00:00 2001 From: apa420 <17131426+apa420@users.noreply.github.com> Date: Sun, 16 Sep 2018 17:27:51 +0200 Subject: [PATCH] fixed chatterino badges (#698) * fixed chatterino badges --- src/Application.cpp | 2 + src/Application.hpp | 2 + src/providers/chatterino/ChatterinoBadges.cpp | 58 +++++++++++-------- src/providers/chatterino/ChatterinoBadges.hpp | 11 +++- src/providers/twitch/TwitchMessageBuilder.cpp | 21 +++---- 5 files changed, 53 insertions(+), 41 deletions(-) diff --git a/src/Application.cpp b/src/Application.cpp index 68ccc78f8..9f0ad0706 100644 --- a/src/Application.cpp +++ b/src/Application.cpp @@ -9,6 +9,7 @@ #include "debug/Log.hpp" #include "messages/MessageBuilder.hpp" #include "providers/bttv/BttvEmotes.hpp" +#include "providers/chatterino/ChatterinoBadges.hpp" #include "providers/ffz/FfzEmotes.hpp" #include "providers/twitch/PubsubClient.hpp" #include "providers/twitch/TwitchServer.hpp" @@ -52,6 +53,7 @@ Application::Application(Settings &_settings, Paths &_paths) , taggedUsers(&this->emplace()) , moderationActions(&this->emplace()) , twitch2(&this->emplace()) + , chatterinoBadges(&this->emplace()) , logging(&this->emplace()) { this->instance = this; diff --git a/src/Application.hpp b/src/Application.hpp index 6089aef43..531fafea3 100644 --- a/src/Application.hpp +++ b/src/Application.hpp @@ -26,6 +26,7 @@ class Emotes; class Settings; class Fonts; class Resources2; +class ChatterinoBadges; class Application { @@ -60,6 +61,7 @@ public: TaggedUsersController *const taggedUsers{}; ModerationActions *const moderationActions{}; TwitchServer *const twitch2{}; + ChatterinoBadges *const chatterinoBadges{}; /*[[deprecated]]*/ Logging *const logging{}; diff --git a/src/providers/chatterino/ChatterinoBadges.cpp b/src/providers/chatterino/ChatterinoBadges.cpp index 37dc8f6f0..28bb57f3e 100644 --- a/src/providers/chatterino/ChatterinoBadges.cpp +++ b/src/providers/chatterino/ChatterinoBadges.cpp @@ -5,8 +5,18 @@ #include #include #include "common/NetworkRequest.hpp" +#include "common/Outcome.hpp" +#include "messages/Emote.hpp" + +#include + +#include namespace chatterino { +void ChatterinoBadges::initialize(Settings &settings, Paths &paths) +{ + this->loadChatterinoBadges(); +} ChatterinoBadges::ChatterinoBadges() { @@ -14,41 +24,41 @@ ChatterinoBadges::ChatterinoBadges() boost::optional 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(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 diff --git a/src/providers/chatterino/ChatterinoBadges.hpp b/src/providers/chatterino/ChatterinoBadges.hpp index 89870fd16..b77e2de4c 100644 --- a/src/providers/chatterino/ChatterinoBadges.hpp +++ b/src/providers/chatterino/ChatterinoBadges.hpp @@ -1,25 +1,30 @@ #pragma once #include +#include #include "common/Aliases.hpp" +#include +#include + namespace chatterino { struct Emote; using EmotePtr = std::shared_ptr; -class ChatterinoBadges +class ChatterinoBadges : public Singleton { public: + virtual void initialize(Settings &settings, Paths &paths) override; ChatterinoBadges(); boost::optional getBadge(const UserName &username); private: void loadChatterinoBadges(); - - // UniqueAccess> badges; + std::map badgeMap; + std::vector emotes; }; } // namespace chatterino diff --git a/src/providers/twitch/TwitchMessageBuilder.cpp b/src/providers/twitch/TwitchMessageBuilder.cpp index 62a005601..13f02ae5d 100644 --- a/src/providers/twitch/TwitchMessageBuilder.cpp +++ b/src/providers/twitch/TwitchMessageBuilder.cpp @@ -7,6 +7,7 @@ #include "debug/Log.hpp" #include "messages/Message.hpp" #include "providers/LinkResolver.hpp" +#include "providers/chatterino/ChatterinoBadges.hpp" #include "providers/twitch/TwitchBadges.hpp" #include "providers/twitch/TwitchChannel.hpp" #include "singletons/Emotes.hpp" @@ -796,20 +797,12 @@ void TwitchMessageBuilder::appendTwitchBadges() void TwitchMessageBuilder::appendChatterinoBadges() { - // auto app = getApp(); - - // auto &badges = app->resources->chatterinoBadges; - // auto it = badges.find(this->userName.toStdString()); - - // if (it == badges.end()) { - // return; - // } - - // const auto badge = it->second; - - // this->emplace(badge->image, - // MessageElementFlag::BadgeChatterino) - // ->setTooltip(QString::fromStdString(badge->tooltip)); + auto chatterinoBadgePtr = + getApp()->chatterinoBadges->getBadge({this->userName}); + if (chatterinoBadgePtr) { + this->emplace(*chatterinoBadgePtr, + MessageElementFlag::BadgeChatterino); + } } Outcome TwitchMessageBuilder::tryParseCheermote(const QString &string)