Added result code to ignore/unignore calls

Add ignore/unignore calls that take the user ID to avoid double api
calls
Fully implement the account popup ignore/unignore feature
Fix #247
This commit is contained in:
Rasmus Karlsson
2018-05-13 17:53:24 +02:00
parent d4c31309f0
commit 7bc63ba38f
6 changed files with 260 additions and 122 deletions
+59 -39
View File
@@ -98,32 +98,51 @@ AccountPopupWidget::AccountPopupWidget(ChannelPtr _channel)
"/follows/channels/" + this->popupWidgetUser.userID);
this->ui->follow->setEnabled(false);
if (!this->relationship.following) {
if (!this->relationship.isFollowing()) {
util::twitch::put(requestUrl, [this](QJsonObject obj) {
qDebug() << "follows channel: " << obj;
this->relationship.following = true;
this->relationship.setFollowing(true);
emit refreshButtons();
});
} else {
util::twitch::sendDelete(requestUrl, [this] {
this->relationship.following = false;
this->relationship.setFollowing(false);
emit refreshButtons();
});
}
});
QObject::connect(this->ui->ignore, &QPushButton::clicked, this, [=]() {
QUrl requestUrl("https://api.twitch.tv/kraken/users/" + this->loggedInUser.userID +
"/blocks/" + this->popupWidgetUser.userID);
auto currentUser = getApp()->accounts->Twitch.getCurrent();
if (!this->relationship.ignoring) {
util::twitch::put(requestUrl, [this](auto) {
this->relationship.ignoring = true; //
});
if (!this->relationship.isIgnoring()) {
currentUser->ignoreByID(this->popupWidgetUser.userID, this->popupWidgetUser.username,
[=](auto result, const auto &message) {
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 {
util::twitch::sendDelete(requestUrl, [this] {
this->relationship.ignoring = false; //
});
currentUser->unignoreByID(this->popupWidgetUser.userID, this->popupWidgetUser.username,
[=](auto result, const auto &message) {
switch (result) {
case UnignoreResult_Success: {
this->relationship.setIgnoring(false);
emit refreshButtons();
} break;
case UnignoreResult_Failed: {
} break;
}
});
}
});
@@ -158,8 +177,7 @@ AccountPopupWidget::AccountPopupWidget(ChannelPtr _channel)
void AccountPopupWidget::setName(const QString &name)
{
this->relationship.following = false;
this->relationship.ignoring = false;
this->relationship.reset();
this->popupWidgetUser.username = name;
this->ui->lblUsername->setText(name);
@@ -206,17 +224,24 @@ void AccountPopupWidget::getUserData()
this->loadAvatar(QUrl(obj.value("logo").toString()));
});
util::twitch::get("https://api.twitch.tv/kraken/users/" + this->loggedInUser.userID +
"/follows/channels/" + this->popupWidgetUser.userID,
this, [=](const QJsonObject &obj) {
this->ui->follow->setEnabled(true);
this->relationship.following = obj.contains("channel");
auto app = getApp();
auto currentUser = app->accounts->Twitch.getCurrent();
emit refreshButtons();
});
currentUser->checkFollow(this->popupWidgetUser.userID, [=](auto result) {
this->relationship.setFollowing(result == FollowResult_Following);
// TODO: Get ignore relationship between logged in user and popup widget user and update
// relationship.ignoring
emit refreshButtons();
});
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)
@@ -327,28 +352,24 @@ void AccountPopupWidget::refreshLayouts()
void AccountPopupWidget::actuallyRefreshButtons()
{
if (this->relationship.following) {
if (this->ui->follow->text() != "Unfollow") {
if (this->relationship.isFollowingSet()) {
if (this->relationship.isFollowing()) {
this->ui->follow->setText("Unfollow");
this->ui->follow->setEnabled(true);
}
} else {
if (this->ui->follow->text() != "Follow") {
} else {
this->ui->follow->setText("Follow");
this->ui->follow->setEnabled(true);
}
this->ui->follow->setEnabled(true);
}
if (this->relationship.ignoring) {
if (this->ui->ignore->text() != "Unignore") {
if (this->relationship.isIgnoringSet()) {
if (this->relationship.isIgnoring()) {
this->ui->ignore->setText("Unignore");
this->ui->ignore->setEnabled(true);
}
} else {
if (this->ui->ignore->text() != "Ignore") {
} else {
this->ui->ignore->setText("Ignore");
this->ui->ignore->setEnabled(true);
}
this->ui->ignore->setEnabled(true);
}
}
@@ -368,8 +389,7 @@ void AccountPopupWidget::showEvent(QShowEvent *)
this->popupWidgetUser.refreshUserType(this->channel, false);
this->ui->follow->setEnabled(false);
// XXX: Uncomment when ignore/unignore is fully implemented
// this->ui->ignore->setEnabled(false);
this->ui->ignore->setEnabled(false);
this->refreshButtons();
+47 -2
View File
@@ -71,8 +71,53 @@ private:
User popupWidgetUser;
struct {
bool following = false;
bool ignoring = false;
void reset()
{
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;
protected: