mirror of
https://github.com/Chatterino/chatterino2.git
synced 2024-11-21 22:24:07 +01:00
Refactored NetworkRequest and misc
This commit is contained in:
parent
477d4751f7
commit
3993708164
8 changed files with 244 additions and 243 deletions
|
@ -22,10 +22,10 @@ Channel::Channel(const QString &_name, Type _type)
|
|||
, completionModel(this->name)
|
||||
, type_(_type)
|
||||
{
|
||||
QObject::connect(&this->clearCompletionModelTimer, &QTimer::timeout, [this]() {
|
||||
this->completionModel.ClearExpiredStrings(); //
|
||||
QObject::connect(&this->clearCompletionModelTimer_, &QTimer::timeout, [this]() {
|
||||
this->completionModel.clearExpiredStrings(); //
|
||||
});
|
||||
this->clearCompletionModelTimer.start(60 * 1000);
|
||||
this->clearCompletionModelTimer_.start(60 * 1000);
|
||||
}
|
||||
|
||||
Channel::~Channel()
|
||||
|
|
|
@ -213,7 +213,7 @@ void CompletionModel::clearExpiredStrings()
|
|||
for (auto it = this->emotes_.begin(); it != this->emotes_.end();) {
|
||||
const auto &taggedString = *it;
|
||||
|
||||
if (taggedString.HasExpired(now)) {
|
||||
if (taggedString.isExpired(now)) {
|
||||
// Log("String {} expired", taggedString.str);
|
||||
it = this->emotes_.erase(it);
|
||||
} else {
|
||||
|
|
|
@ -14,12 +14,12 @@ struct EmoteData {
|
|||
bool isValid() const;
|
||||
Image *getImage(float scale) const;
|
||||
|
||||
// Link to the emote page i.e. https://www.frankerfacez.com/emoticon/144722-pajaCringe
|
||||
QString pageLink;
|
||||
|
||||
Image *image1x = nullptr;
|
||||
Image *image2x = nullptr;
|
||||
Image *image3x = nullptr;
|
||||
|
||||
// Link to the emote page i.e. https://www.frankerfacez.com/emoticon/144722-pajaCringe
|
||||
QString pageLink;
|
||||
};
|
||||
|
||||
using EmoteMap = ConcurrentMap<QString, EmoteData>;
|
||||
|
|
|
@ -10,29 +10,29 @@ class LockedObject
|
|||
public:
|
||||
LockedObject &operator=(const LockedObject<Type> &other)
|
||||
{
|
||||
this->mutex.lock();
|
||||
this->mutex_.lock();
|
||||
|
||||
this->data = other.getValue();
|
||||
|
||||
this->mutex.unlock();
|
||||
this->mutex_.unlock();
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
LockedObject &operator=(const Type &other)
|
||||
{
|
||||
this->mutex.lock();
|
||||
this->mutex_.lock();
|
||||
|
||||
this->data = other;
|
||||
|
||||
this->mutex.unlock();
|
||||
this->mutex_.unlock();
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
Type value;
|
||||
std::mutex mutex;
|
||||
Type value_;
|
||||
std::mutex mutex_;
|
||||
};
|
||||
|
||||
} // namespace chatterino
|
||||
|
|
|
@ -8,32 +8,33 @@ namespace chatterino {
|
|||
template <typename T>
|
||||
class MutexValue : boost::noncopyable
|
||||
{
|
||||
mutable std::mutex mutex;
|
||||
T value;
|
||||
|
||||
public:
|
||||
MutexValue()
|
||||
{
|
||||
}
|
||||
|
||||
MutexValue(T &&val)
|
||||
: value(val)
|
||||
: value_(val)
|
||||
{
|
||||
}
|
||||
|
||||
T get() const
|
||||
{
|
||||
std::lock_guard<std::mutex> guard(this->mutex);
|
||||
std::lock_guard<std::mutex> guard(this->mutex_);
|
||||
|
||||
return this->value;
|
||||
return this->value_;
|
||||
}
|
||||
|
||||
void set(const T &val)
|
||||
{
|
||||
std::lock_guard<std::mutex> guard(this->mutex);
|
||||
std::lock_guard<std::mutex> guard(this->mutex_);
|
||||
|
||||
this->value = val;
|
||||
this->value_ = val;
|
||||
}
|
||||
|
||||
private:
|
||||
mutable std::mutex mutex_;
|
||||
T value_;
|
||||
};
|
||||
|
||||
} // namespace chatterino
|
||||
|
|
|
@ -14,22 +14,6 @@
|
|||
|
||||
namespace chatterino {
|
||||
|
||||
static QJsonObject parseJSONFromReplyxD(QNetworkReply *reply)
|
||||
{
|
||||
if (reply->error() != QNetworkReply::NetworkError::NoError) {
|
||||
return QJsonObject();
|
||||
}
|
||||
|
||||
QByteArray data = reply->readAll();
|
||||
QJsonDocument jsonDoc(QJsonDocument::fromJson(data));
|
||||
|
||||
if (jsonDoc.isNull()) {
|
||||
return QJsonObject();
|
||||
}
|
||||
|
||||
return jsonDoc.object();
|
||||
}
|
||||
|
||||
class NetworkManager : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
|
|
@ -19,6 +19,48 @@ NetworkRequest::NetworkRequest(const QString &url)
|
|||
this->data.request.setUrl(QUrl(url));
|
||||
}
|
||||
|
||||
void NetworkRequest::setRequestType(RequestType newRequestType)
|
||||
{
|
||||
this->data.requestType = newRequestType;
|
||||
}
|
||||
|
||||
void NetworkRequest::setCaller(const QObject *_caller)
|
||||
{
|
||||
this->data.caller = _caller;
|
||||
}
|
||||
|
||||
void NetworkRequest::setOnReplyCreated(std::function<void(QNetworkReply *)> f)
|
||||
{
|
||||
this->data.onReplyCreated = f;
|
||||
}
|
||||
|
||||
void NetworkRequest::setRawHeader(const char *headerName, const char *value)
|
||||
{
|
||||
this->data.request.setRawHeader(headerName, value);
|
||||
}
|
||||
|
||||
void NetworkRequest::setRawHeader(const char *headerName, const QByteArray &value)
|
||||
{
|
||||
this->data.request.setRawHeader(headerName, value);
|
||||
}
|
||||
|
||||
void NetworkRequest::setRawHeader(const char *headerName, const QString &value)
|
||||
{
|
||||
this->data.request.setRawHeader(headerName, value.toUtf8());
|
||||
}
|
||||
|
||||
void NetworkRequest::setTimeout(int ms)
|
||||
{
|
||||
this->data.timeoutMS = ms;
|
||||
}
|
||||
|
||||
void NetworkRequest::makeAuthorizedV5(const QString &clientID, const QString &oauthToken)
|
||||
{
|
||||
this->setRawHeader("Client-ID", clientID);
|
||||
this->setRawHeader("Accept", "application/vnd.twitchtv.v5+json");
|
||||
this->setRawHeader("Authorization", "OAuth " + oauthToken);
|
||||
}
|
||||
|
||||
void NetworkRequest::setUseQuickLoadCache(bool value)
|
||||
{
|
||||
this->data.useQuickLoadCache = value;
|
||||
|
@ -39,4 +81,170 @@ void NetworkRequest::Data::writeToCache(const QByteArray &bytes)
|
|||
}
|
||||
}
|
||||
|
||||
void NetworkRequest::execute()
|
||||
{
|
||||
switch (this->data.requestType) {
|
||||
case GetRequest: {
|
||||
this->executeGet();
|
||||
} break;
|
||||
|
||||
case PutRequest: {
|
||||
this->executePut();
|
||||
} break;
|
||||
|
||||
case DeleteRequest: {
|
||||
this->executeDelete();
|
||||
} break;
|
||||
|
||||
default: {
|
||||
Log("[Execute] Unhandled request type {}", (int)this->data.requestType);
|
||||
} break;
|
||||
}
|
||||
}
|
||||
|
||||
void NetworkRequest::useCache()
|
||||
{
|
||||
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();
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void NetworkRequest::doRequest()
|
||||
{
|
||||
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, [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);
|
||||
data.onSuccess(parseJSONFromData2(bytes));
|
||||
|
||||
reply->deleteLater();
|
||||
});
|
||||
}
|
||||
|
||||
if (timer != nullptr) {
|
||||
timer->start(this->data.timeoutMS);
|
||||
}
|
||||
|
||||
QObject::connect(&requester, &NetworkRequester::requestUrl, worker,
|
||||
[ timer, data = std::move(this->data), worker ]() {
|
||||
QNetworkReply *reply = nullptr;
|
||||
switch (data.requestType) {
|
||||
case GetRequest: {
|
||||
reply = NetworkManager::NaM.get(data.request);
|
||||
} break;
|
||||
|
||||
case PutRequest: {
|
||||
reply = NetworkManager::NaM.put(data.request, data.payload);
|
||||
} break;
|
||||
|
||||
case DeleteRequest: {
|
||||
reply = NetworkManager::NaM.deleteResource(data.request);
|
||||
} break;
|
||||
}
|
||||
|
||||
if (reply == nullptr) {
|
||||
Log("Unhandled request type {}", (int)data.requestType);
|
||||
return;
|
||||
}
|
||||
|
||||
if (timer != nullptr) {
|
||||
QObject::connect(timer, &QTimer::timeout, worker,
|
||||
[reply, timer, data]() {
|
||||
Log("Aborted!");
|
||||
reply->abort();
|
||||
timer->deleteLater();
|
||||
data.onError(-2);
|
||||
});
|
||||
}
|
||||
|
||||
if (data.onReplyCreated) {
|
||||
data.onReplyCreated(reply);
|
||||
}
|
||||
|
||||
QObject::connect(reply, &QNetworkReply::finished, worker,
|
||||
[ data = std::move(data), worker, reply ]() mutable {
|
||||
if (data.caller == nullptr) {
|
||||
QByteArray bytes = reply->readAll();
|
||||
data.writeToCache(bytes);
|
||||
|
||||
if (data.onSuccess) {
|
||||
data.onSuccess(parseJSONFromData2(bytes));
|
||||
} else {
|
||||
qWarning() << "data.onSuccess not found";
|
||||
}
|
||||
|
||||
reply->deleteLater();
|
||||
} else {
|
||||
emit worker->doneUrl(reply);
|
||||
}
|
||||
|
||||
delete worker;
|
||||
});
|
||||
});
|
||||
|
||||
emit requester.requestUrl();
|
||||
}
|
||||
|
||||
void NetworkRequest::executeGet()
|
||||
{
|
||||
this->useCache();
|
||||
|
||||
this->doRequest();
|
||||
}
|
||||
|
||||
void NetworkRequest::executePut()
|
||||
{
|
||||
this->doRequest();
|
||||
}
|
||||
|
||||
void NetworkRequest::executeDelete()
|
||||
{
|
||||
this->doRequest();
|
||||
}
|
||||
} // namespace chatterino
|
||||
|
|
|
@ -115,10 +115,7 @@ public:
|
|||
explicit NetworkRequest(const std::string &url);
|
||||
explicit NetworkRequest(const QString &url);
|
||||
|
||||
void setRequestType(RequestType newRequestType)
|
||||
{
|
||||
this->data.requestType = newRequestType;
|
||||
}
|
||||
void setRequestType(RequestType newRequestType);
|
||||
|
||||
template <typename Func>
|
||||
void onError(Func cb)
|
||||
|
@ -138,43 +135,13 @@ public:
|
|||
}
|
||||
|
||||
void setUseQuickLoadCache(bool value);
|
||||
|
||||
void setCaller(const QObject *_caller)
|
||||
{
|
||||
this->data.caller = _caller;
|
||||
}
|
||||
|
||||
void setOnReplyCreated(std::function<void(QNetworkReply *)> f)
|
||||
{
|
||||
this->data.onReplyCreated = f;
|
||||
}
|
||||
|
||||
void setRawHeader(const char *headerName, const char *value)
|
||||
{
|
||||
this->data.request.setRawHeader(headerName, value);
|
||||
}
|
||||
|
||||
void setRawHeader(const char *headerName, const QByteArray &value)
|
||||
{
|
||||
this->data.request.setRawHeader(headerName, value);
|
||||
}
|
||||
|
||||
void setRawHeader(const char *headerName, const QString &value)
|
||||
{
|
||||
this->data.request.setRawHeader(headerName, value.toUtf8());
|
||||
}
|
||||
|
||||
void setTimeout(int ms)
|
||||
{
|
||||
this->data.timeoutMS = ms;
|
||||
}
|
||||
|
||||
void makeAuthorizedV5(const QString &clientID, const QString &oauthToken)
|
||||
{
|
||||
this->setRawHeader("Client-ID", clientID);
|
||||
this->setRawHeader("Accept", "application/vnd.twitchtv.v5+json");
|
||||
this->setRawHeader("Authorization", "OAuth " + oauthToken);
|
||||
}
|
||||
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 QByteArray &value);
|
||||
void setRawHeader(const char *headerName, const QString &value);
|
||||
void setTimeout(int ms);
|
||||
void makeAuthorizedV5(const QString &clientID, const QString &oauthToken);
|
||||
|
||||
template <typename FinishedCallback>
|
||||
void get(FinishedCallback onFinished)
|
||||
|
@ -300,173 +267,14 @@ public:
|
|||
});
|
||||
}
|
||||
|
||||
void execute()
|
||||
{
|
||||
switch (this->data.requestType) {
|
||||
case GetRequest: {
|
||||
this->executeGet();
|
||||
} break;
|
||||
|
||||
case PutRequest: {
|
||||
this->executePut();
|
||||
} break;
|
||||
|
||||
case DeleteRequest: {
|
||||
this->executeDelete();
|
||||
} break;
|
||||
|
||||
default: {
|
||||
Log("[Execute] Unhandled request type {}", (int)this->data.requestType);
|
||||
} break;
|
||||
}
|
||||
}
|
||||
void execute();
|
||||
|
||||
private:
|
||||
void useCache()
|
||||
{
|
||||
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();
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void doRequest()
|
||||
{
|
||||
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, [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);
|
||||
data.onSuccess(parseJSONFromData2(bytes));
|
||||
|
||||
reply->deleteLater();
|
||||
});
|
||||
}
|
||||
|
||||
if (timer != nullptr) {
|
||||
timer->start(this->data.timeoutMS);
|
||||
}
|
||||
|
||||
QObject::connect(&requester, &NetworkRequester::requestUrl, worker,
|
||||
[ timer, data = std::move(this->data), worker ]() {
|
||||
QNetworkReply *reply = nullptr;
|
||||
switch (data.requestType) {
|
||||
case GetRequest: {
|
||||
reply = NetworkManager::NaM.get(data.request);
|
||||
} break;
|
||||
|
||||
case PutRequest: {
|
||||
reply = NetworkManager::NaM.put(data.request, data.payload);
|
||||
} break;
|
||||
|
||||
case DeleteRequest: {
|
||||
reply = NetworkManager::NaM.deleteResource(data.request);
|
||||
} break;
|
||||
}
|
||||
|
||||
if (reply == nullptr) {
|
||||
Log("Unhandled request type {}", (int)data.requestType);
|
||||
return;
|
||||
}
|
||||
|
||||
if (timer != nullptr) {
|
||||
QObject::connect(timer, &QTimer::timeout, worker,
|
||||
[reply, timer, data]() {
|
||||
Log("Aborted!");
|
||||
reply->abort();
|
||||
timer->deleteLater();
|
||||
data.onError(-2);
|
||||
});
|
||||
}
|
||||
|
||||
if (data.onReplyCreated) {
|
||||
data.onReplyCreated(reply);
|
||||
}
|
||||
|
||||
QObject::connect(reply, &QNetworkReply::finished, worker,
|
||||
[ data = std::move(data), worker, reply ]() mutable {
|
||||
if (data.caller == nullptr) {
|
||||
QByteArray bytes = reply->readAll();
|
||||
data.writeToCache(bytes);
|
||||
|
||||
if (data.onSuccess) {
|
||||
data.onSuccess(parseJSONFromData2(bytes));
|
||||
} else {
|
||||
qWarning() << "data.onSuccess not found";
|
||||
}
|
||||
|
||||
reply->deleteLater();
|
||||
} else {
|
||||
emit worker->doneUrl(reply);
|
||||
}
|
||||
|
||||
delete worker;
|
||||
});
|
||||
});
|
||||
|
||||
emit requester.requestUrl();
|
||||
}
|
||||
|
||||
void executeGet()
|
||||
{
|
||||
this->useCache();
|
||||
|
||||
this->doRequest();
|
||||
}
|
||||
|
||||
void executePut()
|
||||
{
|
||||
this->doRequest();
|
||||
}
|
||||
|
||||
void executeDelete()
|
||||
{
|
||||
this->doRequest();
|
||||
}
|
||||
void useCache();
|
||||
void doRequest();
|
||||
void executeGet();
|
||||
void executePut();
|
||||
void executeDelete();
|
||||
};
|
||||
|
||||
} // namespace chatterino
|
||||
|
|
Loading…
Reference in a new issue