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:11:45 +02:00
|
|
|
#include "providers/twitch/Const.hpp"
|
2018-06-26 14:09:39 +02:00
|
|
|
#include "debug/Log.hpp"
|
|
|
|
#include "util/NetworkRequest.hpp"
|
|
|
|
#include "util/RapidjsonHelpers.hpp"
|
|
|
|
#include "util/UrlFetch.hpp"
|
2018-02-05 15:11:50 +01:00
|
|
|
|
|
|
|
namespace chatterino {
|
|
|
|
namespace providers {
|
|
|
|
namespace twitch {
|
2018-03-31 13:44:15 +02:00
|
|
|
|
2018-02-05 15:11:50 +01:00
|
|
|
TwitchAccount::TwitchAccount(const QString &_username, const QString &_oauthToken,
|
2018-04-15 15:09:31 +02:00
|
|
|
const QString &_oauthClient, const QString &_userID)
|
2018-05-28 08:34:54 +02:00
|
|
|
: controllers::accounts::Account(ProviderId::Twitch)
|
2018-05-06 12:52:47 +02:00
|
|
|
, oauthClient(_oauthClient)
|
2018-02-05 15:11:50 +01:00
|
|
|
, oauthToken(_oauthToken)
|
2018-03-31 13:44:15 +02:00
|
|
|
, userName(_username)
|
2018-04-15 15:09:31 +02:00
|
|
|
, userId(_userID)
|
2018-02-05 15:11:50 +01:00
|
|
|
, _isAnon(_username == ANONYMOUS_USERNAME)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
return this->userName;
|
|
|
|
}
|
|
|
|
|
|
|
|
const QString &TwitchAccount::getOAuthClient() const
|
|
|
|
{
|
|
|
|
return this->oauthClient;
|
|
|
|
}
|
|
|
|
|
|
|
|
const QString &TwitchAccount::getOAuthToken() const
|
|
|
|
{
|
|
|
|
return this->oauthToken;
|
|
|
|
}
|
|
|
|
|
|
|
|
const QString &TwitchAccount::getUserId() const
|
|
|
|
{
|
|
|
|
return this->userId;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TwitchAccount::setOAuthClient(const QString &newClientID)
|
|
|
|
{
|
|
|
|
if (this->oauthClient.compare(newClientID) == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->oauthClient = newClientID;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TwitchAccount::setOAuthToken(const QString &newOAuthToken)
|
|
|
|
{
|
|
|
|
if (this->oauthToken.compare(newOAuthToken) == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->oauthToken = newOAuthToken;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TwitchAccount::isAnon() const
|
|
|
|
{
|
|
|
|
return this->_isAnon;
|
|
|
|
}
|
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");
|
|
|
|
|
|
|
|
util::NetworkRequest req(url);
|
2018-05-12 19:50:22 +02:00
|
|
|
req.setRequestType(util::NetworkRequest::GetRequest);
|
2018-05-12 20:34:13 +02:00
|
|
|
req.setCaller(QThread::currentThread());
|
|
|
|
req.makeAuthorizedV5(this->getOAuthClient(), this->getOAuthToken());
|
|
|
|
req.onSuccess([=](const rapidjson::Document &document) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
this->ignores.insert(TwitchUser::fromJSON(userIt->value));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
{
|
|
|
|
util::twitch::getUserID(targetName, QThread::currentThread(), [=](QString targetUserID) {
|
2018-05-13 17:53:24 +02:00
|
|
|
this->ignoreByID(targetUserID, targetName, onFinished); //
|
|
|
|
});
|
|
|
|
}
|
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-05-12 20:34:13 +02:00
|
|
|
|
2018-05-13 17:53:24 +02:00
|
|
|
util::NetworkRequest req(url);
|
|
|
|
req.setRequestType(util::NetworkRequest::PutRequest);
|
|
|
|
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-05-13 17:53:24 +02:00
|
|
|
req.onSuccess([=](const rapidjson::Document &document) {
|
|
|
|
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-05-13 17:53:24 +02:00
|
|
|
auto ignoredUser = TwitchUser::fromJSON(userIt->value);
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(this->ignoresMutex);
|
2018-05-12 20:34:13 +02:00
|
|
|
|
2018-05-13 17:53:24 +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);
|
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
|
|
|
{
|
|
|
|
util::twitch::getUserID(targetName, QThread::currentThread(), [=](QString targetUserID) {
|
2018-05-13 17:53:24 +02:00
|
|
|
this->unignoreByID(targetUserID, targetName, onFinished); //
|
|
|
|
});
|
|
|
|
}
|
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-05-13 17:53:24 +02:00
|
|
|
util::NetworkRequest req(url);
|
|
|
|
req.setRequestType(util::NetworkRequest::DeleteRequest);
|
|
|
|
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-05-13 17:53:24 +02:00
|
|
|
req.onSuccess([=](const rapidjson::Document &document) {
|
|
|
|
TwitchUser ignoredUser;
|
|
|
|
ignoredUser.id = targetUserID;
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(this->ignoresMutex);
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
util::NetworkRequest req(url);
|
|
|
|
req.setRequestType(util::NetworkRequest::GetRequest);
|
|
|
|
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-05-13 17:53:24 +02:00
|
|
|
req.onSuccess([=](const rapidjson::Document &document) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
std::set<TwitchUser> TwitchAccount::getIgnores() const
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(this->ignoresMutex);
|
|
|
|
|
|
|
|
return this->ignores;
|
|
|
|
}
|
|
|
|
|
2018-02-05 15:11:50 +01:00
|
|
|
} // namespace twitch
|
|
|
|
} // namespace providers
|
|
|
|
} // namespace chatterino
|