mirror-chatterino2/src/providers/twitch/TwitchAccount.cpp

358 lines
10 KiB
C++
Raw Normal View History

2018-06-26 14:09:39 +02:00
#include "providers/twitch/TwitchAccount.hpp"
2018-06-26 15:33:51 +02:00
#include "common/NetworkRequest.hpp"
2018-06-26 17:20:03 +02:00
#include "debug/Log.hpp"
#include "providers/twitch/PartialTwitchUser.hpp"
#include "providers/twitch/TwitchCommon.hpp"
2018-06-26 17:20:03 +02:00
#include "util/RapidjsonHelpers.hpp"
2018-02-05 15:11:50 +01:00
namespace chatterino {
2018-07-06 19:23:47 +02:00
TwitchAccount::TwitchAccount(const QString &username, const QString &oauthToken,
const QString &oauthClient, const QString &userID)
2018-06-26 17:06:17 +02:00
: Account(ProviderId::Twitch)
2018-07-06 19:23:47 +02:00
, oauthClient_(oauthClient)
, oauthToken_(oauthToken)
, userName_(username)
, userId_(userID)
, isAnon_(username == ANONYMOUS_USERNAME)
2018-02-05 15:11:50 +01:00
{
}
2018-05-06 12:52:47 +02:00
QString TwitchAccount::toString() const
{
return this->getUserName();
}
2018-02-05 15:11:50 +01:00
const QString &TwitchAccount::getUserName() const
{
2018-07-06 19:23:47 +02:00
return this->userName_;
2018-02-05 15:11:50 +01:00
}
const QString &TwitchAccount::getOAuthClient() const
{
2018-07-06 19:23:47 +02:00
return this->oauthClient_;
2018-02-05 15:11:50 +01:00
}
const QString &TwitchAccount::getOAuthToken() const
{
2018-07-06 19:23:47 +02:00
return this->oauthToken_;
2018-02-05 15:11:50 +01:00
}
const QString &TwitchAccount::getUserId() const
{
2018-07-06 19:23:47 +02:00
return this->userId_;
2018-02-05 15:11:50 +01:00
}
bool TwitchAccount::setOAuthClient(const QString &newClientID)
{
2018-07-06 19:23:47 +02:00
if (this->oauthClient_.compare(newClientID) == 0) {
2018-02-05 15:11:50 +01:00
return false;
}
2018-07-06 19:23:47 +02:00
this->oauthClient_ = newClientID;
2018-02-05 15:11:50 +01:00
return true;
}
bool TwitchAccount::setOAuthToken(const QString &newOAuthToken)
{
2018-07-06 19:23:47 +02:00
if (this->oauthToken_.compare(newOAuthToken) == 0) {
2018-02-05 15:11:50 +01:00
return false;
}
2018-07-06 19:23:47 +02:00
this->oauthToken_ = newOAuthToken;
2018-02-05 15:11:50 +01:00
return true;
}
bool TwitchAccount::isAnon() const
{
2018-07-06 19:23:47 +02:00
return this->isAnon_;
2018-02-05 15:11:50 +01:00
}
void TwitchAccount::loadIgnores()
{
QString url("https://api.twitch.tv/kraken/users/" + this->getUserId() + "/blocks");
2018-06-26 17:06:17 +02:00
NetworkRequest req(url);
req.setCaller(QThread::currentThread());
req.makeAuthorizedV5(this->getOAuthClient(), this->getOAuthToken());
req.onSuccess([=](auto result) {
auto document = result.parseRapidJson();
if (!document.IsObject()) {
return false;
}
auto blocksIt = document.FindMember("blocks");
if (blocksIt == document.MemberEnd()) {
return false;
}
const auto &blocks = blocksIt->value;
if (!blocks.IsArray()) {
return false;
}
{
2018-07-06 19:23:47 +02:00
std::lock_guard<std::mutex> lock(this->ignoresMutex_);
this->ignores_.clear();
for (const auto &block : blocks.GetArray()) {
if (!block.IsObject()) {
continue;
}
auto userIt = block.FindMember("user");
if (userIt == block.MemberEnd()) {
continue;
}
TwitchUser ignoredUser;
if (!rj::getSafe(userIt->value, ignoredUser)) {
Log("Error parsing twitch user JSON {}", rj::stringify(userIt->value));
continue;
}
2018-07-06 19:23:47 +02:00
this->ignores_.insert(ignoredUser);
}
}
return true;
});
req.execute();
}
void TwitchAccount::ignore(const QString &targetName,
std::function<void(IgnoreResult, const QString &)> onFinished)
{
const auto onIdFetched = [this, targetName, onFinished](QString targetUserId) {
this->ignoreByID(targetUserId, targetName, onFinished); //
};
PartialTwitchUser::byName(targetName).getId(onIdFetched);
}
void TwitchAccount::ignoreByID(const QString &targetUserID, const QString &targetName,
std::function<void(IgnoreResult, const QString &)> onFinished)
{
QString url("https://api.twitch.tv/kraken/users/" + this->getUserId() + "/blocks/" +
targetUserID);
NetworkRequest req(url, NetworkRequestType::Put);
req.setCaller(QThread::currentThread());
req.makeAuthorizedV5(this->getOAuthClient(), this->getOAuthToken());
req.onError([=](int errorCode) {
onFinished(IgnoreResult_Failed, "An unknown error occured while trying to ignore user " +
targetName + " (" + QString::number(errorCode) + ")");
return true;
});
req.onSuccess([=](auto result) {
auto document = result.parseRapidJson();
if (!document.IsObject()) {
onFinished(IgnoreResult_Failed, "Bad JSON data while ignoring user " + targetName);
return false;
}
auto userIt = document.FindMember("user");
if (userIt == document.MemberEnd()) {
onFinished(IgnoreResult_Failed,
"Bad JSON data while ignoring user (missing user) " + targetName);
return false;
}
TwitchUser ignoredUser;
if (!rj::getSafe(userIt->value, ignoredUser)) {
onFinished(IgnoreResult_Failed,
"Bad JSON data while ignoring user (invalid user) " + targetName);
return false;
}
{
2018-07-06 19:23:47 +02:00
std::lock_guard<std::mutex> lock(this->ignoresMutex_);
2018-07-06 19:23:47 +02:00
auto res = this->ignores_.insert(ignoredUser);
if (!res.second) {
const TwitchUser &existingUser = *(res.first);
existingUser.update(ignoredUser);
onFinished(IgnoreResult_AlreadyIgnored,
"User " + targetName + " is already ignored");
return false;
}
}
onFinished(IgnoreResult_Success, "Successfully ignored user " + targetName);
return true;
});
req.execute();
}
void TwitchAccount::unignore(const QString &targetName,
std::function<void(UnignoreResult, const QString &message)> onFinished)
{
const auto onIdFetched = [this, targetName, onFinished](QString targetUserId) {
this->unignoreByID(targetUserId, targetName, onFinished); //
};
PartialTwitchUser::byName(targetName).getId(onIdFetched);
}
void TwitchAccount::unignoreByID(
const QString &targetUserID, const QString &targetName,
std::function<void(UnignoreResult, const QString &message)> onFinished)
{
QString url("https://api.twitch.tv/kraken/users/" + this->getUserId() + "/blocks/" +
targetUserID);
NetworkRequest req(url, NetworkRequestType::Delete);
req.setCaller(QThread::currentThread());
req.makeAuthorizedV5(this->getOAuthClient(), this->getOAuthToken());
req.onError([=](int errorCode) {
onFinished(UnignoreResult_Failed,
"An unknown error occured while trying to unignore user " + targetName + " (" +
QString::number(errorCode) + ")");
return true;
});
req.onSuccess([=](auto result) {
auto document = result.parseRapidJson();
TwitchUser ignoredUser;
ignoredUser.id = targetUserID;
{
2018-07-06 19:23:47 +02:00
std::lock_guard<std::mutex> lock(this->ignoresMutex_);
2018-07-06 19:23:47 +02:00
this->ignores_.erase(ignoredUser);
}
onFinished(UnignoreResult_Success, "Successfully unignored user " + targetName);
return true;
});
req.execute();
}
void TwitchAccount::checkFollow(const QString targetUserID,
std::function<void(FollowResult)> onFinished)
{
QString url("https://api.twitch.tv/kraken/users/" + this->getUserId() + "/follows/channels/" +
targetUserID);
2018-06-26 17:06:17 +02:00
NetworkRequest req(url);
req.setCaller(QThread::currentThread());
req.makeAuthorizedV5(this->getOAuthClient(), this->getOAuthToken());
req.onError([=](int errorCode) {
if (errorCode == 203) {
onFinished(FollowResult_NotFollowing);
} else {
onFinished(FollowResult_Failed);
}
return true;
});
req.onSuccess([=](auto result) {
auto document = result.parseRapidJson();
onFinished(FollowResult_Following);
return true;
});
req.execute();
}
void TwitchAccount::followUser(const QString userID, std::function<void()> successCallback)
{
QUrl requestUrl("https://api.twitch.tv/kraken/users/" + this->getUserId() +
"/follows/channels/" + userID);
NetworkRequest request(requestUrl, NetworkRequestType::Put);
request.setCaller(QThread::currentThread());
request.makeAuthorizedV5(this->getOAuthClient(), this->getOAuthToken());
// TODO: Properly check result of follow request
request.onSuccess([successCallback](auto result) {
successCallback();
return true;
});
request.execute();
}
void TwitchAccount::unfollowUser(const QString userID, std::function<void()> successCallback)
{
QUrl requestUrl("https://api.twitch.tv/kraken/users/" + this->getUserId() +
"/follows/channels/" + userID);
NetworkRequest request(requestUrl, NetworkRequestType::Delete);
request.setCaller(QThread::currentThread());
request.makeAuthorizedV5(this->getOAuthClient(), this->getOAuthToken());
request.onError([successCallback](int code) {
if (code >= 200 && code <= 299) {
successCallback();
}
return true;
});
request.onSuccess([successCallback](const auto &document) {
successCallback();
return true;
});
request.execute();
}
std::set<TwitchUser> TwitchAccount::getIgnores() const
{
2018-07-06 19:23:47 +02:00
std::lock_guard<std::mutex> lock(this->ignoresMutex_);
2018-07-06 19:23:47 +02:00
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.setCaller(QThread::currentThread());
req.makeAuthorizedV5(this->getOAuthClient(), this->getOAuthToken());
req.onError([=](int errorCode) {
2018-07-07 21:59:17 +02:00
Log("[TwitchAccount::loadEmotes] Error {}", errorCode);
if (errorCode == 203) {
// onFinished(FollowResult_NotFollowing);
} else {
// onFinished(FollowResult_Failed);
}
return true;
});
req.onSuccess([=](auto result) {
cb(result.parseRapidJson());
return true;
});
req.execute();
}
2018-02-05 15:11:50 +01:00
} // namespace chatterino