summary refs log tree commit diff
path: root/src/MatrixClient.cc
diff options
context:
space:
mode:
authorjansol <jhs@psonet.com>2017-07-08 14:41:49 +0300
committermujx <mujx@users.noreply.github.com>2017-07-08 14:41:49 +0300
commitf5ba63946b46877111dc48b760f03e98cc10548d (patch)
treef45a65f6fe012dc98afe68e7769ba6171d88a302 /src/MatrixClient.cc
parentFix emoji alignment issue (#43) (diff)
downloadnheko-f5ba63946b46877111dc48b760f03e98cc10548d.tar.xz
Improve login flow (#35)
* Validate both inferred and explicitly entered server addresses by attempting to call the /versions endpoint
* If the domain from the mxid fails validation, try prefixing it with 'matrix'
* Only show server address field if address validation ultimately fails
Diffstat (limited to 'src/MatrixClient.cc')
-rw-r--r--src/MatrixClient.cc27
1 files changed, 25 insertions, 2 deletions
diff --git a/src/MatrixClient.cc b/src/MatrixClient.cc

index ebecb05a..11ac61c7 100644 --- a/src/MatrixClient.cc +++ b/src/MatrixClient.cc
@@ -30,6 +30,7 @@ #include "MatrixClient.h" #include "Profile.h" #include "Register.h" +#include "Versions.h" MatrixClient::MatrixClient(QString server, QObject *parent) : QNetworkAccessManager(parent) @@ -57,12 +58,34 @@ void MatrixClient::onVersionsResponse(QNetworkReply *reply) { reply->deleteLater(); - qDebug() << "Handling the versions response"; + int status_code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); + + if (status_code == 404) { + emit versionError("Versions endpoint was not found on the server. Possibly not a Matrix server"); + return; + } + + if (status_code >= 400) { + qWarning() << "API version error: " << reply->errorString(); + emit versionError("An unknown error occured. Please try again."); + return; + } auto data = reply->readAll(); auto json = QJsonDocument::fromJson(data); - qDebug() << json; + VersionsResponse response; + + try { + response.deserialize(json); + if (!response.isVersionSupported(0, 2, 0)) + emit versionError("Server does not support required API version."); + else + emit versionSuccess(); + } catch (DeserializationException &e) { + qWarning() << "Malformed JSON response" << e.what(); + emit versionError("Malformed response. Possibly not a Matrix server"); + } } void MatrixClient::onLoginResponse(QNetworkReply *reply)