Migrated block, unblock and get user block list methods to Helix (#2370)
Co-authored-by: pajlada <rasmus.karlsson@pajlada.com>
This commit is contained in:
@@ -7,7 +7,9 @@
|
||||
#include "common/NetworkRequest.hpp"
|
||||
#include "common/Outcome.hpp"
|
||||
#include "common/QLogging.hpp"
|
||||
#include "controllers/accounts/AccountController.hpp"
|
||||
#include "providers/twitch/TwitchCommon.hpp"
|
||||
#include "providers/twitch/TwitchUser.hpp"
|
||||
#include "providers/twitch/api/Helix.hpp"
|
||||
#include "singletons/Emotes.hpp"
|
||||
#include "util/RapidjsonHelpers.hpp"
|
||||
@@ -89,189 +91,60 @@ bool TwitchAccount::isAnon() const
|
||||
return this->isAnon_;
|
||||
}
|
||||
|
||||
void TwitchAccount::loadIgnores()
|
||||
void TwitchAccount::loadBlocks()
|
||||
{
|
||||
QString url("https://api.twitch.tv/kraken/users/" + this->getUserId() +
|
||||
"/blocks");
|
||||
getHelix()->loadBlocks(
|
||||
getApp()->accounts->twitch.getCurrent()->userId_,
|
||||
[this](std::vector<HelixBlock> blocks) {
|
||||
std::lock_guard<std::mutex> lock(this->ignoresMutex_);
|
||||
this->ignores_.clear();
|
||||
|
||||
NetworkRequest(url)
|
||||
|
||||
.authorizeTwitchV5(this->getOAuthClient(), this->getOAuthToken())
|
||||
.onSuccess([=](auto result) -> Outcome {
|
||||
auto document = result.parseRapidJson();
|
||||
if (!document.IsObject())
|
||||
for (const HelixBlock &block : blocks)
|
||||
{
|
||||
return Failure;
|
||||
TwitchUser blockedUser;
|
||||
blockedUser.fromHelixBlock(block);
|
||||
this->ignores_.insert(blockedUser);
|
||||
}
|
||||
|
||||
auto blocksIt = document.FindMember("blocks");
|
||||
if (blocksIt == document.MemberEnd())
|
||||
{
|
||||
return Failure;
|
||||
}
|
||||
const auto &blocks = blocksIt->value;
|
||||
|
||||
if (!blocks.IsArray())
|
||||
{
|
||||
return Failure;
|
||||
}
|
||||
|
||||
{
|
||||
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))
|
||||
{
|
||||
qCWarning(chatterinoTwitch)
|
||||
<< "Error parsing twitch user JSON"
|
||||
<< rj::stringify(userIt->value).c_str();
|
||||
continue;
|
||||
}
|
||||
|
||||
this->ignores_.insert(ignoredUser);
|
||||
}
|
||||
}
|
||||
|
||||
return Success;
|
||||
})
|
||||
.execute();
|
||||
},
|
||||
[] {
|
||||
qDebug() << "Fetching blocks failed!";
|
||||
});
|
||||
}
|
||||
|
||||
void TwitchAccount::ignore(
|
||||
const QString &targetName,
|
||||
std::function<void(IgnoreResult, const QString &)> onFinished)
|
||||
void TwitchAccount::blockUser(QString userId, std::function<void()> onSuccess,
|
||||
std::function<void()> onFailure)
|
||||
{
|
||||
const auto onUserFetched = [this, targetName,
|
||||
onFinished](const auto &user) {
|
||||
this->ignoreByID(user.id, targetName, onFinished);
|
||||
};
|
||||
|
||||
const auto onUserFetchFailed = [] {};
|
||||
|
||||
getHelix()->getUserByName(targetName, onUserFetched, onUserFetchFailed);
|
||||
}
|
||||
|
||||
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(url, NetworkRequestType::Put)
|
||||
|
||||
.authorizeTwitchV5(this->getOAuthClient(), this->getOAuthToken())
|
||||
.onError([=](NetworkResult result) {
|
||||
onFinished(IgnoreResult_Failed,
|
||||
QString("An unknown error occurred while trying to "
|
||||
"ignore user %1 (%2)")
|
||||
.arg(targetName)
|
||||
.arg(result.status()));
|
||||
})
|
||||
.onSuccess([=](auto result) -> Outcome {
|
||||
auto document = result.parseRapidJson();
|
||||
if (!document.IsObject())
|
||||
{
|
||||
onFinished(IgnoreResult_Failed,
|
||||
"Bad JSON data while ignoring user " + targetName);
|
||||
return Failure;
|
||||
}
|
||||
|
||||
auto userIt = document.FindMember("user");
|
||||
if (userIt == document.MemberEnd())
|
||||
{
|
||||
onFinished(IgnoreResult_Failed,
|
||||
"Bad JSON data while ignoring user (missing user) " +
|
||||
targetName);
|
||||
return Failure;
|
||||
}
|
||||
|
||||
TwitchUser ignoredUser;
|
||||
if (!rj::getSafe(userIt->value, ignoredUser))
|
||||
{
|
||||
onFinished(IgnoreResult_Failed,
|
||||
"Bad JSON data while ignoring user (invalid user) " +
|
||||
targetName);
|
||||
return Failure;
|
||||
}
|
||||
getHelix()->blockUser(
|
||||
userId,
|
||||
[this, userId, onSuccess] {
|
||||
TwitchUser blockedUser;
|
||||
blockedUser.id = userId;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(this->ignoresMutex_);
|
||||
|
||||
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;
|
||||
}
|
||||
this->ignores_.insert(blockedUser);
|
||||
}
|
||||
onFinished(IgnoreResult_Success,
|
||||
"Successfully ignored user " + targetName);
|
||||
|
||||
return Success;
|
||||
})
|
||||
.execute();
|
||||
onSuccess();
|
||||
},
|
||||
onFailure);
|
||||
}
|
||||
|
||||
void TwitchAccount::unignore(
|
||||
const QString &targetName,
|
||||
std::function<void(UnignoreResult, const QString &message)> onFinished)
|
||||
void TwitchAccount::unblockUser(QString userId, std::function<void()> onSuccess,
|
||||
std::function<void()> onFailure)
|
||||
{
|
||||
const auto onUserFetched = [this, targetName,
|
||||
onFinished](const auto &user) {
|
||||
this->unignoreByID(user.id, targetName, onFinished);
|
||||
};
|
||||
|
||||
const auto onUserFetchFailed = [] {};
|
||||
|
||||
getHelix()->getUserByName(targetName, onUserFetched, onUserFetchFailed);
|
||||
}
|
||||
|
||||
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(url, NetworkRequestType::Delete)
|
||||
|
||||
.authorizeTwitchV5(this->getOAuthClient(), this->getOAuthToken())
|
||||
.onError([=](NetworkResult result) {
|
||||
onFinished(
|
||||
UnignoreResult_Failed,
|
||||
"An unknown error occurred while trying to unignore user " +
|
||||
targetName + " (" + QString::number(result.status()) + ")");
|
||||
})
|
||||
.onSuccess([=](auto result) -> Outcome {
|
||||
auto document = result.parseRapidJson();
|
||||
getHelix()->unblockUser(
|
||||
userId,
|
||||
[this, userId, onSuccess] {
|
||||
TwitchUser ignoredUser;
|
||||
ignoredUser.id = targetUserID;
|
||||
ignoredUser.id = userId;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(this->ignoresMutex_);
|
||||
|
||||
this->ignores_.erase(ignoredUser);
|
||||
}
|
||||
onFinished(UnignoreResult_Success,
|
||||
"Successfully unignored user " + targetName);
|
||||
|
||||
return Success;
|
||||
})
|
||||
.execute();
|
||||
onSuccess();
|
||||
},
|
||||
onFailure);
|
||||
}
|
||||
|
||||
void TwitchAccount::checkFollow(const QString targetUserID,
|
||||
@@ -291,7 +164,7 @@ void TwitchAccount::checkFollow(const QString targetUserID,
|
||||
[] {});
|
||||
}
|
||||
|
||||
std::set<TwitchUser> TwitchAccount::getIgnores() const
|
||||
std::set<TwitchUser> TwitchAccount::getBlocks() const
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(this->ignoresMutex_);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user