Refactor NetworkRequest class

Add followUser and unfollowUser methods to TwitchAccount
This commit is contained in:
Rasmus Karlsson 2018-07-07 11:08:57 +00:00
parent cada32edfd
commit 6a418e6e59
27 changed files with 835 additions and 669 deletions

View file

@ -103,6 +103,8 @@ SOURCES += \
src/common/CompletionModel.cpp \ src/common/CompletionModel.cpp \
src/common/Emotemap.cpp \ src/common/Emotemap.cpp \
src/common/NetworkManager.cpp \ src/common/NetworkManager.cpp \
src/common/NetworkResult.cpp \
src/common/NetworkData.cpp \
src/common/NetworkRequest.cpp \ src/common/NetworkRequest.cpp \
src/controllers/accounts/Account.cpp \ src/controllers/accounts/Account.cpp \
src/controllers/accounts/AccountController.cpp \ src/controllers/accounts/AccountController.cpp \
@ -138,6 +140,7 @@ SOURCES += \
src/providers/irc/IrcConnection2.cpp \ src/providers/irc/IrcConnection2.cpp \
src/providers/irc/IrcServer.cpp \ src/providers/irc/IrcServer.cpp \
src/providers/twitch/IrcMessageHandler.cpp \ src/providers/twitch/IrcMessageHandler.cpp \
src/providers/twitch/PartialTwitchUser.cpp \
src/providers/twitch/PubsubActions.cpp \ src/providers/twitch/PubsubActions.cpp \
src/providers/twitch/PubsubHelpers.cpp \ src/providers/twitch/PubsubHelpers.cpp \
src/providers/twitch/TwitchAccount.cpp \ src/providers/twitch/TwitchAccount.cpp \
@ -239,6 +242,8 @@ HEADERS += \
src/common/LockedObject.hpp \ src/common/LockedObject.hpp \
src/common/MutexValue.hpp \ src/common/MutexValue.hpp \
src/common/NetworkManager.hpp \ src/common/NetworkManager.hpp \
src/common/NetworkResult.hpp \
src/common/NetworkData.hpp \
src/common/NetworkRequest.hpp \ src/common/NetworkRequest.hpp \
src/common/NetworkRequester.hpp \ src/common/NetworkRequester.hpp \
src/common/NetworkWorker.hpp \ src/common/NetworkWorker.hpp \
@ -294,6 +299,7 @@ HEADERS += \
src/providers/irc/IrcServer.hpp \ src/providers/irc/IrcServer.hpp \
src/providers/twitch/EmoteValue.hpp \ src/providers/twitch/EmoteValue.hpp \
src/providers/twitch/IrcMessageHandler.hpp \ src/providers/twitch/IrcMessageHandler.hpp \
src/providers/twitch/PartialTwitchUser.hpp \
src/providers/twitch/PubsubActions.hpp \ src/providers/twitch/PubsubActions.hpp \
src/providers/twitch/PubsubHelpers.hpp \ src/providers/twitch/PubsubHelpers.hpp \
src/providers/twitch/TwitchAccount.hpp \ src/providers/twitch/TwitchAccount.hpp \

View file

@ -0,0 +1,22 @@
#pragma once
#include <functional>
class QNetworkReply;
namespace chatterino {
class NetworkResult;
using NetworkSuccessCallback = std::function<bool(NetworkResult)>;
using NetworkErrorCallback = std::function<bool(int)>;
using NetworkReplyCreatedCallback = std::function<void(QNetworkReply *)>;
enum class NetworkRequestType {
Get,
Post,
Put,
Delete,
};
} // namespace chatterino

View file

@ -0,0 +1,45 @@
#include "common/NetworkData.hpp"
#include "Application.hpp"
#include "singletons/Paths.hpp"
#include <QCryptographicHash>
#include <QFile>
namespace chatterino {
QString NetworkData::getHash()
{
if (this->hash_.isEmpty()) {
QByteArray bytes;
bytes.append(this->request_.url().toString());
for (const auto &header : this->request_.rawHeaderList()) {
bytes.append(header);
}
QByteArray hashBytes(QCryptographicHash::hash(bytes, QCryptographicHash::Sha256));
this->hash_ = hashBytes.toHex();
}
return this->hash_;
}
void NetworkData::writeToCache(const QByteArray &bytes)
{
if (this->useQuickLoadCache_) {
auto app = getApp();
QFile cachedFile(app->paths->cacheDirectory + "/" + this->getHash());
if (cachedFile.open(QIODevice::WriteOnly)) {
cachedFile.write(bytes);
cachedFile.close();
}
}
}
} // namespace chatterino

View file

@ -0,0 +1,36 @@
#pragma once
#include "common/NetworkCommon.hpp"
#include <QNetworkRequest>
#include <functional>
class QNetworkReply;
namespace chatterino {
class NetworkResult;
struct NetworkData {
QNetworkRequest request_;
const QObject *caller_ = nullptr;
bool useQuickLoadCache_{};
NetworkReplyCreatedCallback onReplyCreated_;
NetworkErrorCallback onError_;
NetworkSuccessCallback onSuccess_;
NetworkRequestType requestType_ = NetworkRequestType::Get;
QByteArray payload_;
QString getHash();
void writeToCache(const QByteArray &bytes);
private:
QString hash_;
};
} // namespace chatterino

View file

@ -1,62 +1,78 @@
#include "common/NetworkRequest.hpp" #include "common/NetworkRequest.hpp"
#include "Application.hpp" #include "Application.hpp"
#include "common/NetworkManager.hpp"
#include "providers/twitch/TwitchCommon.hpp"
#include "singletons/Paths.hpp"
#include <QFile>
#include <cassert>
namespace chatterino { namespace chatterino {
NetworkRequest::NetworkRequest(const char *url) NetworkRequest::NetworkRequest(const std::string &url, NetworkRequestType requestType)
: timer(new NetworkTimer)
{ {
this->data.request.setUrl(QUrl(url)); this->data.request_.setUrl(QUrl(QString::fromStdString(url)));
this->data.requestType_ = requestType;
} }
NetworkRequest::NetworkRequest(const std::string &url) NetworkRequest::NetworkRequest(QUrl url, NetworkRequestType requestType)
: timer(new NetworkTimer)
{ {
this->data.request.setUrl(QUrl(QString::fromStdString(url))); this->data.request_.setUrl(url);
this->data.requestType_ = requestType;
} }
NetworkRequest::NetworkRequest(const QString &url) NetworkRequest::~NetworkRequest()
{ {
this->data.request.setUrl(QUrl(url)); assert(this->executed_);
} }
NetworkRequest::NetworkRequest(QUrl url) void NetworkRequest::setRequestType(NetworkRequestType newRequestType)
{ {
this->data.request.setUrl(url); this->data.requestType_ = newRequestType;
}
void NetworkRequest::setRequestType(RequestType newRequestType)
{
this->data.requestType = newRequestType;
} }
void NetworkRequest::setCaller(const QObject *caller) void NetworkRequest::setCaller(const QObject *caller)
{ {
this->data.caller = caller; this->data.caller_ = caller;
} }
void NetworkRequest::setOnReplyCreated(std::function<void(QNetworkReply *)> f) void NetworkRequest::onReplyCreated(NetworkReplyCreatedCallback cb)
{ {
this->data.onReplyCreated = f; this->data.onReplyCreated_ = cb;
}
void NetworkRequest::onError(NetworkErrorCallback cb)
{
this->data.onError_ = cb;
}
void NetworkRequest::onSuccess(NetworkSuccessCallback cb)
{
this->data.onSuccess_ = cb;
} }
void NetworkRequest::setRawHeader(const char *headerName, const char *value) void NetworkRequest::setRawHeader(const char *headerName, const char *value)
{ {
this->data.request.setRawHeader(headerName, value); this->data.request_.setRawHeader(headerName, value);
} }
void NetworkRequest::setRawHeader(const char *headerName, const QByteArray &value) void NetworkRequest::setRawHeader(const char *headerName, const QByteArray &value)
{ {
this->data.request.setRawHeader(headerName, value); this->data.request_.setRawHeader(headerName, value);
} }
void NetworkRequest::setRawHeader(const char *headerName, const QString &value) void NetworkRequest::setRawHeader(const char *headerName, const QString &value)
{ {
this->data.request.setRawHeader(headerName, value.toUtf8()); this->data.request_.setRawHeader(headerName, value.toUtf8());
} }
void NetworkRequest::setTimeout(int ms) void NetworkRequest::setTimeout(int ms)
{ {
this->data.timeoutMS = ms; this->timer->timeoutMS_ = ms;
} }
void NetworkRequest::makeAuthorizedV5(const QString &clientID, const QString &oauthToken) void NetworkRequest::makeAuthorizedV5(const QString &clientID, const QString &oauthToken)
@ -68,190 +84,174 @@ void NetworkRequest::makeAuthorizedV5(const QString &clientID, const QString &oa
} }
} }
void NetworkRequest::setUseQuickLoadCache(bool value) void NetworkRequest::setPayload(const QByteArray &payload)
{ {
this->data.useQuickLoadCache = value; this->data.payload_ = payload;
} }
void NetworkRequest::Data::writeToCache(const QByteArray &bytes) void NetworkRequest::setUseQuickLoadCache(bool value)
{ {
if (this->useQuickLoadCache) { this->data.useQuickLoadCache_ = value;
auto app = getApp();
QFile cachedFile(app->paths->cacheDirectory + "/" + this->getHash());
if (cachedFile.open(QIODevice::WriteOnly)) {
cachedFile.write(bytes);
cachedFile.close();
}
}
} }
void NetworkRequest::execute() void NetworkRequest::execute()
{ {
switch (this->data.requestType) { this->executed_ = true;
case GetRequest: {
this->executeGet(); switch (this->data.requestType_) {
case NetworkRequestType::Get: {
// Get requests try to load from cache, then perform the request
if (this->data.useQuickLoadCache_) {
if (this->tryLoadCachedFile()) {
Log("Loaded from cache");
// Successfully loaded from cache
return;
}
}
this->doRequest();
} break; } break;
case PutRequest: { case NetworkRequestType::Put: {
this->executePut(); // Put requests cannot be cached, therefore the request is called immediately
this->doRequest();
} break; } break;
case DeleteRequest: { case NetworkRequestType::Delete: {
this->executeDelete(); // Delete requests cannot be cached, therefore the request is called immediately
this->doRequest();
} break; } break;
default: { default: {
Log("[Execute] Unhandled request type {}", (int)this->data.requestType); Log("[Execute] Unhandled request type");
} break; } break;
} }
} }
void NetworkRequest::useCache() bool NetworkRequest::tryLoadCachedFile()
{ {
if (this->data.useQuickLoadCache) { auto app = getApp();
auto app = getApp();
QFile cachedFile(app->paths->cacheDirectory + "/" + this->data.getHash()); QFile cachedFile(app->paths->cacheDirectory + "/" + this->data.getHash());
if (cachedFile.exists()) { if (!cachedFile.exists()) {
if (cachedFile.open(QIODevice::ReadOnly)) { // File didn't exist
QByteArray bytes = cachedFile.readAll(); return false;
// qDebug() << "Loaded cached resource" << this->data.request.url();
auto document = parseJSONFromData2(bytes);
bool success = false;
if (!document.IsNull()) {
success = this->data.onSuccess(document);
}
cachedFile.close();
if (!success) {
// The images were not successfully loaded from the file
// XXX: Invalidate the cache file so we don't attempt to load it again next
// time
}
}
}
} }
if (!cachedFile.open(QIODevice::ReadOnly)) {
// File could not be opened
return false;
}
QByteArray bytes = cachedFile.readAll();
NetworkResult result(bytes);
bool success = this->data.onSuccess_(result);
cachedFile.close();
// XXX: If success is false, we should invalidate the cache file somehow/somewhere
return success;
} }
void NetworkRequest::doRequest() void NetworkRequest::doRequest()
{ {
QTimer *timer = nullptr;
if (this->data.timeoutMS > 0) {
timer = new QTimer;
}
NetworkRequester requester; NetworkRequester requester;
NetworkWorker *worker = new NetworkWorker; NetworkWorker *worker = new NetworkWorker;
worker->moveToThread(&NetworkManager::workerThread); worker->moveToThread(&NetworkManager::workerThread);
if (this->data.caller != nullptr) { this->timer->start();
QObject::connect(worker, &NetworkWorker::doneUrl, this->data.caller,
[data = this->data](auto reply) mutable {
if (reply->error() != QNetworkReply::NetworkError::NoError) {
if (data.onError) {
data.onError(reply->error());
}
return;
}
QByteArray readBytes = reply->readAll(); auto onUrlRequested = [data = std::move(this->data), timer = std::move(this->timer),
QByteArray bytes; worker]() mutable {
bytes.setRawData(readBytes.data(), readBytes.size()); QNetworkReply *reply = nullptr;
data.writeToCache(bytes); switch (data.requestType_) {
data.onSuccess(parseJSONFromData2(bytes)); case NetworkRequestType::Get: {
reply = NetworkManager::NaM.get(data.request_);
} break;
reply->deleteLater(); case NetworkRequestType::Put: {
}); reply = NetworkManager::NaM.put(data.request_, data.payload_);
} } break;
if (timer != nullptr) { case NetworkRequestType::Delete: {
timer->start(this->data.timeoutMS); reply = NetworkManager::NaM.deleteResource(data.request_);
} } break;
}
QObject::connect(&requester, &NetworkRequester::requestUrl, worker, if (reply == nullptr) {
[timer, data = std::move(this->data), worker]() { Log("Unhandled request type");
QNetworkReply *reply = nullptr; return;
switch (data.requestType) { }
case GetRequest: {
reply = NetworkManager::NaM.get(data.request);
} break;
case PutRequest: { if (timer->isStarted()) {
reply = NetworkManager::NaM.put(data.request, data.payload); timer->onTimeout(worker, [reply, data]() {
} break; Log("Aborted!");
reply->abort();
if (data.onError_) {
data.onError_(-2);
}
});
}
case DeleteRequest: { if (data.onReplyCreated_) {
reply = NetworkManager::NaM.deleteResource(data.request); data.onReplyCreated_(reply);
} break; }
}
if (reply == nullptr) { bool directAction = (data.caller_ == nullptr);
Log("Unhandled request type {}", (int)data.requestType);
return;
}
if (timer != nullptr) { auto handleReply = [data = std::move(data), timer = std::move(timer), reply]() mutable {
QObject::connect(timer, &QTimer::timeout, worker, if (reply->error() != QNetworkReply::NetworkError::NoError) {
[reply, timer, data]() { if (data.onError_) {
Log("Aborted!"); data.onError_(reply->error());
reply->abort(); }
timer->deleteLater(); return;
data.onError(-2); }
});
}
if (data.onReplyCreated) { QByteArray readBytes = reply->readAll();
data.onReplyCreated(reply); QByteArray bytes;
} bytes.setRawData(readBytes.data(), readBytes.size());
data.writeToCache(bytes);
QObject::connect(reply, &QNetworkReply::finished, worker, NetworkResult result(bytes);
[data = std::move(data), worker, reply]() mutable { data.onSuccess_(result);
if (data.caller == nullptr) {
QByteArray bytes = reply->readAll();
data.writeToCache(bytes);
if (data.onSuccess) { reply->deleteLater();
data.onSuccess(parseJSONFromData2(bytes)); };
} else {
qWarning() << "data.onSuccess not found";
}
reply->deleteLater(); if (data.caller_ != nullptr) {
} else { QObject::connect(worker, &NetworkWorker::doneUrl, data.caller_, std::move(handleReply));
emit worker->doneUrl(reply); QObject::connect(reply, &QNetworkReply::finished, worker, [worker]() mutable {
} emit worker->doneUrl();
delete worker; delete worker;
}); });
}); } else {
QObject::connect(reply, &QNetworkReply::finished, worker,
[handleReply = std::move(handleReply), worker]() mutable {
handleReply();
delete worker;
});
}
};
QObject::connect(&requester, &NetworkRequester::requestUrl, worker, std::move(onUrlRequested));
emit requester.requestUrl(); emit requester.requestUrl();
} }
void NetworkRequest::executeGet() // Helper creator functions
NetworkRequest NetworkRequest::twitchRequest(QUrl url)
{ {
this->useCache(); NetworkRequest request(url);
this->doRequest(); request.makeAuthorizedV5(getDefaultClientID());
return request;
} }
void NetworkRequest::executePut()
{
this->doRequest();
}
void NetworkRequest::executeDelete()
{
this->doRequest();
}
} // namespace chatterino } // namespace chatterino

View file

@ -1,248 +1,70 @@
#pragma once #pragma once
#include "Application.hpp" #include "Application.hpp"
#include "common/NetworkManager.hpp" #include "common/NetworkCommon.hpp"
#include "common/NetworkData.hpp"
#include "common/NetworkRequester.hpp" #include "common/NetworkRequester.hpp"
#include "common/NetworkResult.hpp"
#include "common/NetworkTimer.hpp"
#include "common/NetworkWorker.hpp" #include "common/NetworkWorker.hpp"
#include "singletons/Paths.hpp"
#include <rapidjson/document.h>
#include <rapidjson/error/en.h>
#include <QCryptographicHash>
#include <QFile>
namespace chatterino { namespace chatterino {
static QJsonObject parseJSONFromData(const QByteArray &data)
{
QJsonDocument jsonDoc(QJsonDocument::fromJson(data));
if (jsonDoc.isNull()) {
return QJsonObject();
}
return jsonDoc.object();
}
static rapidjson::Document parseJSONFromData2(const QByteArray &data)
{
rapidjson::Document ret(rapidjson::kNullType);
rapidjson::ParseResult result = ret.Parse(data.data(), data.length());
if (result.Code() != rapidjson::kParseErrorNone) {
Log("JSON parse error: {} ({})", rapidjson::GetParseError_En(result.Code()),
result.Offset());
return ret;
}
return ret;
}
class NetworkRequest class NetworkRequest
{ {
public: // Stores all data about the request that needs to be passed around to each part of the request
enum RequestType { NetworkData data;
GetRequest,
PostRequest,
PutRequest,
DeleteRequest,
};
private: // Timer that tracks the timeout
struct Data { // By default, there's no explicit timeout for the request
QNetworkRequest request; // to enable the timer, the "setTimeout" function needs to be called before execute is called
const QObject *caller = nullptr; std::unique_ptr<NetworkTimer> timer;
std::function<void(QNetworkReply *)> onReplyCreated;
int timeoutMS = -1;
bool useQuickLoadCache = false;
std::function<bool(int)> onError; // The NetworkRequest destructor will assert if executed_ hasn't been set to true before dying
std::function<bool(const rapidjson::Document &)> onSuccess; bool executed_ = false;
NetworkRequest::RequestType requestType;
QByteArray payload;
QString getHash()
{
if (this->hash.isEmpty()) {
QByteArray bytes;
bytes.append(this->request.url().toString());
for (const auto &header : this->request.rawHeaderList()) {
bytes.append(header);
}
QByteArray hashBytes(QCryptographicHash::hash(bytes, QCryptographicHash::Sha256));
this->hash = hashBytes.toHex();
}
return this->hash;
}
void writeToCache(const QByteArray &bytes);
private:
QString hash;
} data;
public: public:
NetworkRequest() = delete; NetworkRequest() = delete;
explicit NetworkRequest(const char *url); NetworkRequest(const NetworkRequest &other) = delete;
explicit NetworkRequest(const std::string &url); NetworkRequest &operator=(const NetworkRequest &other) = delete;
explicit NetworkRequest(const QString &url);
NetworkRequest(QUrl url);
void setRequestType(RequestType newRequestType); NetworkRequest(NetworkRequest &&other) = default;
NetworkRequest &operator=(NetworkRequest &&other) = default;
template <typename Func> explicit NetworkRequest(const std::string &url,
void onError(Func cb) NetworkRequestType requestType = NetworkRequestType::Get);
{ NetworkRequest(QUrl url, NetworkRequestType requestType = NetworkRequestType::Get);
this->data.onError = cb;
}
template <typename Func> ~NetworkRequest();
void onSuccess(Func cb)
{
this->data.onSuccess = cb;
}
void setPayload(const QByteArray &payload) void setRequestType(NetworkRequestType newRequestType);
{
this->data.payload = payload;
}
void onReplyCreated(NetworkReplyCreatedCallback cb);
void onError(NetworkErrorCallback cb);
void onSuccess(NetworkSuccessCallback cb);
void setPayload(const QByteArray &payload);
void setUseQuickLoadCache(bool value); void setUseQuickLoadCache(bool value);
void setCaller(const QObject *caller); void setCaller(const QObject *caller);
void setOnReplyCreated(std::function<void(QNetworkReply *)> f);
void setRawHeader(const char *headerName, const char *value); void setRawHeader(const char *headerName, const char *value);
void setRawHeader(const char *headerName, const QByteArray &value); void setRawHeader(const char *headerName, const QByteArray &value);
void setRawHeader(const char *headerName, const QString &value); void setRawHeader(const char *headerName, const QString &value);
void setTimeout(int ms); void setTimeout(int ms);
void makeAuthorizedV5(const QString &clientID, const QString &oauthToken = QString()); void makeAuthorizedV5(const QString &clientID, const QString &oauthToken = QString());
template <typename FinishedCallback>
void get(FinishedCallback onFinished)
{
if (this->data.useQuickLoadCache) {
auto app = getApp();
QFile cachedFile(app->paths->cacheDirectory + "/" + this->data.getHash());
if (cachedFile.exists()) {
if (cachedFile.open(QIODevice::ReadOnly)) {
QByteArray bytes = cachedFile.readAll();
// qDebug() << "Loaded cached resource" << this->data.request.url();
bool success = onFinished(bytes);
cachedFile.close();
if (!success) {
// The images were not successfully loaded from the file
// XXX: Invalidate the cache file so we don't attempt to load it again next
// time
}
}
}
}
QTimer *timer = nullptr;
if (this->data.timeoutMS > 0) {
timer = new QTimer;
}
NetworkRequester requester;
NetworkWorker *worker = new NetworkWorker;
worker->moveToThread(&NetworkManager::workerThread);
if (this->data.caller != nullptr) {
QObject::connect(worker, &NetworkWorker::doneUrl, this->data.caller,
[onFinished, data = this->data](auto reply) mutable {
if (reply->error() != QNetworkReply::NetworkError::NoError) {
if (data.onError) {
data.onError(reply->error());
}
return;
}
QByteArray readBytes = reply->readAll();
QByteArray bytes;
bytes.setRawData(readBytes.data(), readBytes.size());
data.writeToCache(bytes);
onFinished(bytes);
reply->deleteLater();
});
}
if (timer != nullptr) {
timer->start(this->data.timeoutMS);
}
QObject::connect(
&requester, &NetworkRequester::requestUrl, worker,
[timer, data = std::move(this->data), worker, onFinished{std::move(onFinished)}]() {
QNetworkReply *reply = NetworkManager::NaM.get(data.request);
if (timer != nullptr) {
QObject::connect(timer, &QTimer::timeout, worker, [reply, timer]() {
Log("Aborted!");
reply->abort();
timer->deleteLater();
});
}
if (data.onReplyCreated) {
data.onReplyCreated(reply);
}
QObject::connect(reply, &QNetworkReply::finished, worker,
[data = std::move(data), worker, reply,
onFinished = std::move(onFinished)]() mutable {
if (data.caller == nullptr) {
QByteArray bytes = reply->readAll();
data.writeToCache(bytes);
onFinished(bytes);
reply->deleteLater();
} else {
emit worker->doneUrl(reply);
}
delete worker;
});
});
emit requester.requestUrl();
}
template <typename FinishedCallback>
void getJSON(FinishedCallback onFinished)
{
this->get([onFinished{std::move(onFinished)}](const QByteArray &bytes) -> bool {
auto object = parseJSONFromData(bytes);
onFinished(object);
// XXX: Maybe return onFinished? For now I don't want to force onFinished to have a
// return value
return true;
});
}
void execute(); void execute();
private: private:
void useCache(); // Returns true if the file was successfully loaded from cache
// Returns false if the cache file either didn't exist, or it contained "invalid" data
// "invalid" is specified by the onSuccess callback
bool tryLoadCachedFile();
void doRequest(); void doRequest();
void executeGet();
void executePut(); public:
void executeDelete(); // Helper creator functions
static NetworkRequest twitchRequest(QUrl url);
}; };
} // namespace chatterino } // namespace chatterino

View file

@ -0,0 +1,46 @@
#include "common/NetworkResult.hpp"
#include "debug/Log.hpp"
#include <rapidjson/document.h>
#include <rapidjson/error/en.h>
#include <QJsonDocument>
namespace chatterino {
NetworkResult::NetworkResult(const QByteArray &data)
: data_(data)
{
}
QJsonObject NetworkResult::parseJson() const
{
QJsonDocument jsonDoc(QJsonDocument::fromJson(this->data_));
if (jsonDoc.isNull()) {
return QJsonObject{};
}
return jsonDoc.object();
}
rapidjson::Document NetworkResult::parseRapidJson() const
{
rapidjson::Document ret(rapidjson::kNullType);
rapidjson::ParseResult result = ret.Parse(this->data_.data(), this->data_.length());
if (result.Code() != rapidjson::kParseErrorNone) {
Log("JSON parse error: {} ({})", rapidjson::GetParseError_En(result.Code()),
result.Offset());
return ret;
}
return ret;
}
QByteArray NetworkResult::getData() const
{
return this->data_;
}
} // namespace chatterino

View file

@ -0,0 +1,20 @@
#pragma once
#include <rapidjson/document.h>
#include <QJsonObject>
namespace chatterino {
class NetworkResult
{
QByteArray data_;
public:
NetworkResult(const QByteArray &data);
QJsonObject parseJson() const;
rapidjson::Document parseRapidJson() const;
QByteArray getData() const;
};
} // namespace chatterino

View file

@ -0,0 +1,58 @@
#pragma once
#include "common/NetworkWorker.hpp"
#include <QTimer>
#include <cassert>
#include <functional>
#include <memory>
namespace chatterino {
class NetworkTimer
{
std::unique_ptr<QTimer> timer_;
bool started_{};
public:
int timeoutMS_ = -1;
NetworkTimer() = default;
~NetworkTimer() = default;
NetworkTimer(const NetworkTimer &other) = delete;
NetworkTimer &operator=(const NetworkTimer &other) = delete;
NetworkTimer(NetworkTimer &&other) = default;
NetworkTimer &operator=(NetworkTimer &&other) = default;
void start()
{
if (this->timeoutMS_ <= 0) {
return;
}
this->timer_ = std::make_unique<QTimer>();
this->timer_->start(this->timeoutMS_);
this->started_ = true;
}
bool isStarted() const
{
return this->started_;
}
void onTimeout(NetworkWorker *worker, std::function<void()> cb) const
{
if (!this->timer_) {
return;
}
QObject::connect(this->timer_.get(), &QTimer::timeout, worker, cb);
}
};
} // namespace chatterino

View file

@ -2,8 +2,6 @@
#include <QObject> #include <QObject>
class QNetworkReply;
namespace chatterino { namespace chatterino {
class NetworkWorker : public QObject class NetworkWorker : public QObject
@ -11,7 +9,7 @@ class NetworkWorker : public QObject
Q_OBJECT Q_OBJECT
signals: signals:
void doneUrl(QNetworkReply *); void doneUrl();
}; };
} // namespace chatterino } // namespace chatterino

View file

@ -1,147 +1,36 @@
#pragma once #pragma once
#include "common/NetworkManager.hpp"
#include "common/NetworkRequest.hpp" #include "common/NetworkRequest.hpp"
#include "controllers/accounts/AccountController.hpp"
#include "debug/Log.hpp"
#include "providers/twitch/TwitchCommon.hpp"
#include <rapidjson/document.h> #include <QObject>
#include <rapidjson/error/en.h>
#include <rapidjson/error/error.h>
#include <QByteArray>
#include <QEventLoop>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonValue>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QString> #include <QString>
#include <functional>
namespace chatterino { namespace chatterino {
static void twitchApiGet(QString url, const QObject *caller, // Not sure if I like these, but I'm trying them out
std::function<void(const QJsonObject &)> successCallback)
{
NetworkRequest req(url);
req.setCaller(caller);
req.setRawHeader("Client-ID", getDefaultClientID());
req.setRawHeader("Accept", "application/vnd.twitchtv.v5+json");
req.getJSON([=](const QJsonObject &node) { static NetworkRequest makeGetChannelRequest(const QString &channelId,
successCallback(node); // const QObject *caller = nullptr)
});
}
static void twitchApiGet2(QString url, const QObject *caller, bool useQuickLoadCache,
std::function<void(const rapidjson::Document &)> successCallback)
{ {
NetworkRequest request(url); QString url("https://api.twitch.tv/kraken/channels/" + channelId);
request.setRequestType(NetworkRequest::GetRequest);
auto request = NetworkRequest::twitchRequest(url);
request.setCaller(caller); request.setCaller(caller);
request.makeAuthorizedV5(getDefaultClientID());
request.setUseQuickLoadCache(useQuickLoadCache);
request.onSuccess([successCallback](const rapidjson::Document &document) { return request;
successCallback(document); //
return true;
});
request.execute();
} }
static void twitchApiGetUserID(QString username, const QObject *caller, static NetworkRequest makeGetStreamRequest(const QString &channelId,
std::function<void(QString)> successCallback) const QObject *caller = nullptr)
{ {
twitchApiGet( QString url("https://api.twitch.tv/kraken/streams/" + channelId);
"https://api.twitch.tv/kraken/users?login=" + username, caller,
[=](const QJsonObject &root) {
if (!root.value("users").isArray()) {
Log("API Error while getting user id, users is not an array");
return;
}
auto users = root.value("users").toArray(); auto request = NetworkRequest::twitchRequest(url);
if (users.size() != 1) {
Log("API Error while getting user id, users array size is not 1");
return;
}
if (!users[0].isObject()) {
Log("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()) {
Log("API Error: while getting user id, first user object `_id` key is not a "
"string");
return;
}
successCallback(id.toString());
});
}
static void twitchApiPut(QUrl url, std::function<void(const rapidjson::Document &)> successCallback)
{
NetworkRequest request(url);
request.setRequestType(NetworkRequest::PutRequest);
request.setCaller(QThread::currentThread());
auto currentTwitchUser = getApp()->accounts->twitch.getCurrent(); request.setCaller(caller);
QByteArray oauthToken;
if (currentTwitchUser) {
oauthToken = currentTwitchUser->getOAuthToken().toUtf8();
} else {
// XXX(pajlada): Bail out?
}
request.makeAuthorizedV5(getDefaultClientID(), currentTwitchUser->getOAuthToken()); return request;
request.onSuccess([successCallback](const auto &document) {
if (!document.IsNull()) {
successCallback(document);
}
return true;
});
request.execute();
}
static void twitchApiDelete(QUrl url, std::function<void()> successCallback)
{
NetworkRequest request(url);
request.setRequestType(NetworkRequest::DeleteRequest);
request.setCaller(QThread::currentThread());
auto currentTwitchUser = getApp()->accounts->twitch.getCurrent();
QByteArray oauthToken;
if (currentTwitchUser) {
oauthToken = currentTwitchUser->getOAuthToken().toUtf8();
} else {
// XXX(pajlada): Bail out?
}
request.makeAuthorizedV5(getDefaultClientID(), currentTwitchUser->getOAuthToken());
request.onError([successCallback](int code) {
if (code >= 200 && code <= 299) {
successCallback();
}
return true;
});
request.onSuccess([successCallback](const auto &document) {
successCallback();
return true;
});
request.execute();
} }
} // namespace chatterino } // namespace chatterino

View file

@ -66,7 +66,8 @@ void Image::loadImage()
NetworkRequest req(this->getUrl()); NetworkRequest req(this->getUrl());
req.setCaller(this); req.setCaller(this);
req.setUseQuickLoadCache(true); req.setUseQuickLoadCache(true);
req.get([this](QByteArray bytes) -> bool { req.onSuccess([this](auto result) -> bool {
auto bytes = result.getData();
QByteArray copy = QByteArray::fromRawData(bytes.constData(), bytes.length()); QByteArray copy = QByteArray::fromRawData(bytes.constData(), bytes.length());
QBuffer buffer(&copy); QBuffer buffer(&copy);
buffer.open(QIODevice::ReadOnly); buffer.open(QIODevice::ReadOnly);
@ -156,6 +157,8 @@ void Image::loadImage()
return true; return true;
}); });
req.execute();
} }
void Image::gifUpdateTimout() void Image::gifUpdateTimout()

View file

@ -21,11 +21,12 @@ void BTTVEmotes::loadGlobalEmotes()
{ {
QString url("https://api.betterttv.net/2/emotes"); QString url("https://api.betterttv.net/2/emotes");
NetworkRequest req(url); NetworkRequest request(url);
req.setCaller(QThread::currentThread()); request.setCaller(QThread::currentThread());
req.setTimeout(30000); request.setTimeout(30000);
req.setUseQuickLoadCache(true); request.setUseQuickLoadCache(true);
req.getJSON([this](QJsonObject &root) { request.onSuccess([this](auto result) {
auto root = result.parseJson();
auto emotes = root.value("emotes").toArray(); auto emotes = root.value("emotes").toArray();
QString urlTemplate = "https:" + root.value("urlTemplate").toString(); QString urlTemplate = "https:" + root.value("urlTemplate").toString();
@ -49,7 +50,11 @@ void BTTVEmotes::loadGlobalEmotes()
} }
this->globalEmoteCodes = codes; this->globalEmoteCodes = codes;
return true;
}); });
request.execute();
} }
void BTTVEmotes::loadChannelEmotes(const QString &channelName, std::weak_ptr<EmoteMap> _map) void BTTVEmotes::loadChannelEmotes(const QString &channelName, std::weak_ptr<EmoteMap> _map)
@ -60,15 +65,16 @@ void BTTVEmotes::loadChannelEmotes(const QString &channelName, std::weak_ptr<Emo
Log("Request bttv channel emotes for {}", channelName); Log("Request bttv channel emotes for {}", channelName);
NetworkRequest req(url); NetworkRequest request(url);
req.setCaller(QThread::currentThread()); request.setCaller(QThread::currentThread());
req.setTimeout(3000); request.setTimeout(3000);
req.setUseQuickLoadCache(true); request.setUseQuickLoadCache(true);
req.getJSON([this, channelName, _map](QJsonObject &rootNode) { request.onSuccess([this, channelName, _map](auto result) {
auto rootNode = result.parseJson();
auto map = _map.lock(); auto map = _map.lock();
if (_map.expired()) { if (_map.expired()) {
return; return false;
} }
map->clear(); map->clear();
@ -110,7 +116,11 @@ void BTTVEmotes::loadChannelEmotes(const QString &channelName, std::weak_ptr<Emo
} }
this->channelEmoteCodes[channelName] = codes; this->channelEmoteCodes[channelName] = codes;
return true;
}); });
request.execute();
} }
} // namespace chatterino } // namespace chatterino

View file

@ -46,11 +46,12 @@ void FFZEmotes::loadGlobalEmotes()
{ {
QString url("https://api.frankerfacez.com/v1/set/global"); QString url("https://api.frankerfacez.com/v1/set/global");
NetworkRequest req(url); NetworkRequest request(url);
req.setCaller(QThread::currentThread()); request.setCaller(QThread::currentThread());
req.setTimeout(30000); request.setTimeout(30000);
req.setUseQuickLoadCache(true); request.setUseQuickLoadCache(true);
req.getJSON([this](QJsonObject &root) { request.onSuccess([this](auto result) {
auto root = result.parseJson();
auto sets = root.value("sets").toObject(); auto sets = root.value("sets").toObject();
std::vector<QString> codes; std::vector<QString> codes;
@ -75,7 +76,11 @@ void FFZEmotes::loadGlobalEmotes()
this->globalEmoteCodes = codes; this->globalEmoteCodes = codes;
} }
return true;
}); });
request.execute();
} }
void FFZEmotes::loadChannelEmotes(const QString &channelName, std::weak_ptr<EmoteMap> _map) void FFZEmotes::loadChannelEmotes(const QString &channelName, std::weak_ptr<EmoteMap> _map)
@ -84,15 +89,16 @@ void FFZEmotes::loadChannelEmotes(const QString &channelName, std::weak_ptr<Emot
QString url("https://api.frankerfacez.com/v1/room/" + channelName); QString url("https://api.frankerfacez.com/v1/room/" + channelName);
NetworkRequest req(url); NetworkRequest request(url);
req.setCaller(QThread::currentThread()); request.setCaller(QThread::currentThread());
req.setTimeout(3000); request.setTimeout(3000);
req.setUseQuickLoadCache(true); request.setUseQuickLoadCache(true);
req.getJSON([this, channelName, _map](QJsonObject &rootNode) { request.onSuccess([this, channelName, _map](auto result) {
auto rootNode = result.parseJson();
auto map = _map.lock(); auto map = _map.lock();
if (_map.expired()) { if (_map.expired()) {
return; return false;
} }
map->clear(); map->clear();
@ -128,7 +134,11 @@ void FFZEmotes::loadChannelEmotes(const QString &channelName, std::weak_ptr<Emot
this->channelEmoteCodes[channelName] = codes; this->channelEmoteCodes[channelName] = codes;
} }
return true;
}); });
request.execute();
} }
} // namespace chatterino } // namespace chatterino

View file

@ -0,0 +1,70 @@
#include "providers/twitch/PartialTwitchUser.hpp"
#include "common/NetworkRequest.hpp"
#include "debug/Log.hpp"
#include "providers/twitch/TwitchCommon.hpp"
#include <cassert>
namespace chatterino {
PartialTwitchUser PartialTwitchUser::byName(const QString &username)
{
PartialTwitchUser user;
user.username_ = username;
return user;
}
PartialTwitchUser PartialTwitchUser::byId(const QString &id)
{
PartialTwitchUser user;
user.id_ = id;
return user;
}
void PartialTwitchUser::getId(std::function<void(QString)> successCallback, const QObject *caller)
{
assert(!this->username_.isEmpty());
if (caller == nullptr) {
caller = QThread::currentThread();
}
NetworkRequest request("https://api.twitch.tv/kraken/users?login=" + this->username_);
request.setCaller(caller);
request.makeAuthorizedV5(getDefaultClientID());
request.onSuccess([successCallback](auto result) {
auto root = result.parseJson();
if (!root.value("users").isArray()) {
Log("API Error while getting user id, users is not an array");
return false;
}
auto users = root.value("users").toArray();
if (users.size() != 1) {
Log("API Error while getting user id, users array size is not 1");
return false;
}
if (!users[0].isObject()) {
Log("API Error while getting user id, first user is not an object");
return false;
}
auto firstUser = users[0].toObject();
auto id = firstUser.value("_id");
if (!id.isString()) {
Log("API Error: while getting user id, first user object `_id` key is not a "
"string");
return false;
}
successCallback(id.toString());
return true;
});
request.execute();
}
} // namespace chatterino

View file

