Move twitch emote-related stuff to TwitchEmotes class

This commit is contained in:
Rasmus Karlsson 2018-06-05 17:13:29 +02:00 committed by fourtf
parent 3a8ceede4e
commit e12d386a5a
8 changed files with 177 additions and 136 deletions

View file

@ -114,6 +114,7 @@ SOURCES += \
src/providers/twitch/twitchmessagebuilder.cpp \ src/providers/twitch/twitchmessagebuilder.cpp \
src/providers/twitch/twitchserver.cpp \ src/providers/twitch/twitchserver.cpp \
src/providers/twitch/pubsub.cpp \ src/providers/twitch/pubsub.cpp \
src/providers/twitch/twitchemotes.cpp \
src/singletons/commandmanager.cpp \ src/singletons/commandmanager.cpp \
src/singletons/emotemanager.cpp \ src/singletons/emotemanager.cpp \
src/singletons/fontmanager.cpp \ src/singletons/fontmanager.cpp \
@ -241,6 +242,7 @@ HEADERS += \
src/providers/twitch/twitchmessagebuilder.hpp \ src/providers/twitch/twitchmessagebuilder.hpp \
src/providers/twitch/twitchserver.hpp \ src/providers/twitch/twitchserver.hpp \
src/providers/twitch/pubsub.hpp \ src/providers/twitch/pubsub.hpp \
src/providers/twitch/twitchemotes.hpp \
src/singletons/commandmanager.hpp \ src/singletons/commandmanager.hpp \
src/singletons/emotemanager.hpp \ src/singletons/emotemanager.hpp \
src/singletons/fontmanager.hpp \ src/singletons/fontmanager.hpp \

View file

@ -0,0 +1,114 @@
#include "providers/twitch/twitchemotes.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 twitch {
namespace {
QString getEmoteLink(long id, const QString &emoteScale)
{
QString value = TWITCH_EMOTE_TEMPLATE;
value.detach();
return value.replace("{id}", QString::number(id)).replace("{scale}", emoteScale);
}
} // namespace
// id is used for lookup
// emoteName is used for giving a name to the emote in case it doesn't exist
util::EmoteData TwitchEmotes::getEmoteById(long id, const QString &emoteName)
{
QString _emoteName = emoteName;
_emoteName.replace("<", "&lt;");
_emoteName.replace(">", "&gt;");
// clang-format off
static QMap<QString, QString> emoteNameReplacements{
{"[oO](_|\\.)[oO]", "O_o"}, {"\\&gt\\;\\(", "&gt;("}, {"\\&lt\\;3", "&lt;3"},
{"\\:-?(o|O)", ":O"}, {"\\:-?(p|P)", ":P"}, {"\\:-?[\\\\/]", ":/"},
{"\\:-?[z|Z|\\|]", ":Z"}, {"\\:-?\\(", ":("}, {"\\:-?\\)", ":)"},
{"\\:-?D", ":D"}, {"\\;-?(p|P)", ";P"}, {"\\;-?\\)", ";)"},
{"R-?\\)", "R)"}, {"B-?\\)", "B)"},
};
// clang-format on
auto it = emoteNameReplacements.find(_emoteName);
if (it != emoteNameReplacements.end()) {
_emoteName = it.value();
}
return _twitchEmoteFromCache.getOrAdd(id, [&emoteName, &_emoteName, &id] {
util::EmoteData newEmoteData;
newEmoteData.image1x = new messages::Image(getEmoteLink(id, "1.0"), 1, emoteName,
_emoteName + "<br/>Twitch Emote 1x");
newEmoteData.image2x = new messages::Image(getEmoteLink(id, "2.0"), .5, emoteName,
_emoteName + "<br/>Twitch Emote 2x");
newEmoteData.image3x = new messages::Image(getEmoteLink(id, "3.0"), .25, emoteName,
_emoteName + "<br/>Twitch Emote 3x");
return newEmoteData;
});
}
void TwitchEmotes::refresh(const std::shared_ptr<TwitchAccount> &user)
{
debug::Log("Loading Twitch emotes for user {}", user->getUserName());
const auto &roomID = user->getUserId();
const auto &clientID = user->getOAuthClient();
const auto &oauthToken = user->getOAuthToken();
if (clientID.isEmpty() || oauthToken.isEmpty()) {
debug::Log("Missing Client ID or OAuth token");
return;
}
TwitchAccountEmoteData &emoteData = this->emotes[roomID.toStdString()];
if (emoteData.filled) {
qDebug() << "Already loaded for room id " << roomID;
return;
}
QString url("https://api.twitch.tv/kraken/users/" + roomID + "/emotes");
util::twitch::getAuthorized(
url, clientID, oauthToken, QThread::currentThread(),
[=, &emoteData](const QJsonObject &root) {
emoteData.emoteSets.clear();
emoteData.emoteCodes.clear();
auto emoticonSets = root.value("emoticon_sets").toObject();
for (QJsonObject::iterator it = emoticonSets.begin(); it != emoticonSets.end(); ++it) {
std::string emoteSetString = it.key().toStdString();
QJsonArray emoteSetList = it.value().toArray();
for (QJsonValue emoteValue : emoteSetList) {
QJsonObject emoticon = emoteValue.toObject();
std::string id = QString::number(emoticon["id"].toInt()).toStdString();
std::string code = emoticon["code"].toString().toStdString();
emoteData.emoteSets[emoteSetString].push_back({id, code});
emoteData.emoteCodes.push_back(code);
util::EmoteData emote =
this->getEmoteById(emoticon["id"].toInt(), emoticon["code"].toString());
emoteData.emotes.insert(emoticon["code"].toString(), emote);
}
}
emoteData.filled = true;
});
}
} // namespace twitch
} // namespace providers
} // namespace chatterino

View file

@ -0,0 +1,51 @@
#pragma once
#include "providers/twitch/emotevalue.hpp"
#include "providers/twitch/twitchaccount.hpp"
#include "providers/twitch/twitchemotes.hpp"
#include "util/concurrentmap.hpp"
#include "util/emotemap.hpp"
#include <map>
namespace chatterino {
namespace providers {
namespace twitch {
class TwitchEmotes
{
public:
util::EmoteData getEmoteById(long int id, const QString &emoteName);
/// Twitch emotes
void refresh(const std::shared_ptr<providers::twitch::TwitchAccount> &user);
struct TwitchAccountEmoteData {
struct TwitchEmote {
std::string id;
std::string code;
};
// emote set
std::map<std::string, std::vector<TwitchEmote>> emoteSets;
std::vector<std::string> emoteCodes;
util::EmoteMap emotes;
bool filled = false;
};
std::map<std::string, TwitchAccountEmoteData> emotes;
private:
// emote code
util::ConcurrentMap<QString, providers::twitch::EmoteValue *> _twitchEmotes;
// emote id
util::ConcurrentMap<long, util::EmoteData> _twitchEmoteFromCache;
};
} // namespace twitch
} // namespace providers
} // namespace chatterino

View file

@ -516,8 +516,8 @@ void TwitchMessageBuilder::appendTwitchEmote(const Communi::IrcMessage *ircMessa
QString name = this->originalMessage.mid(start, end - start + 1); QString name = this->originalMessage.mid(start, end - start + 1);
vec.push_back( vec.push_back(std::pair<long int, util::EmoteData>(
std::pair<long int, util::EmoteData>(start, app->emotes->getTwitchEmoteById(id, name))); start, app->emotes->twitch.getEmoteById(id, name)));
} }
} }

View file

