mirror of
https://github.com/Nheko-Reborn/nheko.git
synced 2024-11-22 19:08:58 +03:00
Implement initial registration stage
This commit is contained in:
parent
c7c3ee19ee
commit
f50fb34fb6
7 changed files with 216 additions and 21 deletions
|
@ -70,6 +70,7 @@ set(SRC_FILES
|
||||||
src/RoomInfo.cc
|
src/RoomInfo.cc
|
||||||
src/RoomInfoListItem.cc
|
src/RoomInfoListItem.cc
|
||||||
src/RoomList.cc
|
src/RoomList.cc
|
||||||
|
src/Register.cc
|
||||||
src/RegisterPage.cc
|
src/RegisterPage.cc
|
||||||
src/SlidingStackWidget.cc
|
src/SlidingStackWidget.cc
|
||||||
src/Sync.cc
|
src/Sync.cc
|
||||||
|
@ -108,6 +109,7 @@ qt5_wrap_cpp(MOC_HEADERS
|
||||||
include/LoginPage.h
|
include/LoginPage.h
|
||||||
include/MainWindow.h
|
include/MainWindow.h
|
||||||
include/MatrixClient.h
|
include/MatrixClient.h
|
||||||
|
include/Register.h
|
||||||
include/RegisterPage.h
|
include/RegisterPage.h
|
||||||
include/RoomInfoListItem.h
|
include/RoomInfoListItem.h
|
||||||
include/RoomList.h
|
include/RoomList.h
|
||||||
|
|
|
@ -38,7 +38,7 @@ public:
|
||||||
void sync();
|
void sync();
|
||||||
void sendTextMessage(const QString &roomid, const QString &msg);
|
void sendTextMessage(const QString &roomid, const QString &msg);
|
||||||
void login(const QString &username, const QString &password);
|
void login(const QString &username, const QString &password);
|
||||||
void registerUser(const QString &username, const QString &password);
|
void registerUser(const QString &username, const QString &password, const QString &server);
|
||||||
void versions();
|
void versions();
|
||||||
|
|
||||||
inline QString getHomeServer();
|
inline QString getHomeServer();
|
||||||
|
@ -53,11 +53,11 @@ public slots:
|
||||||
inline void setNextBatchToken(const QString &next_batch);
|
inline void setNextBatchToken(const QString &next_batch);
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
// Emitted after a error during the login.
|
|
||||||
void loginError(const QString &error);
|
void loginError(const QString &error);
|
||||||
|
void registerError(const QString &error);
|
||||||
|
|
||||||
// Emitted after succesfull user login. A new access token is returned by the server.
|
|
||||||
void loginSuccess(const QString &userid, const QString &homeserver, const QString &token);
|
void loginSuccess(const QString &userid, const QString &homeserver, const QString &token);
|
||||||
|
void registerSuccess(const QString &userid, const QString &homeserver, const QString &token);
|
||||||
|
|
||||||
// Returned profile data for the user's account.
|
// Returned profile data for the user's account.
|
||||||
void getOwnProfileResponse(const QUrl &avatar_url, const QString &display_name);
|
void getOwnProfileResponse(const QUrl &avatar_url, const QString &display_name);
|
||||||
|
|
81
include/Register.h
Normal file
81
include/Register.h
Normal file
|
@ -0,0 +1,81 @@
|
||||||
|
/*
|
||||||
|
* nheko Copyright (C) 2017 Konstantinos Sideris <siderisk@auth.gr>
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef REGISTER_H
|
||||||
|
#define REGISTER_H
|
||||||
|
|
||||||
|
#include <QJsonDocument>
|
||||||
|
|
||||||
|
#include "Deserializable.h"
|
||||||
|
|
||||||
|
class RegisterRequest
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
RegisterRequest();
|
||||||
|
RegisterRequest(const QString &username, const QString &password);
|
||||||
|
|
||||||
|
QByteArray serialize();
|
||||||
|
|
||||||
|
inline void setPassword(QString password);
|
||||||
|
inline void setUser(QString username);
|
||||||
|
|
||||||
|
private:
|
||||||
|
QString user_;
|
||||||
|
QString password_;
|
||||||
|
};
|
||||||
|
|
||||||
|
inline void RegisterRequest::setPassword(QString password)
|
||||||
|
{
|
||||||
|
password_ = password;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void RegisterRequest::setUser(QString username)
|
||||||
|
{
|
||||||
|
user_ = username;
|
||||||
|
}
|
||||||
|
|
||||||
|
class RegisterResponse : public Deserializable
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
void deserialize(const QJsonDocument &data) throw(DeserializationException) override;
|
||||||
|
|
||||||
|
inline QString getAccessToken();
|
||||||
|
inline QString getHomeServer();
|
||||||
|
inline QString getUserId();
|
||||||
|
|
||||||
|
private:
|
||||||
|
QString access_token_;
|
||||||
|
QString home_server_;
|
||||||
|
QString user_id_;
|
||||||
|
};
|
||||||
|
|
||||||
|
inline QString RegisterResponse::getAccessToken()
|
||||||
|
{
|
||||||
|
return access_token_;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline QString RegisterResponse::getHomeServer()
|
||||||
|
{
|
||||||
|
return home_server_;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline QString RegisterResponse::getUserId()
|
||||||
|
{
|
||||||
|
return user_id_;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // REGISTER_H
|
|
@ -57,6 +57,11 @@ MainWindow::MainWindow(QWidget *parent)
|
||||||
this,
|
this,
|
||||||
SLOT(matrixRegister(const QString &, const QString &, const QString &)));
|
SLOT(matrixRegister(const QString &, const QString &, const QString &)));
|
||||||
|
|
||||||
|
connect(matrix_client_,
|
||||||
|
SIGNAL(registerError(const QString &)),
|
||||||
|
register_page_,
|
||||||
|
SLOT(registerError(const QString &)));
|
||||||
|
|
||||||
connect(matrix_client_, SIGNAL(loginError(QString)), login_page_, SLOT(loginError(QString)));
|
connect(matrix_client_, SIGNAL(loginError(QString)), login_page_, SLOT(loginError(QString)));
|
||||||
connect(matrix_client_,
|
connect(matrix_client_,
|
||||||
SIGNAL(loginSuccess(QString, QString, QString)),
|
SIGNAL(loginSuccess(QString, QString, QString)),
|
||||||
|
@ -66,8 +71,7 @@ MainWindow::MainWindow(QWidget *parent)
|
||||||
|
|
||||||
void MainWindow::matrixLogin(const QString &username, const QString &password, const QString &home_server)
|
void MainWindow::matrixLogin(const QString &username, const QString &password, const QString &home_server)
|
||||||
{
|
{
|
||||||
qDebug() << "About to login into Matrix";
|
qDebug() << "Logging in..." << username;
|
||||||
qDebug() << "Userame: " << username;
|
|
||||||
|
|
||||||
matrix_client_->setServer(home_server);
|
matrix_client_->setServer(home_server);
|
||||||
matrix_client_->login(username, password);
|
matrix_client_->login(username, password);
|
||||||
|
@ -88,9 +92,8 @@ void MainWindow::showChatPage(QString userid, QString homeserver, QString token)
|
||||||
|
|
||||||
void MainWindow::matrixRegister(const QString &username, const QString &password, const QString &server)
|
void MainWindow::matrixRegister(const QString &username, const QString &password, const QString &server)
|
||||||
{
|
{
|
||||||
Q_UNUSED(password);
|
|
||||||
|
|
||||||
qDebug() << "Registering" << username << "at" << server;
|
qDebug() << "Registering" << username << "at" << server;
|
||||||
|
matrix_client_->registerUser(username, password, server);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWindow::showWelcomePage()
|
void MainWindow::showWelcomePage()
|
||||||
|
|
|
@ -27,6 +27,7 @@
|
||||||
#include "Login.h"
|
#include "Login.h"
|
||||||
#include "MatrixClient.h"
|
#include "MatrixClient.h"
|
||||||
#include "Profile.h"
|
#include "Profile.h"
|
||||||
|
#include "Register.h"
|
||||||
|
|
||||||
MatrixClient::MatrixClient(QString server, QObject *parent)
|
MatrixClient::MatrixClient(QString server, QObject *parent)
|
||||||
: QNetworkAccessManager(parent)
|
: QNetworkAccessManager(parent)
|
||||||
|
@ -70,14 +71,10 @@ void MatrixClient::onLoginResponse(QNetworkReply *reply)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (status_code != 200) {
|
if (status_code >= 400) {
|
||||||
qDebug() << "Login response: status code " << status_code;
|
qWarning() << "Login error: " << reply->errorString();
|
||||||
|
emit loginError("An unknown error occured. Please try again.");
|
||||||
if (status_code >= 400) {
|
return;
|
||||||
qWarning() << "Login error: " << reply->errorString();
|
|
||||||
emit loginError("An unknown error occured. Please try again.");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
auto data = reply->readAll();
|
auto data = reply->readAll();
|
||||||
|
@ -100,7 +97,31 @@ void MatrixClient::onRegisterResponse(QNetworkReply *reply)
|
||||||
{
|
{
|
||||||
reply->deleteLater();
|
reply->deleteLater();
|
||||||
|
|
||||||
qDebug() << "Handling the register response";
|
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
||||||
|
|
||||||
|
auto data = reply->readAll();
|
||||||
|
auto json = QJsonDocument::fromJson(data);
|
||||||
|
|
||||||
|
if (status == 0 || status >= 400) {
|
||||||
|
if (json.isObject() && json.object().contains("error"))
|
||||||
|
emit registerError(json.object().value("error").toString());
|
||||||
|
else
|
||||||
|
emit registerError(reply->errorString());
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
RegisterResponse response;
|
||||||
|
|
||||||
|
try {
|
||||||
|
response.deserialize(json);
|
||||||
|
emit registerSuccess(response.getUserId(),
|
||||||
|
response.getHomeServer(),
|
||||||
|
response.getAccessToken());
|
||||||
|
} catch (DeserializationException &e) {
|
||||||
|
qWarning() << "Register" << e.what();
|
||||||
|
emit registerError("Received malformed response.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void MatrixClient::onGetOwnProfileResponse(QNetworkReply *reply)
|
void MatrixClient::onGetOwnProfileResponse(QNetworkReply *reply)
|
||||||
|
@ -123,7 +144,7 @@ void MatrixClient::onGetOwnProfileResponse(QNetworkReply *reply)
|
||||||
response.deserialize(json);
|
response.deserialize(json);
|
||||||
emit getOwnProfileResponse(response.getAvatarUrl(), response.getDisplayName());
|
emit getOwnProfileResponse(response.getAvatarUrl(), response.getDisplayName());
|
||||||
} catch (DeserializationException &e) {
|
} catch (DeserializationException &e) {
|
||||||
qWarning() << "Profile malformed response" << e.what();
|
qWarning() << "Profile:" << e.what();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -209,7 +230,7 @@ void MatrixClient::onSendTextMessageResponse(QNetworkReply *reply)
|
||||||
auto object = json.object();
|
auto object = json.object();
|
||||||
|
|
||||||
if (!object.contains("event_id")) {
|
if (!object.contains("event_id")) {
|
||||||
qDebug() << "SendTextMessage: missnig event_id from response";
|
qDebug() << "SendTextMessage: missing event_id from response";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -230,14 +251,19 @@ void MatrixClient::onResponse(QNetworkReply *reply)
|
||||||
break;
|
break;
|
||||||
case Endpoint::Register:
|
case Endpoint::Register:
|
||||||
onRegisterResponse(reply);
|
onRegisterResponse(reply);
|
||||||
|
break;
|
||||||
case Endpoint::GetOwnProfile:
|
case Endpoint::GetOwnProfile:
|
||||||
onGetOwnProfileResponse(reply);
|
onGetOwnProfileResponse(reply);
|
||||||
|
break;
|
||||||
case Endpoint::InitialSync:
|
case Endpoint::InitialSync:
|
||||||
onInitialSyncResponse(reply);
|
onInitialSyncResponse(reply);
|
||||||
|
break;
|
||||||
case Endpoint::Sync:
|
case Endpoint::Sync:
|
||||||
onSyncResponse(reply);
|
onSyncResponse(reply);
|
||||||
|
break;
|
||||||
case Endpoint::SendTextMessage:
|
case Endpoint::SendTextMessage:
|
||||||
onSendTextMessageResponse(reply);
|
onSendTextMessageResponse(reply);
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -257,6 +283,26 @@ void MatrixClient::login(const QString &username, const QString &password)
|
||||||
reply->setProperty("endpoint", Endpoint::Login);
|
reply->setProperty("endpoint", Endpoint::Login);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void MatrixClient::registerUser(const QString &user, const QString &pass, const QString &server)
|
||||||
|
{
|
||||||
|
setServer(server);
|
||||||
|
|
||||||
|
QUrlQuery query;
|
||||||
|
query.addQueryItem("kind", "user");
|
||||||
|
|
||||||
|
QUrl endpoint(server_);
|
||||||
|
endpoint.setPath(api_url_ + "/register");
|
||||||
|
endpoint.setQuery(query);
|
||||||
|
|
||||||
|
QNetworkRequest request(QString(endpoint.toEncoded()));
|
||||||
|
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
|
||||||
|
|
||||||
|
RegisterRequest body(user, pass);
|
||||||
|
|
||||||
|
QNetworkReply *reply = post(request, body.serialize());
|
||||||
|
reply->setProperty("endpoint", Endpoint::Register);
|
||||||
|
}
|
||||||
|
|
||||||
void MatrixClient::sync()
|
void MatrixClient::sync()
|
||||||
{
|
{
|
||||||
QJsonObject filter{{"room",
|
QJsonObject filter{{"room",
|
||||||
|
|
|
@ -25,15 +25,15 @@
|
||||||
void ProfileResponse::deserialize(const QJsonDocument &data) throw(DeserializationException)
|
void ProfileResponse::deserialize(const QJsonDocument &data) throw(DeserializationException)
|
||||||
{
|
{
|
||||||
if (!data.isObject())
|
if (!data.isObject())
|
||||||
throw DeserializationException("Profile response is not a JSON object");
|
throw DeserializationException("Response is not a JSON object");
|
||||||
|
|
||||||
QJsonObject object = data.object();
|
QJsonObject object = data.object();
|
||||||
|
|
||||||
if (object.value("avatar_url") == QJsonValue::Undefined)
|
if (object.value("avatar_url") == QJsonValue::Undefined)
|
||||||
throw DeserializationException("Profile: missing avatar_url param");
|
throw DeserializationException("Missing avatar_url param");
|
||||||
|
|
||||||
if (object.value("displayname") == QJsonValue::Undefined)
|
if (object.value("displayname") == QJsonValue::Undefined)
|
||||||
throw DeserializationException("Profile: missing displayname param");
|
throw DeserializationException("Missing displayname param");
|
||||||
|
|
||||||
avatar_url_ = QUrl(object.value("avatar_url").toString());
|
avatar_url_ = QUrl(object.value("avatar_url").toString());
|
||||||
display_name_ = object.value("displayname").toString();
|
display_name_ = object.value("displayname").toString();
|
||||||
|
|
63
src/Register.cc
Normal file
63
src/Register.cc
Normal file
|
@ -0,0 +1,63 @@
|
||||||
|
/*
|
||||||
|
* nheko Copyright (C) 2017 Konstantinos Sideris <siderisk@auth.gr>
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <QJsonDocument>
|
||||||
|
#include <QJsonObject>
|
||||||
|
#include <QJsonValue>
|
||||||
|
|
||||||
|
#include "Deserializable.h"
|
||||||
|
#include "Register.h"
|
||||||
|
|
||||||
|
RegisterRequest::RegisterRequest()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
RegisterRequest::RegisterRequest(const QString &username, const QString &password)
|
||||||
|
: user_(username)
|
||||||
|
, password_(password)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
QByteArray RegisterRequest::serialize()
|
||||||
|
{
|
||||||
|
QJsonObject body{
|
||||||
|
{"username", user_},
|
||||||
|
{"password", password_}};
|
||||||
|
|
||||||
|
return QJsonDocument(body).toJson(QJsonDocument::Compact);
|
||||||
|
}
|
||||||
|
|
||||||
|
void RegisterResponse::deserialize(const QJsonDocument &data) throw(DeserializationException)
|
||||||
|
{
|
||||||
|
if (!data.isObject())
|
||||||
|
throw DeserializationException("Response is not a JSON object");
|
||||||
|
|
||||||
|
QJsonObject object = data.object();
|
||||||
|
|
||||||
|
if (!object.contains("access_token"))
|
||||||
|
throw DeserializationException("Missing access_token param");
|
||||||
|
|
||||||
|
if (!object.contains("home_server"))
|
||||||
|
throw DeserializationException("Missing home_server param");
|
||||||
|
|
||||||
|
if (!object.contains("user_id"))
|
||||||
|
throw DeserializationException("Missing user_id param");
|
||||||
|
|
||||||
|
access_token_ = object.value("access_token").toString();
|
||||||
|
home_server_ = object.value("home_server").toString();
|
||||||
|
user_id_ = object.value("user_id").toString();
|
||||||
|
}
|
Loading…
Reference in a new issue