aboutsummaryrefslogtreecommitdiff
path: root/lib/connection.cpp
diff options
context:
space:
mode:
authorKitsune Ral <Kitsune-Ral@users.sf.net>2020-03-30 19:16:11 +0200
committerKitsune Ral <Kitsune-Ral@users.sf.net>2020-03-30 19:16:11 +0200
commitcfbc71bc70707ff1ed691cc04ecbe246024f6734 (patch)
treedf77e5687c528de82a8132ce2f36d8fcb9f76cd2 /lib/connection.cpp
parent41a74ae1175b4b8723ef829270f7149c302a13c1 (diff)
downloadlibquotient-cfbc71bc70707ff1ed691cc04ecbe246024f6734.tar.gz
libquotient-cfbc71bc70707ff1ed691cc04ecbe246024f6734.zip
Connection::resolveServer(): refactor
Also: use 4-arg connect() to make sure lambdas are disconnected if the connection is gone.
Diffstat (limited to 'lib/connection.cpp')
-rw-r--r--lib/connection.cpp75
1 files changed, 39 insertions, 36 deletions
diff --git a/lib/connection.cpp b/lib/connection.cpp
index 8c87d775..cc140ae4 100644
--- a/lib/connection.cpp
+++ b/lib/connection.cpp
@@ -175,52 +175,55 @@ void Connection::resolveServer(const QString& mxidOrDomain)
maybeBaseUrl.setScheme("https"); // Instead of the Qt-default "http"
if (!match.hasMatch() || !maybeBaseUrl.isValid())
{
- emit resolveError(
- tr("%1 is not a valid homeserver address")
- .arg(maybeBaseUrl.toString()));
+ emit resolveError(tr("%1 is not a valid homeserver address")
+ .arg(maybeBaseUrl.toString()));
return;
}
- setHomeserver(maybeBaseUrl);
-
auto domain = maybeBaseUrl.host();
qCDebug(MAIN) << "Finding the server" << domain;
+ d->data->setBaseUrl(maybeBaseUrl); // Just enough to check .well-known file
auto getWellKnownJob = callApi<GetWellknownJob>();
- connect(getWellKnownJob, &BaseJob::finished, [this, getWellKnownJob, maybeBaseUrl] {
- if (getWellKnownJob->status() == BaseJob::NotFoundError) {
- qCDebug(MAIN) << "No .well-known file, IGNORE";
- } else if (getWellKnownJob->status() != BaseJob::Success) {
- qCDebug(MAIN) << "Fetching .well-known file failed, FAIL_PROMPT";
- emit resolveError(tr("Fetching .well-known file failed"));
- return;
- } else if (getWellKnownJob->data().homeserver.baseUrl.isEmpty()) {
- qCDebug(MAIN) << "base_url not provided, FAIL_PROMPT";
- emit resolveError(tr("base_url not provided"));
- return;
- } else if (!QUrl(getWellKnownJob->data().homeserver.baseUrl).isValid()) {
- qCDebug(MAIN) << "base_url invalid, FAIL_ERROR";
- emit resolveError(tr("base_url invalid"));
- return;
- } else {
- QUrl baseUrl(getWellKnownJob->data().homeserver.baseUrl);
-
- qCDebug(MAIN) << ".well-known for" << maybeBaseUrl.host() << "is" << baseUrl.authority();
- setHomeserver(baseUrl);
- }
-
- auto getVersionsJob = callApi<GetVersionsJob>();
-
- connect(getVersionsJob, &BaseJob::finished, [this, getVersionsJob] {
- if (getVersionsJob->status() == BaseJob::Success) {
- qCDebug(MAIN) << "homeserver url is valid";
- emit resolved();
+ connect(getWellKnownJob, &BaseJob::finished, this,
+ [this, getWellKnownJob, maybeBaseUrl] {
+ if (getWellKnownJob->status() != BaseJob::NotFoundError) {
+ if (getWellKnownJob->status() != BaseJob::Success) {
+ qCWarning(MAIN)
+ << "Fetching .well-known file failed, FAIL_PROMPT";
+ emit resolveError(tr("Failed resolving the homeserver"));
+ return;
+ }
+ QUrl baseUrl { getWellKnownJob->data().homeserver.baseUrl };
+ if (baseUrl.isEmpty()) {
+ qCWarning(MAIN) << "base_url not provided, FAIL_PROMPT";
+ emit resolveError(
+ tr("The homeserver base URL is not provided"));
+ return;
+ }
+ if (!baseUrl.isValid()) {
+ qCWarning(MAIN) << "base_url invalid, FAIL_ERROR";
+ emit resolveError(tr("The homeserver base URL is invalid"));
+ return;
+ }
+ qCInfo(MAIN) << ".well-known URL for" << maybeBaseUrl.host()
+ << "is" << baseUrl.authority();
+ setHomeserver(baseUrl);
} else {
- qCDebug(MAIN) << "homeserver url invalid";
- emit resolveError(tr("homeserver url invalid"));
+ qCInfo(MAIN) << "No .well-known file, using" << maybeBaseUrl
+ << "for base URL";
+ setHomeserver(maybeBaseUrl);
}
+
+ auto getVersionsJob = callApi<GetVersionsJob>();
+ connect(getVersionsJob, &BaseJob::success, this,
+ &Connection::resolved);
+ connect(getVersionsJob, &BaseJob::failure, this, [this] {
+ qCWarning(MAIN) << "Homeserver base URL invalid";
+ emit resolveError(tr("The homeserver base URL "
+ "doesn't seem to work"));
+ });
});
- });
}
inline UserIdentifier makeUserIdentifier(const QString& id)