twitch emotes are now refreshed properly (sub and global emotes)

this only works if you're logged in I think
This commit is contained in:
Rasmus Karlsson 2017-12-22 17:37:24 +01:00
parent afd4549c3f
commit 51fe00dfec
2 changed files with 40 additions and 43 deletions

View file

@ -24,11 +24,12 @@ namespace chatterino {
EmoteManager::EmoteManager() EmoteManager::EmoteManager()
: findShortCodesRegex(":([-+\\w]+):") : findShortCodesRegex(":([-+\\w]+):")
{ {
pajlada::Settings::Setting<std::string> roomID( auto &accountManager = AccountManager::getInstance();
"/accounts/current/roomID", "", pajlada::Settings::SettingOption::DoNotWriteToJSON);
roomID.getValueChangedSignal().connect([this](const std::string &roomID, auto) { accountManager.Twitch.userChanged.connect([this] {
this->refreshTwitchEmotes(roomID); // auto currentUser = AccountManager::getInstance().Twitch.getCurrent();
assert(currentUser);
this->refreshTwitchEmotes(currentUser);
}); });
} }
@ -354,38 +355,31 @@ QString EmoteManager::replaceShortCodes(const QString &text)
return ret; return ret;
} }
void EmoteManager::refreshTwitchEmotes(const std::string &roomID) void EmoteManager::refreshTwitchEmotes(const std::shared_ptr<twitch::TwitchUser> &user)
{ {
std::string oauthClient = debug::Log("Loading Twitch emotes for user {}", user->getNickName());
pajlada::Settings::Setting<std::string>::get("/accounts/" + roomID + "/oauthClient");
std::string oauthToken =
pajlada::Settings::Setting<std::string>::get("/accounts/" + roomID + "/oauthToken");
TwitchAccountEmoteData &emoteData = this->twitchAccountEmotes[roomID]; 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) { if (emoteData.filled) {
qDebug() << "Already loaded for room id " << qS(roomID); qDebug() << "Already loaded for room id " << roomID;
return; return;
} }
qDebug() << "Loading emotes for room id " << qS(roomID); QString url("https://api.twitch.tv/kraken/users/" + roomID + "/emotes");
if (oauthClient.empty() || oauthToken.empty()) { util::twitch::getAuthorized(
qDebug() << "Missing oauth client/token"; url, clientID, oauthToken, QThread::currentThread(),
return; [=, &emoteData](QJsonObject &root) {
}
// api:v5
QString url("https://api.twitch.tv/kraken/users/" + qS(roomID) +
"/emotes?api_version=5&oauth_token=" + qS(oauthToken) +
"&client_id=" + qS(oauthClient));
qDebug() << url;
util::NetworkRequest req(url);
req.setCaller(QThread::currentThread());
req.setTimeout(3000);
req.getJSON([=, &emoteData](QJsonObject &root) {
emoteData.emoteSets.clear(); emoteData.emoteSets.clear();
emoteData.emoteCodes.clear(); emoteData.emoteCodes.clear();
auto emoticonSets = root.value("emoticon_sets").toObject(); auto emoticonSets = root.value("emoticon_sets").toObject();
@ -403,7 +397,9 @@ void EmoteManager::refreshTwitchEmotes(const std::string &roomID)
} }
emoteData.filled = true; emoteData.filled = true;
}); }
);
} }
void EmoteManager::loadBTTVEmotes() void EmoteManager::loadBTTVEmotes()

View file

@ -7,6 +7,7 @@
#include "messages/lazyloadedimage.hpp" #include "messages/lazyloadedimage.hpp"
#include "signalvector.hpp" #include "signalvector.hpp"
#include "twitch/emotevalue.hpp" #include "twitch/emotevalue.hpp"
#include "twitch/twitchuser.hpp"
#include <QMap> #include <QMap>
#include <QMutex> #include <QMutex>
@ -100,7 +101,7 @@ public:
std::vector<std::string> emojiShortCodes; std::vector<std::string> emojiShortCodes;
/// Twitch emotes /// Twitch emotes
void refreshTwitchEmotes(const std::string &roomID); void refreshTwitchEmotes(const std::shared_ptr<twitch::TwitchUser> &user);
struct TwitchAccountEmoteData { struct TwitchAccountEmoteData {
struct TwitchEmote { struct TwitchEmote {