mirror of
https://github.com/Chatterino/chatterino2.git
synced 2024-11-13 19:49:51 +01:00
Move timeout logic to NetworkRequest
This commit is contained in:
parent
41ec892bf8
commit
2de98dc1f8
|
@ -48,9 +48,14 @@ void EmoteManager::reloadBTTVChannelEmotes(const QString &channelName, std::weak
|
||||||
printf("[EmoteManager] Reload BTTV Channel Emotes for channel %s\n", qPrintable(channelName));
|
printf("[EmoteManager] Reload BTTV Channel Emotes for channel %s\n", qPrintable(channelName));
|
||||||
|
|
||||||
QString url("https://api.betterttv.net/2/channels/" + channelName);
|
QString url("https://api.betterttv.net/2/channels/" + channelName);
|
||||||
util::urlFetchJSONTimeout(
|
|
||||||
url, QThread::currentThread(),
|
debug::Log("Request bttv channel emotes for {}", channelName);
|
||||||
[this, channelName, _map](QJsonObject &rootNode) {
|
|
||||||
|
util::NetworkRequest req(url);
|
||||||
|
req.setCaller(QThread::currentThread());
|
||||||
|
req.setTimeout(3000);
|
||||||
|
req.getJSON([this, channelName, _map](QJsonObject &rootNode) {
|
||||||
|
debug::Log("Got bttv channel emotes for {}", channelName);
|
||||||
auto map = _map.lock();
|
auto map = _map.lock();
|
||||||
|
|
||||||
if (_map.expired()) {
|
if (_map.expired()) {
|
||||||
|
@ -76,10 +81,9 @@ void EmoteManager::reloadBTTVChannelEmotes(const QString &channelName, std::weak
|
||||||
|
|
||||||
link = link.replace("{{id}}", id).replace("{{image}}", "1x");
|
link = link.replace("{{id}}", id).replace("{{image}}", "1x");
|
||||||
|
|
||||||
auto emote =
|
auto emote = this->getBTTVChannelEmoteFromCaches().getOrAdd(id, [this, &code, &link] {
|
||||||
this->getBTTVChannelEmoteFromCaches().getOrAdd(id, [this, &code, &link] {
|
return EmoteData(new LazyLoadedImage(*this, this->windowManager, link, 1, code,
|
||||||
return EmoteData(new LazyLoadedImage(*this, this->windowManager, link, 1,
|
code + "\nChannel BTTV Emote"));
|
||||||
code, code + "\nChannel BTTV Emote"));
|
|
||||||
});
|
});
|
||||||
|
|
||||||
this->bttvChannelEmotes.insert(code, emote);
|
this->bttvChannelEmotes.insert(code, emote);
|
||||||
|
@ -88,8 +92,7 @@ void EmoteManager::reloadBTTVChannelEmotes(const QString &channelName, std::weak
|
||||||
}
|
}
|
||||||
|
|
||||||
this->bttvChannelEmoteCodes[channelName.toStdString()] = codes;
|
this->bttvChannelEmoteCodes[channelName.toStdString()] = codes;
|
||||||
},
|
});
|
||||||
1500);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void EmoteManager::reloadFFZChannelEmotes(const QString &channelName, std::weak_ptr<EmoteMap> _map)
|
void EmoteManager::reloadFFZChannelEmotes(const QString &channelName, std::weak_ptr<EmoteMap> _map)
|
||||||
|
@ -98,9 +101,10 @@ void EmoteManager::reloadFFZChannelEmotes(const QString &channelName, std::weak_
|
||||||
|
|
||||||
QString url("http://api.frankerfacez.com/v1/room/" + channelName);
|
QString url("http://api.frankerfacez.com/v1/room/" + channelName);
|
||||||
|
|
||||||
util::urlFetchJSONTimeout(
|
util::NetworkRequest req(url);
|
||||||
url, QThread::currentThread(),
|
req.setCaller(QThread::currentThread());
|
||||||
[this, channelName, _map](QJsonObject &rootNode) {
|
req.setTimeout(3000);
|
||||||
|
req.getJSON([this, channelName, _map](QJsonObject &rootNode) {
|
||||||
auto map = _map.lock();
|
auto map = _map.lock();
|
||||||
|
|
||||||
if (_map.expired()) {
|
if (_map.expired()) {
|
||||||
|
@ -125,8 +129,8 @@ void EmoteManager::reloadFFZChannelEmotes(const QString &channelName, std::weak_
|
||||||
QJsonObject urls = emoteObject.value("urls").toObject();
|
QJsonObject urls = emoteObject.value("urls").toObject();
|
||||||
QString url1 = "http:" + urls.value("1").toString();
|
QString url1 = "http:" + urls.value("1").toString();
|
||||||
|
|
||||||
auto emote = this->getFFZChannelEmoteFromCaches().getOrAdd(id, [this, &code,
|
auto emote =
|
||||||
&url1] {
|
this->getFFZChannelEmoteFromCaches().getOrAdd(id, [this, &code, &url1] {
|
||||||
return EmoteData(new LazyLoadedImage(*this, this->windowManager, url1, 1,
|
return EmoteData(new LazyLoadedImage(*this, this->windowManager, url1, 1,
|
||||||
code, code + "\nGlobal FFZ Emote"));
|
code, code + "\nGlobal FFZ Emote"));
|
||||||
});
|
});
|
||||||
|
@ -138,8 +142,7 @@ void EmoteManager::reloadFFZChannelEmotes(const QString &channelName, std::weak_
|
||||||
|
|
||||||
this->ffzChannelEmoteCodes[channelName.toStdString()] = codes;
|
this->ffzChannelEmoteCodes[channelName.toStdString()] = codes;
|
||||||
}
|
}
|
||||||
},
|
});
|
||||||
1500);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ConcurrentMap<QString, twitch::EmoteValue *> &EmoteManager::getTwitchEmotes()
|
ConcurrentMap<QString, twitch::EmoteValue *> &EmoteManager::getTwitchEmotes()
|
||||||
|
@ -383,9 +386,10 @@ void EmoteManager::refreshTwitchEmotes(const std::string &roomID)
|
||||||
|
|
||||||
qDebug() << url;
|
qDebug() << url;
|
||||||
|
|
||||||
util::urlFetchJSONTimeout(
|
util::NetworkRequest req(url);
|
||||||
url, QThread::currentThread(),
|
req.setCaller(QThread::currentThread());
|
||||||
[=, &emoteData](QJsonObject &root) {
|
req.setTimeout(3000);
|
||||||
|
req.getJSON([=, &emoteData](QJsonObject &root) {
|
||||||
emoteData.emoteSets.clear();
|
emoteData.emoteSets.clear();
|
||||||
emoteData.emoteCodes.clear();
|
emoteData.emoteCodes.clear();
|
||||||
auto emoticonSets = root.value("emoticon_sets").toObject();
|
auto emoticonSets = root.value("emoticon_sets").toObject();
|
||||||
|
@ -403,8 +407,7 @@ void EmoteManager::refreshTwitchEmotes(const std::string &roomID)
|
||||||
}
|
}
|
||||||
|
|
||||||
emoteData.filled = true;
|
emoteData.filled = true;
|
||||||
},
|
});
|
||||||
3000);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void EmoteManager::loadBTTVEmotes()
|
void EmoteManager::loadBTTVEmotes()
|
||||||
|
|
|
@ -1,10 +1,13 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "debug/log.hpp"
|
||||||
|
|
||||||
#include <QJsonDocument>
|
#include <QJsonDocument>
|
||||||
#include <QJsonObject>
|
#include <QJsonObject>
|
||||||
#include <QNetworkAccessManager>
|
#include <QNetworkAccessManager>
|
||||||
#include <QNetworkReply>
|
#include <QNetworkReply>
|
||||||
#include <QThread>
|
#include <QThread>
|
||||||
|
#include <QTimer>
|
||||||
#include <QUrl>
|
#include <QUrl>
|
||||||
|
|
||||||
namespace chatterino {
|
namespace chatterino {
|
||||||
|
@ -32,6 +35,7 @@ class NetworkRequest
|
||||||
QNetworkRequest request;
|
QNetworkRequest request;
|
||||||
const QObject *caller = nullptr;
|
const QObject *caller = nullptr;
|
||||||
std::function<void(QNetworkReply *)> onReplyCreated;
|
std::function<void(QNetworkReply *)> onReplyCreated;
|
||||||
|
int timeoutMS = -1;
|
||||||
} data;
|
} data;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
@ -67,9 +71,19 @@ public:
|
||||||
this->data.request.setRawHeader(headerName, value);
|
this->data.request.setRawHeader(headerName, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void setTimeout(int ms)
|
||||||
|
{
|
||||||
|
this->data.timeoutMS = ms;
|
||||||
|
}
|
||||||
|
|
||||||
template <typename FinishedCallback>
|
template <typename FinishedCallback>
|
||||||
void get(FinishedCallback onFinished)
|
void get(FinishedCallback onFinished)
|
||||||
{
|
{
|
||||||
|
QTimer *timer = nullptr;
|
||||||
|
if (this->data.timeoutMS > 0) {
|
||||||
|
timer = new QTimer;
|
||||||
|
}
|
||||||
|
|
||||||
NetworkRequester requester;
|
NetworkRequester requester;
|
||||||
NetworkWorker *worker = new NetworkWorker;
|
NetworkWorker *worker = new NetworkWorker;
|
||||||
|
|
||||||
|
@ -83,11 +97,23 @@ public:
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (timer != nullptr) {
|
||||||
|
timer->start(this->data.timeoutMS);
|
||||||
|
}
|
||||||
|
|
||||||
QObject::connect(
|
QObject::connect(
|
||||||
&requester, &NetworkRequester::requestUrl, worker,
|
&requester, &NetworkRequester::requestUrl, worker,
|
||||||
[ data = std::move(this->data), worker, onFinished{std::move(onFinished)} ]() {
|
[ timer, data = std::move(this->data), worker, onFinished{std::move(onFinished)} ]() {
|
||||||
QNetworkReply *reply = NetworkManager::NaM.get(data.request);
|
QNetworkReply *reply = NetworkManager::NaM.get(data.request);
|
||||||
|
|
||||||
|
if (timer != nullptr) {
|
||||||
|
QObject::connect(timer, &QTimer::timeout, worker, [reply, timer]() {
|
||||||
|
debug::Log("Aborted!");
|
||||||
|
reply->abort();
|
||||||
|
timer->deleteLater();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
if (data.onReplyCreated) {
|
if (data.onReplyCreated) {
|
||||||
data.onReplyCreated(reply);
|
data.onReplyCreated(reply);
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,51 +38,6 @@ static QJsonObject parseJSONFromReply(QNetworkReply *reply)
|
||||||
return jsonDoc.object();
|
return jsonDoc.object();
|
||||||
}
|
}
|
||||||
|
|
||||||
static void urlFetchTimeout(const QString &url, const QObject *caller,
|
|
||||||
std::function<void(QNetworkReply *)> successCallback, int timeoutMs)
|
|
||||||
{
|
|
||||||
QTimer *timer = new QTimer;
|
|
||||||
timer->setSingleShot(true);
|
|
||||||
|
|
||||||
QEventLoop *loop = new QEventLoop;
|
|
||||||
|
|
||||||
util::NetworkRequest req(url);
|
|
||||||
req.setCaller(loop);
|
|
||||||
req.setOnReplyCreated([loop, timer](QNetworkReply *reply) {
|
|
||||||
QObject::connect(timer, &QTimer::timeout, loop, [=]() {
|
|
||||||
QObject::disconnect(reply, &QNetworkReply::finished, loop, &QEventLoop::quit);
|
|
||||||
reply->abort();
|
|
||||||
reply->deleteLater();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
req.get([=](QNetworkReply *reply) {
|
|
||||||
if (reply->error() == QNetworkReply::NetworkError::NoError) {
|
|
||||||
successCallback(reply);
|
|
||||||
}
|
|
||||||
|
|
||||||
reply->deleteLater();
|
|
||||||
loop->quit();
|
|
||||||
});
|
|
||||||
|
|
||||||
QObject::connect(timer, SIGNAL(timeout()), loop, SLOT(quit()));
|
|
||||||
|
|
||||||
timer->start(timeoutMs);
|
|
||||||
loop->exec();
|
|
||||||
delete timer;
|
|
||||||
delete loop;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void urlFetchJSONTimeout(const QString &url, const QObject *caller,
|
|
||||||
std::function<void(QJsonObject &)> successCallback, int timeoutMs)
|
|
||||||
{
|
|
||||||
urlFetchTimeout(url, caller,
|
|
||||||
[=](QNetworkReply *reply) {
|
|
||||||
auto node = parseJSONFromReply(reply);
|
|
||||||
successCallback(node);
|
|
||||||
},
|
|
||||||
timeoutMs);
|
|
||||||
}
|
|
||||||
|
|
||||||
namespace twitch {
|
namespace twitch {
|
||||||
|
|
||||||
static void get(QString url, const QObject *caller,
|
static void get(QString url, const QObject *caller,
|
||||||
|
|
Loading…
Reference in a new issue