Refactor NetworkRequest class

Add followUser and unfollowUser methods to TwitchAccount
This commit is contained in:
Rasmus Karlsson
2018-07-07 11:08:57 +00:00
parent cada32edfd
commit 6a418e6e59
27 changed files with 835 additions and 669 deletions
+7 -2
View File
@@ -1,6 +1,10 @@
#include "widgets/dialogs/LoginDialog.hpp"
#include "common/Common.hpp"
#include "common/UrlFetch.hpp"
#include "controllers/accounts/AccountController.hpp"
#include "providers/twitch/PartialTwitchUser.hpp"
#ifdef USEWINSDK
#include <Windows.h>
#endif
@@ -173,9 +177,10 @@ AdvancedLoginWidget::AdvancedLoginWidget()
this->ui_.buttonLowerRow.layout.addWidget(&this->ui_.buttonLowerRow.fillInUserIDButton);
connect(&this->ui_.buttonLowerRow.fillInUserIDButton, &QPushButton::clicked, [=]() {
twitchApiGetUserID(this->ui_.usernameInput.text(), this, [=](const QString &userID) {
const auto onIdFetched = [=](const QString &userID) {
this->ui_.userIDInput.setText(userID); //
});
};
PartialTwitchUser::byName(this->ui_.usernameInput.text()).getId(onIdFetched, this);
});
}
+11 -3
View File
@@ -7,6 +7,7 @@
#include "widgets/helper/ChannelView.hpp"
#include <QDateTime>
#include <QJsonArray>
#include <QMessageBox>
#include <QVBoxLayout>
@@ -65,8 +66,8 @@ void LogsPopup::getLogviewerLogs()
return true;
});
req.getJSON([this, channelName](QJsonObject &data) {
req.onSuccess([this, channelName](auto result) {
auto data = result.parseJson();
std::vector<MessagePtr> messages;
ChannelPtr logsChannel(new Channel("logs", Channel::Type::None));
@@ -87,6 +88,8 @@ void LogsPopup::getLogviewerLogs()
messages.push_back(builder.build());
};
this->setMessages(messages);
return true;
});
req.execute();
@@ -113,10 +116,12 @@ void LogsPopup::getOverrustleLogs()
box->setAttribute(Qt::WA_DeleteOnClose);
box->show();
box->raise();
return true;
});
req.getJSON([this, channelName](QJsonObject &data) {
req.onSuccess([this, channelName](auto result) {
auto data = result.parseJson();
std::vector<MessagePtr> messages;
if (data.contains("lines")) {
QJsonArray dataMessages = data.value("lines").toArray();
@@ -135,7 +140,10 @@ void LogsPopup::getOverrustleLogs()
}
}
this->setMessages(messages);
return true;
});
req.execute();
}
} // namespace chatterino
+37 -20
View File
@@ -2,6 +2,8 @@
#include "Application.hpp"
#include "common/UrlFetch.hpp"
#include "controllers/accounts/AccountController.hpp"
#include "providers/twitch/PartialTwitchUser.hpp"
#include "providers/twitch/TwitchChannel.hpp"
#include "singletons/Resources.hpp"
#include "util/LayoutCreator.hpp"
@@ -176,11 +178,15 @@ void UserInfoPopup::installEvents()
QUrl requestUrl("https://api.twitch.tv/kraken/users/" + currentUser->getUserId() +
"/follows/channels/" + this->userId_);
const auto reenableFollowCheckbox = [this] {
this->ui_.follow->setEnabled(true); //
};
this->ui_.follow->setEnabled(false);
if (this->ui_.follow->isChecked()) {
twitchApiPut(requestUrl, [this](const auto &) { this->ui_.follow->setEnabled(true); });
currentUser->followUser(this->userId_, reenableFollowCheckbox);
} else {
twitchApiDelete(requestUrl, [this] { this->ui_.follow->setEnabled(true); });
currentUser->unfollowUser(this->userId_, reenableFollowCheckbox);
}
});
@@ -239,24 +245,28 @@ void UserInfoPopup::updateUserData()
{
std::weak_ptr<bool> hack = this->hack_;
// get user info
twitchApiGetUserID(this->userName_, this, [this, hack](QString id) {
const auto onIdFetched = [this, hack](QString id) {
auto currentUser = getApp()->accounts->twitch.getCurrent();
this->userId_ = id;
// get channel info
twitchApiGet(
"https://api.twitch.tv/kraken/channels/" + id, this, [this](const QJsonObject &obj) {
this->ui_.followerCountLabel->setText(
TEXT_FOLLOWERS + QString::number(obj.value("followers").toInt()));
this->ui_.viewCountLabel->setText(TEXT_VIEWS +
QString::number(obj.value("views").toInt()));
this->ui_.createdDateLabel->setText(
TEXT_CREATED + obj.value("created_at").toString().section("T", 0, 0));
auto request = makeGetChannelRequest(id, this);
this->loadAvatar(QUrl(obj.value("logo").toString()));
});
request.onSuccess([this](auto result) {
auto obj = result.parseJson();
this->ui_.followerCountLabel->setText(TEXT_FOLLOWERS +
QString::number(obj.value("followers").toInt()));
this->ui_.viewCountLabel->setText(TEXT_VIEWS +
QString::number(obj.value("views").toInt()));
this->ui_.createdDateLabel->setText(
TEXT_CREATED + obj.value("created_at").toString().section("T", 0, 0));
this->loadAvatar(QUrl(obj.value("logo").toString()));
return true;
});
request.execute();
// get follow state
currentUser->checkFollow(id, [this, hack](auto result) {
@@ -279,7 +289,9 @@ void UserInfoPopup::updateUserData()
this->ui_.ignore->setEnabled(true);
this->ui_.ignore->setChecked(isIgnoring);
});
};
PartialTwitchUser::byName(this->userName_).getId(onIdFetched, this);
this->ui_.follow->setEnabled(false);
this->ui_.ignore->setEnabled(false);
@@ -386,16 +398,21 @@ UserInfoPopup::TimeoutWidget::TimeoutWidget()
addTimeouts("sec", {{"1", 1}});
addTimeouts("min", {
{"1", 1 * 60}, {"5", 5 * 60}, {"10", 10 * 60},
{"1", 1 * 60},
{"5", 5 * 60},
{"10", 10 * 60},
});
addTimeouts("hour", {
{"1", 1 * 60 * 60}, {"4", 4 * 60 * 60},
{"1", 1 * 60 * 60},
{"4", 4 * 60 * 60},
});
addTimeouts("days", {
{"1", 1 * 60 * 60 * 24}, {"3", 3 * 60 * 60 * 24},
{"1", 1 * 60 * 60 * 24},
{"3", 3 * 60 * 60 * 24},
});
addTimeouts("weeks", {
{"1", 1 * 60 * 60 * 24 * 7}, {"2", 2 * 60 * 60 * 24 * 7},
{"1", 1 * 60 * 60 * 24 * 7},
{"2", 2 * 60 * 60 * 24 * 7},
});
addButton(Ban, "ban", getApp()->resources->buttons.ban);
+18 -11
View File
@@ -451,18 +451,25 @@ void Split::doOpenViewerList()
}
auto loadingLabel = new QLabel("Loading...");
twitchApiGet("https://tmi.twitch.tv/group/user/" + this->getChannel()->name + "/chatters", this,
[=](QJsonObject obj) {
QJsonObject chattersObj = obj.value("chatters").toObject();
auto request = NetworkRequest::twitchRequest("https://tmi.twitch.tv/group/user/" +
this->getChannel()->name + "/chatters");
loadingLabel->hide();
for (int i = 0; i < jsonLabels.size(); i++) {
chattersList->addItem(labelList.at(i));
foreach (const QJsonValue &v,
chattersObj.value(jsonLabels.at(i)).toArray())
chattersList->addItem(v.toString());
}
});
request.setCaller(this);
request.onSuccess([=](auto result) {
auto obj = result.parseJson();
QJsonObject chattersObj = obj.value("chatters").toObject();
loadingLabel->hide();
for (int i = 0; i < jsonLabels.size(); i++) {
chattersList->addItem(labelList.at(i));
foreach (const QJsonValue &v, chattersObj.value(jsonLabels.at(i)).toArray())
chattersList->addItem(v.toString());
}
return true;
});
request.execute();
searchBar->setPlaceholderText("Search User...");
QObject::connect(searchBar, &QLineEdit::textEdited, this, [=]() {
+10 -9
View File
@@ -2,6 +2,7 @@
#include "Application.hpp"
#include "common/UrlFetch.hpp"
#include "controllers/accounts/AccountController.hpp"
#include "providers/twitch/TwitchChannel.hpp"
#include "providers/twitch/TwitchServer.hpp"
#include "singletons/Resources.hpp"
@@ -323,13 +324,13 @@ void SplitHeader::updateChannelText()
if (streamStatus.live) {
this->isLive_ = true;
this->tooltip_ = "<style>.center { text-align: center; }</style>"
"<p class = \"center\">" +
streamStatus.title + "<br><br>" + streamStatus.game + "<br>" +
(streamStatus.rerun ? "Vod-casting" : "Live") + " for " +
streamStatus.uptime + " with " +
QString::number(streamStatus.viewerCount) +
" viewers"
"</p>";
"<p class = \"center\">" +
streamStatus.title + "<br><br>" + streamStatus.game + "<br>" +
(streamStatus.rerun ? "Vod-casting" : "Live") + " for " +
streamStatus.uptime + " with " +
QString::number(streamStatus.viewerCount) +
" viewers"
"</p>";
if (streamStatus.rerun) {
title += " (rerun)";
} else if (streamStatus.streamType.isEmpty()) {
@@ -355,8 +356,8 @@ void SplitHeader::updateModerationModeIcon()
auto app = getApp();
this->moderationButton_->setPixmap(this->split_->getModerationMode()
? *app->resources->moderationmode_enabled->getPixmap()
: *app->resources->moderationmode_disabled->getPixmap());
? *app->resources->moderationmode_enabled->getPixmap()
: *app->resources->moderationmode_disabled->getPixmap());
bool modButtonVisible = false;
ChannelPtr channel = this->split_->getChannel();