aboutsummaryrefslogtreecommitdiff
path: root/lib/jobs
diff options
context:
space:
mode:
Diffstat (limited to 'lib/jobs')
-rw-r--r--lib/jobs/basejob.cpp45
1 files changed, 32 insertions, 13 deletions
diff --git a/lib/jobs/basejob.cpp b/lib/jobs/basejob.cpp
index 0d9b9f10..0e6a8403 100644
--- a/lib/jobs/basejob.cpp
+++ b/lib/jobs/basejob.cpp
@@ -104,6 +104,7 @@ BaseJob::BaseJob(HttpVerb verb, const QString& name, const QString& endpoint,
BaseJob::~BaseJob()
{
stop();
+ d->retryTimer.stop(); // See #398
qCDebug(d->logCat) << this << "destroyed";
}
@@ -197,8 +198,9 @@ void BaseJob::Private::sendRequest(bool inBackground)
{ makeRequestUrl(connection->baseUrl(), apiEndpoint, requestQuery) };
if (!requestHeaders.contains("Content-Type"))
req.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
- req.setRawHeader("Authorization",
- QByteArray("Bearer ") + connection->accessToken());
+ if (needsToken)
+ req.setRawHeader("Authorization",
+ QByteArray("Bearer ") + connection->accessToken());
req.setAttribute(QNetworkRequest::BackgroundRequestAttribute, inBackground);
#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
req.setAttribute(QNetworkRequest::FollowRedirectsAttribute, true);
@@ -209,6 +211,7 @@ void BaseJob::Private::sendRequest(bool inBackground)
// some sources claim that there are issues with QT 5.8
req.setAttribute(QNetworkRequest::HTTP2AllowedAttribute, true);
#endif
+ Q_ASSERT(req.url().isValid());
for (auto it = requestHeaders.cbegin(); it != requestHeaders.cend(); ++it)
req.setRawHeader(it.key(), it.value());
switch( verb )
@@ -239,16 +242,23 @@ void BaseJob::beforeAbandon(QNetworkReply*)
void BaseJob::start(const ConnectionData* connData, bool inBackground)
{
- d->connection = connData;
- d->retryTimer.setSingleShot(true);
- connect (&d->retryTimer, &QTimer::timeout,
- this, [this,inBackground] { sendRequest(inBackground); });
-
- beforeStart(connData);
- if (status().good())
- sendRequest(inBackground);
- if (status().good())
- afterStart(connData, d->reply.data());
+ if (connData && connData->baseUrl().isValid()) {
+ d->connection = connData;
+ d->retryTimer.setSingleShot(true);
+ connect(&d->retryTimer, &QTimer::timeout, this,
+ [this, inBackground] { sendRequest(inBackground); });
+
+ beforeStart(connData);
+ if (status().good())
+ sendRequest(inBackground);
+ if (status().good())
+ afterStart(connData, d->reply.data());
+ } else {
+ qCCritical(d->logCat)
+ << "Developers, ensure the Connection is valid before using it";
+ Q_ASSERT(false);
+ setStatus(IncorrectRequestError, tr("Invalid server connection"));
+ }
if (!status().good())
QTimer::singleShot(0, this, &BaseJob::finishJob);
}
@@ -333,7 +343,14 @@ void BaseJob::gotReply()
d->status.message =
tr("Requested room version: %1")
.arg(json.value("room_version").toString());
- } else if (!json.isEmpty()) // Not localisable on the client side
+ }
+ else if (errCode == "M_CANNOT_LEAVE_SERVER_NOTICE_ROOM")
+ setStatus(IncorrectRequestError,
+ tr("It's not allowed to leave a server notices room"));
+ else if (errCode == "M_USER_DEACTIVATED")
+ setStatus(ContentAccessError,
+ tr("The user has been deactivated"));
+ else if (!json.isEmpty()) // Not localisable on the client side
setStatus(d->status.code, json.value("error"_ls).toString());
}
}
@@ -633,6 +650,8 @@ void BaseJob::setStatus(int code, QString message)
void BaseJob::abandon()
{
beforeAbandon(d->reply ? d->reply.data() : nullptr);
+ d->timer.stop();
+ d->retryTimer.stop(); // In case abandon() was called between retries
setStatus(Abandoned);
if (d->reply)
d->reply->disconnect(this);