From f83c07be53ed1acc4377e790e5ae729cea76b1ef Mon Sep 17 00:00:00 2001 From: Rasmus Karlsson Date: Tue, 5 Jun 2018 17:39:49 +0200 Subject: [PATCH] Move bttv emotes to its own class --- chatterino.pro | 2 + src/providers/bttv/bttvemotes.cpp | 129 ++++++++++++++++++ src/providers/bttv/bttvemotes.hpp | 35 +++++ src/providers/twitch/twitchchannel.cpp | 2 +- src/providers/twitch/twitchmessagebuilder.cpp | 2 +- src/singletons/emotemanager.cpp | 110 +-------------- src/singletons/emotemanager.hpp | 19 +-- src/util/completionmodel.cpp | 4 +- src/widgets/emotepopup.cpp | 2 +- 9 files changed, 174 insertions(+), 131 deletions(-) create mode 100644 src/providers/bttv/bttvemotes.cpp create mode 100644 src/providers/bttv/bttvemotes.hpp diff --git a/chatterino.pro b/chatterino.pro index 7dea2bbd2..875f98150 100644 --- a/chatterino.pro +++ b/chatterino.pro @@ -115,6 +115,7 @@ SOURCES += \ src/providers/twitch/twitchserver.cpp \ src/providers/twitch/pubsub.cpp \ src/providers/twitch/twitchemotes.cpp \ + src/providers/bttv/bttvemotes.cpp \ src/singletons/commandmanager.cpp \ src/singletons/emotemanager.cpp \ src/singletons/fontmanager.cpp \ @@ -243,6 +244,7 @@ HEADERS += \ src/providers/twitch/twitchserver.hpp \ src/providers/twitch/pubsub.hpp \ src/providers/twitch/twitchemotes.hpp \ + src/providers/bttv/bttvemotes.hpp \ src/singletons/commandmanager.hpp \ src/singletons/emotemanager.hpp \ src/singletons/fontmanager.hpp \ diff --git a/src/providers/bttv/bttvemotes.cpp b/src/providers/bttv/bttvemotes.cpp new file mode 100644 index 000000000..bff4581d0 --- /dev/null +++ b/src/providers/bttv/bttvemotes.cpp @@ -0,0 +1,129 @@ +#include "providers/bttv/bttvemotes.hpp" + +#include "debug/log.hpp" +#include "messages/image.hpp" +#include "util/urlfetch.hpp" + +#define TWITCH_EMOTE_TEMPLATE "https://static-cdn.jtvnw.net/emoticons/v1/{id}/{scale}" + +namespace chatterino { +namespace providers { +namespace bttv { + +namespace { + +QString getEmoteLink(QString urlTemplate, const QString &id, const QString &emoteScale) +{ + urlTemplate.detach(); + + return urlTemplate.replace("{{id}}", id).replace("{{image}}", emoteScale); +} + +} // namespace + +util::EmoteMap &BTTVEmotes::getBTTVChannelEmoteFromCaches() +{ + return _bttvChannelEmoteFromCaches; +} + +void BTTVEmotes::loadGlobalEmotes() +{ + QString url("https://api.betterttv.net/2/emotes"); + + util::NetworkRequest req(url); + req.setCaller(QThread::currentThread()); + req.setTimeout(30000); + req.setUseQuickLoadCache(true); + req.getJSON([this](QJsonObject &root) { + auto emotes = root.value("emotes").toArray(); + + QString urlTemplate = "https:" + root.value("urlTemplate").toString(); + + std::vector codes; + for (const QJsonValue &emote : emotes) { + QString id = emote.toObject().value("id").toString(); + QString code = emote.toObject().value("code").toString(); + + util::EmoteData emoteData; + emoteData.image1x = new messages::Image(getEmoteLink(urlTemplate, id, "1x"), 1, code, + code + "
Global BTTV Emote"); + emoteData.image2x = new messages::Image(getEmoteLink(urlTemplate, id, "2x"), 0.5, code, + code + "
Global BTTV Emote"); + emoteData.image3x = new messages::Image(getEmoteLink(urlTemplate, id, "3x"), 0.25, code, + code + "
Global BTTV Emote"); + emoteData.pageLink = "https://manage.betterttv.net/emotes/" + id; + + this->globalEmotes.insert(code, emoteData); + codes.push_back(code.toStdString()); + } + + this->globalEmoteCodes = codes; + }); +} + +void BTTVEmotes::loadChannelEmotes(const QString &channelName, std::weak_ptr _map) +{ + printf("[BTTVEmotes] Reload BTTV Channel Emotes for channel %s\n", qPrintable(channelName)); + + QString url("https://api.betterttv.net/2/channels/" + channelName); + + debug::Log("Request bttv channel emotes for {}", channelName); + + util::NetworkRequest req(url); + req.setCaller(QThread::currentThread()); + req.setTimeout(3000); + req.getJSON([this, channelName, _map](QJsonObject &rootNode) { + auto map = _map.lock(); + + if (_map.expired()) { + return; + } + + map->clear(); + + auto emotesNode = rootNode.value("emotes").toArray(); + + QString linkTemplate = "https:" + rootNode.value("urlTemplate").toString(); + + std::vector codes; + for (const QJsonValue &emoteNode : emotesNode) { + QJsonObject emoteObject = emoteNode.toObject(); + + QString id = emoteObject.value("id").toString(); + QString code = emoteObject.value("code").toString(); + // emoteObject.value("imageType").toString(); + + auto emote = this->getBTTVChannelEmoteFromCaches().getOrAdd(id, [&] { + util::EmoteData emoteData; + QString link = linkTemplate; + link.detach(); + emoteData.image1x = + new messages::Image(link.replace("{{id}}", id).replace("{{image}}", "1x"), 1, + code, code + "
Channel BTTV Emote"); + link = linkTemplate; + link.detach(); + emoteData.image2x = + new messages::Image(link.replace("{{id}}", id).replace("{{image}}", "2x"), 0.5, + code, code + "
Channel BTTV Emote"); + link = linkTemplate; + link.detach(); + emoteData.image3x = + new messages::Image(link.replace("{{id}}", id).replace("{{image}}", "3x"), 0.25, + code, code + "
Channel BTTV Emote"); + emoteData.pageLink = "https://manage.betterttv.net/emotes/" + id; + + return emoteData; + }); + + this->channelEmotes.insert(code, emote); + map->insert(code, emote); + codes.push_back(code.toStdString()); + } + + this->channelEmoteCodes[channelName.toStdString()] = codes; + }); +} + +} // namespace bttv +} // namespace providers +} // namespace chatterino diff --git a/src/providers/bttv/bttvemotes.hpp b/src/providers/bttv/bttvemotes.hpp new file mode 100644 index 000000000..edef6a5e2 --- /dev/null +++ b/src/providers/bttv/bttvemotes.hpp @@ -0,0 +1,35 @@ +#pragma once + +#include "signalvector.hpp" +#include "util/concurrentmap.hpp" +#include "util/emotemap.hpp" + +#include + +namespace chatterino { +namespace providers { +namespace bttv { + +class BTTVEmotes +{ +public: + util::EmoteMap globalEmotes; + SignalVector globalEmoteCodes; + + util::EmoteMap channelEmotes; + std::map> channelEmoteCodes; + + util::EmoteMap &getBTTVChannelEmoteFromCaches(); + + void loadGlobalEmotes(); + void loadChannelEmotes(const QString &channelName, + std::weak_ptr channelEmoteMap); + + util::ConcurrentMap channels; + + util::EmoteMap _bttvChannelEmoteFromCaches; +}; + +} // namespace bttv +} // namespace providers +} // namespace chatterino diff --git a/src/providers/twitch/twitchchannel.cpp b/src/providers/twitch/twitchchannel.cpp index 369945b44..64bd943ad 100644 --- a/src/providers/twitch/twitchchannel.cpp +++ b/src/providers/twitch/twitchchannel.cpp @@ -140,7 +140,7 @@ void TwitchChannel::reloadChannelEmotes() debug::Log("[TwitchChannel:{}] Reloading channel emotes", this->name); - app->emotes->reloadBTTVChannelEmotes(this->name, this->bttvChannelEmotes); + app->emotes->bttv.loadChannelEmotes(this->name, this->bttvChannelEmotes); app->emotes->reloadFFZChannelEmotes(this->name, this->ffzChannelEmotes); } diff --git a/src/providers/twitch/twitchmessagebuilder.cpp b/src/providers/twitch/twitchmessagebuilder.cpp index d55aaba9b..8e81741cf 100644 --- a/src/providers/twitch/twitchmessagebuilder.cpp +++ b/src/providers/twitch/twitchmessagebuilder.cpp @@ -531,7 +531,7 @@ bool TwitchMessageBuilder::tryAppendEmote(QString &emoteString) return true; }; - if (app->emotes->bttvGlobalEmotes.tryGet(emoteString, emoteData)) { + if (app->emotes->bttv.globalEmotes.tryGet(emoteString, emoteData)) { // BTTV Global Emote return appendEmote(MessageElement::BttvEmote); } else if (this->twitchChannel != nullptr && diff --git a/src/singletons/emotemanager.cpp b/src/singletons/emotemanager.cpp index eabf29dda..29d859034 100644 --- a/src/singletons/emotemanager.cpp +++ b/src/singletons/emotemanager.cpp @@ -26,13 +26,6 @@ namespace singletons { namespace { -QString GetBTTVEmoteLink(QString urlTemplate, const QString &id, const QString &emoteScale) -{ - urlTemplate.detach(); - - return urlTemplate.replace("{{id}}", id).replace("{{image}}", emoteScale); -} - QString GetFFZEmoteLink(const QJsonObject &urls, const QString &emoteScale) { auto emote = urls.value(emoteScale); @@ -82,71 +75,10 @@ void EmoteManager::initialize() }); this->loadEmojis(); - this->loadBTTVEmotes(); + this->bttv.loadGlobalEmotes(); this->loadFFZEmotes(); } -void EmoteManager::reloadBTTVChannelEmotes(const QString &channelName, - std::weak_ptr _map) -{ - printf("[EmoteManager] Reload BTTV Channel Emotes for channel %s\n", qPrintable(channelName)); - - QString url("https://api.betterttv.net/2/channels/" + channelName); - - debug::Log("Request bttv channel emotes for {}", channelName); - - util::NetworkRequest req(url); - req.setCaller(QThread::currentThread()); - req.setTimeout(3000); - req.getJSON([this, channelName, _map](QJsonObject &rootNode) { - auto map = _map.lock(); - - if (_map.expired()) { - return; - } - - map->clear(); - - auto emotesNode = rootNode.value("emotes").toArray(); - - QString linkTemplate = "https:" + rootNode.value("urlTemplate").toString(); - - std::vector codes; - for (const QJsonValue &emoteNode : emotesNode) { - QJsonObject emoteObject = emoteNode.toObject(); - - QString id = emoteObject.value("id").toString(); - QString code = emoteObject.value("code").toString(); - // emoteObject.value("imageType").toString(); - - auto emote = this->getBTTVChannelEmoteFromCaches().getOrAdd(id, [&] { - util::EmoteData emoteData; - QString link = linkTemplate; - link.detach(); - emoteData.image1x = new Image(link.replace("{{id}}", id).replace("{{image}}", "1x"), - 1, code, code + "
Channel BTTV Emote"); - link = linkTemplate; - link.detach(); - emoteData.image2x = new Image(link.replace("{{id}}", id).replace("{{image}}", "2x"), - 0.5, code, code + "
Channel BTTV Emote"); - link = linkTemplate; - link.detach(); - emoteData.image3x = new Image(link.replace("{{id}}", id).replace("{{image}}", "3x"), - 0.25, code, code + "
Channel BTTV Emote"); - emoteData.pageLink = "https://manage.betterttv.net/emotes/" + id; - - return emoteData; - }); - - this->bttvChannelEmotes.insert(code, emote); - map->insert(code, emote); - codes.push_back(code.toStdString()); - } - - this->bttvChannelEmoteCodes[channelName.toStdString()] = codes; - }); -} - void EmoteManager::reloadFFZChannelEmotes(const QString &channelName, std::weak_ptr _map) { @@ -210,11 +142,6 @@ util::EmoteMap &EmoteManager::getChatterinoEmotes() return _chatterinoEmotes; } -util::EmoteMap &EmoteManager::getBTTVChannelEmoteFromCaches() -{ - return _bttvChannelEmoteFromCaches; -} - util::EmojiMap &EmoteManager::getEmojis() { return this->emojis; @@ -400,41 +327,6 @@ QString EmoteManager::replaceShortCodes(const QString &text) return ret; } -void EmoteManager::loadBTTVEmotes() -{ - QString url("https://api.betterttv.net/2/emotes"); - - util::NetworkRequest req(url); - req.setCaller(QThread::currentThread()); - req.setTimeout(30000); - req.setUseQuickLoadCache(true); - req.getJSON([this](QJsonObject &root) { - auto emotes = root.value("emotes").toArray(); - - QString urlTemplate = "https:" + root.value("urlTemplate").toString(); - - std::vector codes; - for (const QJsonValue &emote : emotes) { - QString id = emote.toObject().value("id").toString(); - QString code = emote.toObject().value("code").toString(); - - util::EmoteData emoteData; - emoteData.image1x = new Image(GetBTTVEmoteLink(urlTemplate, id, "1x"), 1, code, - code + "
Global BTTV Emote"); - emoteData.image2x = new Image(GetBTTVEmoteLink(urlTemplate, id, "2x"), 0.5, code, - code + "
Global BTTV Emote"); - emoteData.image3x = new Image(GetBTTVEmoteLink(urlTemplate, id, "3x"), 0.25, code, - code + "
Global BTTV Emote"); - emoteData.pageLink = "https://manage.betterttv.net/emotes/" + id; - - this->bttvGlobalEmotes.insert(code, emoteData); - codes.push_back(code.toStdString()); - } - - this->bttvGlobalEmoteCodes = codes; - }); -} - void EmoteManager::loadFFZEmotes() { QString url("https://api.frankerfacez.com/v1/set/global"); diff --git a/src/singletons/emotemanager.hpp b/src/singletons/emotemanager.hpp index 53da37e1f..c7686e577 100644 --- a/src/singletons/emotemanager.hpp +++ b/src/singletons/emotemanager.hpp @@ -4,6 +4,7 @@ #include "emojis.hpp" #include "messages/image.hpp" +#include "providers/bttv/bttvemotes.hpp" #include "providers/twitch/twitchemotes.hpp" #include "signalvector.hpp" #include "util/concurrentmap.hpp" @@ -27,17 +28,15 @@ public: ~EmoteManager() = delete; providers::twitch::TwitchEmotes twitch; + providers::bttv::BTTVEmotes bttv; void initialize(); - void reloadBTTVChannelEmotes(const QString &channelName, - std::weak_ptr channelEmoteMap); void reloadFFZChannelEmotes(const QString &channelName, std::weak_ptr channelEmoteMap); util::EmoteMap &getFFZEmotes(); util::EmoteMap &getChatterinoEmotes(); - util::EmoteMap &getBTTVChannelEmoteFromCaches(); util::EmojiMap &getEmojis(); util::ConcurrentMap &getFFZChannelEmoteFromCaches(); @@ -70,20 +69,6 @@ public: std::vector emojiShortCodes; - /// BTTV emotes - util::EmoteMap bttvChannelEmotes; - -public: - util::ConcurrentMap bttvChannels; - util::EmoteMap bttvGlobalEmotes; - SignalVector bttvGlobalEmoteCodes; - // roomID - std::map> bttvChannelEmoteCodes; - util::EmoteMap _bttvChannelEmoteFromCaches; - -private: - void loadBTTVEmotes(); - /// FFZ emotes util::EmoteMap ffzChannelEmotes; diff --git a/src/util/completionmodel.cpp b/src/util/completionmodel.cpp index 1870f0cd0..f3c3b4735 100644 --- a/src/util/completionmodel.cpp +++ b/src/util/completionmodel.cpp @@ -32,7 +32,7 @@ void CompletionModel::refresh() } // Global: BTTV Global Emotes - std::vector &bttvGlobalEmoteCodes = app->emotes->bttvGlobalEmoteCodes; + std::vector &bttvGlobalEmoteCodes = app->emotes->bttv.globalEmoteCodes; for (const auto &m : bttvGlobalEmoteCodes) { this->addString(m, TaggedString::Type::BTTVGlobalEmote); } @@ -45,7 +45,7 @@ void CompletionModel::refresh() // Channel-specific: BTTV Channel Emotes std::vector &bttvChannelEmoteCodes = - app->emotes->bttvChannelEmoteCodes[this->channelName.toStdString()]; + app->emotes->bttv.channelEmoteCodes[this->channelName.toStdString()]; for (const auto &m : bttvChannelEmoteCodes) { this->addString(m, TaggedString::Type::BTTVChannelEmote); } diff --git a/src/widgets/emotepopup.cpp b/src/widgets/emotepopup.cpp index 7c100e2af..8cca4121e 100644 --- a/src/widgets/emotepopup.cpp +++ b/src/widgets/emotepopup.cpp @@ -114,7 +114,7 @@ void EmotePopup::loadChannel(ChannelPtr _channel) emoteChannel->addMessage(builder2.getMessage()); } - addEmotes(app->emotes->bttvGlobalEmotes, "BetterTTV Global Emotes", "BetterTTV Global Emote"); + addEmotes(app->emotes->bttv.globalEmotes, "BetterTTV Global Emotes", "BetterTTV Global Emote"); addEmotes(*channel->bttvChannelEmotes.get(), "BetterTTV Channel Emotes", "BetterTTV Channel Emote"); addEmotes(app->emotes->ffzGlobalEmotes, "FrankerFaceZ Global Emotes",