Move bttv emotes to its own class

This commit is contained in:
Rasmus Karlsson 2018-06-05 17:39:49 +02:00 committed by fourtf
parent e12d386a5a
commit f83c07be53
9 changed files with 174 additions and 131 deletions

View file

@ -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 \

View file

@ -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<std::string> 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 + "<br />Global BTTV Emote");
emoteData.image2x = new messages::Image(getEmoteLink(urlTemplate, id, "2x"), 0.5, code,
code + "<br />Global BTTV Emote");
emoteData.image3x = new messages::Image(getEmoteLink(urlTemplate, id, "3x"), 0.25, code,
code + "<br />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<util::EmoteMap> _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<std::string> 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 + "<br />Channel BTTV Emote");
link = linkTemplate;
link.detach();
emoteData.image2x =
new messages::Image(link.replace("{{id}}", id).replace("{{image}}", "2x"), 0.5,
code, code + "<br />Channel BTTV Emote");
link = linkTemplate;
link.detach();
emoteData.image3x =
new messages::Image(link.replace("{{id}}", id).replace("{{image}}", "3x"), 0.25,
code, code + "<br />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

View file

@ -0,0 +1,35 @@
#pragma once
#include "signalvector.hpp"
#include "util/concurrentmap.hpp"
#include "util/emotemap.hpp"
#include <map>
namespace chatterino {
namespace providers {
namespace bttv {
class BTTVEmotes
{
public:
util::EmoteMap globalEmotes;
SignalVector<std::string> globalEmoteCodes;
util::EmoteMap channelEmotes;
std::map<std::string, SignalVector<std::string>> channelEmoteCodes;
util::EmoteMap &getBTTVChannelEmoteFromCaches();
void loadGlobalEmotes();
void loadChannelEmotes(const QString &channelName,
std::weak_ptr<util::EmoteMap> channelEmoteMap);
util::ConcurrentMap<QString, util::EmoteMap> channels;
util::EmoteMap _bttvChannelEmoteFromCaches;
};
} // namespace bttv
} // namespace providers
} // namespace chatterino

View file

@ -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);
}

View file

@ -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 &&

View file

@ -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<util::EmoteMap> _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<std::string> 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 + "<br />Channel BTTV Emote");
link = linkTemplate;
link.detach();
emoteData.image2x = new Image(link.replace("{{id}}", id).replace("{{image}}", "2x"),
0.5, code, code + "<br />Channel BTTV Emote");
link = linkTemplate;
link.detach();
emoteData.image3x = new Image(link.replace("{{id}}", id).replace("{{image}}", "3x"),
0.25, code, code + "<br />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<util::EmoteMap> _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<std::string> 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 + "<br />Global BTTV Emote");
emoteData.image2x = new Image(GetBTTVEmoteLink(urlTemplate, id, "2x"), 0.5, code,
code + "<br />Global BTTV Emote");
emoteData.image3x = new Image(GetBTTVEmoteLink(urlTemplate, id, "3x"), 0.25, code,
code + "<br />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");

View file

@ -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<util::EmoteMap> channelEmoteMap);
void reloadFFZChannelEmotes(const QString &channelName,
std::weak_ptr<util::EmoteMap> channelEmoteMap);
util::EmoteMap &getFFZEmotes();
util::EmoteMap &getChatterinoEmotes();
util::EmoteMap &getBTTVChannelEmoteFromCaches();
util::EmojiMap &getEmojis();
util::ConcurrentMap<int, util::EmoteData> &getFFZChannelEmoteFromCaches();
@ -70,20 +69,6 @@ public:
std::vector<std::string> emojiShortCodes;
/// BTTV emotes
util::EmoteMap bttvChannelEmotes;
public:
util::ConcurrentMap<QString, util::EmoteMap> bttvChannels;
util::EmoteMap bttvGlobalEmotes;
SignalVector<std::string> bttvGlobalEmoteCodes;
// roomID
std::map<std::string, SignalVector<std::string>> bttvChannelEmoteCodes;
util::EmoteMap _bttvChannelEmoteFromCaches;
private:
void loadBTTVEmotes();
/// FFZ emotes
util::EmoteMap ffzChannelEmotes;

View file

@ -32,7 +32,7 @@ void CompletionModel::refresh()
}
// Global: BTTV Global Emotes
std::vector<std::string> &bttvGlobalEmoteCodes = app->emotes->bttvGlobalEmoteCodes;
std::vector<std::string> &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<std::string> &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);
}

View file

@ -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",