feat: add /lockprediction and /cancelprediction commands for broadcasters (#6612)
Reviewed-by: pajlada <rasmus.karlsson@pajlada.com>
This commit is contained in:
@@ -3426,6 +3426,117 @@ void Helix::createPrediction(const QString broadcasterID, const QString title,
|
||||
.execute();
|
||||
}
|
||||
|
||||
void Helix::getPredictions(const QString broadcasterID, QStringList ids,
|
||||
const int first, const QString after,
|
||||
ResultCallback<HelixPredictions> successCallback,
|
||||
FailureCallback<QString> failureCallback)
|
||||
{
|
||||
QUrlQuery urlQuery;
|
||||
urlQuery.addQueryItem("broadcaster_id", broadcasterID);
|
||||
urlQuery.addQueryItem("first", QString::number(first));
|
||||
|
||||
if (!after.isEmpty())
|
||||
{
|
||||
urlQuery.addQueryItem("after", after);
|
||||
}
|
||||
|
||||
for (const auto &id : ids)
|
||||
{
|
||||
urlQuery.addQueryItem("id", id);
|
||||
}
|
||||
|
||||
this->makeGet("predictions", urlQuery)
|
||||
.onSuccess([successCallback](const auto &result) {
|
||||
if (result.status() != 200)
|
||||
{
|
||||
qCWarning(chatterinoTwitch)
|
||||
<< "Success result for getting predictions was "
|
||||
<< result.formatError() << "but we expected it to be 200";
|
||||
}
|
||||
|
||||
const auto response = result.parseJson();
|
||||
successCallback(HelixPredictions(response));
|
||||
})
|
||||
.onError([failureCallback](const auto &result) -> void {
|
||||
if (!result.status())
|
||||
{
|
||||
failureCallback(result.formatError());
|
||||
return;
|
||||
}
|
||||
|
||||
auto obj = result.parseJson();
|
||||
auto message = obj.value("message").toString();
|
||||
if (!message.isEmpty())
|
||||
{
|
||||
failureCallback(message);
|
||||
}
|
||||
else
|
||||
{
|
||||
failureCallback(result.formatError());
|
||||
}
|
||||
})
|
||||
.execute();
|
||||
}
|
||||
|
||||
// End prediction can lock, cancel, or resolve an outstanding prediction.
|
||||
void Helix::endPrediction(const QString broadcasterID, const QString id,
|
||||
const bool refundPoints,
|
||||
const QString winningOutcomeID,
|
||||
ResultCallback<HelixPrediction> successCallback,
|
||||
FailureCallback<QString> failureCallback)
|
||||
{
|
||||
QJsonObject payload;
|
||||
payload.insert("broadcaster_id", broadcasterID);
|
||||
payload.insert("id", id);
|
||||
if (refundPoints)
|
||||
{
|
||||
payload.insert("status", "CANCELED");
|
||||
}
|
||||
else if (winningOutcomeID.isEmpty())
|
||||
{
|
||||
payload.insert("status", "LOCKED");
|
||||
}
|
||||
else
|
||||
{
|
||||
payload.insert("status", "RESOLVED");
|
||||
payload.insert("winning_outcome_id", winningOutcomeID);
|
||||
}
|
||||
|
||||
this->makePatch("predictions", {})
|
||||
.json(payload)
|
||||
.onSuccess([successCallback](const NetworkResult &result) {
|
||||
if (result.status() != 200)
|
||||
{
|
||||
qCWarning(chatterinoTwitch)
|
||||
<< "Success result for ending a prediction was "
|
||||
<< result.formatError() << "but we expected it to be 200";
|
||||
}
|
||||
|
||||
const auto response = result.parseJson();
|
||||
const auto data = HelixPredictions(response);
|
||||
successCallback(data.predictions.front());
|
||||
})
|
||||
.onError([failureCallback](const NetworkResult &result) -> void {
|
||||
if (!result.status())
|
||||
{
|
||||
failureCallback(result.formatError());
|
||||
return;
|
||||
}
|
||||
|
||||
const auto obj = result.parseJson();
|
||||
const auto message = obj.value("message").toString();
|
||||
if (!message.isEmpty())
|
||||
{
|
||||
failureCallback(message);
|
||||
}
|
||||
else
|
||||
{
|
||||
failureCallback(result.formatError());
|
||||
}
|
||||
})
|
||||
.execute();
|
||||
}
|
||||
|
||||
void Helix::createEventSubSubscription(
|
||||
const eventsub::SubscriptionRequest &request, const QString &sessionID,
|
||||
ResultCallback<HelixCreateEventSubSubscriptionResponse> successCallback,
|
||||
|
||||
@@ -488,8 +488,9 @@ struct HelixPoll {
|
||||
, title(jsonObject.value("title").toString())
|
||||
, status(jsonObject.value("status").toString())
|
||||
{
|
||||
for (const auto &data = jsonObject.value("choices").toArray();
|
||||
const auto &c : data)
|
||||
const auto &data = jsonObject.value("choices").toArray();
|
||||
this->choices.reserve(data.size());
|
||||
for (const auto &c : data)
|
||||
{
|
||||
HelixPollChoice choice(c.toObject());
|
||||
this->choices.push_back(choice);
|
||||
@@ -504,8 +505,9 @@ struct HelixPolls {
|
||||
|
||||
explicit HelixPolls(const QJsonObject &jsonObject)
|
||||
{
|
||||
for (const auto &data = jsonObject.value("data").toArray();
|
||||
const auto &p : data)
|
||||
const auto &data = jsonObject.value("data").toArray();
|
||||
this->polls.reserve(data.size());
|
||||
for (const auto &p : data)
|
||||
{
|
||||
HelixPoll poll(p.toObject());
|
||||
this->polls.push_back(poll);
|
||||
@@ -513,6 +515,61 @@ struct HelixPolls {
|
||||
}
|
||||
};
|
||||
|
||||
struct HelixPredictionOutcome {
|
||||
QString id;
|
||||
QString title;
|
||||
int users;
|
||||
int channelPoints;
|
||||
|
||||
explicit HelixPredictionOutcome(const QJsonObject &jsonObject)
|
||||
: id(jsonObject.value("id").toString())
|
||||
, title(jsonObject.value("title").toString())
|
||||
, users(jsonObject.value("users").toInt())
|
||||
, channelPoints(jsonObject.value("channel_points").toInt())
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
struct HelixPrediction {
|
||||
QString id;
|
||||
QString title;
|
||||
QString winningOutcomeID;
|
||||
QString status;
|
||||
std::vector<HelixPredictionOutcome> outcomes;
|
||||
|
||||
explicit HelixPrediction(const QJsonObject &jsonObject)
|
||||
: id(jsonObject.value("id").toString())
|
||||
, title(jsonObject.value("title").toString())
|
||||
, winningOutcomeID(jsonObject.value("winning_outcome_id").toString())
|
||||
, status(jsonObject.value("status").toString())
|
||||
{
|
||||
const auto &data = jsonObject.value("outcomes").toArray();
|
||||
this->outcomes.reserve(data.size());
|
||||
for (const auto &o : data)
|
||||
{
|
||||
HelixPredictionOutcome outcome(o.toObject());
|
||||
this->outcomes.push_back(outcome);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
struct HelixPredictions {
|
||||
std::vector<HelixPrediction> predictions;
|
||||
|
||||
HelixPredictions() = default;
|
||||
|
||||
explicit HelixPredictions(const QJsonObject &jsonObject)
|
||||
{
|
||||
const auto &data = jsonObject.value("data").toArray();
|
||||
this->predictions.reserve(data.size());
|
||||
for (const auto &p : data)
|
||||
{
|
||||
HelixPrediction prediction(p.toObject());
|
||||
this->predictions.push_back(prediction);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
enum class HelixAnnouncementColor {
|
||||
Blue,
|
||||
Green,
|
||||
@@ -1286,6 +1343,18 @@ public:
|
||||
ResultCallback<> successCallback,
|
||||
FailureCallback<QString> failureCallback) = 0;
|
||||
|
||||
/// https://dev.twitch.tv/docs/api/reference#get-predictions
|
||||
virtual void getPredictions(
|
||||
QString broadcasterID, QStringList ids, int first, QString after,
|
||||
ResultCallback<HelixPredictions> successCallback,
|
||||
FailureCallback<QString> failureCallback) = 0;
|
||||
|
||||
/// https://dev.twitch.tv/docs/api/reference#end-prediction
|
||||
virtual void endPrediction(QString broadcasterID, QString id,
|
||||
bool refundPoints, QString winningOutcomeID,
|
||||
ResultCallback<HelixPrediction> successCallback,
|
||||
FailureCallback<QString> failureCallback) = 0;
|
||||
|
||||
// https://dev.twitch.tv/docs/api/reference/#create-eventsub-subscription
|
||||
virtual void createEventSubSubscription(
|
||||
const eventsub::SubscriptionRequest &request, const QString &sessionID,
|
||||
@@ -1663,6 +1732,18 @@ public:
|
||||
ResultCallback<> successCallback,
|
||||
FailureCallback<QString> failureCallback) final;
|
||||
|
||||
/// https://dev.twitch.tv/docs/api/reference#get-predictions
|
||||
void getPredictions(QString broadcasterID, QStringList ids, int first,
|
||||
QString after,
|
||||
ResultCallback<HelixPredictions> successCallback,
|
||||
FailureCallback<QString> failureCallback) final;
|
||||
|
||||
/// https://dev.twitch.tv/docs/api/reference#end-prediction
|
||||
void endPrediction(QString broadcasterID, QString id, bool refundPoints,
|
||||
QString winningOutcomeID,
|
||||
ResultCallback<HelixPrediction> successCallback,
|
||||
FailureCallback<QString> failureCallback) final;
|
||||
|
||||
// https://dev.twitch.tv/docs/api/reference/#create-eventsub-subscription
|
||||
void createEventSubSubscription(
|
||||
const eventsub::SubscriptionRequest &request, const QString &sessionID,
|
||||
|
||||
Reference in New Issue
Block a user