mirror of
https://github.com/Chatterino/chatterino2.git
synced 2024-11-13 19:49:51 +01:00
Migrate /clear command to Helix API (#3994)
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Rasmus Karlsson <rasmus.karlsson@pajlada.com>
This commit is contained in:
parent
4f1976b1be
commit
6e7b4d8ec7
|
@ -35,6 +35,7 @@
|
||||||
- Minor: Added `Go to message` context menu action to search popup, mentions, usercard and reply threads. (#3953)
|
- Minor: Added `Go to message` context menu action to search popup, mentions, usercard and reply threads. (#3953)
|
||||||
- Minor: Added link back to original message that was deleted. (#3953)
|
- Minor: Added link back to original message that was deleted. (#3953)
|
||||||
- Minor: Migrate /color command to Helix API. (#3988)
|
- Minor: Migrate /color command to Helix API. (#3988)
|
||||||
|
- Minor: Migrate /clear command to Helix API. (#3994)
|
||||||
- Bugfix: Fix crash that can occur when closing and quickly reopening a split, then running a command. (#3852)
|
- 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: Connection to Twitch PubSub now recovers more reliably. (#3643, #3716)
|
||||||
- Bugfix: Fix crash that can occur when changing channels. (#3799)
|
- Bugfix: Fix crash that can occur when changing channels. (#3799)
|
||||||
|
|
|
@ -1269,6 +1269,92 @@ void CommandController::initialize(Settings &, Paths &paths)
|
||||||
|
|
||||||
return "";
|
return "";
|
||||||
});
|
});
|
||||||
|
|
||||||
|
auto deleteMessages = [](auto channel, const QString &messageID) {
|
||||||
|
const auto *commandName = messageID.isEmpty() ? "/clear" : "/delete";
|
||||||
|
auto *twitchChannel = dynamic_cast<TwitchChannel *>(channel.get());
|
||||||
|
if (twitchChannel == nullptr)
|
||||||
|
{
|
||||||
|
channel->addMessage(makeSystemMessage(
|
||||||
|
QString("The %1 command only works in Twitch channels")
|
||||||
|
.arg(commandName)));
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
auto user = getApp()->accounts->twitch.getCurrent();
|
||||||
|
|
||||||
|
// Avoid Helix calls without Client ID and/or OAuth Token
|
||||||
|
if (user->isAnon())
|
||||||
|
{
|
||||||
|
channel->addMessage(makeSystemMessage(
|
||||||
|
QString("You must be logged in to use the %1 command.")
|
||||||
|
.arg(commandName)));
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
getHelix()->deleteChatMessages(
|
||||||
|
twitchChannel->roomId(), user->getUserId(), messageID,
|
||||||
|
[]() {
|
||||||
|
// Success handling, we do nothing: IRC/pubsub-edge will dispatch the correct
|
||||||
|
// events to update state for us.
|
||||||
|
},
|
||||||
|
[channel, messageID](auto error, auto message) {
|
||||||
|
QString errorMessage =
|
||||||
|
QString("Failed to delete chat messages - ");
|
||||||
|
|
||||||
|
switch (error)
|
||||||
|
{
|
||||||
|
case HelixDeleteChatMessagesError::UserMissingScope: {
|
||||||
|
errorMessage +=
|
||||||
|
"Missing required scope. Re-login with your "
|
||||||
|
"account and try again.";
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case HelixDeleteChatMessagesError::UserNotAuthorized: {
|
||||||
|
errorMessage +=
|
||||||
|
"you don't have permission to perform that action.";
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case HelixDeleteChatMessagesError::MessageUnavailable: {
|
||||||
|
// Override default message prefix to match with IRC message format
|
||||||
|
errorMessage =
|
||||||
|
QString(
|
||||||
|
"The message %1 does not exist, was deleted, "
|
||||||
|
"or is too old to be deleted.")
|
||||||
|
.arg(messageID);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case HelixDeleteChatMessagesError::UserNotAuthenticated: {
|
||||||
|
errorMessage += "you need to re-authenticate.";
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case HelixDeleteChatMessagesError::Forwarded: {
|
||||||
|
errorMessage += message + ".";
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case HelixDeleteChatMessagesError::Unknown:
|
||||||
|
default: {
|
||||||
|
errorMessage += "An unknown error has occurred.";
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
channel->addMessage(makeSystemMessage(errorMessage));
|
||||||
|
});
|
||||||
|
|
||||||
|
return "";
|
||||||
|
};
|
||||||
|
|
||||||
|
this->registerCommand(
|
||||||
|
"/clear", [deleteMessages](const QStringList &words, auto channel) {
|
||||||
|
(void)words; // unused
|
||||||
|
return deleteMessages(channel, QString());
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void CommandController::save()
|
void CommandController::save()
|
||||||
|
|
|
@ -843,6 +843,85 @@ void Helix::updateUserChatColor(
|
||||||
.execute();
|
.execute();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
void Helix::deleteChatMessages(
|
||||||
|
QString broadcasterID, QString moderatorID, QString messageID,
|
||||||
|
ResultCallback<> successCallback,
|
||||||
|
FailureCallback<HelixDeleteChatMessagesError, QString> failureCallback)
|
||||||
|
{
|
||||||
|
using Error = HelixDeleteChatMessagesError;
|
||||||
|
|
||||||
|
QUrlQuery urlQuery;
|
||||||
|
|
||||||
|
urlQuery.addQueryItem("broadcaster_id", broadcasterID);
|
||||||
|
urlQuery.addQueryItem("moderator_id", moderatorID);
|
||||||
|
|
||||||
|
if (!messageID.isEmpty())
|
||||||
|
{
|
||||||
|
// If message ID is empty, it's equivalent to /clear
|
||||||
|
urlQuery.addQueryItem("message_id", messageID);
|
||||||
|
}
|
||||||
|
|
||||||
|
this->makeRequest("moderation/chat", urlQuery)
|
||||||
|
.type(NetworkRequestType::Delete)
|
||||||
|
.onSuccess([successCallback, failureCallback](auto result) -> Outcome {
|
||||||
|
if (result.status() != 204)
|
||||||
|
{
|
||||||
|
qCWarning(chatterinoTwitch)
|
||||||
|
<< "Success result for deleting chat messages 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 404: {
|
||||||
|
// A 404 on this endpoint means message id is invalid or unable to be deleted.
|
||||||
|
// See: https://dev.twitch.tv/docs/api/reference#delete-chat-messages
|
||||||
|
failureCallback(Error::MessageUnavailable, message);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 403: {
|
||||||
|
// 403 endpoint means the user does not have permission to perform this action in that channel
|
||||||
|
// Most likely to missing moderator permissions
|
||||||
|
// Missing documentation issue: https://github.com/twitchdev/issues/issues/659
|
||||||
|
// `message` value is well-formed so no need for a specific error type
|
||||||
|
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 deleting chat messages:"
|
||||||
|
<< result.status() << result.getData() << obj;
|
||||||
|
failureCallback(Error::Unknown, message);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.execute();
|
||||||
|
}
|
||||||
|
|
||||||
NetworkRequest Helix::makeRequest(QString url, QUrlQuery urlQuery)
|
NetworkRequest Helix::makeRequest(QString url, QUrlQuery urlQuery)
|
||||||
{
|
{
|
||||||
assert(!url.startsWith("/"));
|
assert(!url.startsWith("/"));
|
||||||
|
|
|
@ -329,6 +329,17 @@ enum class HelixUpdateUserChatColorError {
|
||||||
Forwarded,
|
Forwarded,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
enum class HelixDeleteChatMessagesError {
|
||||||
|
Unknown,
|
||||||
|
UserMissingScope,
|
||||||
|
UserNotAuthenticated,
|
||||||
|
UserNotAuthorized,
|
||||||
|
MessageUnavailable,
|
||||||
|
|
||||||
|
// The error message is forwarded directly from the Twitch API
|
||||||
|
Forwarded,
|
||||||
|
};
|
||||||
|
|
||||||
class IHelix
|
class IHelix
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -458,6 +469,13 @@ public:
|
||||||
FailureCallback<HelixUpdateUserChatColorError, QString>
|
FailureCallback<HelixUpdateUserChatColorError, QString>
|
||||||
failureCallback) = 0;
|
failureCallback) = 0;
|
||||||
|
|
||||||
|
// https://dev.twitch.tv/docs/api/reference#delete-chat-messages
|
||||||
|
virtual void deleteChatMessages(
|
||||||
|
QString broadcasterID, QString moderatorID, QString messageID,
|
||||||
|
ResultCallback<> successCallback,
|
||||||
|
FailureCallback<HelixDeleteChatMessagesError, QString>
|
||||||
|
failureCallback) = 0;
|
||||||
|
|
||||||
virtual void update(QString clientId, QString oauthToken) = 0;
|
virtual void update(QString clientId, QString oauthToken) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -580,6 +598,13 @@ public:
|
||||||
FailureCallback<HelixUpdateUserChatColorError, QString> failureCallback)
|
FailureCallback<HelixUpdateUserChatColorError, QString> failureCallback)
|
||||||
final;
|
final;
|
||||||
|
|
||||||
|
// https://dev.twitch.tv/docs/api/reference#delete-chat-messages
|
||||||
|
void deleteChatMessages(
|
||||||
|
QString broadcasterID, QString moderatorID, QString messageID,
|
||||||
|
ResultCallback<> successCallback,
|
||||||
|
FailureCallback<HelixDeleteChatMessagesError, QString> failureCallback)
|
||||||
|
final;
|
||||||
|
|
||||||
void update(QString clientId, QString oauthToken) final;
|
void update(QString clientId, QString oauthToken) final;
|
||||||
|
|
||||||
static void initialize();
|
static void initialize();
|
||||||
|
|
|
@ -217,6 +217,14 @@ public:
|
||||||
failureCallback)),
|
failureCallback)),
|
||||||
(override));
|
(override));
|
||||||
|
|
||||||
|
// The extra parenthesis around the failure callback is because its type contains a comma
|
||||||
|
MOCK_METHOD(void, deleteChatMessages,
|
||||||
|
(QString broadcasterID, QString moderatorID, QString messageID,
|
||||||
|
ResultCallback<> successCallback,
|
||||||
|
(FailureCallback<HelixDeleteChatMessagesError, QString>
|
||||||
|
failureCallback)),
|
||||||
|
(override));
|
||||||
|
|
||||||
MOCK_METHOD(void, update, (QString clientId, QString oauthToken),
|
MOCK_METHOD(void, update, (QString clientId, QString oauthToken),
|
||||||
(override));
|
(override));
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue