Remove authenticated get function from urlfetch

Implement twitch account emote getter function in TwitchAccount
This commit is contained in:
Rasmus Karlsson
2018-06-27 00:16:30 +00:00
parent d333da3dfa
commit f76512c31e
7 changed files with 86 additions and 52 deletions
+38
View File
@@ -256,4 +256,42 @@ std::set<TwitchUser> TwitchAccount::getIgnores() const
return this->ignores;
}
void TwitchAccount::loadEmotes(std::function<void(const rapidjson::Document &)> cb)
{
Log("Loading Twitch emotes for user {}", this->getUserName());
const auto &clientID = this->getOAuthClient();
const auto &oauthToken = this->getOAuthToken();
if (clientID.isEmpty() || oauthToken.isEmpty()) {
Log("Missing Client ID or OAuth token");
return;
}
QString url("https://api.twitch.tv/kraken/users/" + this->getUserId() + "/emotes");
NetworkRequest req(url);
req.setRequestType(NetworkRequest::GetRequest);
req.setCaller(QThread::currentThread());
req.makeAuthorizedV5(this->getOAuthClient(), this->getOAuthToken());
req.onError([=](int errorCode) {
Log("Error {}", errorCode);
if (errorCode == 203) {
// onFinished(FollowResult_NotFollowing);
} else {
// onFinished(FollowResult_Failed);
}
return true;
});
req.onSuccess([=](const rapidjson::Document &document) {
cb(document);
return true;
});
req.execute();
}
} // namespace chatterino
+5
View File
@@ -3,9 +3,12 @@
#include "controllers/accounts/Account.hpp"
#include "providers/twitch/TwitchUser.hpp"
#include <rapidjson/document.h>
#include <QColor>
#include <QString>
#include <functional>
#include <mutex>
#include <set>
namespace chatterino {
@@ -65,6 +68,8 @@ public:
std::set<TwitchUser> getIgnores() const;
void loadEmotes(std::function<void(const rapidjson::Document &)> cb);
QColor color;
private:
+31 -21
View File
@@ -142,17 +142,7 @@ EmoteData TwitchEmotes::getEmoteById(const QString &id, const QString &emoteName
void TwitchEmotes::refresh(const std::shared_ptr<TwitchAccount> &user)
{
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()) {
Log("Missing Client ID or OAuth token");
return;
}
TwitchAccountEmoteData &emoteData = this->emotes[roomID];
if (emoteData.filled) {
@@ -160,24 +150,44 @@ void TwitchEmotes::refresh(const std::shared_ptr<TwitchAccount> &user)
return;
}
QString url("https://api.twitch.tv/kraken/users/" + roomID + "/emotes");
auto loadEmotes = [=, &emoteData](const QJsonObject &root) {
auto loadEmotes = [=, &emoteData](const rapidjson::Document &root) {
emoteData.emoteSets.clear();
emoteData.emoteCodes.clear();
auto emoticonSets = root.value("emoticon_sets").toObject();
for (QJsonObject::iterator it = emoticonSets.begin(); it != emoticonSets.end(); ++it) {
auto emoticonSets = root.FindMember("emoticon_sets");
if (emoticonSets == root.MemberEnd() || !emoticonSets->value.IsObject()) {
Log("No emoticon_sets in load emotes response");
return;
}
for (const auto &emoteSetJSON : emoticonSets->value.GetObject()) {
auto emoteSet = std::make_shared<EmoteSet>();
emoteSet->key = it.key();
emoteSet->key = emoteSetJSON.name.GetString();
loadSetData(emoteSet);
for (QJsonValue emoteValue : it.value().toArray()) {
QJsonObject emoticon = emoteValue.toObject();
QString id = QString::number(emoticon["id"].toInt());
QString code = emoticon["code"].toString();
for (const rapidjson::Value &emoteJSON : emoteSetJSON.value.GetArray()) {
if (!emoteJSON.IsObject()) {
Log("Emote value was invalid");
return;
}
QString id, code;
uint64_t idNumber;
if (!rj::getSafe(emoteJSON, "id", idNumber)) {
Log("No ID key found in Emote value");
return;
}
if (!rj::getSafe(emoteJSON, "code", code)) {
Log("No code key found in Emote value");
return;
}
id = QString::number(idNumber);
auto cleanCode = cleanUpCode(code);
emoteSet->emotes.emplace_back(id, cleanCode);
@@ -193,7 +203,7 @@ void TwitchEmotes::refresh(const std::shared_ptr<TwitchAccount> &user)
emoteData.filled = true;
};
twitchApiGetAuthorized(url, clientID, oauthToken, QThread::currentThread(), loadEmotes);
user->loadEmotes(loadEmotes);
}
void TwitchEmotes::loadSetData(std::shared_ptr<TwitchEmotes::EmoteSet> emoteSet)