Initial integration with matrix-structs

This commit is contained in:
Konstantinos Sideris 2017-12-03 02:47:37 +02:00
parent f4f78b1d8a
commit 914bdecc0b
11 changed files with 30 additions and 247 deletions

3
.gitmodules vendored
View File

@ -1,3 +1,6 @@
[submodule "libs/lmdbxx"]
path = libs/lmdbxx
url = https://github.com/bendiken/lmdbxx
[submodule "libs/matrix-structs"]
path = libs/matrix-structs
url = https://github.com/mujx/matrix-structs

View File

@ -191,7 +191,6 @@ set(SRC_FILES
src/LoginPage.cc
src/MainWindow.cc
src/MatrixClient.cc
src/Profile.cc
src/QuickSwitcher.cc
src/Register.cc
src/RegisterPage.cc
@ -208,7 +207,6 @@ set(SRC_FILES
src/TypingDisplay.cc
src/UserInfoWidget.cc
src/UserSettingsPage.cc
src/Versions.cc
src/WelcomePage.cc
src/main.cc
)
@ -332,6 +330,8 @@ qt5_add_resources(QRC resources/res.qrc)
add_library(matrix_events ${MATRIX_EVENTS} src/Deserializable.cc)
target_link_libraries(matrix_events Qt5::Core)
add_subdirectory(libs/matrix-structs)
if (BUILD_TESTS)
enable_testing()
@ -352,10 +352,12 @@ if (BUILD_TESTS)
add_test(MatrixMessageEvents message_events)
endif()
set(COMMON_LIBS matrix_events matrix_structs Qt5::Widgets Qt5::Network Qt5::Concurrent)
if(APPVEYOR_BUILD)
set (NHEKO_LIBS matrix_events Qt5::Widgets Qt5::Network Qt5::Concurrent lmdb)
set(NHEKO_LIBS ${COMMON_LIBS} lmdb)
else()
set (NHEKO_LIBS matrix_events Qt5::Widgets Qt5::Network Qt5::Concurrent ${LMDB_LIBRARY})
set(NHEKO_LIBS ${COMMON_LIBS} ${LMDB_LIBRARY})
endif()
set (NHEKO_DEPS ${SRC_FILES} ${UI_HEADERS} ${MOC_HEADERS} ${QRC} ${LANG_QRC} ${QM_SRC} ${META_FILES_TO_INCLUDE})

View File

@ -36,18 +36,3 @@ private:
QString user_;
QString password_;
};
class LoginResponse : public Deserializable
{
public:
void deserialize(const QJsonDocument &data) override;
QString getAccessToken() { return access_token_; };
QString getHomeServer() { return home_server_; };
QString getUserId() { return user_id_; };
private:
QString access_token_;
QString home_server_;
QString user_id_;
};

View File

@ -1,36 +0,0 @@
/*
* 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/>.
*/
#pragma once
#include <QJsonDocument>
#include <QUrl>
class Deserializable;
class ProfileResponse : public Deserializable
{
public:
void deserialize(const QJsonDocument &data) override;
QUrl getAvatarUrl() { return avatar_url_; };
QString getDisplayName() { return display_name_; };
private:
QUrl avatar_url_;
QString display_name_;
};

View File

@ -1,41 +0,0 @@
/*
* 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/>.
*/
#pragma once
#include <QJsonDocument>
#include <QVector>
class Deserializable;
class VersionsResponse : public Deserializable
{
public:
void deserialize(const QJsonDocument &data) override;
bool isVersionSupported(unsigned int major, unsigned int minor, unsigned int patch);
private:
struct Version_
{
unsigned int major_;
unsigned int minor_;
unsigned int patch_;
};
QVector<Version_> supported_versions_;
};

1
libs/matrix-structs Submodule

@ -0,0 +1 @@
Subproject commit 190f297478153930780a119268b908076599c8db

View File

