Implement an advanced method of adding a user to chatterino.
You can now switch between multiple users in the settings dialog. (Requires a restart for reconnecting etc)
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "credentials.hpp"
|
||||
|
||||
#include <QEventLoop>
|
||||
#include <QJsonArray>
|
||||
#include <QJsonDocument>
|
||||
@@ -16,6 +18,67 @@
|
||||
namespace chatterino {
|
||||
namespace util {
|
||||
|
||||
namespace twitch {
|
||||
|
||||
static void get(QString url, std::function<void(QJsonObject &)> successCallback)
|
||||
{
|
||||
auto manager = new QNetworkAccessManager();
|
||||
|
||||
QUrl requestUrl(url);
|
||||
QNetworkRequest request(requestUrl);
|
||||
|
||||
request.setRawHeader("Client-ID", getDefaultClientID());
|
||||
request.setRawHeader("Accept", "application/vnd.twitchtv.v5+json");
|
||||
|
||||
QNetworkReply *reply = manager->get(request);
|
||||
|
||||
QObject::connect(reply, &QNetworkReply::finished, [=] {
|
||||
if (reply->error() == QNetworkReply::NetworkError::NoError) {
|
||||
QByteArray data = reply->readAll();
|
||||
QJsonDocument jsonDoc(QJsonDocument::fromJson(data));
|
||||
|
||||
if (!jsonDoc.isNull()) {
|
||||
QJsonObject rootNode = jsonDoc.object();
|
||||
|
||||
successCallback(rootNode);
|
||||
}
|
||||
}
|
||||
|
||||
reply->deleteLater();
|
||||
manager->deleteLater();
|
||||
});
|
||||
}
|
||||
|
||||
static void getUserID(QString username, std::function<void(QString)> successCallback)
|
||||
{
|
||||
get("https://api.twitch.tv/kraken/users?login=" + username, [=](const QJsonObject &root) {
|
||||
if (!root.value("users").isArray()) {
|
||||
qDebug() << "API Error while getting user id, users is not an array";
|
||||
return;
|
||||
}
|
||||
|
||||
auto users = root.value("users").toArray();
|
||||
if (users.size() != 1) {
|
||||
qDebug() << "API Error while getting user id, users array size is not 1";
|
||||
return;
|
||||
}
|
||||
if (!users[0].isObject()) {
|
||||
qDebug() << "API Error while getting user id, first user is not an object";
|
||||
return;
|
||||
}
|
||||
auto firstUser = users[0].toObject();
|
||||
auto id = firstUser.value("_id");
|
||||
if (!id.isString()) {
|
||||
qDebug()
|
||||
<< "API Error: while getting user id, first user object `_id` key is not a string";
|
||||
return;
|
||||
}
|
||||
successCallback(id.toString());
|
||||
});
|
||||
}
|
||||
|
||||
} // namespace twitch
|
||||
|
||||
static void urlFetch(const QString &url, std::function<void(QNetworkReply &)> successCallback,
|
||||
QNetworkAccessManager *manager = nullptr)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user