From adbc4690afd3a0ce6eec2724be34ba74c771ae40 Mon Sep 17 00:00:00 2001 From: Aiden Date: Sat, 1 Oct 2022 15:00:45 +0100 Subject: [PATCH] Migrate /unvip to Helix API (#4025) Co-authored-by: iProdigy Co-authored-by: pajlada --- CHANGELOG.md | 1 + .../commands/CommandController.cpp | 94 +++++++++++++++++++ src/providers/twitch/api/Helix.cpp | 78 +++++++++++++++ src/providers/twitch/api/Helix.hpp | 22 +++++ tests/src/HighlightController.cpp | 8 ++ 5 files changed, 203 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8fe87b659..710e734f3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -45,6 +45,7 @@ - Minor: Migrated /mod command to Helix API. (#4000) - Minor: Migrated /unmod command to Helix API. (#4001) - Minor: Migrated /vip command to Helix API. (#4010) +- Minor: Migrated /unvip command to Helix API. (#4025) - Bugfix: Connection to Twitch PubSub now recovers more reliably. (#3643, #3716) - Bugfix: Fixed a crash that can occur when closing and quickly reopening a split, then running a command. (#3852) - Bugfix: Fixed a crash that can occur when changing channels. (#3799) diff --git a/src/controllers/commands/CommandController.cpp b/src/controllers/commands/CommandController.cpp index 913fd658f..8202e6c40 100644 --- a/src/controllers/commands/CommandController.cpp +++ b/src/controllers/commands/CommandController.cpp @@ -1725,6 +1725,100 @@ void CommandController::initialize(Settings &, Paths &paths) return ""; }); + + this->registerCommand("/unvip", [](const QStringList &words, auto channel) { + if (words.size() < 2) + { + channel->addMessage(makeSystemMessage( + "Usage: \"/unvip \" - Revoke VIP status from a user. " + "Use \"/vips\" to list the VIPs of this channel.")); + return ""; + } + + auto currentUser = getApp()->accounts->twitch.getCurrent(); + if (currentUser->isAnon()) + { + channel->addMessage( + makeSystemMessage("You must be logged in to UnVIP someone!")); + return ""; + } + + auto *twitchChannel = dynamic_cast(channel.get()); + if (twitchChannel == nullptr) + { + channel->addMessage(makeSystemMessage( + "The /unvip command only works in Twitch channels")); + return ""; + } + + auto target = words.at(1); + stripChannelName(target); + + getHelix()->getUserByName( + target, + [twitchChannel, channel](const HelixUser &targetUser) { + getHelix()->removeChannelVIP( + twitchChannel->roomId(), targetUser.id, + [channel, targetUser] { + channel->addMessage(makeSystemMessage( + QString( + "You have removed %1 as a VIP of this channel.") + .arg(targetUser.displayName))); + }, + [channel, targetUser](auto error, auto message) { + QString errorMessage = + QString("Failed to remove VIP - "); + + using Error = HelixRemoveChannelVIPError; + + switch (error) + { + case Error::UserMissingScope: { + // TODO(pajlada): Phrase MISSING_REQUIRED_SCOPE + errorMessage += "Missing required scope. " + "Re-login with your " + "account and try again."; + } + break; + + case Error::UserNotAuthorized: { + // TODO(pajlada): Phrase MISSING_PERMISSION + errorMessage += "You don't have permission to " + "perform that action."; + } + break; + + case Error::Ratelimited: { + errorMessage += + "You are being ratelimited by Twitch. Try " + "again in a few seconds."; + } + break; + + case Error::Forwarded: { + // These are actually the IRC equivalents, so we can ditch the prefix + errorMessage = message; + } + break; + + case Error::Unknown: + default: { + errorMessage += + "An unknown error has occurred."; + } + break; + } + channel->addMessage(makeSystemMessage(errorMessage)); + }); + }, + [channel, target] { + // Equivalent error from IRC + channel->addMessage(makeSystemMessage( + QString("Invalid username: %1").arg(target))); + }); + + return ""; + }); } void CommandController::save() diff --git a/src/providers/twitch/api/Helix.cpp b/src/providers/twitch/api/Helix.cpp index 0fd906e45..c12cf4d3b 100644 --- a/src/providers/twitch/api/Helix.cpp +++ b/src/providers/twitch/api/Helix.cpp @@ -1257,6 +1257,84 @@ void Helix::addChannelVIP( .execute(); } +void Helix::removeChannelVIP( + QString broadcasterID, QString userID, ResultCallback<> successCallback, + FailureCallback failureCallback) +{ + using Error = HelixRemoveChannelVIPError; + + QUrlQuery urlQuery; + + urlQuery.addQueryItem("broadcaster_id", broadcasterID); + urlQuery.addQueryItem("user_id", userID); + + this->makeRequest("channels/vips", urlQuery) + .type(NetworkRequestType::Delete) + .onSuccess([successCallback, failureCallback](auto result) -> Outcome { + if (result.status() != 204) + { + qCWarning(chatterinoTwitch) + << "Success result for removing channel VIP was" + << result.status() << "but we only expected it to be 204"; + } + + successCallback(); + return Success; + }) + .onError([failureCallback](auto result) { + auto obj = result.parseJson(); + auto message = obj.value("message").toString(); + + switch (result.status()) + { + case 400: + case 409: + case 422: { + // Most of the errors returned by this endpoint are pretty good. We can rely on Twitch's API messages + failureCallback(Error::Forwarded, message); + } + break; + + case 401: { + if (message.startsWith("Missing scope", + Qt::CaseInsensitive)) + { + // Handle this error specifically because its API error is especially unfriendly + failureCallback(Error::UserMissingScope, message); + } + else if (message.compare("incorrect user authorization", + Qt::CaseInsensitive) == 0 || + message.startsWith("the id in broadcaster_id must " + "match the user id", + Qt::CaseInsensitive)) + { + // This error is particularly ugly, but is the equivalent to a user not having permissions + failureCallback(Error::UserNotAuthorized, message); + } + else + { + failureCallback(Error::Forwarded, message); + } + } + break; + + case 429: { + failureCallback(Error::Ratelimited, message); + } + break; + + default: { + qCDebug(chatterinoTwitch) + << "Unhandled error removing channel VIP:" + << result.status() << result.getData() << obj; + failureCallback(Error::Unknown, message); + } + break; + } + }) + .execute(); +} + NetworkRequest Helix::makeRequest(QString url, QUrlQuery urlQuery) { assert(!url.startsWith("/")); diff --git a/src/providers/twitch/api/Helix.hpp b/src/providers/twitch/api/Helix.hpp index 8c591c632..20526cd37 100644 --- a/src/providers/twitch/api/Helix.hpp +++ b/src/providers/twitch/api/Helix.hpp @@ -391,6 +391,16 @@ enum class HelixAddChannelVIPError { Forwarded, }; +enum class HelixRemoveChannelVIPError { + Unknown, + UserMissingScope, + UserNotAuthorized, + Ratelimited, + + // The error message is forwarded directly from the Twitch API + Forwarded, +}; + class IHelix { public: @@ -551,6 +561,12 @@ public: QString broadcasterID, QString userID, ResultCallback<> successCallback, FailureCallback failureCallback) = 0; + // https://dev.twitch.tv/docs/api/reference#remove-channel-vip + virtual void removeChannelVIP( + QString broadcasterID, QString userID, ResultCallback<> successCallback, + FailureCallback + failureCallback) = 0; + virtual void update(QString clientId, QString oauthToken) = 0; }; @@ -705,6 +721,12 @@ public: FailureCallback failureCallback) final; + // https://dev.twitch.tv/docs/api/reference#remove-channel-vip + void removeChannelVIP(QString broadcasterID, QString userID, + ResultCallback<> successCallback, + FailureCallback + failureCallback) final; + void update(QString clientId, QString oauthToken) final; static void initialize(); diff --git a/tests/src/HighlightController.cpp b/tests/src/HighlightController.cpp index 977fac8c3..2f07776b3 100644 --- a/tests/src/HighlightController.cpp +++ b/tests/src/HighlightController.cpp @@ -257,6 +257,14 @@ public: (FailureCallback failureCallback)), (override)); + // The extra parenthesis around the failure callback is because its type contains a comma + MOCK_METHOD( + void, removeChannelVIP, + (QString broadcasterID, QString userID, + ResultCallback<> successCallback, + (FailureCallback failureCallback)), + (override)); + MOCK_METHOD(void, update, (QString clientId, QString oauthToken), (override)); };