@ -18,8 +18,6 @@
#include <memory> #include <memory>
#define TWITCH_EMOTE_TEMPLATE "https://static-cdn.jtvnw.net/emoticons/v1/{id}/{scale}"
using namespace chatterino::providers::twitch; using namespace chatterino::providers::twitch;
using namespace chatterino::messages; using namespace chatterino::messages;
@ -28,15 +26,6 @@ namespace singletons {
namespace { namespace {
QString GetTwitchEmoteLink(long id, const QString &emoteScale)
{
QString value = TWITCH_EMOTE_TEMPLATE;
value.detach();
return value.replace("{id}", QString::number(id)).replace("{scale}", emoteScale);
}
QString GetBTTVEmoteLink(QString urlTemplate, const QString &id, const QString &emoteScale) QString GetBTTVEmoteLink(QString urlTemplate, const QString &id, const QString &emoteScale)
{ {
urlTemplate.detach(); urlTemplate.detach();
@ -89,7 +78,7 @@ void EmoteManager::initialize()
getApp()->accounts->twitch.currentUserChanged.connect([this] { getApp()->accounts->twitch.currentUserChanged.connect([this] {
auto currentUser = getApp()->accounts->twitch.getCurrent(); auto currentUser = getApp()->accounts->twitch.getCurrent();
assert(currentUser); assert(currentUser);
this->refreshTwitchEmotes(currentUser); this->twitch.refresh(currentUser);
}); });
this->loadEmojis(); this->loadEmojis();
@ -411,56 +400,6 @@ QString EmoteManager::replaceShortCodes(const QString &text)
return ret; return ret;
} }
void EmoteManager::refreshTwitchEmotes(const std::shared_ptr<TwitchAccount> &user)
{
debug::Log("Loading Twitch emotes for user {}", user->getUserName());
const auto &roomID = user->getUserId();
const auto &clientID = user->getOAuthClient();
const auto &oauthToken = user->getOAuthToken();
if (clientID.isEmpty() || oauthToken.isEmpty()) {
debug::Log("Missing Client ID or OAuth token");
return;
}
TwitchAccountEmoteData &emoteData = this->twitchAccountEmotes[roomID.toStdString()];
if (emoteData.filled) {
qDebug() << "Already loaded for room id " << roomID;
return;
}
QString url("https://api.twitch.tv/kraken/users/" + roomID + "/emotes");
util::twitch::getAuthorized(
url, clientID, oauthToken, QThread::currentThread(),
[=, &emoteData](const QJsonObject &root) {
emoteData.emoteSets.clear();
emoteData.emoteCodes.clear();
auto emoticonSets = root.value("emoticon_sets").toObject();
for (QJsonObject::iterator it = emoticonSets.begin(); it != emoticonSets.end(); ++it) {
std::string emoteSetString = it.key().toStdString();
QJsonArray emoteSetList = it.value().toArray();
for (QJsonValue emoteValue : emoteSetList) {
QJsonObject emoticon = emoteValue.toObject();
std::string id = QString::number(emoticon["id"].toInt()).toStdString();
std::string code = emoticon["code"].toString().toStdString();
emoteData.emoteSets[emoteSetString].push_back({id, code});
emoteData.emoteCodes.push_back(code);
util::EmoteData emote =
getTwitchEmoteById(emoticon["id"].toInt(), emoticon["code"].toString());
emoteData.emotes.insert(emoticon["code"].toString(), emote);
}
}
emoteData.filled = true;
});
}
void EmoteManager::loadBTTVEmotes() void EmoteManager::loadBTTVEmotes()
{ {
QString url("https://api.betterttv.net/2/emotes"); QString url("https://api.betterttv.net/2/emotes");
@ -531,42 +470,6 @@ void EmoteManager::loadFFZEmotes()
}); });
} }
// id is used for lookup
// emoteName is used for giving a name to the emote in case it doesn't exist
util::EmoteData EmoteManager::getTwitchEmoteById(long id, const QString &emoteName)
{
QString _emoteName = emoteName;
_emoteName.replace("<", "&lt;");
_emoteName.replace(">", "&gt;");
// clang-format off
static QMap<QString, QString> emoteNameReplacements{
{"[oO](_|\\.)[oO]", "O_o"}, {"\\&gt\\;\\(", "&gt;("}, {"\\&lt\\;3", "&lt;3"},
{"\\:-?(o|O)", ":O"}, {"\\:-?(p|P)", ":P"}, {"\\:-?[\\\\/]", ":/"},
{"\\:-?[z|Z|\\|]", ":Z"}, {"\\:-?\\(", ":("}, {"\\:-?\\)", ":)"},
{"\\:-?D", ":D"}, {"\\;-?(p|P)", ";P"}, {"\\;-?\\)", ";)"},
{"R-?\\)", "R)"}, {"B-?\\)", "B)"},
};
// clang-format on
auto it = emoteNameReplacements.find(_emoteName);
if (it != emoteNameReplacements.end()) {
_emoteName = it.value();
}
return _twitchEmoteFromCache.getOrAdd(id, [&emoteName, &_emoteName, &id] {
util::EmoteData newEmoteData;
newEmoteData.image1x = new Image(GetTwitchEmoteLink(id, "1.0"), 1, emoteName,
_emoteName + "<br/>Twitch Emote 1x");
newEmoteData.image2x = new Image(GetTwitchEmoteLink(id, "2.0"), .5, emoteName,
_emoteName + "<br/>Twitch Emote 2x");
newEmoteData.image3x = new Image(GetTwitchEmoteLink(id, "3.0"), .25, emoteName,
_emoteName + "<br/>Twitch Emote 3x");
return newEmoteData;
});
}
util::EmoteData EmoteManager::getCheerImage(long long amount, bool animated) util::EmoteData EmoteManager::getCheerImage(long long amount, bool animated)
{ {
// TODO: fix this xD // TODO: fix this xD

View file

@ -4,8 +4,7 @@
#include "emojis.hpp" #include "emojis.hpp"
#include "messages/image.hpp" #include "messages/image.hpp"
#include "providers/twitch/emotevalue.hpp" #include "providers/twitch/twitchemotes.hpp"
#include "providers/twitch/twitchaccount.hpp"
#include "signalvector.hpp" #include "signalvector.hpp"
#include "util/concurrentmap.hpp" #include "util/concurrentmap.hpp"
#include "util/emotemap.hpp" #include "util/emotemap.hpp"
@ -27,6 +26,8 @@ public:
~EmoteManager() = delete; ~EmoteManager() = delete;
providers::twitch::TwitchEmotes twitch;
void initialize(); void initialize();
void reloadBTTVChannelEmotes(const QString &channelName, void reloadBTTVChannelEmotes(const QString &channelName,
@ -42,8 +43,6 @@ public:
util::EmoteData getCheerImage(long long int amount, bool animated); util::EmoteData getCheerImage(long long int amount, bool animated);
util::EmoteData getTwitchEmoteById(long int id, const QString &emoteName);
pajlada::Signals::NoArgSignal &getGifUpdateSignal(); pajlada::Signals::NoArgSignal &getGifUpdateSignal();
// Bit badge/emotes? // Bit badge/emotes?
@ -71,34 +70,6 @@ public:
std::vector<std::string> emojiShortCodes; std::vector<std::string> emojiShortCodes;
/// Twitch emotes
void refreshTwitchEmotes(const std::shared_ptr<providers::twitch::TwitchAccount> &user);
struct TwitchAccountEmoteData {
struct TwitchEmote {
std::string id;
std::string code;
};
// emote set
std::map<std::string, std::vector<TwitchEmote>> emoteSets;
std::vector<std::string> emoteCodes;
util::EmoteMap emotes;
bool filled = false;
};
std::map<std::string, TwitchAccountEmoteData> twitchAccountEmotes;
private:
// emote code
util::ConcurrentMap<QString, providers::twitch::EmoteValue *> _twitchEmotes;
// emote id
util::ConcurrentMap<long, util::EmoteData> _twitchEmoteFromCache;
/// BTTV emotes /// BTTV emotes
util::EmoteMap bttvChannelEmotes; util::EmoteMap bttvChannelEmotes;

View file

@ -24,7 +24,7 @@ void CompletionModel::refresh()
// User-specific: Twitch Emotes // User-specific: Twitch Emotes
// TODO: Fix this so it properly updates with the proper api. oauth token needs proper scope // TODO: Fix this so it properly updates with the proper api. oauth token needs proper scope
for (const auto &m : app->emotes->twitchAccountEmotes) { for (const auto &m : app->emotes->twitch.emotes) {
for (const auto &emoteName : m.second.emoteCodes) { for (const auto &emoteName : m.second.emoteCodes) {
// XXX: No way to discern between a twitch global emote and sub emote right now // XXX: No way to discern between a twitch global emote and sub emote right now
this->addString(emoteName, TaggedString::Type::TwitchGlobalEmote); this->addString(emoteName, TaggedString::Type::TwitchGlobalEmote);

View file

@ -88,7 +88,7 @@ void EmotePopup::loadChannel(ChannelPtr _channel)
// fourtf: the entire emote manager needs to be refactored so there's no point in trying to // fourtf: the entire emote manager needs to be refactored so there's no point in trying to
// fix this pile of garbage // fix this pile of garbage
for (const auto &set : app->emotes->twitchAccountEmotes[userID.toStdString()].emoteSets) { for (const auto &set : app->emotes->twitch.emotes[userID.toStdString()].emoteSets) {
// TITLE // TITLE
messages::MessageBuilder builder1; messages::MessageBuilder builder1;
@ -107,7 +107,7 @@ void EmotePopup::loadChannel(ChannelPtr _channel)
builder2.append((new EmoteElement(value, MessageElement::Flags::AlwaysShow)) builder2.append((new EmoteElement(value, MessageElement::Flags::AlwaysShow))
->setLink(Link(Link::InsertText, key))); ->setLink(Link(Link::InsertText, key)));
}(QString::fromStdString(emote.code), }(QString::fromStdString(emote.code),
app->emotes->getTwitchEmoteById(QString::fromStdString(emote.id).toLong(), app->emotes->twitch.getEmoteById(QString::fromStdString(emote.id).toLong(),
QString::fromStdString(emote.code))); QString::fromStdString(emote.code)));
} }