2018-06-26 14:09:39 +02:00
|
|
|
#include "providers/twitch/TwitchAccount.hpp"
|
2018-05-12 20:34:13 +02:00
|
|
|
|
2018-06-26 15:33:51 +02:00
|
|
|
#include "common/NetworkRequest.hpp"
|
2018-06-26 17:20:03 +02:00
|
|
|
#include "debug/Log.hpp"
|
2018-07-07 13:08:57 +02:00
|
|
|
#include "providers/twitch/PartialTwitchUser.hpp"
|
2018-06-26 17:25:24 +02:00
|
|
|
#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-03-31 13:44:15 +02:00
|
|
|
|
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
|
|
|
}
|
2018-03-31 13:44:15 +02:00
|
|
|
|
2018-05-12 20:34:13 +02: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);
|
2018-05-12 20:34:13 +02:00
|
|
|
req.setCaller(QThread::currentThread());
|
|
|
|
req.makeAuthorizedV5(this->getOAuthClient(), this->getOAuthToken());
|
2018-07-07 13:08:57 +02:00
|
|
|
req.onSuccess([=](auto result) {
|
|
|
|
auto document = result.parseRapidJson();
|
2018-05-12 20:34:13 +02:00
|
|
|
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();
|
2018-05-12 20:34:13 +02:00
|
|
|
|
|
|
|
for (const auto &block : blocks.GetArray()) {
|
|
|
|
if (!block.IsObject()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
auto userIt = block.FindMember("user");
|
|
|
|
if (userIt == block.MemberEnd()) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-06-27 02:17:05 +02:00
|
|
|
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);
|
2018-05-12 20:34:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
req.execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TwitchAccount::ignore(const QString &targetName,
|
2018-05-13 17:53:24 +02:00
|
|
|
std::function<void(IgnoreResult, const QString &)> onFinished)
|
2018-05-12 20:34:13 +02:00
|
|
|
{
|
2018-07-07 13:08:57 +02:00
|
|
|
const auto onIdFetched = [this, targetName, onFinished](QString targetUserId) {
|
|
|
|
this->ignoreByID(targetUserId, targetName, onFinished); //
|
|
|
|
};
|
|
|
|
|
2018-07-13 22:23:03 +02:00
|
|
|
PartialTwitchUser::byName(targetName).getId(onIdFetched);
|
2018-05-13 17:53:24 +02:00
|
|
|
}
|
2018-05-12 20:34:13 +02:00
|
|
|
|
2018-05-13 17:53:24 +02:00
|
|
|
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);
|
2018-07-07 13:08:57 +02:00
|
|
|
NetworkRequest req(url, NetworkRequestType::Put);
|
2018-05-13 17:53:24 +02:00
|
|
|
req.setCaller(QThread::currentThread());
|
|
|
|
req.makeAuthorizedV5(this->getOAuthClient(), this->getOAuthToken());
|
2018-05-12 20:34:13 +02:00
|
|
|
|
2018-05-13 17:53:24 +02:00
|
|
|
req.onError([=](int errorCode) {
|
|
|
|
onFinished(IgnoreResult_Failed, "An unknown error occured while trying to ignore user " +
|
|
|
|
targetName + " (" + QString::number(errorCode) + ")");
|
2018-05-12 20:34:13 +02:00
|
|
|
|
2018-05-13 17:53:24 +02:00
|
|
|
return true;
|
|
|
|
});
|
2018-05-12 20:34:13 +02:00
|
|
|
|
2018-07-07 13:08:57 +02:00
|
|
|
req.onSuccess([=](auto result) {
|
|
|
|
auto document = result.parseRapidJson();
|
2018-05-13 17:53:24 +02:00
|
|
|
if (!document.IsObject()) {
|
|
|
|
onFinished(IgnoreResult_Failed, "Bad JSON data while ignoring user " + targetName);
|
|
|
|
return false;
|
|
|
|
}
|
2018-05-12 20:34:13 +02:00
|
|
|
|
2018-05-13 17:53:24 +02:00
|
|
|
auto userIt = document.FindMember("user");
|
|
|
|
if (userIt == document.MemberEnd()) {
|
|
|
|
onFinished(IgnoreResult_Failed,
|
|
|
|
"Bad JSON data while ignoring user (missing user) " + targetName);
|
|
|
|
return false;
|
|
|
|
}
|
2018-05-12 20:34:13 +02:00
|
|
|
|
2018-06-27 02:17:05 +02:00
|
|
|
TwitchUser ignoredUser;
|
|
|
|
if (!rj::getSafe(userIt->value, ignoredUser)) {
|
|
|
|
onFinished(IgnoreResult_Failed,
|
|
|
|
"Bad JSON data while ignoring user (invalid user) " + targetName);
|
|
|
|
return false;
|
|
|
|
}
|
2018-05-13 17:53:24 +02:00
|
|
|
{
|
2018-07-06 19:23:47 +02:00
|
|
|
std::lock_guard<std::mutex> lock(this->ignoresMutex_);
|
2018-05-12 20:34:13 +02:00
|
|
|
|
2018-07-06 19:23:47 +02:00
|
|
|
auto res = this->ignores_.insert(ignoredUser);
|
2018-05-13 17:53:24 +02:00
|
|
|
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);
|
2018-05-12 20:34:13 +02:00
|
|
|
|
2018-05-13 17:53:24 +02:00
|
|
|
return true;
|
2018-05-12 20:34:13 +02:00
|
|
|
});
|
2018-05-13 17:53:24 +02:00
|
|
|
|
|
|
|
req.execute();
|
2018-05-13 17:53:24 +02:00
|
|
|
}
|
2018-05-12 20:34:13 +02:00
|
|
|
|
|
|
|
void TwitchAccount::unignore(const QString &targetName,
|
2018-05-13 17:53:24 +02:00
|
|
|
std::function<void(UnignoreResult, const QString &message)> onFinished)
|
2018-05-12 20:34:13 +02:00
|
|
|
{
|
2018-07-07 13:08:57 +02:00
|
|
|
const auto onIdFetched = [this, targetName, onFinished](QString targetUserId) {
|
|
|
|
this->unignoreByID(targetUserId, targetName, onFinished); //
|
|
|
|
};
|
|
|
|
|
2018-07-13 22:23:03 +02:00
|
|
|
PartialTwitchUser::byName(targetName).getId(onIdFetched);
|
2018-05-13 17:53:24 +02:00
|
|
|
}
|
2018-05-12 20:34:13 +02:00
|
|
|
|
2018-05-13 17:53:24 +02:00
|
|
|
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);
|
2018-05-12 20:34:13 +02:00
|
|
|
|
2018-07-07 13:08:57 +02:00
|
|
|
NetworkRequest req(url, NetworkRequestType::Delete);
|
2018-05-13 17:53:24 +02:00
|
|
|
req.setCaller(QThread::currentThread());
|
|
|
|
req.makeAuthorizedV5(this->getOAuthClient(), this->getOAuthToken());
|
2018-05-12 20:34:13 +02:00
|
|
|
|
2018-05-13 17:53:24 +02:00
|
|
|
req.onError([=](int errorCode) {
|
|
|
|
onFinished(UnignoreResult_Failed,
|
|
|
|
"An unknown error occured while trying to unignore user " + targetName + " (" +
|
|
|
|
QString::number(errorCode) + ")");
|
2018-05-12 20:34:13 +02:00
|
|
|
|
2018-05-13 17:53:24 +02:00
|
|
|
return true;
|
|
|
|
});
|
2018-05-12 20:34:13 +02:00
|
|
|
|
2018-07-07 13:08:57 +02:00
|
|
|
req.onSuccess([=](auto result) {
|
|
|
|
auto document = result.parseRapidJson();
|
2018-05-13 17:53:24 +02:00
|
|
|
TwitchUser ignoredUser;
|
|
|
|
ignoredUser.id = targetUserID;
|
|
|
|
{
|
2018-07-06 19:23:47 +02:00
|
|
|
std::lock_guard<std::mutex> lock(this->ignoresMutex_);
|
2018-05-13 17:53:24 +02:00
|
|
|
|
2018-07-06 19:23:47 +02:00
|
|
|
this->ignores_.erase(ignoredUser);
|
2018-05-13 17:53:24 +02:00
|
|
|
}
|
|
|
|
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);
|
2018-05-13 17:53:24 +02:00
|
|
|
req.setCaller(QThread::currentThread());
|
|
|
|
req.makeAuthorizedV5(this->getOAuthClient(), this->getOAuthToken());
|
2018-05-12 20:34:13 +02:00
|
|
|
|
2018-05-13 17:53:24 +02:00
|
|
|
req.onError([=](int errorCode) {
|
|
|
|
if (errorCode == 203) {
|
|
|
|
onFinished(FollowResult_NotFollowing);
|
|
|
|
} else {
|
|
|
|
onFinished(FollowResult_Failed);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
});
|
2018-05-12 20:34:13 +02:00
|
|
|
|
2018-07-07 13:08:57 +02:00
|
|
|
req.onSuccess([=](auto result) {
|
|
|
|
auto document = result.parseRapidJson();
|
2018-05-13 17:53:24 +02:00
|
|
|
onFinished(FollowResult_Following);
|
|
|
|
return true;
|
2018-05-12 20:34:13 +02:00
|
|
|
});
|
2018-05-13 17:53:24 +02:00
|
|
|
|
|
|
|
req.execute();
|
2018-05-12 20:34:13 +02:00
|
|
|
}
|
|
|
|
|
2018-07-07 13:08:57 +02:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2018-05-12 20:34:13 +02:00
|
|
|
std::set<TwitchUser> TwitchAccount::getIgnores() const
|
|
|
|
{
|
2018-07-06 19:23:47 +02:00
|
|
|
std::lock_guard<std::mutex> lock(this->ignoresMutex_);
|
2018-05-12 20:34:13 +02:00
|
|
|
|
2018-07-06 19:23:47 +02:00
|
|
|
return this->ignores_;
|
2018-05-12 20:34:13 +02:00
|
|
|
}
|
|
|
|
|
2018-06-27 02:16:30 +02:00
|
|
|
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);
|
2018-06-27 02:16:30 +02:00
|
|
|
if (errorCode == 203) {
|
|
|
|
// onFinished(FollowResult_NotFollowing);
|
|
|
|
} else {
|
|
|
|
// onFinished(FollowResult_Failed);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
2018-07-07 13:08:57 +02:00
|
|
|
req.onSuccess([=](auto result) {
|
|
|
|
cb(result.parseRapidJson());
|
2018-06-27 02:16:30 +02:00
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
req.execute();
|
|
|
|
}
|
|
|
|
|
2018-02-05 15:11:50 +01:00
|
|
|
} // namespace chatterino
|