Revert "Added result code to ignore/unignore calls"

This reverts commit 7bc63ba38f.
This commit is contained in:
apa420 2018-05-14 01:23:02 +02:00
parent 40ea585175
commit a2101bc762
6 changed files with 117 additions and 255 deletions

View file

@ -130,7 +130,7 @@ QString CommandController::execCommand(const QString &text, ChannelPtr channel,
return ""; return "";
} }
user->ignore(target, [channel](auto resultCode, const QString &message) { user->ignore(target, [channel](const QString &message) {
channel->addMessage(messages::Message::createSystemMessage(message)); channel->addMessage(messages::Message::createSystemMessage(message));
}); });
@ -147,7 +147,7 @@ QString CommandController::execCommand(const QString &text, ChannelPtr channel,
return ""; return "";
} }
user->unignore(target, [channel](auto resultCode, const QString &message) { user->unignore(target, [channel](const QString &message) {
channel->addMessage(messages::Message::createSystemMessage(message)); channel->addMessage(messages::Message::createSystemMessage(message));
}); });

View file

@ -3,7 +3,6 @@
#include "const.hpp" #include "const.hpp"
#include "debug/log.hpp" #include "debug/log.hpp"
#include "util/networkrequest.hpp" #include "util/networkrequest.hpp"
#include "util/rapidjson-helpers.hpp"
#include "util/urlfetch.hpp" #include "util/urlfetch.hpp"
namespace chatterino { namespace chatterino {
@ -119,138 +118,93 @@ void TwitchAccount::loadIgnores()
} }
void TwitchAccount::ignore(const QString &targetName, void TwitchAccount::ignore(const QString &targetName,
std::function<void(IgnoreResult, const QString &)> onFinished) std::function<void(const QString &message)> onFinished)
{ {
util::twitch::getUserID(targetName, QThread::currentThread(), [=](QString targetUserID) { util::twitch::getUserID(targetName, QThread::currentThread(), [=](QString targetUserID) {
this->ignoreByID(targetUserID, targetName, onFinished); // QString url("https://api.twitch.tv/kraken/users/" + this->getUserId() + "/blocks/" +
}); targetUserID);
}
void TwitchAccount::ignoreByID(const QString &targetUserID, const QString &targetName, util::NetworkRequest req(url);
std::function<void(IgnoreResult, const QString &)> onFinished) req.setRequestType(util::NetworkRequest::PutRequest);
{ req.setCaller(QThread::currentThread());
QString url("https://api.twitch.tv/kraken/users/" + this->getUserId() + "/blocks/" + req.makeAuthorizedV5(this->getOAuthClient(), this->getOAuthToken());
targetUserID);
util::NetworkRequest req(url); req.onError([=](int errorCode) {
req.setRequestType(util::NetworkRequest::PutRequest); onFinished("An unknown error occured while trying to ignore user " + targetName + " (" +
req.setCaller(QThread::currentThread()); QString::number(errorCode) + ")");
req.makeAuthorizedV5(this->getOAuthClient(), this->getOAuthToken());
req.onError([=](int errorCode) { return true;
onFinished(IgnoreResult_Failed, "An unknown error occured while trying to ignore user " + });
targetName + " (" + QString::number(errorCode) + ")");
return true; req.onSuccess([=](const rapidjson::Document &document) {
}); if (!document.IsObject()) {
onFinished("Bad JSON data while ignoring user " + targetName);
req.onSuccess([=](const rapidjson::Document &document) {
if (!document.IsObject()) {
onFinished(IgnoreResult_Failed, "Bad JSON data while ignoring user " + targetName);
return false;
}
auto userIt = document.FindMember("user");
if (userIt == document.MemberEnd()) {
onFinished(IgnoreResult_Failed,
"Bad JSON data while ignoring user (missing user) " + targetName);
return false;
}
auto ignoredUser = TwitchUser::fromJSON(userIt->value);
{
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 false; return false;
} }
}
onFinished(IgnoreResult_Success, "Successfully ignored user " + targetName);
return true; auto userIt = document.FindMember("user");
if (userIt == document.MemberEnd()) {
onFinished("Bad JSON data while ignoring user (missing user) " + targetName);
return false;
}
auto ignoredUser = TwitchUser::fromJSON(userIt->value);
{
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("User " + targetName + " is already ignored");
return false;
}
}
onFinished("Successfully ignored user " + targetName);
return true;
});
req.execute();
}); });
req.execute();
} }
void TwitchAccount::unignore(const QString &targetName, void TwitchAccount::unignore(const QString &targetName,
std::function<void(UnignoreResult, const QString &message)> onFinished) std::function<void(const QString &message)> onFinished)
{ {
util::twitch::getUserID(targetName, QThread::currentThread(), [=](QString targetUserID) { util::twitch::getUserID(targetName, QThread::currentThread(), [=](QString targetUserID) {
this->unignoreByID(targetUserID, targetName, onFinished); // QString url("https://api.twitch.tv/kraken/users/" + this->getUserId() + "/blocks/" +
targetUserID);
util::NetworkRequest req(url);
req.setRequestType(util::NetworkRequest::DeleteRequest);
req.setCaller(QThread::currentThread());
req.makeAuthorizedV5(this->getOAuthClient(), this->getOAuthToken());
req.onError([=](int errorCode) {
onFinished("An unknown error occured while trying to unignore user " + targetName +
" (" + QString::number(errorCode) + ")");
return true;
});
req.onSuccess([=](const rapidjson::Document &document) {
TwitchUser ignoredUser;
ignoredUser.id = targetUserID;
{
std::lock_guard<std::mutex> lock(this->ignoresMutex);
this->ignores.erase(ignoredUser);
}
onFinished("Successfully unignored user " + targetName);
return true;
});
req.execute();
}); });
} }
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);
util::NetworkRequest req(url);
req.setRequestType(util::NetworkRequest::DeleteRequest);
req.setCaller(QThread::currentThread());
req.makeAuthorizedV5(this->getOAuthClient(), this->getOAuthToken());
req.onError([=](int errorCode) {
onFinished(UnignoreResult_Failed,
"An unknown error occured while trying to unignore user " + targetName + " (" +
QString::number(errorCode) + ")");
return true;
});
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());
req.onError([=](int errorCode) {
if (errorCode == 203) {
onFinished(FollowResult_NotFollowing);
} else {
onFinished(FollowResult_Failed);
}
return true;
});
req.onSuccess([=](const rapidjson::Document &document) {
onFinished(FollowResult_Following);
return true;
});
req.execute();
}
std::set<TwitchUser> TwitchAccount::getIgnores() const std::set<TwitchUser> TwitchAccount::getIgnores() const
{ {
std::lock_guard<std::mutex> lock(this->ignoresMutex); std::lock_guard<std::mutex> lock(this->ignoresMutex);

View file

@ -9,24 +9,6 @@
#include <set> #include <set>
namespace chatterino { namespace chatterino {
enum IgnoreResult {
IgnoreResult_Success,
IgnoreResult_AlreadyIgnored,
IgnoreResult_Failed,
};
enum UnignoreResult {
UnignoreResult_Success,
UnignoreResult_Failed,
};
enum FollowResult {
FollowResult_Following,
FollowResult_NotFollowing,
FollowResult_Failed,
};
namespace providers { namespace providers {
namespace twitch { namespace twitch {
@ -55,17 +37,8 @@ public:
bool isAnon() const; bool isAnon() const;
void loadIgnores(); void loadIgnores();
void ignore(const QString &targetName, std::function<void(const QString &)> onFinished);
void ignore(const QString &targetName, void unignore(const QString &targetName, std::function<void(const QString &)> onFinished);
std::function<void(IgnoreResult, const QString &)> onFinished);
void ignoreByID(const QString &targetUserID, const QString &targetName,
std::function<void(IgnoreResult, const QString &)> onFinished);
void unignore(const QString &targetName,
std::function<void(UnignoreResult, const QString &)> onFinished);
void unignoreByID(const QString &targetUserID, const QString &targetName,
std::function<void(UnignoreResult, const QString &message)> onFinished);
void checkFollow(const QString targetUserID, std::function<void(FollowResult)> onFinished);
std::set<TwitchUser> getIgnores() const; std::set<TwitchUser> getIgnores() const;

View file

@ -372,7 +372,7 @@ private:
QObject::connect(worker, &NetworkWorker::doneUrl, this->data.caller, QObject::connect(worker, &NetworkWorker::doneUrl, this->data.caller,
[data = this->data](auto reply) mutable { [data = this->data](auto reply) mutable {
if (reply->error() != QNetworkReply::NetworkError::NoError) { if (reply->error() != QNetworkReply::NetworkError::NoError) {
data.onError(reply->error()); // TODO: We might want to call an onError callback here
return; return;
} }

View file

@ -98,51 +98,32 @@ AccountPopupWidget::AccountPopupWidget(ChannelPtr _channel)
"/follows/channels/" + this->popupWidgetUser.userID); "/follows/channels/" + this->popupWidgetUser.userID);
this->ui->follow->setEnabled(false); this->ui->follow->setEnabled(false);
if (!this->relationship.isFollowing()) { if (!this->relationship.following) {
util::twitch::put(requestUrl, [this](QJsonObject obj) { util::twitch::put(requestUrl, [this](QJsonObject obj) {
qDebug() << "follows channel: " << obj; qDebug() << "follows channel: " << obj;
this->relationship.setFollowing(true); this->relationship.following = true;
emit refreshButtons(); emit refreshButtons();
}); });
} else { } else {
util::twitch::sendDelete(requestUrl, [this] { util::twitch::sendDelete(requestUrl, [this] {
this->relationship.setFollowing(false); this->relationship.following = false;
emit refreshButtons(); emit refreshButtons();
}); });
} }
}); });
QObject::connect(this->ui->ignore, &QPushButton::clicked, this, [=]() { QObject::connect(this->ui->ignore, &QPushButton::clicked, this, [=]() {
auto currentUser = getApp()->accounts->Twitch.getCurrent(); QUrl requestUrl("https://api.twitch.tv/kraken/users/" + this->loggedInUser.userID +
"/blocks/" + this->popupWidgetUser.userID);
if (!this->relationship.isIgnoring()) { if (!this->relationship.ignoring) {
currentUser->ignoreByID(this->popupWidgetUser.userID, this->popupWidgetUser.username, util::twitch::put(requestUrl, [this](auto) {
[=](auto result, const auto &message) { this->relationship.ignoring = true; //
switch (result) { });
case IgnoreResult_Success: {
this->relationship.setIgnoring(true);
emit refreshButtons();
} break;
case IgnoreResult_AlreadyIgnored: {
this->relationship.setIgnoring(true);
emit refreshButtons();
} break;
case IgnoreResult_Failed: {
} break;
}
});
} else { } else {
currentUser->unignoreByID(this->popupWidgetUser.userID, this->popupWidgetUser.username, util::twitch::sendDelete(requestUrl, [this] {
[=](auto result, const auto &message) { this->relationship.ignoring = false; //
switch (result) { });
case UnignoreResult_Success: {
this->relationship.setIgnoring(false);
emit refreshButtons();
} break;
case UnignoreResult_Failed: {
} break;
}
});
} }
}); });
@ -177,7 +158,8 @@ AccountPopupWidget::AccountPopupWidget(ChannelPtr _channel)
void AccountPopupWidget::setName(const QString &name) void AccountPopupWidget::setName(const QString &name)
{ {
this->relationship.reset(); this->relationship.following = false;
this->relationship.ignoring = false;
this->popupWidgetUser.username = name; this->popupWidgetUser.username = name;
this->ui->lblUsername->setText(name); this->ui->lblUsername->setText(name);
@ -224,24 +206,17 @@ void AccountPopupWidget::getUserData()
this->loadAvatar(QUrl(obj.value("logo").toString())); this->loadAvatar(QUrl(obj.value("logo").toString()));
}); });
auto app = getApp(); util::twitch::get("https://api.twitch.tv/kraken/users/" + this->loggedInUser.userID +
auto currentUser = app->accounts->Twitch.getCurrent(); "/follows/channels/" + this->popupWidgetUser.userID,
this, [=](const QJsonObject &obj) {
this->ui->follow->setEnabled(true);
this->relationship.following = obj.contains("channel");
currentUser->checkFollow(this->popupWidgetUser.userID, [=](auto result) { emit refreshButtons();
this->relationship.setFollowing(result == FollowResult_Following); });
emit refreshButtons(); // TODO: Get ignore relationship between logged in user and popup widget user and update
}); // relationship.ignoring
bool isIgnoring = false;
for (const auto &ignoredUser : currentUser->getIgnores()) {
if (this->popupWidgetUser.userID == ignoredUser.id) {
isIgnoring = true;
break;
}
}
this->relationship.setIgnoring(isIgnoring);
emit refreshButtons();
} }
void AccountPopupWidget::loadAvatar(const QUrl &avatarUrl) void AccountPopupWidget::loadAvatar(const QUrl &avatarUrl)
@ -352,24 +327,28 @@ void AccountPopupWidget::refreshLayouts()
void AccountPopupWidget::actuallyRefreshButtons() void AccountPopupWidget::actuallyRefreshButtons()
{ {
if (this->relationship.isFollowingSet()) { if (this->relationship.following) {
if (this->relationship.isFollowing()) { if (this->ui->follow->text() != "Unfollow") {
this->ui->follow->setText("Unfollow"); this->ui->follow->setText("Unfollow");
} else { this->ui->follow->setEnabled(true);
this->ui->follow->setText("Follow"); }
} else {
if (this->ui->follow->text() != "Follow") {
this->ui->follow->setText("Follow");
this->ui->follow->setEnabled(true);
} }
this->ui->follow->setEnabled(true);
} }
if (this->relationship.isIgnoringSet()) { if (this->relationship.ignoring) {
if (this->relationship.isIgnoring()) { if (this->ui->ignore->text() != "Unignore") {
this->ui->ignore->setText("Unignore"); this->ui->ignore->setText("Unignore");
} else { this->ui->ignore->setEnabled(true);
this->ui->ignore->setText("Ignore"); }
} else {
if (this->ui->ignore->text() != "Ignore") {
this->ui->ignore->setText("Ignore");
this->ui->ignore->setEnabled(true);
} }
this->ui->ignore->setEnabled(true);
} }
} }
@ -389,7 +368,8 @@ void AccountPopupWidget::showEvent(QShowEvent *)
this->popupWidgetUser.refreshUserType(this->channel, false); this->popupWidgetUser.refreshUserType(this->channel, false);
this->ui->follow->setEnabled(false); this->ui->follow->setEnabled(false);
this->ui->ignore->setEnabled(false); // XXX: Uncomment when ignore/unignore is fully implemented
// this->ui->ignore->setEnabled(false);
this->refreshButtons(); this->refreshButtons();

View file

@ -71,53 +71,8 @@ private:
User popupWidgetUser; User popupWidgetUser;
struct { struct {
void reset() bool following = false;
{ bool ignoring = false;
this->following = -1;
this->ignoring = -1;
}
bool isFollowing() const
{
return this->following == 1;
}
bool isFollowingSet() const
{
return this->following != -1;
}
void setFollowing(bool newVal)
{
if (newVal) {
this->following = 1;
} else {
this->following = 0;
}
}
bool isIgnoring() const
{
return this->ignoring == 1;
}
bool isIgnoringSet() const
{
return this->ignoring != -1;
}
void setIgnoring(bool newVal)
{
if (newVal) {
this->ignoring = 1;
} else {
this->ignoring = 0;
}
}
private:
int following = -1;
int ignoring = -1;
} relationship; } relationship;
protected: protected: