2018-06-26 14:09:39 +02:00
|
|
|
#include "providers/twitch/TwitchAccount.hpp"
|
2018-05-12 20:34:13 +02:00
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
#include <QThread>
|
|
|
|
|
|
|
|
#include "Application.hpp"
|
2019-05-26 21:44:13 +02:00
|
|
|
#include "common/Env.hpp"
|
2018-06-26 15:33:51 +02:00
|
|
|
#include "common/NetworkRequest.hpp"
|
2018-08-11 22:23:06 +02:00
|
|
|
#include "common/Outcome.hpp"
|
2018-06-26 17:25:24 +02:00
|
|
|
#include "providers/twitch/TwitchCommon.hpp"
|
2020-03-14 12:13:57 +01:00
|
|
|
#include "providers/twitch/api/Helix.hpp"
|
2018-08-02 14:23:27 +02:00
|
|
|
#include "singletons/Emotes.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
|
|
|
}
|
|
|
|
|
2018-08-11 17:35:46 +02:00
|
|
|
QColor TwitchAccount::color()
|
|
|
|
{
|
|
|
|
return this->color_.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TwitchAccount::setColor(QColor color)
|
|
|
|
{
|
|
|
|
this->color_.set(color);
|
|
|
|
}
|
|
|
|
|
2018-02-05 15:11:50 +01:00
|
|
|
bool TwitchAccount::setOAuthClient(const QString &newClientID)
|
|
|
|
{
|
2018-10-21 13:43:02 +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-10-21 13:43:02 +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()
|
|
|
|
{
|
2018-08-06 21:17:03 +02:00
|
|
|
QString url("https://api.twitch.tv/kraken/users/" + this->getUserId() +
|
|
|
|
"/blocks");
|
2018-05-12 20:34:13 +02:00
|
|
|
|
2019-08-20 21:50:36 +02:00
|
|
|
NetworkRequest(url)
|
2019-09-09 15:21:49 +02:00
|
|
|
|
2019-08-20 21:50:36 +02:00
|
|
|
.authorizeTwitchV5(this->getOAuthClient(), this->getOAuthToken())
|
|
|
|
.onSuccess([=](auto result) -> Outcome {
|
|
|
|
auto document = result.parseRapidJson();
|
|
|
|
if (!document.IsObject())
|
|
|
|
{
|
|
|
|
return Failure;
|
|
|
|
}
|
2018-05-12 20:34:13 +02:00
|
|
|
|
2019-08-20 21:50:36 +02:00
|
|
|
auto blocksIt = document.FindMember("blocks");
|
|
|
|
if (blocksIt == document.MemberEnd())
|
|
|
|
{
|
|
|
|
return Failure;
|
|
|
|
}
|
|
|
|
const auto &blocks = blocksIt->value;
|
2018-05-12 20:34:13 +02:00
|
|
|
|
2019-08-20 21:50:36 +02:00
|
|
|
if (!blocks.IsArray())
|
|
|
|
{
|
|
|
|
return Failure;
|
|
|
|
}
|
2018-05-12 20:34:13 +02:00
|
|
|
|
2018-10-21 13:43:02 +02:00
|
|
|
{
|
2019-08-20 21:50:36 +02:00
|
|
|
std::lock_guard<std::mutex> lock(this->ignoresMutex_);
|
|
|
|
this->ignores_.clear();
|
|
|
|
|
|
|
|
for (const auto &block : blocks.GetArray())
|
2018-10-21 13:43:02 +02:00
|
|
|
{
|
2019-08-20 21:50:36 +02:00
|
|
|
if (!block.IsObject())
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
auto userIt = block.FindMember("user");
|
|
|
|
if (userIt == block.MemberEnd())
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
TwitchUser ignoredUser;
|
|
|
|
if (!rj::getSafe(userIt->value, ignoredUser))
|
|
|
|
{
|
2020-01-03 20:51:37 +01:00
|
|
|
qDebug() << "Error parsing twitch user JSON"
|
|
|
|
<< rj::stringify(userIt->value).c_str();
|
2019-08-20 21:50:36 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->ignores_.insert(ignoredUser);
|
2018-06-27 02:17:05 +02:00
|
|
|
}
|
2018-05-12 20:34:13 +02:00
|
|
|
}
|
|
|
|
|
2019-08-20 21:50:36 +02:00
|
|
|
return Success;
|
|
|
|
})
|
|
|
|
.execute();
|
2018-05-12 20:34:13 +02:00
|
|
|
}
|
|
|
|
|
2018-08-06 21:17:03 +02:00
|
|
|
void TwitchAccount::ignore(
|
|
|
|
const QString &targetName,
|
|
|
|
std::function<void(IgnoreResult, const QString &)> onFinished)
|
2018-05-12 20:34:13 +02:00
|
|
|
{
|
2020-03-14 12:13:57 +01:00
|
|
|
const auto onUserFetched = [this, targetName,
|
|
|
|
onFinished](const auto &user) {
|
2020-11-08 12:02:19 +01:00
|
|
|
this->ignoreByID(user.id, targetName, onFinished);
|
2018-07-07 13:08:57 +02:00
|
|
|
};
|
|
|
|
|
2020-03-14 12:13:57 +01:00
|
|
|
const auto onUserFetchFailed = [] {};
|
|
|
|
|
|
|
|
getHelix()->getUserByName(targetName, onUserFetched, onUserFetchFailed);
|
2018-05-13 17:53:24 +02:00
|
|
|
}
|
2018-05-12 20:34:13 +02:00
|
|
|
|
2018-08-06 21:17:03 +02:00
|
|
|
void TwitchAccount::ignoreByID(
|
|
|
|
const QString &targetUserID, const QString &targetName,
|
|
|
|
std::function<void(IgnoreResult, const QString &)> onFinished)
|
2018-05-13 17:53:24 +02:00
|
|
|
{
|
2018-08-06 21:17:03 +02:00
|
|
|
QString url("https://api.twitch.tv/kraken/users/" + this->getUserId() +
|
|
|
|
"/blocks/" + targetUserID);
|
2018-05-12 20:34:13 +02:00
|
|
|
|
2019-08-20 21:50:36 +02:00
|
|
|
NetworkRequest(url, NetworkRequestType::Put)
|
2019-09-09 15:21:49 +02:00
|
|
|
|
2019-08-20 21:50:36 +02:00
|
|
|
.authorizeTwitchV5(this->getOAuthClient(), this->getOAuthToken())
|
2019-09-19 19:03:50 +02:00
|
|
|
.onError([=](NetworkResult result) {
|
2018-05-13 17:53:24 +02:00
|
|
|
onFinished(IgnoreResult_Failed,
|
2019-08-20 21:50:36 +02:00
|
|
|
"An unknown error occured while trying to ignore user " +
|
2019-09-19 19:03:50 +02:00
|
|
|
targetName + " (" +
|
|
|
|
QString::number(result.status()) + ")");
|
2019-08-20 21:50:36 +02:00
|
|
|
})
|
|
|
|
.onSuccess([=](auto result) -> Outcome {
|
|
|
|
auto document = result.parseRapidJson();
|
|
|
|
if (!document.IsObject())
|
|
|
|
{
|
|
|
|
onFinished(IgnoreResult_Failed,
|
|
|
|
"Bad JSON data while ignoring user " + targetName);
|
|
|
|
return Failure;
|
|
|
|
}
|
2018-05-12 20:34:13 +02:00
|
|
|
|
2019-08-20 21:50:36 +02:00
|
|
|
auto userIt = document.FindMember("user");
|
|
|
|
if (userIt == document.MemberEnd())
|
|
|
|
{
|
|
|
|
onFinished(IgnoreResult_Failed,
|
|
|
|
"Bad JSON data while ignoring user (missing user) " +
|
|
|
|
targetName);
|
|
|
|
return Failure;
|
|
|
|
}
|
2018-05-12 20:34:13 +02:00
|
|
|
|
2019-08-20 21:50:36 +02:00
|
|
|
TwitchUser ignoredUser;
|
|
|
|
if (!rj::getSafe(userIt->value, ignoredUser))
|
2018-10-21 13:43:02 +02:00
|
|
|
{
|
2019-08-20 21:50:36 +02:00
|
|
|
onFinished(IgnoreResult_Failed,
|
|
|
|
"Bad JSON data while ignoring user (invalid user) " +
|
|
|
|
targetName);
|
2018-08-02 14:23:27 +02:00
|
|
|
return Failure;
|
2018-05-13 17:53:24 +02:00
|
|
|
}
|
2019-08-20 21:50:36 +02:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(this->ignoresMutex_);
|
2018-05-12 20:34:13 +02:00
|
|
|
|
2019-08-20 21:50:36 +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 Failure;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
onFinished(IgnoreResult_Success,
|
|
|
|
"Successfully ignored user " + targetName);
|
2018-05-13 17:53:24 +02:00
|
|
|
|
2019-08-20 21:50:36 +02:00
|
|
|
return Success;
|
|
|
|
})
|
|
|
|
.execute();
|
2018-05-13 17:53:24 +02:00
|
|
|
}
|
2018-05-12 20:34:13 +02:00
|
|
|
|
2018-08-06 21:17:03 +02:00
|
|
|
void TwitchAccount::unignore(
|
|
|
|
const QString &targetName,
|
|
|
|
std::function<void(UnignoreResult, const QString &message)> onFinished)
|
2018-05-12 20:34:13 +02:00
|
|
|
{
|
2020-03-14 12:13:57 +01:00
|
|
|
const auto onUserFetched = [this, targetName,
|
|
|
|
onFinished](const auto &user) {
|
2020-11-08 12:02:19 +01:00
|
|
|
this->unignoreByID(user.id, targetName, onFinished);
|
2018-07-07 13:08:57 +02:00
|
|
|
};
|
|
|
|
|
2020-03-14 12:13:57 +01:00
|
|
|
const auto onUserFetchFailed = [] {};
|
|
|
|
|
|
|
|
getHelix()->getUserByName(targetName, onUserFetched, onUserFetchFailed);
|
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)
|
|
|
|
{
|
2018-08-06 21:17:03 +02:00
|
|
|
QString url("https://api.twitch.tv/kraken/users/" + this->getUserId() +
|
|
|
|
"/blocks/" + targetUserID);
|
2018-05-12 20:34:13 +02:00
|
|
|
|
2019-08-20 21:50:36 +02:00
|
|
|
NetworkRequest(url, NetworkRequestType::Delete)
|
2019-09-09 15:21:49 +02:00
|
|
|
|
2019-08-20 21:50:36 +02:00
|
|
|
.authorizeTwitchV5(this->getOAuthClient(), this->getOAuthToken())
|
2019-09-19 19:03:50 +02:00
|
|
|
.onError([=](NetworkResult result) {
|
2019-08-20 21:50:36 +02:00
|
|
|
onFinished(
|
|
|
|
UnignoreResult_Failed,
|
|
|
|
"An unknown error occured while trying to unignore user " +
|
2019-09-19 19:03:50 +02:00
|
|
|
targetName + " (" + QString::number(result.status()) + ")");
|
2019-08-20 21:50:36 +02:00
|
|
|
})
|
|
|
|
.onSuccess([=](auto result) -> Outcome {
|
|
|
|
auto document = result.parseRapidJson();
|
|
|
|
TwitchUser ignoredUser;
|
|
|
|
ignoredUser.id = targetUserID;
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(this->ignoresMutex_);
|
2018-05-13 17:53:24 +02:00
|
|
|
|
2019-08-20 21:50:36 +02:00
|
|
|
this->ignores_.erase(ignoredUser);
|
|
|
|
}
|
|
|
|
onFinished(UnignoreResult_Success,
|
|
|
|
"Successfully unignored user " + targetName);
|
2018-05-13 17:53:24 +02:00
|
|
|
|
2019-08-20 21:50:36 +02:00
|
|
|
return Success;
|
|
|
|
})
|
|
|
|
.execute();
|
2018-05-13 17:53:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void TwitchAccount::checkFollow(const QString targetUserID,
|
|
|
|
std::function<void(FollowResult)> onFinished)
|
|
|
|
{
|
2020-03-14 12:13:57 +01:00
|
|
|
const auto onResponse = [onFinished](bool following, const auto &record) {
|
|
|
|
if (!following)
|
|
|
|
{
|
|
|
|
onFinished(FollowResult_NotFollowing);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
onFinished(FollowResult_Following);
|
|
|
|
};
|
2019-09-09 15:21:49 +02:00
|
|
|
|
2020-03-14 12:13:57 +01:00
|
|
|
getHelix()->getUserFollow(this->getUserId(), targetUserID, onResponse,
|
|
|
|
[] {});
|
2018-05-12 20:34:13 +02:00
|
|
|
}
|
|
|
|
|
2018-08-06 21:17:03 +02:00
|
|
|
void TwitchAccount::followUser(const QString userID,
|
|
|
|
std::function<void()> successCallback)
|
2018-07-07 13:08:57 +02:00
|
|
|
{
|
|
|
|
QUrl requestUrl("https://api.twitch.tv/kraken/users/" + this->getUserId() +
|
|
|
|
"/follows/channels/" + userID);
|
|
|
|
|
2019-08-20 21:50:36 +02:00
|
|
|
NetworkRequest(requestUrl, NetworkRequestType::Put)
|
2019-09-09 15:21:49 +02:00
|
|
|
|
2019-08-20 21:50:36 +02:00
|
|
|
.authorizeTwitchV5(this->getOAuthClient(), this->getOAuthToken())
|
|
|
|
.onSuccess([successCallback](auto result) -> Outcome {
|
|
|
|
// TODO: Properly check result of follow request
|
|
|
|
successCallback();
|
2018-07-07 13:08:57 +02:00
|
|
|
|
2019-08-20 21:50:36 +02:00
|
|
|
return Success;
|
|
|
|
})
|
|
|
|
.execute();
|
2018-07-07 13:08:57 +02:00
|
|
|
}
|
|
|
|
|
2018-08-06 21:17:03 +02:00
|
|
|
void TwitchAccount::unfollowUser(const QString userID,
|
|
|
|
std::function<void()> successCallback)
|
2018-07-07 13:08:57 +02:00
|
|
|
{
|
|
|
|
QUrl requestUrl("https://api.twitch.tv/kraken/users/" + this->getUserId() +
|
|
|
|
"/follows/channels/" + userID);
|
|
|
|
|
2019-08-20 21:50:36 +02:00
|
|
|
NetworkRequest(requestUrl, NetworkRequestType::Delete)
|
2019-09-09 15:21:49 +02:00
|
|
|
|
2019-08-20 21:50:36 +02:00
|
|
|
.authorizeTwitchV5(this->getOAuthClient(), this->getOAuthToken())
|
2019-09-19 19:03:50 +02:00
|
|
|
.onError([successCallback](NetworkResult result) {
|
|
|
|
if (result.status() >= 200 && result.status() <= 299)
|
2019-08-20 21:50:36 +02:00
|
|
|
{
|
|
|
|
successCallback();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.onSuccess([successCallback](const auto &document) -> Outcome {
|
2018-07-07 13:08:57 +02:00
|
|
|
successCallback();
|
|
|
|
|
2019-08-20 21:50:36 +02:00
|
|
|
return Success;
|
|
|
|
})
|
|
|
|
.execute();
|
2018-07-07 13:08:57 +02:00
|
|
|
}
|
|
|
|
|
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-08-02 14:23:27 +02:00
|
|
|
void TwitchAccount::loadEmotes()
|
2018-06-27 02:16:30 +02:00
|
|
|
{
|
2020-01-03 20:51:37 +01:00
|
|
|
qDebug() << "Loading Twitch emotes for user" << this->getUserName();
|
2018-06-27 02:16:30 +02:00
|
|
|
|
|
|
|
const auto &clientID = this->getOAuthClient();
|
|
|
|
const auto &oauthToken = this->getOAuthToken();
|
|
|
|
|
2018-10-21 13:43:02 +02:00
|
|
|
if (clientID.isEmpty() || oauthToken.isEmpty())
|
|
|
|
{
|
2020-01-03 20:51:37 +01:00
|
|
|
qDebug() << "Missing Client ID or OAuth token";
|
2018-06-27 02:16:30 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-06 21:17:03 +02:00
|
|
|
QString url("https://api.twitch.tv/kraken/users/" + this->getUserId() +
|
|
|
|
"/emotes");
|
2018-06-27 02:16:30 +02:00
|
|
|
|
2019-08-20 21:50:36 +02:00
|
|
|
NetworkRequest(url)
|
2019-09-09 15:21:49 +02:00
|
|
|
|
2019-08-20 21:50:36 +02:00
|
|
|
.authorizeTwitchV5(this->getOAuthClient(), this->getOAuthToken())
|
2019-09-19 19:03:50 +02:00
|
|
|
.onError([=](NetworkResult result) {
|
2020-01-03 20:51:37 +01:00
|
|
|
qDebug() << "[TwitchAccount::loadEmotes] Error" << result.status();
|
2019-09-19 19:03:50 +02:00
|
|
|
if (result.status() == 203)
|
2019-08-20 21:50:36 +02:00
|
|
|
{
|
|
|
|
// onFinished(FollowResult_NotFollowing);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// onFinished(FollowResult_Failed);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.onSuccess([=](auto result) -> Outcome {
|
|
|
|
this->parseEmotes(result.parseRapidJson());
|
2018-08-02 14:23:27 +02:00
|
|
|
|
2019-08-20 21:50:36 +02:00
|
|
|
return Success;
|
|
|
|
})
|
|
|
|
.execute();
|
2018-08-02 14:23:27 +02:00
|
|
|
}
|
|
|
|
|
2018-08-06 21:17:03 +02:00
|
|
|
AccessGuard<const TwitchAccount::TwitchAccountEmoteData>
|
2018-08-15 22:46:20 +02:00
|
|
|
TwitchAccount::accessEmotes() const
|
2018-08-02 14:23:27 +02:00
|
|
|
{
|
|
|
|
return this->emotes_.accessConst();
|
|
|
|
}
|
|
|
|
|
2019-01-20 14:45:59 +01:00
|
|
|
// AutoModActions
|
2019-01-20 16:07:31 +01:00
|
|
|
void TwitchAccount::autoModAllow(const QString msgID)
|
2019-01-20 14:47:04 +01:00
|
|
|
{
|
2019-01-20 16:07:31 +01:00
|
|
|
QString url("https://api.twitch.tv/kraken/chat/twitchbot/approve");
|
|
|
|
|
|
|
|
auto qba = (QString("{\"msg_id\":\"") + msgID + "\"}").toUtf8();
|
2019-01-20 17:03:45 +01:00
|
|
|
|
2019-08-20 21:50:36 +02:00
|
|
|
NetworkRequest(url, NetworkRequestType::Post)
|
|
|
|
.header("Content-Type", "application/json")
|
|
|
|
.header("Content-Length", QByteArray::number(qba.size()))
|
|
|
|
.payload(qba)
|
2019-09-09 15:21:49 +02:00
|
|
|
|
2019-08-20 21:50:36 +02:00
|
|
|
.authorizeTwitchV5(this->getOAuthClient(), this->getOAuthToken())
|
2019-09-19 19:03:50 +02:00
|
|
|
.onError([=](NetworkResult result) {
|
2020-01-03 20:51:37 +01:00
|
|
|
qDebug() << "[TwitchAccounts::autoModAllow] Error"
|
|
|
|
<< result.status();
|
2019-08-20 21:50:36 +02:00
|
|
|
})
|
|
|
|
.execute();
|
2019-01-20 14:47:04 +01:00
|
|
|
}
|
|
|
|
|
2019-01-20 16:07:31 +01:00
|
|
|
void TwitchAccount::autoModDeny(const QString msgID)
|
2019-01-20 14:47:04 +01:00
|
|
|
{
|
2019-01-20 16:07:31 +01:00
|
|
|
QString url("https://api.twitch.tv/kraken/chat/twitchbot/deny");
|
|
|
|
|
|
|
|
auto qba = (QString("{\"msg_id\":\"") + msgID + "\"}").toUtf8();
|
2019-01-20 17:03:45 +01:00
|
|
|
|
2019-08-20 21:50:36 +02:00
|
|
|
NetworkRequest(url, NetworkRequestType::Post)
|
|
|
|
.header("Content-Type", "application/json")
|
|
|
|
.header("Content-Length", QByteArray::number(qba.size()))
|
|
|
|
.payload(qba)
|
2019-09-09 15:21:49 +02:00
|
|
|
|
2019-08-20 21:50:36 +02:00
|
|
|
.authorizeTwitchV5(this->getOAuthClient(), this->getOAuthToken())
|
2019-09-19 19:03:50 +02:00
|
|
|
.onError([=](NetworkResult result) {
|
2020-01-03 20:51:37 +01:00
|
|
|
qDebug() << "[TwitchAccounts::autoModDeny] Error"
|
|
|
|
<< result.status();
|
2019-08-20 21:50:36 +02:00
|
|
|
})
|
|
|
|
.execute();
|
2019-01-20 14:47:04 +01:00
|
|
|
}
|
2019-01-20 14:45:59 +01:00
|
|
|
|
2018-08-02 14:23:27 +02:00
|
|
|
void TwitchAccount::parseEmotes(const rapidjson::Document &root)
|
|
|
|
{
|
|
|
|
auto emoteData = this->emotes_.access();
|
|
|
|
|
|
|
|
emoteData->emoteSets.clear();
|
|
|
|
emoteData->allEmoteNames.clear();
|
|
|
|
|
|
|
|
auto emoticonSets = root.FindMember("emoticon_sets");
|
2018-10-21 13:43:02 +02:00
|
|
|
if (emoticonSets == root.MemberEnd() || !emoticonSets->value.IsObject())
|
|
|
|
{
|
2020-01-03 20:51:37 +01:00
|
|
|
qDebug() << "No emoticon_sets in load emotes response";
|
2018-08-02 14:23:27 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-10-21 13:43:02 +02:00
|
|
|
for (const auto &emoteSetJSON : emoticonSets->value.GetObject())
|
|
|
|
{
|
2018-08-02 14:23:27 +02:00
|
|
|
auto emoteSet = std::make_shared<EmoteSet>();
|
|
|
|
|
|
|
|
emoteSet->key = emoteSetJSON.name.GetString();
|
|
|
|
|
|
|
|
this->loadEmoteSetData(emoteSet);
|
|
|
|
|
2018-10-21 13:43:02 +02:00
|
|
|
for (const rapidjson::Value &emoteJSON : emoteSetJSON.value.GetArray())
|
|
|
|
{
|
|
|
|
if (!emoteJSON.IsObject())
|
|
|
|
{
|
2020-01-03 20:51:37 +01:00
|
|
|
qDebug() << "Emote value was invalid";
|
2018-08-02 14:23:27 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t idNumber;
|
2018-10-21 13:43:02 +02:00
|
|
|
if (!rj::getSafe(emoteJSON, "id", idNumber))
|
|
|
|
{
|
2020-01-03 20:51:37 +01:00
|
|
|
qDebug() << "No ID key found in Emote value";
|
2018-08-02 14:23:27 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-07 06:25:58 +02:00
|
|
|
QString _code;
|
2018-10-21 13:43:02 +02:00
|
|
|
if (!rj::getSafe(emoteJSON, "code", _code))
|
|
|
|
{
|
2020-01-03 20:51:37 +01:00
|
|
|
qDebug() << "No code key found in Emote value";
|
2018-08-02 14:23:27 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-07 06:25:58 +02:00
|
|
|
auto code = EmoteName{_code};
|
2018-08-02 14:23:27 +02:00
|
|
|
auto id = EmoteId{QString::number(idNumber)};
|
|
|
|
|
2018-11-05 17:04:52 +01:00
|
|
|
auto cleanCode = EmoteName{TwitchEmotes::cleanUpEmoteCode(code)};
|
2018-08-02 14:23:27 +02:00
|
|
|
emoteSet->emotes.emplace_back(TwitchEmote{id, cleanCode});
|
|
|
|
emoteData->allEmoteNames.push_back(cleanCode);
|
|
|
|
|
|
|
|
auto emote = getApp()->emotes->twitch.getOrCreateEmote(id, code);
|
|
|
|
emoteData->emotes.emplace(code, emote);
|
|
|
|
}
|
|
|
|
|
2020-01-25 13:05:59 +01:00
|
|
|
std::sort(emoteSet->emotes.begin(), emoteSet->emotes.end(),
|
|
|
|
[](const TwitchEmote &l, const TwitchEmote &r) {
|
|
|
|
return l.name.string < r.name.string;
|
|
|
|
});
|
2018-08-02 14:23:27 +02:00
|
|
|
emoteData->emoteSets.emplace_back(emoteSet);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void TwitchAccount::loadEmoteSetData(std::shared_ptr<EmoteSet> emoteSet)
|
|
|
|
{
|
2018-10-21 13:43:02 +02:00
|
|
|
if (!emoteSet)
|
|
|
|
{
|
2020-01-03 20:51:37 +01:00
|
|
|
qDebug() << "null emote set sent";
|
2018-08-02 14:23:27 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto staticSetIt = this->staticEmoteSets.find(emoteSet->key);
|
2018-10-21 13:43:02 +02:00
|
|
|
if (staticSetIt != this->staticEmoteSets.end())
|
|
|
|
{
|
2018-08-02 14:23:27 +02:00
|
|
|
const auto &staticSet = staticSetIt->second;
|
|
|
|
emoteSet->channelName = staticSet.channelName;
|
|
|
|
emoteSet->text = staticSet.text;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-08-20 21:50:36 +02:00
|
|
|
NetworkRequest(Env::get().twitchEmoteSetResolverUrl.arg(emoteSet->key))
|
|
|
|
.cache()
|
2019-09-19 19:03:50 +02:00
|
|
|
.onError([](NetworkResult result) {
|
2020-01-03 20:51:37 +01:00
|
|
|
qDebug() << "Error code" << result.status()
|
|
|
|
<< "while loading emote set data";
|
2019-08-20 21:50:36 +02:00
|
|
|
})
|
|
|
|
.onSuccess([emoteSet](auto result) -> Outcome {
|
|
|
|
auto root = result.parseRapidJson();
|
|
|
|
if (!root.IsObject())
|
|
|
|
{
|
|
|
|
return Failure;
|
|
|
|
}
|
2018-08-02 14:23:27 +02:00
|
|
|
|
2019-08-20 21:50:36 +02:00
|
|
|
std::string emoteSetID;
|
|
|
|
QString channelName;
|
|
|
|
QString type;
|
|
|
|
if (!rj::getSafe(root, "channel_name", channelName))
|
|
|
|
{
|
|
|
|
return Failure;
|
|
|
|
}
|
2018-08-02 14:23:27 +02:00
|
|
|
|
2019-08-20 21:50:36 +02:00
|
|
|
if (!rj::getSafe(root, "type", type))
|
|
|
|
{
|
|
|
|
return Failure;
|
|
|
|
}
|
2018-08-02 14:23:27 +02:00
|
|
|
|
2020-01-03 20:51:37 +01:00
|
|
|
qDebug() << "Loaded twitch emote set data for" << emoteSet->key;
|
2018-08-11 14:20:53 +02:00
|
|
|
|
2019-08-20 21:50:36 +02:00
|
|
|
auto name = channelName;
|
|
|
|
name.detach();
|
|
|
|
name[0] = name[0].toUpper();
|
2018-08-02 14:23:27 +02:00
|
|
|
|
2019-08-20 21:50:36 +02:00
|
|
|
emoteSet->text = name;
|
2018-08-02 14:23:27 +02:00
|
|
|
|
2019-08-20 21:50:36 +02:00
|
|
|
emoteSet->type = type;
|
|
|
|
emoteSet->channelName = channelName;
|
2018-08-02 14:23:27 +02:00
|
|
|
|
2019-08-20 21:50:36 +02:00
|
|
|
return Success;
|
|
|
|
})
|
|
|
|
.execute();
|
2018-06-27 02:16:30 +02:00
|
|
|
}
|
|
|
|
|
2018-02-05 15:11:50 +01:00
|
|
|
} // namespace chatterino
|