From 914bdecc0b8e57a3b4e75b218b02909172ada7e3 Mon Sep 17 00:00:00 2001
From: Konstantinos Sideris <sideris.konstantin@gmail.com>
Date: Sun, 3 Dec 2017 02:47:37 +0200
Subject: [PATCH] Initial integration with matrix-structs

---
 .gitmodules         |  3 +++
 CMakeLists.txt      | 10 +++++---
 include/Login.h     | 15 -----------
 include/Profile.h   | 36 --------------------------
 include/Versions.h  | 41 ------------------------------
 libs/matrix-structs |  1 +
 src/ChatPage.cc     |  4 +--
 src/Login.cc        | 22 ----------------
 src/MatrixClient.cc | 48 +++++++++++++----------------------
 src/Profile.cc      | 35 -------------------------
 src/Versions.cc     | 62 ---------------------------------------------
 11 files changed, 30 insertions(+), 247 deletions(-)
 delete mode 100644 include/Profile.h
 delete mode 100644 include/Versions.h
 create mode 160000 libs/matrix-structs
 delete mode 100644 src/Profile.cc
 delete mode 100644 src/Versions.cc

diff --git a/.gitmodules b/.gitmodules
index 86924ed..737a3e0 100644
--- a/.gitmodules
+++ b/.gitmodules
@@ -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
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 168bda5..0d518fe 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -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})
diff --git a/include/Login.h b/include/Login.h
index 2f74fab..1c8b789 100644
--- a/include/Login.h
+++ b/include/Login.h
@@ -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_;
-};
diff --git a/include/Profile.h b/include/Profile.h
deleted file mode 100644
index d640db0..0000000
--- a/include/Profile.h
+++ /dev/null
@@ -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_;
-};
diff --git a/include/Versions.h b/include/Versions.h
deleted file mode 100644
index a603e39..0000000
--- a/include/Versions.h
+++ /dev/null
@@ -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_;
-};
diff --git a/libs/matrix-structs b/libs/matrix-structs
new file mode 160000
index 0000000..190f297
--- /dev/null
+++ b/libs/matrix-structs
@@ -0,0 +1 @@
+Subproject commit 190f297478153930780a119268b908076599c8db
diff --git a/src/ChatPage.cc b/src/ChatPage.cc
index f773ff2..bbed735 100644
--- a/src/ChatPage.cc
+++ b/src/ChatPage.cc
@@ -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,
diff --git a/src/Login.cc b/src/Login.cc
index ca41d01..3e681a7 100644
--- a/src/Login.cc
+++ b/src/Login.cc
@@ -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();
-}
diff --git a/src/MatrixClient.cc b/src/MatrixClient.cc
index 3326a47..1887eb9 100644
--- a/src/MatrixClient.cc
+++ b/src/MatrixClient.cc
@@ -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();
                 }
         });
diff --git a/src/Profile.cc b/src/Profile.cc
deleted file mode 100644
index 8eaafe0..0000000
--- a/src/Profile.cc
+++ /dev/null
@@ -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();
-}
diff --git a/src/Versions.cc b/src/Versions.cc
deleted file mode 100644
index 9de1aaf..0000000
--- a/src/Versions.cc
+++ /dev/null
@@ -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;
-}