Migrate /chatters commands to use Helix api (#4088)
Co-authored-by: Rasmus Karlsson <rasmus.karlsson@pajlada.com>
This commit is contained in:
@@ -1782,6 +1782,83 @@ void Helix::updateChatSettings(
|
||||
.execute();
|
||||
}
|
||||
|
||||
// https://dev.twitch.tv/docs/api/reference#get-chatters
|
||||
void Helix::fetchChatters(
|
||||
QString broadcasterID, QString moderatorID, int first, QString after,
|
||||
ResultCallback<HelixChatters> successCallback,
|
||||
FailureCallback<HelixGetChattersError, QString> failureCallback)
|
||||
{
|
||||
using Error = HelixGetChattersError;
|
||||
|
||||
QUrlQuery urlQuery;
|
||||
|
||||
urlQuery.addQueryItem("broadcaster_id", broadcasterID);
|
||||
urlQuery.addQueryItem("moderator_id", moderatorID);
|
||||
urlQuery.addQueryItem("first", QString::number(first));
|
||||
|
||||
if (!after.isEmpty())
|
||||
{
|
||||
urlQuery.addQueryItem("after", after);
|
||||
}
|
||||
|
||||
this->makeRequest("chat/chatters", urlQuery)
|
||||
.onSuccess([successCallback](auto result) -> Outcome {
|
||||
if (result.status() != 200)
|
||||
{
|
||||
qCWarning(chatterinoTwitch)
|
||||
<< "Success result for getting chatters was "
|
||||
<< result.status() << "but we expected it to be 200";
|
||||
}
|
||||
|
||||
auto response = result.parseJson();
|
||||
successCallback(HelixChatters(response));
|
||||
return Success;
|
||||
})
|
||||
.onError([failureCallback](auto result) {
|
||||
auto obj = result.parseJson();
|
||||
auto message = obj.value("message").toString();
|
||||
|
||||
switch (result.status())
|
||||
{
|
||||
case 400: {
|
||||
failureCallback(Error::Forwarded, message);
|
||||
}
|
||||
break;
|
||||
|
||||
case 401: {
|
||||
if (message.startsWith("Missing scope",
|
||||
Qt::CaseInsensitive))
|
||||
{
|
||||
failureCallback(Error::UserMissingScope, message);
|
||||
}
|
||||
else if (message.contains("OAuth token"))
|
||||
{
|
||||
failureCallback(Error::UserNotAuthorized, message);
|
||||
}
|
||||
else
|
||||
{
|
||||
failureCallback(Error::Forwarded, message);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case 403: {
|
||||
failureCallback(Error::UserNotAuthorized, message);
|
||||
}
|
||||
break;
|
||||
|
||||
default: {
|
||||
qCDebug(chatterinoTwitch)
|
||||
<< "Unhandled error data:" << result.status()
|
||||
<< result.getData() << obj;
|
||||
failureCallback(Error::Unknown, message);
|
||||
}
|
||||
break;
|
||||
}
|
||||
})
|
||||
.execute();
|
||||
}
|
||||
|
||||
// Ban/timeout a user
|
||||
// https://dev.twitch.tv/docs/api/reference#ban-user
|
||||
void Helix::banUser(QString broadcasterID, QString moderatorID, QString userID,
|
||||
@@ -1991,6 +2068,43 @@ void Helix::sendWhisper(
|
||||
.execute();
|
||||
}
|
||||
|
||||
// https://dev.twitch.tv/docs/api/reference#get-chatters
|
||||
void Helix::getChatters(
|
||||
QString broadcasterID, QString moderatorID, int maxChattersToFetch,
|
||||
ResultCallback<HelixChatters> successCallback,
|
||||
FailureCallback<HelixGetChattersError, QString> failureCallback)
|
||||
{
|
||||
static const auto NUM_CHATTERS_TO_FETCH = 1000;
|
||||
|
||||
auto finalChatters = std::make_shared<HelixChatters>();
|
||||
|
||||
ResultCallback<HelixChatters> fetchSuccess;
|
||||
|
||||
fetchSuccess = [this, broadcasterID, moderatorID, maxChattersToFetch,
|
||||
finalChatters, &fetchSuccess, successCallback,
|
||||
failureCallback](auto chatters) {
|
||||
qCDebug(chatterinoTwitch)
|
||||
<< "Fetched" << chatters.chatters.size() << "chatters";
|
||||
finalChatters->chatters.merge(chatters.chatters);
|
||||
finalChatters->total = chatters.total;
|
||||
|
||||
if (chatters.cursor.isEmpty() ||
|
||||
finalChatters->chatters.size() >= maxChattersToFetch)
|
||||
{
|
||||
// Done paginating
|
||||
successCallback(*finalChatters);
|
||||
return;
|
||||
}
|
||||
|
||||
this->fetchChatters(broadcasterID, moderatorID, NUM_CHATTERS_TO_FETCH,
|
||||
chatters.cursor, fetchSuccess, failureCallback);
|
||||
};
|
||||
|
||||
// Initiate the recursive calls
|
||||
this->fetchChatters(broadcasterID, moderatorID, NUM_CHATTERS_TO_FETCH, "",
|
||||
fetchSuccess, failureCallback);
|
||||
}
|
||||
|
||||
// List the VIPs of a channel
|
||||
// https://dev.twitch.tv/docs/api/reference#get-vips
|
||||
void Helix::getChannelVIPs(
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#include "common/Aliases.hpp"
|
||||
#include "common/NetworkRequest.hpp"
|
||||
#include "providers/twitch/TwitchEmotes.hpp"
|
||||
#include "util/QStringHash.hpp"
|
||||
|
||||
#include <QJsonArray>
|
||||
#include <QString>
|
||||
@@ -12,6 +13,7 @@
|
||||
#include <boost/optional.hpp>
|
||||
|
||||
#include <functional>
|
||||
#include <unordered_set>
|
||||
#include <vector>
|
||||
|
||||
namespace chatterino {
|
||||
@@ -342,6 +344,29 @@ struct HelixVip {
|
||||
}
|
||||
};
|
||||
|
||||
struct HelixChatters {
|
||||
std::unordered_set<QString> chatters;
|
||||
int total;
|
||||
QString cursor;
|
||||
|
||||
HelixChatters() = default;
|
||||
|
||||
explicit HelixChatters(const QJsonObject &jsonObject)
|
||||
: total(jsonObject.value("total").toInt())
|
||||
, cursor(jsonObject.value("pagination")
|
||||
.toObject()
|
||||
.value("cursor")
|
||||
.toString())
|
||||
{
|
||||
const auto &data = jsonObject.value("data").toArray();
|
||||
for (const auto &chatter : data)
|
||||
{
|
||||
auto userLogin = chatter.toObject().value("user_login").toString();
|
||||
this->chatters.insert(userLogin);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// TODO(jammehcow): when implementing mod list, just alias HelixVip to HelixMod
|
||||
// as they share the same model.
|
||||
// Alternatively, rename base struct to HelixUser or something and alias both
|
||||
@@ -519,6 +544,15 @@ enum class HelixWhisperError { // /w
|
||||
Forwarded,
|
||||
}; // /w
|
||||
|
||||
enum class HelixGetChattersError {
|
||||
Unknown,
|
||||
UserMissingScope,
|
||||
UserNotAuthorized,
|
||||
|
||||
// The error message is forwarded directly from the Twitch API
|
||||
Forwarded,
|
||||
};
|
||||
|
||||
enum class HelixListVIPsError { // /vips
|
||||
Unknown,
|
||||
UserMissingScope,
|
||||
@@ -784,6 +818,14 @@ public:
|
||||
ResultCallback<> successCallback,
|
||||
FailureCallback<HelixWhisperError, QString> failureCallback) = 0;
|
||||
|
||||
// Get Chatters from the `broadcasterID` channel
|
||||
// This will follow the returned cursor and return up to `maxChattersToFetch` chatters
|
||||
// https://dev.twitch.tv/docs/api/reference#get-chatters
|
||||
virtual void getChatters(
|
||||
QString broadcasterID, QString moderatorID, int maxChattersToFetch,
|
||||
ResultCallback<HelixChatters> successCallback,
|
||||
FailureCallback<HelixGetChattersError, QString> failureCallback) = 0;
|
||||
|
||||
// https://dev.twitch.tv/docs/api/reference#get-vips
|
||||
virtual void getChannelVIPs(
|
||||
QString broadcasterID,
|
||||
@@ -1045,6 +1087,14 @@ public:
|
||||
ResultCallback<> successCallback,
|
||||
FailureCallback<HelixWhisperError, QString> failureCallback) final;
|
||||
|
||||
// Get Chatters from the `broadcasterID` channel
|
||||
// This will follow the returned cursor and return up to `maxChattersToFetch` chatters
|
||||
// https://dev.twitch.tv/docs/api/reference#get-chatters
|
||||
void getChatters(
|
||||
QString broadcasterID, QString moderatorID, int maxChattersToFetch,
|
||||
ResultCallback<HelixChatters> successCallback,
|
||||
FailureCallback<HelixGetChattersError, QString> failureCallback) final;
|
||||
|
||||
// https://dev.twitch.tv/docs/api/reference#get-vips
|
||||
void getChannelVIPs(
|
||||
QString broadcasterID,
|
||||
@@ -1063,6 +1113,13 @@ protected:
|
||||
FailureCallback<HelixUpdateChatSettingsError, QString> failureCallback)
|
||||
final;
|
||||
|
||||
// Get chatters list - This method is what actually runs the API request
|
||||
// https://dev.twitch.tv/docs/api/reference#get-chatters
|
||||
void fetchChatters(
|
||||
QString broadcasterID, QString moderatorID, int first, QString after,
|
||||
ResultCallback<HelixChatters> successCallback,
|
||||
FailureCallback<HelixGetChattersError, QString> failureCallback);
|
||||
|
||||
private:
|
||||
NetworkRequest makeRequest(QString url, QUrlQuery urlQuery);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user