diff options
author | Konstantinos Sideris <sideris.konstantin@gmail.com> | 2017-12-04 18:41:19 +0200 |
---|---|---|
committer | Konstantinos Sideris <sideris.konstantin@gmail.com> | 2017-12-04 18:41:19 +0200 |
commit | a605e4486f4b9d90d668d6d1844ba5f0d58bbc26 (patch) | |
tree | c82001904cb120d975361edb38a62b5b77fa0644 /include | |
parent | 1976a3280cc392df722ebc6cb6dd74f0746ec017 (diff) |
Migrate to matrix-structs for event and response parsing
Diffstat (limited to 'include')
35 files changed, 189 insertions, 1722 deletions
diff --git a/include/ChatPage.h b/include/ChatPage.h index 01f6c5d7..94c54f0b 100644 --- a/include/ChatPage.h +++ b/include/ChatPage.h @@ -24,9 +24,7 @@ #include <QTimer> #include <QWidget> -#include "MemberEventContent.h" -#include "MessageEvent.h" -#include "StateEvent.h" +#include <mtx.hpp> class Cache; class MatrixClient; @@ -37,14 +35,11 @@ class RoomSettings; class RoomState; class SideBarActions; class Splitter; -class SyncResponse; class TextInputWidget; class TimelineViewManager; class TopRoomBar; class TypingDisplay; class UserInfoWidget; -class JoinedRoom; -class LeftRoom; constexpr int CONSENSUS_TIMEOUT = 1000; constexpr int SHOW_CONTENT_TIMEOUT = 3000; @@ -76,8 +71,8 @@ private slots: void updateTopBarAvatar(const QString &roomid, const QPixmap &img); void updateOwnProfileInfo(const QUrl &avatar_url, const QString &display_name); void setOwnAvatar(const QPixmap &img); - void initialSyncCompleted(const SyncResponse &response); - void syncCompleted(const SyncResponse &response); + void initialSyncCompleted(const mtx::responses::Sync &response); + void syncCompleted(const mtx::responses::Sync &response); void syncFailed(const QString &msg); void changeTopRoomInfo(const QString &room_id); void logout(); @@ -87,26 +82,34 @@ private slots: private: using UserID = QString; using RoomStates = QMap<UserID, RoomState>; - using JoinedRooms = QMap<UserID, JoinedRoom>; - using LeftRooms = QMap<UserID, LeftRoom>; - using Membership = matrix::events::StateEvent<matrix::events::MemberEventContent>; - using Memberships = QMap<UserID, Membership>; + using Membership = mtx::events::StateEvent<mtx::events::state::Member>; + using Memberships = std::map<std::string, Membership>; + + using JoinedRooms = std::map<std::string, mtx::responses::JoinedRoom>; + using LeftRooms = std::map<std::string, mtx::responses::LeftRoom>; void removeLeftRooms(const LeftRooms &rooms); void updateJoinedRooms(const JoinedRooms &rooms); - Memberships getMemberships(const QJsonArray &events) const; RoomStates generateMembershipDifference(const JoinedRooms &rooms, const RoomStates &states) const; - void updateTypingUsers(const QString &roomid, const QList<QString> &user_ids); - void updateUserMetadata(const QJsonArray &events); - void updateUserDisplayName(const Membership &event); - void updateUserAvatarUrl(const Membership &event); + void updateTypingUsers(const QString &roomid, const std::vector<std::string> &user_ids); + + using MemberEvent = mtx::events::StateEvent<mtx::events::state::Member>; + void updateUserDisplayName(const MemberEvent &event); + void updateUserAvatarUrl(const MemberEvent &event); + void loadStateFromCache(); void deleteConfigs(); void resetUI(); + template<class Collection> + Memberships getMemberships(const std::vector<Collection> &events) const; + + template<class Collection> + void updateUserMetadata(const std::vector<Collection> &collection); + QHBoxLayout *topLayout_; Splitter *splitter; @@ -153,3 +156,37 @@ private: // return to the login page. int initialSyncFailures = 0; }; + +template<class Collection> +void +ChatPage::updateUserMetadata(const std::vector<Collection> &collection) +{ + using Member = mtx::events::StateEvent<mtx::events::state::Member>; + + for (auto &event : collection) { + if (mpark::holds_alternative<Member>(event)) { + auto member = mpark::get<Member>(event); + + updateUserAvatarUrl(member); + updateUserDisplayName(member); + } + } +} + +template<class Collection> +std::map<std::string, mtx::events::StateEvent<mtx::events::state::Member>> +ChatPage::getMemberships(const std::vector<Collection> &collection) const +{ + std::map<std::string, mtx::events::StateEvent<mtx::events::state::Member>> memberships; + + using Member = mtx::events::StateEvent<mtx::events::state::Member>; + + for (auto &event : collection) { + if (mpark::holds_alternative<Member>(event)) { + auto member = mpark::get<Member>(event); + memberships.emplace(member.state_key, member); + } + } + + return memberships; +} diff --git a/include/MatrixClient.h b/include/MatrixClient.h index 722a8611..397ba11d 100644 --- a/include/MatrixClient.h +++ b/include/MatrixClient.h @@ -20,12 +20,7 @@ #include <QFileInfo> #include <QNetworkAccessManager> #include <QUrl> - -#include "MessageEvent.h" - -class SyncResponse; -class Profile; -class RoomMessages; +#include <mtx.hpp> /* * MatrixClient provides the high level API to communicate with @@ -40,7 +35,7 @@ public: // Client API. void initialSync() noexcept; void sync() noexcept; - void sendRoomMessage(matrix::events::MessageEventType ty, + void sendRoomMessage(mtx::events::MessageType ty, int txnId, const QString &roomid, const QString &msg, @@ -107,15 +102,15 @@ signals: // Returned profile data for the user's account. void getOwnProfileResponse(const QUrl &avatar_url, const QString &display_name); - void initialSyncCompleted(const SyncResponse &response); + void initialSyncCompleted(const mtx::responses::Sync &response); void initialSyncFailed(const QString &msg); - void syncCompleted(const SyncResponse &response); + void syncCompleted(const mtx::responses::Sync &response); void syncFailed(const QString &msg); void joinFailed(const QString &msg); void messageSent(const QString &event_id, const QString &roomid, const int txn_id); void messageSendFailed(const QString &roomid, const int txn_id); void emoteSent(const QString &event_id, const QString &roomid, const int txn_id); - void messagesRetrieved(const QString &room_id, const RoomMessages &msgs); + void messagesRetrieved(const QString &room_id, const mtx::responses::Messages &msgs); void joinedRoom(const QString &room_id); void leftRoom(const QString &room_id); diff --git a/include/RoomState.h b/include/RoomState.h index db1cdc68..0e91410c 100644 --- a/include/RoomState.h +++ b/include/RoomState.h @@ -21,28 +21,14 @@ #include <QPixmap> #include <QUrl> -#include "AliasesEventContent.h" -#include "AvatarEventContent.h" -#include "CanonicalAliasEventContent.h" -#include "CreateEventContent.h" -#include "HistoryVisibilityEventContent.h" -#include "JoinRulesEventContent.h" -#include "MemberEventContent.h" -#include "NameEventContent.h" -#include "PowerLevelsEventContent.h" -#include "TopicEventContent.h" - -#include "Event.h" -#include "RoomEvent.h" -#include "StateEvent.h" - -namespace events = matrix::events; +#include <mtx.hpp> class RoomState { public: RoomState(); - RoomState(const QJsonArray &events); + RoomState(const mtx::responses::Timeline &timeline); + RoomState(const mtx::responses::State &state); // Calculate room data that are not immediatly accessible. Like room name and // avatar. @@ -50,32 +36,37 @@ public: // e.g If the room is 1-on-1 name and avatar should be extracted from a user. void resolveName(); void resolveAvatar(); - void parse(const QJsonObject &object); + void parse(const nlohmann::json &object); QUrl getAvatar() const { return avatar_; }; QString getName() const { return name_; }; - QString getTopic() const { return topic.content().topic().simplified(); }; + QString getTopic() const + { + return QString::fromStdString(topic.content.topic).simplified(); + }; void removeLeaveMemberships(); void update(const RoomState &state); - void updateFromEvents(const QJsonArray &events); - QJsonObject serialize() const; + template<class Collection> + void updateFromEvents(const std::vector<Collection> &collection); + + std::string serialize() const; // The latest state events. - events::StateEvent<events::AliasesEventContent> aliases; - events::StateEvent<events::AvatarEventContent> avatar; - events::StateEvent<events::CanonicalAliasEventContent> canonical_alias; - events::StateEvent<events::CreateEventContent> create; - events::StateEvent<events::HistoryVisibilityEventContent> history_visibility; - events::StateEvent<events::JoinRulesEventContent> join_rules; - events::StateEvent<events::NameEventContent> name; - events::StateEvent<events::PowerLevelsEventContent> power_levels; - events::StateEvent<events::TopicEventContent> topic; + mtx::events::StateEvent<mtx::events::state::Aliases> aliases; + mtx::events::StateEvent<mtx::events::state::Avatar> avatar; + mtx::events::StateEvent<mtx::events::state::CanonicalAlias> canonical_alias; + mtx::events::StateEvent<mtx::events::state::Create> create; + mtx::events::StateEvent<mtx::events::state::HistoryVisibility> history_visibility; + mtx::events::StateEvent<mtx::events::state::JoinRules> join_rules; + mtx::events::StateEvent<mtx::events::state::Name> name; + mtx::events::StateEvent<mtx::events::state::PowerLevels> power_levels; + mtx::events::StateEvent<mtx::events::state::Topic> topic; // Contains the m.room.member events for all the joined users. - using UserID = QString; - QMap<UserID, events::StateEvent<events::MemberEventContent>> memberships; + using UserID = std::string; + std::map<UserID, mtx::events::StateEvent<mtx::events::state::Member>> memberships; private: QUrl avatar_; @@ -85,3 +76,44 @@ private: // avatar event this should be empty. QString userAvatar_; }; + +template<class Collection> +void +RoomState::updateFromEvents(const std::vector<Collection> &collection) +{ + using Aliases = mtx::events::StateEvent<mtx::events::state::Aliases>; + using Avatar = mtx::events::StateEvent<mtx::events::state::Avatar>; + using CanonicalAlias = mtx::events::StateEvent<mtx::events::state::CanonicalAlias>; + using Create = mtx::events::StateEvent<mtx::events::state::Create>; + using HistoryVisibility = mtx::events::StateEvent<mtx::events::state::HistoryVisibility>; + using JoinRules = mtx::events::StateEvent<mtx::events::state::JoinRules>; + using Member = mtx::events::StateEvent<mtx::events::state::Member>; + using Name = mtx::events::StateEvent<mtx::events::state::Name>; + using PowerLevels = mtx::events::StateEvent<mtx::events::state::PowerLevels>; + using Topic = mtx::events::StateEvent<mtx::events::state::Topic>; + + for (const auto &event : collection) { + if (mpark::holds_alternative<Aliases>(event)) { + this->aliases = mpark::get<Aliases>(event); + } else if (mpark::holds_alternative<Avatar>(event)) { + this->avatar = mpark::get<Avatar>(event); + } else if (mpark::holds_alternative<CanonicalAlias>(event)) { + this->canonical_alias = mpark::get<CanonicalAlias>(event); + } else if (mpark::holds_alternative<Create>(event)) { + this->create = mpark::get<Create>(event); + } else if (mpark::holds_alternative<HistoryVisibility>(event)) { + this->history_visibility = mpark::get<HistoryVisibility>(event); + } else if (mpark::holds_alternative<JoinRules>(event)) { + this->join_rules = mpark::get<JoinRules>(event); + } else if (mpark::holds_alternative<Name>(event)) { + this->name = mpark::get<Name>(event); + } else if (mpark::holds_alternative<Member>(event)) { + auto membership = mpark::get<Member>(event); + this->memberships.emplace(membership.state_key, membership); + } else if (mpark::holds_alternative<PowerLevels>(event)) { + this->power_levels = mpark::get<PowerLevels>(event); + } else if (mpark::holds_alternative<Topic>(event)) { + this->topic = mpark::get<Topic>(event); + } + } +} diff --git a/include/Sync.h b/include/Sync.h deleted file mode 100644 index d59a57dc..00000000 --- a/include/Sync.h +++ /dev/null @@ -1,131 +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 <QJsonArray> -#include <QMap> - -#include "Deserializable.h" - -class Event : public Deserializable -{ -public: - QJsonObject content() const { return content_; }; - QJsonObject unsigned_content() const { return unsigned_; }; - - QString sender() const { return sender_; }; - QString state_key() const { return state_key_; }; - QString type() const { return type_; }; - QString eventId() const { return event_id_; }; - - uint64_t timestamp() const { return origin_server_ts_; }; - - void deserialize(const QJsonValue &data) override; - -private: - QJsonObject content_; - QJsonObject unsigned_; - - QString sender_; - QString state_key_; - QString type_; - QString event_id_; - - uint64_t origin_server_ts_; -}; - -class State : public Deserializable -{ -public: - void deserialize(const QJsonValue &data) override; - QJsonArray events() const { return events_; }; - -private: - QJsonArray events_; -}; - -class Timeline : public Deserializable -{ -public: - QJsonArray events() const { return events_; }; - QString previousBatch() const { return prev_batch_; }; - bool limited() const { return limited_; }; - - void deserialize(const QJsonValue &data) override; - -private: - QJsonArray events_; - QString prev_batch_; - bool limited_; -}; - -// TODO: Add support for account_data, undread_notifications -class JoinedRoom : public Deserializable -{ -public: - State state() const { return state_; }; - Timeline timeline() const { return timeline_; }; - QList<QString> typingUserIDs() const { return typingUserIDs_; }; - - void deserialize(const QJsonValue &data) override; - -private: - State state_; - Timeline timeline_; - QList<QString> typingUserIDs_; - /* AccountData account_data_; */ - /* UnreadNotifications unread_notifications_; */ -}; - -class LeftRoom : public Deserializable -{ -public: - State state() const { return state_; }; - Timeline timeline() const { return timeline_; }; - - void deserialize(const QJsonValue &data) override; - -private: - State state_; - Timeline timeline_; -}; - -// TODO: Add support for invited and left rooms. -class Rooms : public Deserializable -{ -public: - QMap<QString, JoinedRoom> join() const { return join_; }; - QMap<QString, LeftRoom> leave() const { return leave_; }; - void deserialize(const QJsonValue &data) override; - -private: - QMap<QString, JoinedRoom> join_; - QMap<QString, LeftRoom> leave_; -}; - -class SyncResponse : public Deserializable -{ -public: - void deserialize(const QJsonDocument &data) override; - QString nextBatch() const { return next_batch_; }; - Rooms rooms() const { return rooms_; }; - -private: - QString next_batch_; - Rooms rooms_; -}; diff --git a/include/TextInputWidget.h b/include/TextInputWidget.h index b208d3f4..df309e27 100644 --- a/include/TextInputWidget.h +++ b/include/TextInputWidget.h @@ -25,13 +25,10 @@ #include <QWidget> #include "FlatButton.h" -#include "Image.h" #include "LoadingIndicator.h" #include "emoji/PickButton.h" -namespace msgs = matrix::events::messages; - class FilteredTextEdit : public QTextEdit { Q_OBJECT diff --git a/include/events/AliasesEventContent.h b/include/events/AliasesEventContent.h deleted file mode 100644 index 7784fad7..00000000 --- a/include/events/AliasesEventContent.h +++ /dev/null @@ -1,42 +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 <QJsonValue> -#include <QList> - -#include "Deserializable.h" - -namespace matrix { -namespace events { -class AliasesEventContent - : public Deserializable - , public Serializable -{ -public: - void deserialize(const QJsonValue &data) override; - QJsonObject serialize() const override; - - QList<QString> aliases() const { return aliases_; }; - -private: - QList<QString> aliases_; -}; - -} // namespace events -} // namespace matrix diff --git a/include/events/AvatarEventContent.h b/include/events/AvatarEventContent.h deleted file mode 100644 index 55284aa4..00000000 --- a/include/events/AvatarEventContent.h +++ /dev/null @@ -1,46 +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 <QJsonValue> -#include <QUrl> - -#include "Deserializable.h" - -namespace matrix { -namespace events { -/* - * A picture that is associated with the room. - */ - -class AvatarEventContent - : public Deserializable - , public Serializable -{ -public: - void deserialize(const QJsonValue &data) override; - QJsonObject serialize() const override; - - QUrl url() const { return url_; }; - -private: - QUrl url_; -}; - -} // namespace events -} // namespace matrix diff --git a/include/events/CanonicalAliasEventContent.h b/include/events/CanonicalAliasEventContent.h deleted file mode 100644 index 6322c001..00000000 --- a/include/events/CanonicalAliasEventContent.h +++ /dev/null @@ -1,48 +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 <QJsonValue> - -#include "CanonicalAliasEventContent.h" -#include "Deserializable.h" - -namespace matrix { -namespace events { -/* - * This event is used to inform the room about which alias should be considered - * the canonical one. This could be for display purposes or as suggestion to - * users which alias to use to advertise the room. - */ - -class CanonicalAliasEventContent - : public Deserializable - , public Serializable -{ -public: - void deserialize(const QJsonValue &data) override; - QJsonObject serialize() const override; - - QString alias() const { return alias_; }; - -private: - QString alias_; -}; - -} // namespace events -} // namespace matrix diff --git a/include/events/CreateEventContent.h b/include/events/CreateEventContent.h deleted file mode 100644 index 0a47860e..00000000 --- a/include/events/CreateEventContent.h +++ /dev/null @@ -1,47 +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 <QJsonValue> - -#include "Deserializable.h" - -namespace matrix { -namespace events { -/* - * This is the first event in a room and cannot be changed. It acts as the root - * of all other events. - */ - -class CreateEventContent - : public Deserializable - , public Serializable -{ -public: - void deserialize(const QJsonValue &data) override; - QJsonObject serialize() const override; - - QString creator() const { return creator_; }; - -private: - // The user_id of the room creator. This is set by the homeserver. - QString creator_; -}; - -} // namespace events -} // namespace matrix diff --git a/include/events/Event.h b/include/events/Event.h deleted file mode 100644 index f6620a2c..00000000 --- a/include/events/Event.h +++ /dev/null @@ -1,183 +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 - |