@ -0,0 +1,25 @@
#pragma once
#include <QObject>
#include <QString>
#include <functional>
namespace chatterino {
// Experimental class to test a method of calling APIs on twitch users
class PartialTwitchUser
{
PartialTwitchUser() = default;
QString username_;
QString id_;
public:
static PartialTwitchUser byName(const QString &username);
static PartialTwitchUser byId(const QString &id);
void getId(std::function<void(QString)> successCallback, const QObject *caller = nullptr);
};
} // namespace chatterino

View file

@ -3,6 +3,7 @@
#include "common/NetworkRequest.hpp" #include "common/NetworkRequest.hpp"
#include "common/UrlFetch.hpp" #include "common/UrlFetch.hpp"
#include "debug/Log.hpp" #include "debug/Log.hpp"
#include "providers/twitch/PartialTwitchUser.hpp"
#include "providers/twitch/TwitchCommon.hpp" #include "providers/twitch/TwitchCommon.hpp"
#include "util/RapidjsonHelpers.hpp" #include "util/RapidjsonHelpers.hpp"
@ -76,10 +77,10 @@ void TwitchAccount::loadIgnores()
QString url("https://api.twitch.tv/kraken/users/" + this->getUserId() + "/blocks"); QString url("https://api.twitch.tv/kraken/users/" + this->getUserId() + "/blocks");
NetworkRequest req(url); NetworkRequest req(url);
req.setRequestType(NetworkRequest::GetRequest);
req.setCaller(QThread::currentThread()); req.setCaller(QThread::currentThread());
req.makeAuthorizedV5(this->getOAuthClient(), this->getOAuthToken()); req.makeAuthorizedV5(this->getOAuthClient(), this->getOAuthToken());
req.onSuccess([=](const rapidjson::Document &document) { req.onSuccess([=](auto result) {
auto document = result.parseRapidJson();
if (!document.IsObject()) { if (!document.IsObject()) {
return false; return false;
} }
@ -125,9 +126,11 @@ void TwitchAccount::loadIgnores()
void TwitchAccount::ignore(const QString &targetName, void TwitchAccount::ignore(const QString &targetName,
std::function<void(IgnoreResult, const QString &)> onFinished) std::function<void(IgnoreResult, const QString &)> onFinished)
{ {
twitchApiGetUserID(targetName, QThread::currentThread(), [=](QString targetUserID) { const auto onIdFetched = [this, targetName, onFinished](QString targetUserId) {
this->ignoreByID(targetUserID, targetName, onFinished); // this->ignoreByID(targetUserId, targetName, onFinished); //
}); };
PartialTwitchUser::byName(this->userName_).getId(onIdFetched);
} }
void TwitchAccount::ignoreByID(const QString &targetUserID, const QString &targetName, void TwitchAccount::ignoreByID(const QString &targetUserID, const QString &targetName,
@ -136,8 +139,7 @@ void TwitchAccount::ignoreByID(const QString &targetUserID, const QString &targe
QString url("https://api.twitch.tv/kraken/users/" + this->getUserId() + "/blocks/" + QString url("https://api.twitch.tv/kraken/users/" + this->getUserId() + "/blocks/" +
targetUserID); targetUserID);
NetworkRequest req(url); NetworkRequest req(url, NetworkRequestType::Put);
req.setRequestType(NetworkRequest::PutRequest);
req.setCaller(QThread::currentThread()); req.setCaller(QThread::currentThread());
req.makeAuthorizedV5(this->getOAuthClient(), this->getOAuthToken()); req.makeAuthorizedV5(this->getOAuthClient(), this->getOAuthToken());
@ -148,7 +150,8 @@ void TwitchAccount::ignoreByID(const QString &targetUserID, const QString &targe
return true; return true;
}); });
req.onSuccess([=](const rapidjson::Document &document) { req.onSuccess([=](auto result) {
auto document = result.parseRapidJson();
if (!document.IsObject()) { if (!document.IsObject()) {
onFinished(IgnoreResult_Failed, "Bad JSON data while ignoring user " + targetName); onFinished(IgnoreResult_Failed, "Bad JSON data while ignoring user " + targetName);
return false; return false;
@ -190,9 +193,11 @@ void TwitchAccount::ignoreByID(const QString &targetUserID, const QString &targe
void TwitchAccount::unignore(const QString &targetName, void TwitchAccount::unignore(const QString &targetName,
std::function<void(UnignoreResult, const QString &message)> onFinished) std::function<void(UnignoreResult, const QString &message)> onFinished)
{ {
twitchApiGetUserID(targetName, QThread::currentThread(), [=](QString targetUserID) { const auto onIdFetched = [this, targetName, onFinished](QString targetUserId) {
this->unignoreByID(targetUserID, targetName, onFinished); // this->unignoreByID(targetUserId, targetName, onFinished); //
}); };
PartialTwitchUser::byName(this->userName_).getId(onIdFetched);
} }
void TwitchAccount::unignoreByID( void TwitchAccount::unignoreByID(
@ -202,8 +207,7 @@ void TwitchAccount::unignoreByID(
QString url("https://api.twitch.tv/kraken/users/" + this->getUserId() + "/blocks/" + QString url("https://api.twitch.tv/kraken/users/" + this->getUserId() + "/blocks/" +
targetUserID); targetUserID);
NetworkRequest req(url); NetworkRequest req(url, NetworkRequestType::Delete);
req.setRequestType(NetworkRequest::DeleteRequest);
req.setCaller(QThread::currentThread()); req.setCaller(QThread::currentThread());
req.makeAuthorizedV5(this->getOAuthClient(), this->getOAuthToken()); req.makeAuthorizedV5(this->getOAuthClient(), this->getOAuthToken());
@ -215,7 +219,8 @@ void TwitchAccount::unignoreByID(
return true; return true;
}); });
req.onSuccess([=](const rapidjson::Document &document) { req.onSuccess([=](auto result) {
auto document = result.parseRapidJson();
TwitchUser ignoredUser; TwitchUser ignoredUser;
ignoredUser.id = targetUserID; ignoredUser.id = targetUserID;
{ {
@ -238,7 +243,6 @@ void TwitchAccount::checkFollow(const QString targetUserID,
targetUserID); targetUserID);
NetworkRequest req(url); NetworkRequest req(url);
req.setRequestType(NetworkRequest::GetRequest);
req.setCaller(QThread::currentThread()); req.setCaller(QThread::currentThread());
req.makeAuthorizedV5(this->getOAuthClient(), this->getOAuthToken()); req.makeAuthorizedV5(this->getOAuthClient(), this->getOAuthToken());
@ -252,7 +256,8 @@ void TwitchAccount::checkFollow(const QString targetUserID,
return true; return true;
}); });
req.onSuccess([=](const rapidjson::Document &document) { req.onSuccess([=](auto result) {
auto document = result.parseRapidJson();
onFinished(FollowResult_Following); onFinished(FollowResult_Following);
return true; return true;
}); });
@ -260,6 +265,53 @@ void TwitchAccount::checkFollow(const QString targetUserID,
req.execute(); req.execute();
} }
void TwitchAccount::followUser(const QString userID, std::function<void()> successCallback)
{
QUrl requestUrl("https://api.twitch.tv/kraken/users/" + this->getUserId() +
"/follows/channels/" + userID);
NetworkRequest request(requestUrl, NetworkRequestType::Put);
request.setCaller(QThread::currentThread());
request.makeAuthorizedV5(this->getOAuthClient(), this->getOAuthToken());
// TODO: Properly check result of follow request
request.onSuccess([successCallback](auto result) {
successCallback();
return true;
});
request.execute();
}
void TwitchAccount::unfollowUser(const QString userID, std::function<void()> successCallback)
{
QUrl requestUrl("https://api.twitch.tv/kraken/users/" + this->getUserId() +
"/follows/channels/" + userID);
NetworkRequest request(requestUrl, NetworkRequestType::Delete);
request.setCaller(QThread::currentThread());
request.makeAuthorizedV5(this->getOAuthClient(), this->getOAuthToken());
request.onError([successCallback](int code) {
if (code >= 200 && code <= 299) {
successCallback();
}
return true;
});
request.onSuccess([successCallback](const auto &document) {
successCallback();
return true;
});
request.execute();
}
std::set<TwitchUser> TwitchAccount::getIgnores() const std::set<TwitchUser> TwitchAccount::getIgnores() const
{ {
std::lock_guard<std::mutex> lock(this->ignoresMutex_); std::lock_guard<std::mutex> lock(this->ignoresMutex_);
@ -282,7 +334,6 @@ void TwitchAccount::loadEmotes(std::function<void(const rapidjson::Document &)>
QString url("https://api.twitch.tv/kraken/users/" + this->getUserId() + "/emotes"); QString url("https://api.twitch.tv/kraken/users/" + this->getUserId() + "/emotes");
NetworkRequest req(url); NetworkRequest req(url);
req.setRequestType(NetworkRequest::GetRequest);
req.setCaller(QThread::currentThread()); req.setCaller(QThread::currentThread());
req.makeAuthorizedV5(this->getOAuthClient(), this->getOAuthToken()); req.makeAuthorizedV5(this->getOAuthClient(), this->getOAuthToken());
@ -297,8 +348,8 @@ void TwitchAccount::loadEmotes(std::function<void(const rapidjson::Document &)>
return true; return true;
}); });
req.onSuccess([=](const rapidjson::Document &document) { req.onSuccess([=](auto result) {
cb(document); cb(result.parseRapidJson());
return true; return true;
}); });

View file

@ -65,6 +65,8 @@ public:
std::function<void(UnignoreResult, const QString &message)> onFinished); std::function<void(UnignoreResult, const QString &message)> onFinished);
void checkFollow(const QString targetUserID, std::function<void(FollowResult)> onFinished); void checkFollow(const QString targetUserID, std::function<void(FollowResult)> onFinished);
void followUser(const QString userID, std::function<void()> successCallback);
void unfollowUser(const QString userID, std::function<void()> successCallback);
std::set<TwitchUser> getIgnores() const; std::set<TwitchUser> getIgnores() const;

View file

@ -2,9 +2,11 @@
#include "common/Common.hpp" #include "common/Common.hpp"
#include "common/UrlFetch.hpp" #include "common/UrlFetch.hpp"
#include "controllers/accounts/AccountController.hpp"
#include "debug/Log.hpp" #include "debug/Log.hpp"
#include "messages/Message.hpp" #include "messages/Message.hpp"
#include "providers/twitch/PubsubClient.hpp" #include "providers/twitch/PubsubClient.hpp"
#include "providers/twitch/TwitchCommon.hpp"
#include "providers/twitch/TwitchMessageBuilder.hpp" #include "providers/twitch/TwitchMessageBuilder.hpp"
#include "singletons/Emotes.hpp" #include "singletons/Emotes.hpp"
#include "singletons/Settings.hpp" #include "singletons/Settings.hpp"
@ -82,8 +84,16 @@ TwitchChannel::TwitchChannel(const QString &channelName, Communi::IrcConnection
} }
} }
twitchApiGet("https://tmi.twitch.tv/group/user/" + this->name + "/chatters", NetworkRequest request("https://tmi.twitch.tv/group/user/" + this->name + "/chatters");
QThread::currentThread(), refreshChatters);
request.setCaller(QThread::currentThread());
request.onSuccess([refreshChatters](auto result) {
refreshChatters(result.parseJson()); //
return true;
});
request.execute();
}; };
doRefreshChatters(); doRefreshChatters();
@ -153,7 +163,7 @@ void TwitchChannel::sendMessage(const QString &message)
// Do last message processing // Do last message processing
QString parsedMessage = app->emotes->emojis.replaceShortCodes(message); QString parsedMessage = app->emotes->emojis.replaceShortCodes(message);
parsedMessage.trim(); parsedMessage = parsedMessage.trimmed();
if (parsedMessage.isEmpty()) { if (parsedMessage.isEmpty()) {
return; return;
@ -325,23 +335,26 @@ void TwitchChannel::refreshLiveStatus()
std::weak_ptr<Channel> weak = this->shared_from_this(); std::weak_ptr<Channel> weak = this->shared_from_this();
twitchApiGet2(url, QThread::currentThread(), false, [weak](const rapidjson::Document &d) { auto request = makeGetStreamRequest(this->roomID, QThread::currentThread());
request.onSuccess([weak](auto result) {
auto d = result.parseRapidJson();
ChannelPtr shared = weak.lock(); ChannelPtr shared = weak.lock();
if (!shared) { if (!shared) {
return; return false;
} }
TwitchChannel *channel = dynamic_cast<TwitchChannel *>(shared.get()); TwitchChannel *channel = dynamic_cast<TwitchChannel *>(shared.get());
if (!d.IsObject()) { if (!d.IsObject()) {
Log("[TwitchChannel:refreshLiveStatus] root is not an object"); Log("[TwitchChannel:refreshLiveStatus] root is not an object");
return; return false;
} }
if (!d.HasMember("stream")) { if (!d.HasMember("stream")) {
Log("[TwitchChannel:refreshLiveStatus] Missing stream in root"); Log("[TwitchChannel:refreshLiveStatus] Missing stream in root");
return; return false;
} }
const auto &stream = d["stream"]; const auto &stream = d["stream"];
@ -349,21 +362,21 @@ void TwitchChannel::refreshLiveStatus()
if (!stream.IsObject()) { if (!stream.IsObject()) {
// Stream is offline (stream is most likely null) // Stream is offline (stream is most likely null)
channel->setLive(false); channel->setLive(false);
return; return false;
} }
if (!stream.HasMember("viewers") || !stream.HasMember("game") || if (!stream.HasMember("viewers") || !stream.HasMember("game") ||
!stream.HasMember("channel") || !stream.HasMember("created_at")) { !stream.HasMember("channel") || !stream.HasMember("created_at")) {
Log("[TwitchChannel:refreshLiveStatus] Missing members in stream"); Log("[TwitchChannel:refreshLiveStatus] Missing members in stream");
channel->setLive(false); channel->setLive(false);
return; return false;
} }
const rapidjson::Value &streamChannel = stream["channel"]; const rapidjson::Value &streamChannel = stream["channel"];
if (!streamChannel.IsObject() || !streamChannel.HasMember("status")) { if (!streamChannel.IsObject() || !streamChannel.HasMember("status")) {
Log("[TwitchChannel:refreshLiveStatus] Missing member \"status\" in channel"); Log("[TwitchChannel:refreshLiveStatus] Missing member \"status\" in channel");
return; return false;
} }
// Stream is live // Stream is live
@ -400,7 +413,11 @@ void TwitchChannel::refreshLiveStatus()
// Signal all listeners that the stream status has been updated // Signal all listeners that the stream status has been updated
channel->updateLiveInfo.invoke(); channel->updateLiveInfo.invoke();
return true;
}); });
request.execute();
} }
void TwitchChannel::startRefreshLiveStatusTimer(int intervalMS) void TwitchChannel::startRefreshLiveStatusTimer(int intervalMS)
@ -423,13 +440,18 @@ void TwitchChannel::fetchRecentMessages()
static QString genericURL = static QString genericURL =
"https://tmi.twitch.tv/api/rooms/%1/recent_messages?client_id=" + getDefaultClientID(); "https://tmi.twitch.tv/api/rooms/%1/recent_messages?client_id=" + getDefaultClientID();
NetworkRequest request(genericURL.arg(this->roomID));
request.makeAuthorizedV5(getDefaultClientID());
request.setCaller(QThread::currentThread());
std::weak_ptr<Channel> weak = this->shared_from_this(); std::weak_ptr<Channel> weak = this->shared_from_this();
twitchApiGet(genericURL.arg(roomID), QThread::currentThread(), [weak](QJsonObject obj) { request.onSuccess([weak](auto result) {
auto obj = result.parseJson();
ChannelPtr shared = weak.lock(); ChannelPtr shared = weak.lock();
if (!shared) { if (!shared) {
return; return false;
} }
auto channel = dynamic_cast<TwitchChannel *>(shared.get()); auto channel = dynamic_cast<TwitchChannel *>(shared.get());
@ -439,7 +461,7 @@ void TwitchChannel::fetchRecentMessages()
QJsonArray msgArray = obj.value("messages").toArray(); QJsonArray msgArray = obj.value("messages").toArray();
if (msgArray.empty()) { if (msgArray.empty()) {
return; return false;
} }
std::vector<MessagePtr> messages; std::vector<MessagePtr> messages;
@ -455,8 +477,13 @@ void TwitchChannel::fetchRecentMessages()
messages.push_back(builder.build()); messages.push_back(builder.build());
} }
} }
channel->addMessagesAtStart(messages); channel->addMessagesAtStart(messages);
return true;
}); });
request.execute();
} }
} // namespace chatterino } // namespace chatterino

View file

@ -45,39 +45,6 @@ QString cleanUpCode(const QString &dirtyEmoteCode)
return cleanCode; return cleanCode;
} }
void loadSetData(std::shared_ptr<TwitchEmotes::EmoteSet> emoteSet)
{
Log("Load twitch emote set data for {}", emoteSet->key);
NetworkRequest req("https://braize.pajlada.com/chatterino/twitchemotes/set/" + emoteSet->key +
"/");
req.setRequestType(NetworkRequest::GetRequest);
req.onError([](int errorCode) -> bool {
Log("Emote sets on ERROR {}", errorCode);
return true;
});
req.onSuccess([emoteSet](const rapidjson::Document &root) -> bool {
Log("Emote sets on success");
if (!root.IsObject()) {
return false;
}
std::string emoteSetID;
QString channelName;
if (!rj::getSafe(root, "channel_name", channelName)) {
return false;
}
emoteSet->channelName = channelName;
return true;
});
req.execute();
}
} // namespace } // namespace
TwitchEmotes::TwitchEmotes() TwitchEmotes::TwitchEmotes()
@ -165,7 +132,7 @@ void TwitchEmotes::refresh(const std::shared_ptr<TwitchAccount> &user)
emoteSet->key = emoteSetJSON.name.GetString(); emoteSet->key = emoteSetJSON.name.GetString();
loadSetData(emoteSet); this->loadSetData(emoteSet);
for (const rapidjson::Value &emoteJSON : emoteSetJSON.value.GetArray()) { for (const rapidjson::Value &emoteJSON : emoteSetJSON.value.GetArray()) {
if (!emoteJSON.IsObject()) { if (!emoteJSON.IsObject()) {
@ -221,18 +188,17 @@ void TwitchEmotes::loadSetData(std::shared_ptr<TwitchEmotes::EmoteSet> emoteSet)
return; return;
} }
Log("Load twitch emote set data for {}..", emoteSet->key);
NetworkRequest req("https://braize.pajlada.com/chatterino/twitchemotes/set/" + emoteSet->key + NetworkRequest req("https://braize.pajlada.com/chatterino/twitchemotes/set/" + emoteSet->key +
"/"); "/");
req.setUseQuickLoadCache(true);
req.setRequestType(NetworkRequest::GetRequest);
req.onError([](int errorCode) -> bool { req.onError([](int errorCode) -> bool {
Log("Emote sets on ERROR {}", errorCode); Log("Error code {} while loading emote set data", errorCode);
return true; return true;
}); });
req.onSuccess([emoteSet](const rapidjson::Document &root) -> bool { req.onSuccess([emoteSet](auto result) -> bool {
auto root = result.parseRapidJson();
if (!root.IsObject()) { if (!root.IsObject()) {
return false; return false;
} }

View file

@ -327,7 +327,8 @@ void Resources::loadChannelData(const QString &roomID, bool bypassCache)
NetworkRequest req(url); NetworkRequest req(url);
req.setCaller(QThread::currentThread()); req.setCaller(QThread::currentThread());
req.getJSON([this, roomID](QJsonObject &root) { req.onSuccess([this, roomID](auto result) {
auto root = result.parseJson();
QJsonObject sets = root.value("badge_sets").toObject(); QJsonObject sets = root.value("badge_sets").toObject();
Resources::Channel &ch = this->channels[roomID]; Resources::Channel &ch = this->channels[roomID];
@ -348,49 +349,58 @@ void Resources::loadChannelData(const QString &roomID, bool bypassCache)
} }
ch.loaded = true; ch.loaded = true;
return true;
}); });
QString cheermoteURL = "https://api.twitch.tv/kraken/bits/actions?channel_id=" + roomID; req.execute();
twitchApiGet2( QString cheermoteUrl = "https://api.twitch.tv/kraken/bits/actions?channel_id=" + roomID;
cheermoteURL, QThread::currentThread(), true, [this, roomID](const rapidjson::Document &d) { auto request = NetworkRequest::twitchRequest(cheermoteUrl);
Resources::Channel &ch = this->channels[roomID]; request.setCaller(QThread::currentThread());
ParseCheermoteSets(ch.jsonCheermoteSets, d); request.onSuccess([this, roomID](auto result) {
auto d = result.parseRapidJson();
Resources::Channel &ch = this->channels[roomID];
for (auto &set : ch.jsonCheermoteSets) { ParseCheermoteSets(ch.jsonCheermoteSets, d);
CheermoteSet cheermoteSet;
cheermoteSet.regex =
QRegularExpression("^" + set.prefix.toLower() + "([1-9][0-9]*)$");
for (auto &tier : set.tiers) { for (auto &set : ch.jsonCheermoteSets) {
Cheermote cheermote; CheermoteSet cheermoteSet;
cheermoteSet.regex = QRegularExpression("^" + set.prefix.toLower() + "([1-9][0-9]*)$");
cheermote.color = QColor(tier.color); for (auto &tier : set.tiers) {
cheermote.minBits = tier.minBits; Cheermote cheermote;
// TODO(pajlada): We currently hardcode dark here :| cheermote.color = QColor(tier.color);
// We will continue to do so for now since we haven't had to cheermote.minBits = tier.minBits;
// solve that anywhere else
cheermote.emoteDataAnimated.image1x = tier.images["dark"]["animated"]["1"];
cheermote.emoteDataAnimated.image2x = tier.images["dark"]["animated"]["2"];
cheermote.emoteDataAnimated.image3x = tier.images["dark"]["animated"]["4"];
cheermote.emoteDataStatic.image1x = tier.images["dark"]["static"]["1"]; // TODO(pajlada): We currently hardcode dark here :|
cheermote.emoteDataStatic.image2x = tier.images["dark"]["static"]["2"]; // We will continue to do so for now since we haven't had to
cheermote.emoteDataStatic.image3x = tier.images["dark"]["static"]["4"]; // solve that anywhere else
cheermote.emoteDataAnimated.image1x = tier.images["dark"]["animated"]["1"];
cheermote.emoteDataAnimated.image2x = tier.images["dark"]["animated"]["2"];
cheermote.emoteDataAnimated.image3x = tier.images["dark"]["animated"]["4"];
cheermoteSet.cheermotes.emplace_back(cheermote); cheermote.emoteDataStatic.image1x = tier.images["dark"]["static"]["1"];
} cheermote.emoteDataStatic.image2x = tier.images["dark"]["static"]["2"];
cheermote.emoteDataStatic.image3x = tier.images["dark"]["static"]["4"];
std::sort(cheermoteSet.cheermotes.begin(), cheermoteSet.cheermotes.end(), cheermoteSet.cheermotes.emplace_back(cheermote);
[](const auto &lhs, const auto &rhs) {
return lhs.minBits < rhs.minBits; //
});
ch.cheermoteSets.emplace_back(cheermoteSet);
} }
});
std::sort(cheermoteSet.cheermotes.begin(), cheermoteSet.cheermotes.end(),
[](const auto &lhs, const auto &rhs) {
return lhs.minBits < rhs.minBits; //
});
ch.cheermoteSets.emplace_back(cheermoteSet);
}
return true;
});
request.execute();
} }
void Resources::loadDynamicTwitchBadges() void Resources::loadDynamicTwitchBadges()
@ -399,7 +409,8 @@ void Resources::loadDynamicTwitchBadges()
NetworkRequest req(url); NetworkRequest req(url);
req.setCaller(QThread::currentThread()); req.setCaller(QThread::currentThread());
req.getJSON([this](QJsonObject &root) { req.onSuccess([this](auto result) {
auto root = result.parseJson();
QJsonObject sets = root.value("badge_sets").toObject(); QJsonObject sets = root.value("badge_sets").toObject();
for (QJsonObject::iterator it = sets.begin(); it != sets.end(); ++it) { for (QJsonObject::iterator it = sets.begin(); it != sets.end(); ++it) {
QJsonObject versions = it.value().toObject().value("versions").toObject(); QJsonObject versions = it.value().toObject().value("versions").toObject();
@ -417,7 +428,11 @@ void Resources::loadDynamicTwitchBadges()
} }
this->dynamicBadgesLoaded = true; this->dynamicBadgesLoaded = true;
return true;
}); });
req.execute();
} }
void Resources::loadChatterinoBadges() void Resources::loadChatterinoBadges()
@ -429,7 +444,8 @@ void Resources::loadChatterinoBadges()
NetworkRequest req(url); NetworkRequest req(url);
req.setCaller(QThread::currentThread()); req.setCaller(QThread::currentThread());
req.getJSON([this](QJsonObject &root) { req.onSuccess([this](auto result) {
auto root = result.parseJson();
QJsonArray badgeVariants = root.value("badges").toArray(); QJsonArray badgeVariants = root.value("badges").toArray();
for (QJsonArray::iterator it = badgeVariants.begin(); it != badgeVariants.end(); ++it) { for (QJsonArray::iterator it = badgeVariants.begin(); it != badgeVariants.end(); ++it) {
QJsonObject badgeVariant = it->toObject(); QJsonObject badgeVariant = it->toObject();
@ -449,7 +465,11 @@ void Resources::loadChatterinoBadges()
std::shared_ptr<ChatterinoBadge>(badgeVariantPtr); std::shared_ptr<ChatterinoBadge>(badgeVariantPtr);
} }
} }
return true;
}); });
req.execute();
} }
} // namespace chatterino } // namespace chatterino

View file

@ -5,6 +5,7 @@
#include "util/CombinePath.hpp" #include "util/CombinePath.hpp"
#include "util/PostToThread.hpp" #include "util/PostToThread.hpp"
#include <QDebug>
#include <QMessageBox> #include <QMessageBox>
#include <QProcess> #include <QProcess>
@ -94,7 +95,8 @@ void Updates::checkForUpdates()
NetworkRequest req(url); NetworkRequest req(url);
req.setTimeout(30000); req.setTimeout(30000);
req.getJSON([this](QJsonObject &object) { req.onSuccess([this](auto result) {
auto object = result.parseJson();
QJsonValue version_val = object.value("version"); QJsonValue version_val = object.value("version");
QJsonValue update_val = object.value("update"); QJsonValue update_val = object.value("update");

View file

@ -1,6 +1,10 @@
#include "widgets/dialogs/LoginDialog.hpp" #include "widgets/dialogs/LoginDialog.hpp"
#include "common/Common.hpp" #include "common/Common.hpp"
#include "common/UrlFetch.hpp" #include "common/UrlFetch.hpp"
#include "controllers/accounts/AccountController.hpp"
#include "providers/twitch/PartialTwitchUser.hpp"
#ifdef USEWINSDK #ifdef USEWINSDK
#include <Windows.h> #include <Windows.h>
#endif #endif
@ -173,9 +177,10 @@ AdvancedLoginWidget::AdvancedLoginWidget()
this->ui_.buttonLowerRow.layout.addWidget(&this->ui_.buttonLowerRow.fillInUserIDButton); this->ui_.buttonLowerRow.layout.addWidget(&this->ui_.buttonLowerRow.fillInUserIDButton);
connect(&this->ui_.buttonLowerRow.fillInUserIDButton, &QPushButton::clicked, [=]() { connect(&this->ui_.buttonLowerRow.fillInUserIDButton, &QPushButton::clicked, [=]() {
twitchApiGetUserID(this->ui_.usernameInput.text(), this, [=](const QString &userID) { const auto onIdFetched = [=](const QString &userID) {
this->ui_.userIDInput.setText(userID); // this->ui_.userIDInput.setText(userID); //
}); };
PartialTwitchUser::byName(this->ui_.usernameInput.text()).getId(onIdFetched, this);
}); });
} }

View file

@ -7,6 +7,7 @@
#include "widgets/helper/ChannelView.hpp" #include "widgets/helper/ChannelView.hpp"
#include <QDateTime> #include <QDateTime>
#include <QJsonArray>
#include <QMessageBox> #include <QMessageBox>
#include <QVBoxLayout> #include <QVBoxLayout>
@ -65,8 +66,8 @@ void LogsPopup::getLogviewerLogs()
return true; return true;
}); });
req.getJSON([this, channelName](QJsonObject &data) { req.onSuccess([this, channelName](auto result) {
auto data = result.parseJson();
std::vector<MessagePtr> messages; std::vector<MessagePtr> messages;
ChannelPtr logsChannel(new Channel("logs", Channel::Type::None)); ChannelPtr logsChannel(new Channel("logs", Channel::Type::None));
@ -87,6 +88,8 @@ void LogsPopup::getLogviewerLogs()
messages.push_back(builder.build()); messages.push_back(builder.build());
}; };
this->setMessages(messages); this->setMessages(messages);
return true;
}); });
req.execute(); req.execute();
@ -113,10 +116,12 @@ void LogsPopup::getOverrustleLogs()
box->setAttribute(Qt::WA_DeleteOnClose); box->setAttribute(Qt::WA_DeleteOnClose);
box->show(); box->show();
box->raise(); box->raise();
return true; return true;
}); });
req.getJSON([this, channelName](QJsonObject &data) { req.onSuccess([this, channelName](auto result) {
auto data = result.parseJson();
std::vector<MessagePtr> messages; std::vector<MessagePtr> messages;
if (data.contains("lines")) { if (data.contains("lines")) {
QJsonArray dataMessages = data.value("lines").toArray(); QJsonArray dataMessages = data.value("lines").toArray();
@ -135,7 +140,10 @@ void LogsPopup::getOverrustleLogs()
} }
} }
this->setMessages(messages); this->setMessages(messages);
return true;
}); });
req.execute(); req.execute();
} }
} // namespace chatterino } // namespace chatterino

View file

@ -2,6 +2,8 @@
#include "Application.hpp" #include "Application.hpp"
#include "common/UrlFetch.hpp" #include "common/UrlFetch.hpp"
#include "controllers/accounts/AccountController.hpp"
#include "providers/twitch/PartialTwitchUser.hpp"
#include "providers/twitch/TwitchChannel.hpp" #include "providers/twitch/TwitchChannel.hpp"
#include "singletons/Resources.hpp" #include "singletons/Resources.hpp"
#include "util/LayoutCreator.hpp" #include "util/LayoutCreator.hpp"
@ -176,11 +178,15 @@ void UserInfoPopup::installEvents()
QUrl requestUrl("https://api.twitch.tv/kraken/users/" + currentUser->getUserId() + QUrl requestUrl("https://api.twitch.tv/kraken/users/" + currentUser->getUserId() +
"/follows/channels/" + this->userId_); "/follows/channels/" + this->userId_);
const auto reenableFollowCheckbox = [this] {
this->ui_.follow->setEnabled(true); //
};
this->ui_.follow->setEnabled(false); this->ui_.follow->setEnabled(false);
if (this->ui_.follow->isChecked()) { if (this->ui_.follow->isChecked()) {
twitchApiPut(requestUrl, [this](const auto &) { this->ui_.follow->setEnabled(true); }); currentUser->followUser(this->userId_, reenableFollowCheckbox);
} else { } else {
twitchApiDelete(requestUrl, [this] { this->ui_.follow->setEnabled(true); }); currentUser->unfollowUser(this->userId_, reenableFollowCheckbox);
} }
}); });
@ -239,24 +245,28 @@ void UserInfoPopup::updateUserData()
{ {
std::weak_ptr<bool> hack = this->hack_; std::weak_ptr<bool> hack = this->hack_;
// get user info const auto onIdFetched = [this, hack](QString id) {
twitchApiGetUserID(this->userName_, this, [this, hack](QString id) {
auto currentUser = getApp()->accounts->twitch.getCurrent(); auto currentUser = getApp()->accounts->twitch.getCurrent();
this->userId_ = id; this->userId_ = id;
// get channel info auto request = makeGetChannelRequest(id, this);
twitchApiGet(
"https://api.twitch.tv/kraken/channels/" + id, this, [this](const QJsonObject &obj) {
this->ui_.followerCountLabel->setText(
TEXT_FOLLOWERS + QString::number(obj.value("followers").toInt()));
this->ui_.viewCountLabel->setText(TEXT_VIEWS +
QString::number(obj.value("views").toInt()));
this->ui_.createdDateLabel->setText(
TEXT_CREATED + obj.value("created_at").toString().section("T", 0, 0));
this->loadAvatar(QUrl(obj.value("logo").toString())); request.onSuccess([this](auto result) {
}); auto obj = result.parseJson();
this->ui_.followerCountLabel->setText(TEXT_FOLLOWERS +
QString::number(obj.value("followers").toInt()));
this->ui_.viewCountLabel->setText(TEXT_VIEWS +
QString::number(obj.value("views").toInt()));
this->ui_.createdDateLabel->setText(
TEXT_CREATED + obj.value("created_at").toString().section("T", 0, 0));
this->loadAvatar(QUrl(obj.value("logo").toString()));
return true;
});
request.execute();
// get follow state // get follow state
currentUser->checkFollow(id, [this, hack](auto result) { currentUser->checkFollow(id, [this, hack](auto result) {
@ -279,7 +289,9 @@ void UserInfoPopup::updateUserData()
this->ui_.ignore->setEnabled(true); this->ui_.ignore->setEnabled(true);
this->ui_.ignore->setChecked(isIgnoring); this->ui_.ignore->setChecked(isIgnoring);
}); };
PartialTwitchUser::byName(this->userName_).getId(onIdFetched, this);
this->ui_.follow->setEnabled(false); this->ui_.follow->setEnabled(false);
this->ui_.ignore->setEnabled(false); this->ui_.ignore->setEnabled(false);
@ -386,16 +398,21 @@ UserInfoPopup::TimeoutWidget::TimeoutWidget()
addTimeouts("sec", {{"1", 1}}); addTimeouts("sec", {{"1", 1}});
addTimeouts("min", { addTimeouts("min", {
{"1", 1 * 60}, {"5", 5 * 60}, {"10", 10 * 60}, {"1", 1 * 60},
{"5", 5 * 60},
{"10", 10 * 60},
}); });
addTimeouts("hour", { addTimeouts("hour", {
{"1", 1 * 60 * 60}, {"4", 4 * 60 * 60}, {"1", 1 * 60 * 60},
{"4", 4 * 60 * 60},
}); });
addTimeouts("days", { addTimeouts("days", {
{"1", 1 * 60 * 60 * 24}, {"3", 3 * 60 * 60 * 24}, {"1", 1 * 60 * 60 * 24},
{"3", 3 * 60 * 60 * 24},
}); });
addTimeouts("weeks", { addTimeouts("weeks", {
{"1", 1 * 60 * 60 * 24 * 7}, {"2", 2 * 60 * 60 * 24 * 7}, {"1", 1 * 60 * 60 * 24 * 7},
{"2", 2 * 60 * 60 * 24 * 7},
}); });
addButton(Ban, "ban", getApp()->resources->buttons.ban); addButton(Ban, "ban", getApp()->resources->buttons.ban);

View file

@ -451,18 +451,25 @@ void Split::doOpenViewerList()
} }
auto loadingLabel = new QLabel("Loading..."); auto loadingLabel = new QLabel("Loading...");
twitchApiGet("https://tmi.twitch.tv/group/user/" + this->getChannel()->name + "/chatters", this, auto request = NetworkRequest::twitchRequest("https://tmi.twitch.tv/group/user/" +
[=](QJsonObject obj) { this->getChannel()->name + "/chatters");
QJsonObject chattersObj = obj.value("chatters").toObject();
loadingLabel->hide(); request.setCaller(this);
for (int i = 0; i < jsonLabels.size(); i++) { request.onSuccess([=](auto result) {
chattersList->addItem(labelList.at(i)); auto obj = result.parseJson();
foreach (const QJsonValue &v, QJsonObject chattersObj = obj.value("chatters").toObject();
chattersObj.value(jsonLabels.at(i)).toArray())
chattersList->addItem(v.toString()); loadingLabel->hide();
} for (int i = 0; i < jsonLabels.size(); i++) {
}); chattersList->addItem(labelList.at(i));
foreach (const QJsonValue &v, chattersObj.value(jsonLabels.at(i)).toArray())
chattersList->addItem(v.toString());
}
return true;
});
request.execute();
searchBar->setPlaceholderText("Search User..."); searchBar->setPlaceholderText("Search User...");
QObject::connect(searchBar, &QLineEdit::textEdited, this, [=]() { QObject::connect(searchBar, &QLineEdit::textEdited, this, [=]() {

View file

@ -2,6 +2,7 @@
#include "Application.hpp" #include "Application.hpp"
#include "common/UrlFetch.hpp" #include "common/UrlFetch.hpp"
#include "controllers/accounts/AccountController.hpp"
#include "providers/twitch/TwitchChannel.hpp" #include "providers/twitch/TwitchChannel.hpp"
#include "providers/twitch/TwitchServer.hpp" #include "providers/twitch/TwitchServer.hpp"
#include "singletons/Resources.hpp" #include "singletons/Resources.hpp"
@ -323,13 +324,13 @@ void SplitHeader::updateChannelText()
if (streamStatus.live) { if (streamStatus.live) {
this->isLive_ = true; this->isLive_ = true;
this->tooltip_ = "<style>.center { text-align: center; }</style>" this->tooltip_ = "<style>.center { text-align: center; }</style>"
"<p class = \"center\">" + "<p class = \"center\">" +
streamStatus.title + "<br><br>" + streamStatus.game + "<br>" + streamStatus.title + "<br><br>" + streamStatus.game + "<br>" +
(streamStatus.rerun ? "Vod-casting" : "Live") + " for " + (streamStatus.rerun ? "Vod-casting" : "Live") + " for " +
streamStatus.uptime + " with " + streamStatus.uptime + " with " +
QString::number(streamStatus.viewerCount) + QString::number(streamStatus.viewerCount) +
" viewers" " viewers"
"</p>"; "</p>";
if (streamStatus.rerun) { if (streamStatus.rerun) {
title += " (rerun)"; title += " (rerun)";
} else if (streamStatus.streamType.isEmpty()) { } else if (streamStatus.streamType.isEmpty()) {
@ -355,8 +356,8 @@ void SplitHeader::updateModerationModeIcon()
auto app = getApp(); auto app = getApp();
this->moderationButton_->setPixmap(this->split_->getModerationMode() this->moderationButton_->setPixmap(this->split_->getModerationMode()
? *app->resources->moderationmode_enabled->getPixmap() ? *app->resources->moderationmode_enabled->getPixmap()
: *app->resources->moderationmode_disabled->getPixmap()); : *app->resources->moderationmode_disabled->getPixmap());
bool modButtonVisible = false; bool modButtonVisible = false;
ChannelPtr channel = this->split_->getChannel(); ChannelPtr channel = this->split_->getChannel();