@ -260,9 +260,9 @@ ChatPage::ChatPage(QSharedPointer<MatrixClient> client, QWidget *parent)
this,
SLOT(syncFailed(const QString &)));
connect(client_.data(),
SIGNAL(getOwnProfileResponse(const QUrl &, const QString &)),
&MatrixClient::getOwnProfileResponse,
this,
SLOT(updateOwnProfileInfo(const QUrl &, const QString &)));
&ChatPage::updateOwnProfileInfo);
connect(client_.data(),
SIGNAL(ownAvatarRetrieved(const QPixmap &)),
this,

View File

@ -47,25 +47,3 @@ LoginRequest::serialize() noexcept
return QJsonDocument(body).toJson(QJsonDocument::Compact);
}
void
LoginResponse::deserialize(const QJsonDocument &data)
{
if (!data.isObject())
throw DeserializationException("Login response is not a JSON object");
QJsonObject object = data.object();
if (object.value("access_token") == QJsonValue::Undefined)
throw DeserializationException("Login: missing access_token param");
if (object.value("home_server") == QJsonValue::Undefined)
throw DeserializationException("Login: missing home_server param");
if (object.value("user_id") == QJsonValue::Undefined)
throw DeserializationException("Login: 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();
}

View File

@ -31,11 +31,11 @@
#include "Login.h"
#include "MatrixClient.h"
#include "MessageEvent.h"
#include "Profile.h"
#include "Register.h"
#include "RoomMessages.h"
#include "Sync.h"
#include "Versions.h"
#include "mtx.hpp"
MatrixClient::MatrixClient(QString server, QObject *parent)
: QNetworkAccessManager(parent)
@ -99,21 +99,18 @@ MatrixClient::login(const QString &username, const QString &password) noexcept
return;
}
auto data = reply->readAll();
auto json = QJsonDocument::fromJson(data);
LoginResponse response;
try {
response.deserialize(json);
mtx::responses::Login login =
nlohmann::json::parse(reply->readAll().data());
auto hostname = server_.host();
if (server_.port() > 0)
hostname = QString("%1:%2").arg(server_.host()).arg(server_.port());
emit loginSuccess(
response.getUserId(), hostname, response.getAccessToken());
emit loginSuccess(QString::fromStdString(login.user_id.toString()),
hostname,
QString::fromStdString(login.access_token));
} catch (DeserializationException &e) {
qWarning() << "Malformed JSON response" << e.what();
emit loginError(tr("Malformed response. Possibly not a Matrix server"));
@ -420,18 +417,12 @@ MatrixClient::versions() noexcept
return;
}
auto data = reply->readAll();
auto json = QJsonDocument::fromJson(data);
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) {
mtx::responses::Versions versions =
nlohmann::json::parse(reply->readAll().data());
emit versionSuccess();
} catch (std::exception &e) {
emit versionError("Malformed response. Possibly not a Matrix server");
}
});
@ -465,16 +456,13 @@ MatrixClient::getOwnProfile() noexcept
return;
}
auto data = reply->readAll();
auto json = QJsonDocument::fromJson(data);
ProfileResponse response;
try {
response.deserialize(json);
emit getOwnProfileResponse(response.getAvatarUrl(),
response.getDisplayName());
} catch (DeserializationException &e) {
mtx::responses::Profile profile =
nlohmann::json::parse(reply->readAll().data());
emit getOwnProfileResponse(QUrl(QString::fromStdString(profile.avatar_url)),
QString::fromStdString(profile.display_name));
} catch (std::exception &e) {
qWarning() << "Profile:" << e.what();
}
});

View File

@ -1,35 +0,0 @@
/*
* 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 "Deserializable.h"
#include "Profile.h"
void
ProfileResponse::deserialize(const QJsonDocument &data)
{
if (!data.isObject())
throw DeserializationException("Response is not a JSON object");
QJsonObject object = data.object();
if (object.contains("avatar_url"))
avatar_url_ = QUrl(object.value("avatar_url").toString());
if (object.contains("displayname"))
display_name_ = object.value("displayname").toString();
}

View File

@ -1,62 +0,0 @@
/*
* nheko Copyright (C) 2017 Jan Solanti <jhs@psonet.com>
*
* 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 <QJsonArray>
#include <QRegExp>
#include "Deserializable.h"
#include "Versions.h"
void
VersionsResponse::deserialize(const QJsonDocument &data)
{
if (!data.isObject())
throw DeserializationException("Versions response is not a JSON object");
QJsonObject object = data.object();
if (object.value("versions") == QJsonValue::Undefined)
throw DeserializationException("Versions: missing version list");
auto versions = object.value("versions").toArray();
for (auto const &elem : versions) {
QString str = elem.toString();
QRegExp rx("r(\\d+)\\.(\\d+)\\.(\\d+)");
if (rx.indexIn(str) == -1)
throw DeserializationException(
"Invalid version string in versions response");
struct Version_ v;
v.major_ = rx.cap(1).toUInt();
v.minor_ = rx.cap(2).toUInt();
v.patch_ = rx.cap(3).toUInt();
supported_versions_.push_back(v);
}
}
bool
VersionsResponse::isVersionSupported(unsigned int major, unsigned int minor, unsigned int patch)
{
for (auto &v : supported_versions_) {
if (v.major_ == major && v.minor_ == minor && v.patch_ >= patch)
return true;
}
return false;
}