Migrate /color command to Helix API (#3988)

This commit is contained in:
pajlada
2022-09-16 23:15:28 +02:00
committed by GitHub
parent c6ebb70e05
commit 4f1976b1be
9 changed files with 270 additions and 0 deletions
+73
View File
@@ -770,6 +770,79 @@ void Helix::getChannelEmotes(
.execute();
}
void Helix::updateUserChatColor(
QString userID, QString color, ResultCallback<> successCallback,
FailureCallback<HelixUpdateUserChatColorError, QString> failureCallback)
{
using Error = HelixUpdateUserChatColorError;
QJsonObject payload;
payload.insert("user_id", QJsonValue(userID));
payload.insert("color", QJsonValue(color));
this->makeRequest("chat/color", QUrlQuery())
.type(NetworkRequestType::Put)
.header("Content-Type", "application/json")
.payload(QJsonDocument(payload).toJson(QJsonDocument::Compact))
.onSuccess([successCallback, failureCallback](auto result) -> Outcome {
auto obj = result.parseJson();
if (result.status() != 204)
{
qCWarning(chatterinoTwitch)
<< "Success result for updating chat color 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: {
if (message.startsWith("invalid color",
Qt::CaseInsensitive))
{
// Handle this error specifically since it allows us to list out the available colors
failureCallback(Error::InvalidColor, message);
}
else
{
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
{
failureCallback(Error::Forwarded, message);
}
}
break;
default: {
qCDebug(chatterinoTwitch)
<< "Unhandled error changing user color:"
<< result.status() << result.getData() << obj;
failureCallback(Error::Unknown, message);
}
break;
}
})
.execute();
};
NetworkRequest Helix::makeRequest(QString url, QUrlQuery urlQuery)
{
assert(!url.startsWith("/"));
+24
View File
@@ -320,9 +320,21 @@ enum class HelixAutoModMessageError {
MessageNotFound,
};
enum class HelixUpdateUserChatColorError {
Unknown,
UserMissingScope,
InvalidColor,
// The error message is forwarded directly from the Twitch API
Forwarded,
};
class IHelix
{
public:
template <typename... T>
using FailureCallback = std::function<void(T...)>;
// https://dev.twitch.tv/docs/api/reference#get-users
virtual void fetchUsers(
QStringList userIds, QStringList userLogins,
@@ -440,6 +452,12 @@ public:
ResultCallback<std::vector<HelixChannelEmote>> successCallback,
HelixFailureCallback failureCallback) = 0;
// https://dev.twitch.tv/docs/api/reference#update-user-chat-color
virtual void updateUserChatColor(
QString userID, QString color, ResultCallback<> successCallback,
FailureCallback<HelixUpdateUserChatColorError, QString>
failureCallback) = 0;
virtual void update(QString clientId, QString oauthToken) = 0;
};
@@ -556,6 +574,12 @@ public:
ResultCallback<std::vector<HelixChannelEmote>> successCallback,
HelixFailureCallback failureCallback) final;
// https://dev.twitch.tv/docs/api/reference#update-user-chat-color
void updateUserChatColor(
QString userID, QString color, ResultCallback<> successCallback,
FailureCallback<HelixUpdateUserChatColorError, QString> failureCallback)
final;
void update(QString clientId, QString oauthToken) final;
static void initialize();
+12
View File
@@ -6,6 +6,18 @@ this folder describes what sort of API requests we do, what permissions are requ
Full Helix API reference: https://dev.twitch.tv/docs/api/reference
### Adding support for a new endpoint
If you're adding support for a new endpoint, these are the things you should know.
1. Add a virtual function in the `IHelix` class. Naming should reflect the API name as best as possible.
1. Override the virtual function in the `Helix` class.
1. Mock the function in the `MockHelix` class in the `tests/src/HighlightController.cpp` file.
1. (Optional) Make a new error enum for the failure callback.
For a simple example, see the `updateUserChatColor` function and its error enum `HelixUpdateUserChatColorError`.
The API is used in the "/color" command in [CommandController.cpp](../../../controllers/commands/CommandController.cpp)
### Get Users
URL: https://dev.twitch.tv/docs/api/reference#get-users