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

571 lines
15 KiB
C++
Raw Normal View History

2018-06-26 14:09:39 +02:00
#include "providers/twitch/TwitchAccount.hpp"
2018-08-02 14:23:27 +02:00
#include <QThread>
#include "Application.hpp"
2018-06-26 15:33:51 +02:00
#include "common/NetworkRequest.hpp"
#include "common/Outcome.hpp"
2018-06-26 17:20:03 +02:00
#include "debug/Log.hpp"
#include "providers/twitch/PartialTwitchUser.hpp"
#include "providers/twitch/TwitchCommon.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-08-02 14:23:27 +02:00
namespace {
2018-08-15 22:46:20 +02:00
EmoteName cleanUpCode(const EmoteName &dirtyEmoteCode)
{
auto cleanCode = dirtyEmoteCode.string;
cleanCode.detach();
static QMap<QString, QString> emoteNameReplacements{
{"[oO](_|\\.)[oO]", "O_o"}, {"\\&gt\\;\\(", "&gt;("},
{"\\&lt\\;3", "&lt;3"}, {"\\:-?(o|O)", ":O"},
{"\\:-?(p|P)", ":P"}, {"\\:-?[\\\\/]", ":/"},
{"\\:-?[z|Z|\\|]", ":Z"}, {"\\:-?\\(", ":("},
{"\\:-?\\)", ":)"}, {"\\:-?D", ":D"},
{"\\;-?(p|P)", ";P"}, {"\\;-?\\)", ";)"},
{"R-?\\)", "R)"}, {"B-?\\)", "B)"},
};
auto it = emoteNameReplacements.find(dirtyEmoteCode.string);
2018-10-21 13:43:02 +02:00
if (it != emoteNameReplacements.end())
{
2018-08-15 22:46:20 +02:00
cleanCode = it.value();
}
2018-08-02 14:23:27 +02:00
2018-08-15 22:46:20 +02:00
cleanCode.replace("&lt;", "<");
cleanCode.replace("&gt;", ">");
2018-08-02 14:23:27 +02:00
2018-08-15 22:46:20 +02:00
return {cleanCode};
}
2018-08-02 14:23:27 +02:00
} // namespace
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
}
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
}
void TwitchAccount::loadIgnores()
{
2018-08-06 21:17:03 +02:00
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());
2018-08-02 14:23:27 +02:00
req.onSuccess([=](auto result) -> Outcome {
auto document = result.parseRapidJson();
2018-10-21 13:43:02 +02:00
if (!document.IsObject())
{
2018-08-02 14:23:27 +02:00
return Failure;
}
auto blocksIt = document.FindMember("blocks");
2018-10-21 13:43:02 +02:00
if (blocksIt == document.MemberEnd())
{
2018-08-02 14:23:27 +02:00
return Failure;
}
const auto &blocks = blocksIt->value;
2018-10-21 13:43:02 +02:00
if (!blocks.IsArray())
{
2018-08-02 14:23:27 +02:00
return Failure;
}
{
2018-07-06 19:23:47 +02:00
std::lock_guard<std::mutex> lock(this->ignoresMutex_);
this->ignores_.clear();
2018-10-21 13:43:02 +02:00
for (const auto &block : blocks.GetArray())
{
if (!block.IsObject())
{
continue;
}
auto userIt = block.FindMember("user");
2018-10-21 13:43:02 +02:00
if (userIt == block.MemberEnd())
{
continue;
}
TwitchUser ignoredUser;
2018-10-21 13:43:02 +02:00
if (!rj::getSafe(userIt->value, ignoredUser))
{
2018-08-11 14:20:53 +02:00
log("Error parsing twitch user JSON {}",
2018-08-06 21:17:03 +02:00
rj::stringify(userIt->value));
continue;
}
2018-07-06 19:23:47 +02:00
this->ignores_.insert(ignoredUser);
}
}
2018-08-02 14:23:27 +02:00
return Success;
});
req.execute();
}
2018-08-06 21:17:03 +02:00
void TwitchAccount::ignore(
const QString &targetName,
std::function<void(IgnoreResult, const QString &)> onFinished)
{
2018-08-06 21:17:03 +02:00
const auto onIdFetched = [this, targetName,
onFinished](QString targetUserId) {
this->ignoreByID(targetUserId, targetName, onFinished); //
};
PartialTwitchUser::byName(targetName).getId(onIdFetched);
}
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-08-06 21:17:03 +02:00
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) {
2018-08-06 21:17:03 +02:00
onFinished(IgnoreResult_Failed,
"An unknown error occured while trying to ignore user " +
targetName + " (" + QString::number(errorCode) + ")");
return true;
});
2018-08-02 14:23:27 +02:00
req.onSuccess([=](auto result) -> Outcome {
auto document = result.parseRapidJson();
2018-10-21 13:43:02 +02:00
if (!document.IsObject())
{
2018-08-06 21:17:03 +02:00
onFinished(IgnoreResult_Failed,
"Bad JSON data while ignoring user " + targetName);
2018-08-02 14:23:27 +02:00
return Failure;
}
auto userIt = document.FindMember("user");
2018-10-21 13:43:02 +02:00
if (userIt == document.MemberEnd())
{
onFinished(IgnoreResult_Failed,
2018-08-06 21:17:03 +02:00
"Bad JSON data while ignoring user (missing user) " +
targetName);
2018-08-02 14:23:27 +02:00
return Failure;
}
TwitchUser ignoredUser;
2018-10-21 13:43:02 +02:00
if (!rj::getSafe(userIt->value, ignoredUser))
{
onFinished(IgnoreResult_Failed,
2018-08-06 21:17:03 +02:00
"Bad JSON data while ignoring user (invalid user) " +
targetName);
2018-08-02 14:23:27 +02:00
return Failure;
}
{
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);
2018-10-21 13:43:02 +02:00
if (!res.second)
{
const TwitchUser &existingUser = *(res.first);
existingUser.update(ignoredUser);
onFinished(IgnoreResult_AlreadyIgnored,
"User " + targetName + " is already ignored");
2018-08-02 14:23:27 +02:00
return Failure;
}
}
2018-08-06 21:17:03 +02:00
onFinished(IgnoreResult_Success,
"Successfully ignored user " + targetName);
2018-08-02 14:23:27 +02:00
return Success;
});
req.execute();
}
2018-08-06 21:17:03 +02:00
void TwitchAccount::unignore(
const QString &targetName,
std::function<void(UnignoreResult, const QString &message)> onFinished)
{
2018-08-06 21:17:03 +02:00
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)
{
2018-08-06 21:17:03 +02:00
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,
2018-08-06 21:17:03 +02:00
"An unknown error occured while trying to unignore user " +
targetName + " (" + QString::number(errorCode) + ")");
return true;
});
2018-08-02 14:23:27 +02:00
req.onSuccess([=](auto result) -> Outcome {
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);
}
2018-08-06 21:17:03 +02:00
onFinished(UnignoreResult_Success,
"Successfully unignored user " + targetName);
2018-08-02 14:23:27 +02:00
return Success;
});
req.execute();
}
void TwitchAccount::checkFollow(const QString targetUserID,
std::function<void(FollowResult)> onFinished)
{
2018-08-06 21:17:03 +02:00
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) {
2018-10-21 13:43:02 +02:00
if (errorCode == 203)
{
onFinished(FollowResult_NotFollowing);
2018-10-21 13:43:02 +02:00
}
else
{
onFinished(FollowResult_Failed);
}
return true;
});
2018-08-02 14:23:27 +02:00
req.onSuccess([=](auto result) -> Outcome {
auto document = result.parseRapidJson();
onFinished(FollowResult_Following);
2018-08-02 14:23:27 +02:00
return Success;
});
req.execute();
}
2018-08-06 21:17:03 +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
2018-08-02 14:23:27 +02:00
request.onSuccess([successCallback](auto result) -> Outcome {
successCallback();
2018-08-02 14:23:27 +02:00
return Success;
});
request.execute();
}
2018-08-06 21:17:03 +02:00
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) {
2018-10-21 13:43:02 +02:00
if (code >= 200 && code <= 299)
{
successCallback();
}
return true;
});
2018-08-02 14:23:27 +02:00
request.onSuccess([successCallback](const auto &document) -> Outcome {
successCallback();
2018-08-02 14:23:27 +02:00
return Success;
});
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_;
}
2018-08-02 14:23:27 +02:00
void TwitchAccount::loadEmotes()
{
2018-08-11 14:20:53 +02:00
log("Loading Twitch emotes for user {}", this->getUserName());
const auto &clientID = this->getOAuthClient();
const auto &oauthToken = this->getOAuthToken();
2018-10-21 13:43:02 +02:00
if (clientID.isEmpty() || oauthToken.isEmpty())
{
2018-08-11 14:20:53 +02:00
log("Missing Client ID or OAuth token");
return;
}
2018-08-06 21:17:03 +02:00
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-08-11 14:20:53 +02:00
log("[TwitchAccount::loadEmotes] Error {}", errorCode);
2018-10-21 13:43:02 +02:00
if (errorCode == 203)
{
// onFinished(FollowResult_NotFollowing);
2018-10-21 13:43:02 +02:00
}
else
{
// onFinished(FollowResult_Failed);
}
return true;
});
2018-08-02 14:23:27 +02:00
req.onSuccess([=](auto result) -> Outcome {
this->parseEmotes(result.parseRapidJson());
return Success;
});
req.execute();
}
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();
}
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())
{
2018-08-11 14:20:53 +02:00
log("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())
{
2018-08-11 14:20:53 +02:00
log("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))
{
2018-08-11 14:20:53 +02:00
log("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))
{
2018-08-11 14:20:53 +02:00
log("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)};
auto cleanCode = cleanUpCode(code);
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);
}
emoteData->emoteSets.emplace_back(emoteSet);
}
};
void TwitchAccount::loadEmoteSetData(std::shared_ptr<EmoteSet> emoteSet)
{
2018-10-21 13:43:02 +02:00
if (!emoteSet)
{
2018-08-11 14:20:53 +02:00
log("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;
}
2018-08-06 21:17:03 +02:00
NetworkRequest req(
"https://braize.pajlada.com/chatterino/twitchemotes/set/" +
emoteSet->key + "/");
2018-08-02 14:23:27 +02:00
req.setUseQuickLoadCache(true);
req.onError([](int errorCode) -> bool {
2018-08-11 14:20:53 +02:00
log("Error code {} while loading emote set data", errorCode);
return true;
});
2018-08-02 14:23:27 +02:00
req.onSuccess([emoteSet](auto result) -> Outcome {
auto root = result.parseRapidJson();
2018-10-21 13:43:02 +02:00
if (!root.IsObject())
{
2018-08-02 14:23:27 +02:00
return Failure;
}
std::string emoteSetID;
QString channelName;
QString type;
2018-10-21 13:43:02 +02:00
if (!rj::getSafe(root, "channel_name", channelName))
{
2018-08-02 14:23:27 +02:00
return Failure;
}
2018-10-21 13:43:02 +02:00
if (!rj::getSafe(root, "type", type))
{
2018-08-02 14:23:27 +02:00
return Failure;
}
2018-08-11 14:20:53 +02:00
log("Loaded twitch emote set data for {}!", emoteSet->key);
2018-08-02 14:23:27 +02:00
2018-08-11 14:20:53 +02:00
auto name = channelName;
name.detach();
name[0] = name[0].toUpper();
emoteSet->text = name;
2018-08-02 14:23:27 +02:00
2018-08-11 14:20:53 +02:00
emoteSet->type = type;
2018-08-02 14:23:27 +02:00
emoteSet->channelName = channelName;
return Success;
});
req.execute();
}
2018-02-05 15:11:50 +01:00
} // namespace chatterino