mirror of
https://github.com/Chatterino/chatterino2.git
synced 2024-11-13 19:49:51 +01:00
Migrate /vip to Helix API (#4010)
Fixes #3983 Co-authored-by: iProdigy <iProdigy@users.noreply.github.com> Co-authored-by: pajlada <rasmus.karlsson@pajlada.com>
This commit is contained in:
parent
abb32f700c
commit
ced1525e75
|
@ -40,6 +40,7 @@
|
|||
- Minor: Migrate /mod command to Helix API. (#4000)
|
||||
- Minor: Migrate /unmod command to Helix API. (#4001)
|
||||
- Minor: Migrate /announce command to Helix API. (#4003)
|
||||
- Minor: Migrate /vip command to Helix API. (#4010)
|
||||
- Bugfix: Fix crash that can occur when closing and quickly reopening a split, then running a command. (#3852)
|
||||
- Bugfix: Connection to Twitch PubSub now recovers more reliably. (#3643, #3716)
|
||||
- Bugfix: Fix crash that can occur when changing channels. (#3799)
|
||||
|
|
|
@ -1632,6 +1632,99 @@ void CommandController::initialize(Settings &, Paths &paths)
|
|||
});
|
||||
return "";
|
||||
});
|
||||
|
||||
this->registerCommand("/vip", [](const QStringList &words, auto channel) {
|
||||
if (words.size() < 2)
|
||||
{
|
||||
channel->addMessage(makeSystemMessage(
|
||||
"Usage: \"/vip <username>\" - Grant VIP status to 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 VIP someone!"));
|
||||
return "";
|
||||
}
|
||||
|
||||
auto *twitchChannel = dynamic_cast<TwitchChannel *>(channel.get());
|
||||
if (twitchChannel == nullptr)
|
||||
{
|
||||
channel->addMessage(makeSystemMessage(
|
||||
"The /vip command only works in Twitch channels"));
|
||||
return "";
|
||||
}
|
||||
|
||||
auto target = words.at(1);
|
||||
stripChannelName(target);
|
||||
|
||||
getHelix()->getUserByName(
|
||||
target,
|
||||
[twitchChannel, channel](const HelixUser &targetUser) {
|
||||
getHelix()->addChannelVIP(
|
||||
twitchChannel->roomId(), targetUser.id,
|
||||
[channel, targetUser] {
|
||||
channel->addMessage(makeSystemMessage(
|
||||
QString(
|
||||
"You have added %1 as a VIP of this channel.")
|
||||
.arg(targetUser.displayName)));
|
||||
},
|
||||
[channel, targetUser](auto error, auto message) {
|
||||
QString errorMessage = QString("Failed to add VIP - ");
|
||||
|
||||
using Error = HelixAddChannelVIPError;
|
||||
|
||||
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()
|
||||
|
|
|
@ -1178,6 +1178,85 @@ void Helix::sendChatAnnouncement(
|
|||
.execute();
|
||||
}
|
||||
|
||||
void Helix::addChannelVIP(
|
||||
QString broadcasterID, QString userID, ResultCallback<> successCallback,
|
||||
FailureCallback<HelixAddChannelVIPError, QString> failureCallback)
|
||||
{
|
||||
using Error = HelixAddChannelVIPError;
|
||||
|
||||
QUrlQuery urlQuery;
|
||||
|
||||
urlQuery.addQueryItem("broadcaster_id", broadcasterID);
|
||||
urlQuery.addQueryItem("user_id", userID);
|
||||
|
||||
this->makeRequest("channels/vips", urlQuery)
|
||||
.type(NetworkRequestType::Post)
|
||||
.onSuccess([successCallback, failureCallback](auto result) -> Outcome {
|
||||
if (result.status() != 204)
|
||||
{
|
||||
qCWarning(chatterinoTwitch)
|
||||
<< "Success result for adding 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:
|
||||
case 425: {
|
||||
// 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 adding channel VIP:"
|
||||
<< result.status() << result.getData() << obj;
|
||||
failureCallback(Error::Unknown, message);
|
||||
}
|
||||
break;
|
||||
}
|
||||
})
|
||||
.execute();
|
||||
}
|
||||
|
||||
NetworkRequest Helix::makeRequest(QString url, QUrlQuery urlQuery)
|
||||
{
|
||||
assert(!url.startsWith("/"));
|
||||
|
|
|
@ -381,6 +381,16 @@ enum class HelixRemoveChannelModeratorError {
|
|||
Forwarded,
|
||||
};
|
||||
|
||||
enum class HelixAddChannelVIPError {
|
||||
Unknown,
|
||||
UserMissingScope,
|
||||
UserNotAuthorized,
|
||||
Ratelimited,
|
||||
|
||||
// The error message is forwarded directly from the Twitch API
|
||||
Forwarded,
|
||||
};
|
||||
|
||||
class IHelix
|
||||
{
|
||||
public:
|
||||
|
@ -536,6 +546,11 @@ public:
|
|||
FailureCallback<HelixSendChatAnnouncementError, QString>
|
||||
failureCallback) = 0;
|
||||
|
||||
// https://dev.twitch.tv/docs/api/reference#add-channel-vip
|
||||
virtual void addChannelVIP(
|
||||
QString broadcasterID, QString userID, ResultCallback<> successCallback,
|
||||
FailureCallback<HelixAddChannelVIPError, QString> failureCallback) = 0;
|
||||
|
||||
virtual void update(QString clientId, QString oauthToken) = 0;
|
||||
};
|
||||
|
||||
|
@ -684,6 +699,12 @@ public:
|
|||
FailureCallback<HelixSendChatAnnouncementError, QString>
|
||||
failureCallback) final;
|
||||
|
||||
// https://dev.twitch.tv/docs/api/reference#add-channel-vip
|
||||
void addChannelVIP(QString broadcasterID, QString userID,
|
||||
ResultCallback<> successCallback,
|
||||
FailureCallback<HelixAddChannelVIPError, QString>
|
||||
failureCallback) final;
|
||||
|
||||
void update(QString clientId, QString oauthToken) final;
|
||||
|
||||
static void initialize();
|
||||
|
|
|
@ -249,6 +249,14 @@ public:
|
|||
failureCallback)),
|
||||
(override));
|
||||
|
||||
// The extra parenthesis around the failure callback is because its type contains a comma
|
||||
MOCK_METHOD(
|
||||
void, addChannelVIP,
|
||||
(QString broadcasterID, QString userID,
|
||||
ResultCallback<> successCallback,
|
||||
(FailureCallback<HelixAddChannelVIPError, QString> failureCallback)),
|
||||
(override));
|
||||
|
||||
MOCK_METHOD(void, update, (QString clientId, QString oauthToken),
|
||||
(override));
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue