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
+1 -98
View File
@@ -18,8 +18,6 @@
#include <memory>
#define TWITCH_EMOTE_TEMPLATE "https://static-cdn.jtvnw.net/emoticons/v1/{id}/{scale}"
using namespace chatterino::providers::twitch;
using namespace chatterino::messages;
@@ -28,15 +26,6 @@ namespace singletons {
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)
{
urlTemplate.detach();
@@ -89,7 +78,7 @@ void EmoteManager::initialize()
getApp()->accounts->twitch.currentUserChanged.connect([this] {
auto currentUser = getApp()->accounts->twitch.getCurrent();
assert(currentUser);
this->refreshTwitchEmotes(currentUser);
this->twitch.refresh(currentUser);
});
this->loadEmojis();
@@ -411,56 +400,6 @@ QString EmoteManager::replaceShortCodes(const QString &text)
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()
{
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)
{
// TODO: fix this xD