mirror of
https://github.com/Chatterino/chatterino2.git
synced 2024-11-21 22:24:07 +01:00
Migrate /chatters commands to use Helix api (#4088)
Co-authored-by: Rasmus Karlsson <rasmus.karlsson@pajlada.com>
This commit is contained in:
parent
abb69f6794
commit
495f3ed4a9
6 changed files with 257 additions and 59 deletions
|
@ -69,6 +69,7 @@
|
|||
- Minor: Migrated /uniquechatoff and /r9kbetaoff to Helix API. (#4057)
|
||||
- Minor: Added stream titles to windows live toast notifications. (#1297)
|
||||
- Minor: Make menus and placeholders display appropriate custom key combos. (#4045)
|
||||
- Minor: Migrated /chatters to Helix API. (#4088)
|
||||
- Minor: Add settings tooltips. (#3437)
|
||||
- Bugfix: Connection to Twitch PubSub now recovers more reliably. (#3643, #3716)
|
||||
- Bugfix: Fixed `Smooth scrolling on new messages` setting sometimes hiding messages. (#4028)
|
||||
|
|
|
@ -878,8 +878,40 @@ void CommandController::initialize(Settings &, Paths &paths)
|
|||
return "";
|
||||
});
|
||||
|
||||
this->registerCommand(
|
||||
"/chatters", [](const auto & /*words*/, auto channel) {
|
||||
this->registerCommand("/chatters", [](const auto &words, auto channel) {
|
||||
auto formatError = [](HelixGetChattersError error, QString message) {
|
||||
using Error = HelixGetChattersError;
|
||||
|
||||
QString errorMessage = QString("Failed to get chatter count: ");
|
||||
|
||||
switch (error)
|
||||
{
|
||||
case Error::Forwarded: {
|
||||
errorMessage += message;
|
||||
}
|
||||
break;
|
||||
|
||||
case Error::UserMissingScope: {
|
||||
errorMessage += "Missing required scope. "
|
||||
"Re-login with your "
|
||||
"account and try again.";
|
||||
}
|
||||
break;
|
||||
|
||||
case Error::UserNotAuthorized: {
|
||||
errorMessage += "You must have moderator permissions to "
|
||||
"use this command.";
|
||||
}
|
||||
break;
|
||||
|
||||
case Error::Unknown: {
|
||||
errorMessage += "An unknown error has occurred.";
|
||||
}
|
||||
break;
|
||||
}
|
||||
return errorMessage;
|
||||
};
|
||||
|
||||
auto twitchChannel = dynamic_cast<TwitchChannel *>(channel.get());
|
||||
|
||||
if (twitchChannel == nullptr)
|
||||
|
@ -889,9 +921,19 @@ void CommandController::initialize(Settings &, Paths &paths)
|
|||
return "";
|
||||
}
|
||||
|
||||
channel->addMessage(makeSystemMessage(
|
||||
QString("Chatter count: %1")
|
||||
.arg(localizeNumbers(twitchChannel->chatterCount()))));
|
||||
// Refresh chatter list via helix api for mods
|
||||
getHelix()->getChatters(
|
||||
twitchChannel->roomId(),
|
||||
getApp()->accounts->twitch.getCurrent()->getUserId(), 1,
|
||||
[channel](auto result) {
|
||||
channel->addMessage(
|
||||
makeSystemMessage(QString("Chatter count: %1")
|
||||
.arg(localizeNumbers(result.total))));
|
||||
},
|
||||
[channel, formatError](auto error, auto message) {
|
||||
auto errorMessage = formatError(error, message);
|
||||
channel->addMessage(makeSystemMessage(errorMessage));
|
||||
});
|
||||
|
||||
return "";
|
||||
});
|
||||
|
|
|
@ -49,29 +49,8 @@ namespace {
|
|||
const QString LOGIN_PROMPT_TEXT("Click here to add your account again.");
|
||||
const Link ACCOUNTS_LINK(Link::OpenAccountsPage, QString());
|
||||
|
||||
std::pair<Outcome, std::unordered_set<QString>> parseChatters(
|
||||
const QJsonObject &jsonRoot)
|
||||
{
|
||||
static QStringList categories = {"broadcaster", "vips", "moderators",
|
||||
"staff", "admins", "global_mods",
|
||||
"viewers"};
|
||||
|
||||
auto usernames = std::unordered_set<QString>();
|
||||
|
||||
// parse json
|
||||
QJsonObject jsonCategories = jsonRoot.value("chatters").toObject();
|
||||
|
||||
for (const auto &category : categories)
|
||||
{
|
||||
for (auto jsonCategory : jsonCategories.value(category).toArray())
|
||||
{
|
||||
usernames.insert(jsonCategory.toString());
|
||||
}
|
||||
}
|
||||
|
||||
return {Success, std::move(usernames)};
|
||||
}
|
||||
|
||||
// Maximum number of chatters to fetch when refreshing chatters
|
||||
constexpr auto MAX_CHATTERS_TO_FETCH = 5000;
|
||||
} // namespace
|
||||
|
||||
TwitchChannel::TwitchChannel(const QString &name)
|
||||
|
@ -136,9 +115,11 @@ TwitchChannel::TwitchChannel(const QString &name)
|
|||
});
|
||||
|
||||
// timers
|
||||
|
||||
QObject::connect(&this->chattersListTimer_, &QTimer::timeout, [=] {
|
||||
this->refreshChatters();
|
||||
});
|
||||
|
||||
this->chattersListTimer_.start(5 * 60 * 1000);
|
||||
|
||||
QObject::connect(&this->threadClearTimer_, &QTimer::timeout, [=] {
|
||||
|
@ -905,6 +886,12 @@ void TwitchChannel::refreshPubSub()
|
|||
|
||||
void TwitchChannel::refreshChatters()
|
||||
{
|
||||
// helix endpoint only works for mods
|
||||
if (!this->hasModRights())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// setting?
|
||||
const auto streamStatus = this->accessStreamStatus();
|
||||
const auto viewerCount = static_cast<int>(streamStatus->viewerCount);
|
||||
|
@ -917,31 +904,19 @@ void TwitchChannel::refreshChatters()
|
|||
}
|
||||
}
|
||||
|
||||
// get viewer list
|
||||
NetworkRequest("https://tmi.twitch.tv/group/user/" + this->getName() +
|
||||
"/chatters")
|
||||
|
||||
.onSuccess(
|
||||
[this, weak = weakOf<Channel>(this)](auto result) -> Outcome {
|
||||
// channel still exists?
|
||||
auto shared = weak.lock();
|
||||
if (!shared)
|
||||
// Get chatter list via helix api
|
||||
getHelix()->getChatters(
|
||||
this->roomId(), getApp()->accounts->twitch.getCurrent()->getUserId(),
|
||||
MAX_CHATTERS_TO_FETCH,
|
||||
[this, weak = weakOf<Channel>(this)](auto result) {
|
||||
if (auto shared = weak.lock())
|
||||
{
|
||||
return Failure;
|
||||
this->updateOnlineChatters(result.chatters);
|
||||
this->chatterCount_ = result.total;
|
||||
}
|
||||
|
||||
auto data = result.parseJson();
|
||||
this->chatterCount_ = data.value("chatter_count").toInt();
|
||||
|
||||
auto pair = parseChatters(std::move(data));
|
||||
if (pair.first)
|
||||
{
|
||||
this->updateOnlineChatters(pair.second);
|
||||
}
|
||||
|
||||
return pair.first;
|
||||
})
|
||||
.execute();
|
||||
},
|
||||
// Refresh chatters should only be used when failing silently is an option
|
||||
[](auto error, auto message) {});
|
||||
}
|
||||
|
||||
void TwitchChannel::fetchDisplayName()
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -360,6 +360,15 @@ public:
|
|||
(FailureCallback<HelixWhisperError, QString> failureCallback)),
|
||||
(override)); // /w
|
||||
|
||||
// getChatters
|
||||
// The extra parenthesis around the failure callback is because its type contains a comma
|
||||
MOCK_METHOD(
|
||||
void, getChatters,
|
||||
(QString broadcasterID, QString moderatorID, int maxChattersToFetch,
|
||||
ResultCallback<HelixChatters> successCallback,
|
||||
(FailureCallback<HelixGetChattersError, QString> failureCallback)),
|
||||
(override)); // getChatters
|
||||
|
||||
// /vips
|
||||
// The extra parenthesis around the failure callback is because its type contains a comma
|
||||
MOCK_METHOD(
|
||||
|
|
Loading…
Reference in a new issue