diff options
author | Kitsune Ral <Kitsune-Ral@users.sf.net> | 2019-07-09 11:49:05 +0900 |
---|---|---|
committer | Kitsune Ral <Kitsune-Ral@users.sf.net> | 2019-07-09 11:49:05 +0900 |
commit | 31e28e2a99e6815da407d201e7287423a4956138 (patch) | |
tree | 049f3b156ad2cca3f328d163c9267ae90d984485 /lib/events | |
parent | b5dd30189df0d7515116b2abac1f93fc0f8a1989 (diff) | |
parent | 651478c1681ba6f93e22c20328a048dbbc263ffe (diff) | |
download | libquotient-31e28e2a99e6815da407d201e7287423a4956138.tar.gz libquotient-31e28e2a99e6815da407d201e7287423a4956138.zip |
Merge branch 'master' into use-clang-format
Diffstat (limited to 'lib/events')
-rw-r--r-- | lib/events/encryptionevent.cpp | 54 | ||||
-rw-r--r-- | lib/events/encryptionevent.h | 81 | ||||
-rw-r--r-- | lib/events/event.h | 6 | ||||
-rw-r--r-- | lib/events/eventcontent.h | 10 | ||||
-rw-r--r-- | lib/events/eventloader.h | 20 | ||||
-rw-r--r-- | lib/events/roomevent.cpp | 12 | ||||
-rw-r--r-- | lib/events/roomevent.h | 3 | ||||
-rw-r--r-- | lib/events/roommemberevent.h | 12 | ||||
-rw-r--r-- | lib/events/simplestateevents.h | 25 | ||||
-rw-r--r-- | lib/events/stateevent.cpp | 8 | ||||
-rw-r--r-- | lib/events/stateevent.h | 22 |
11 files changed, 233 insertions, 20 deletions
diff --git a/lib/events/encryptionevent.cpp b/lib/events/encryptionevent.cpp new file mode 100644 index 00000000..6aa7063b --- /dev/null +++ b/lib/events/encryptionevent.cpp @@ -0,0 +1,54 @@ +// +// Created by rusakov on 26/09/2017. +// Contributed by andreev on 27/06/2019. +// + +#include "encryptionevent.h" + +#include "converters.h" +#include "logging.h" + +#include <array> + +static const std::array<QString, 1> encryptionStrings = { { QStringLiteral( + "m.megolm.v1.aes-sha2") } }; + +namespace QMatrixClient +{ +template <> +struct JsonConverter<EncryptionType> +{ + static EncryptionType load(const QJsonValue& jv) + { + const auto& encryptionString = jv.toString(); + for (auto it = encryptionStrings.begin(); it != encryptionStrings.end(); + ++it) + if (encryptionString == *it) + return EncryptionType(it - encryptionStrings.begin()); + + qCWarning(EVENTS) << "Unknown EncryptionType: " << encryptionString; + return EncryptionType::Undefined; + } +}; +} // namespace QMatrixClient + +using namespace QMatrixClient; + +EncryptionEventContent::EncryptionEventContent(const QJsonObject& json) + : encryption(fromJson<EncryptionType>(json["algorithm"_ls])) + , algorithm(sanitized(json["algorithm"_ls].toString())) + , rotationPeriodMs(json["rotation_period_ms"_ls].toInt(604800000)) + , rotationPeriodMsgs(json["rotation_period_msgs"_ls].toInt(100)) +{} + +void EncryptionEventContent::fillJson(QJsonObject* o) const +{ + Q_ASSERT(o); + Q_ASSERT_X( + encryption != EncryptionType::Undefined, __FUNCTION__, + "The key 'algorithm' must be explicit in EncryptionEventContent"); + if (encryption != EncryptionType::Undefined) + o->insert(QStringLiteral("algorithm"), algorithm); + o->insert(QStringLiteral("rotation_period_ms"), rotationPeriodMs); + o->insert(QStringLiteral("rotation_period_msgs"), rotationPeriodMsgs); +} diff --git a/lib/events/encryptionevent.h b/lib/events/encryptionevent.h new file mode 100644 index 00000000..97119c8d --- /dev/null +++ b/lib/events/encryptionevent.h @@ -0,0 +1,81 @@ +/****************************************************************************** + * Copyright (C) 2017 Kitsune Ral <kitsune-ral@users.sf.net> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#pragma once + +#include "eventcontent.h" +#include "stateevent.h" + +namespace QMatrixClient +{ +class EncryptionEventContent : public EventContent::Base +{ +public: + enum EncryptionType : size_t + { + MegolmV1AesSha2 = 0, + Undefined + }; + + explicit EncryptionEventContent(EncryptionType et = Undefined) + : encryption(et) + {} + explicit EncryptionEventContent(const QJsonObject& json); + + EncryptionType encryption; + QString algorithm; + int rotationPeriodMs; + int rotationPeriodMsgs; + +protected: + void fillJson(QJsonObject* o) const override; +}; + +using EncryptionType = EncryptionEventContent::EncryptionType; + +class EncryptionEvent : public StateEvent<EncryptionEventContent> +{ + Q_GADGET +public: + DEFINE_EVENT_TYPEID("m.room.encryption", EncryptionEvent) + + using EncryptionType = EncryptionEventContent::EncryptionType; + + explicit EncryptionEvent(const QJsonObject& obj = {}) // TODO: apropriate + // default value + : StateEvent(typeId(), obj) + {} + template <typename... ArgTs> + EncryptionEvent(ArgTs&&... contentArgs) + : StateEvent(typeId(), matrixTypeId(), QString(), + std::forward<ArgTs>(contentArgs)...) + {} + + EncryptionType encryption() const { return content().encryption; } + + QString algorithm() const { return content().algorithm; } + int rotationPeriodMs() const { return content().rotationPeriodMs; } + int rotationPeriodMsgs() const { return content().rotationPeriodMsgs; } + +private: + REGISTER_ENUM(EncryptionType) +}; + +REGISTER_EVENT_TYPE(EncryptionEvent) +DEFINE_EVENTTYPE_ALIAS(Encryption, EncryptionEvent) +} // namespace QMatrixClient diff --git a/lib/events/event.h b/lib/events/event.h index 9dcec1ae..8056ccbe 100644 --- a/lib/events/event.h +++ b/lib/events/event.h @@ -61,14 +61,16 @@ static const auto TypeKey = QStringLiteral("type"); static const auto ContentKey = QStringLiteral("content"); static const auto EventIdKey = QStringLiteral("event_id"); static const auto UnsignedKey = QStringLiteral("unsigned"); +static const auto StateKeyKey = QStringLiteral("state_key"); static const auto TypeKeyL = "type"_ls; static const auto ContentKeyL = "content"_ls; static const auto EventIdKeyL = "event_id"_ls; static const auto UnsignedKeyL = "unsigned"_ls; static const auto RedactedCauseKeyL = "redacted_because"_ls; static const auto PrevContentKeyL = "prev_content"_ls; +static const auto StateKeyKeyL = "state_key"_ls; -// Minimal correct Matrix event JSON +/// Make a minimal correct Matrix event JSON template <typename StrT> inline QJsonObject basicEventJson(StrT matrixType, const QJsonObject& content) { @@ -257,7 +259,7 @@ public: } template <typename T> - T content(const QLatin1String& key) const + T content(QLatin1String key) const { return fromJson<T>(contentJson()[key]); } diff --git a/lib/events/eventcontent.h b/lib/events/eventcontent.h index d2b5e477..7a3db1fc 100644 --- a/lib/events/eventcontent.h +++ b/lib/events/eventcontent.h @@ -55,6 +55,9 @@ namespace EventContent QJsonObject originalJson; protected: + Base(const Base&) = default; + Base(Base&&) = default; + virtual void fillJson(QJsonObject* o) const = 0; }; @@ -172,13 +175,16 @@ namespace EventContent class TypedBase : public Base { public: - explicit TypedBase(const QJsonObject& o = {}) - : Base(o) + explicit TypedBase(QJsonObject o = {}) + : Base(std::move(o)) {} virtual QMimeType type() const = 0; virtual const FileInfo* fileInfo() const { return nullptr; } virtual FileInfo* fileInfo() { return nullptr; } virtual const Thumbnail* thumbnailInfo() const { return nullptr; } + + protected: + using Base::Base; }; /** diff --git a/lib/events/eventloader.h b/lib/events/eventloader.h index 9c797701..a203eaa3 100644 --- a/lib/events/eventloader.h +++ b/lib/events/eventloader.h @@ -34,7 +34,8 @@ namespace _impl } } // namespace _impl -/** Create an event with proper type from a JSON object +/*! Create an event with proper type from a JSON object + * * Use this factory template to detect the type from the JSON object * contents (the detected event type should derive from the template * parameter type) and create an event object of that type. @@ -45,7 +46,8 @@ inline event_ptr_tt<BaseEventT> loadEvent(const QJsonObject& fullJson) return _impl::loadEvent<BaseEventT>(fullJson, fullJson[TypeKeyL].toString()); } -/** Create an event from a type string and content JSON +/*! Create an event from a type string and content JSON + * * Use this factory template to resolve the C++ type from the Matrix * type string in \p matrixType and create an event of that type that has * its content part set to \p content. @@ -58,6 +60,20 @@ inline event_ptr_tt<BaseEventT> loadEvent(const QString& matrixType, matrixType); } +/*! Create a state event from a type string, content JSON and state key + * + * Use this factory to resolve the C++ type from the Matrix type string + * in \p matrixType and create a state event of that type with content part + * set to \p content and state key set to \p stateKey (empty by default). + */ +inline StateEventPtr loadStateEvent(const QString& matrixType, + const QJsonObject& content, + const QString& stateKey = {}) +{ + return _impl::loadEvent<StateEventBase>( + basicStateEventJson(matrixType, content, stateKey), matrixType); +} + template <typename EventT> struct JsonConverter<event_ptr_tt<EventT>> { diff --git a/lib/events/roomevent.cpp b/lib/events/roomevent.cpp index c28de559..513a99d0 100644 --- a/lib/events/roomevent.cpp +++ b/lib/events/roomevent.cpp @@ -74,7 +74,17 @@ QString RoomEvent::transactionId() const QString RoomEvent::stateKey() const { - return fullJson()["state_key"_ls].toString(); + return fullJson()[StateKeyKeyL].toString(); +} + +void RoomEvent::setRoomId(const QString& roomId) +{ + editJson().insert(QStringLiteral("room_id"), roomId); +} + +void RoomEvent::setSender(const QString& senderId) +{ + editJson().insert(QStringLiteral("sender"), senderId); } void RoomEvent::setTransactionId(const QString& txnId) diff --git a/lib/events/roomevent.h b/lib/events/roomevent.h index 42cd8fe4..dd0d25eb 100644 --- a/lib/events/roomevent.h +++ b/lib/events/roomevent.h @@ -60,6 +60,9 @@ public: QString transactionId() const; QString stateKey() const; + void setRoomId(const QString& roomId); + void setSender(const QString& senderId); + /** * Sets the transaction id for locally created events. This should be * done before the event is exposed to any code using the respective diff --git a/lib/events/roommemberevent.h b/lib/events/roommemberevent.h index a837b026..c1015df2 100644 --- a/lib/events/roommemberevent.h +++ b/lib/events/roommemberevent.h @@ -63,8 +63,14 @@ public: explicit RoomMemberEvent(const QJsonObject& obj) : StateEvent(typeId(), obj) {} - RoomMemberEvent(MemberEventContent&& c) - : StateEvent(typeId(), matrixTypeId(), c) + [[deprecated("Use RoomMemberEvent(userId, contentArgs) " + "instead")]] RoomMemberEvent(MemberEventContent&& c) + : StateEvent(typeId(), matrixTypeId(), QString(), c) + {} + template <typename... ArgTs> + RoomMemberEvent(const QString& userId, ArgTs&&... contentArgs) + : StateEvent(typeId(), matrixTypeId(), userId, + std::forward<ArgTs>(contentArgs)...) {} /// A special constructor to create unknown RoomMemberEvents @@ -82,7 +88,7 @@ public: {} MembershipType membership() const { return content().membership; } - QString userId() const { return fullJson()["state_key"_ls].toString(); } + QString userId() const { return fullJson()[StateKeyKeyL].toString(); } bool isDirect() const { return content().isDirect; } QString displayName() const { return content().displayName; } QUrl avatarUrl() const { return content().avatarUrl; } diff --git a/lib/events/simplestateevents.h b/lib/events/simplestateevents.h index 7ad2efa6..0078c44d 100644 --- a/lib/events/simplestateevents.h +++ b/lib/events/simplestateevents.h @@ -18,7 +18,6 @@ #pragma once -#include "converters.h" #include "stateevent.h" namespace QMatrixClient @@ -65,7 +64,7 @@ namespace EventContent {} \ template <typename T> \ explicit _Name(T&& value) \ - : StateEvent(typeId(), matrixTypeId(), \ + : StateEvent(typeId(), matrixTypeId(), QString(), \ QStringLiteral(#_ContentKey), std::forward<T>(value)) \ {} \ explicit _Name(QJsonObject obj) \ @@ -79,15 +78,27 @@ namespace EventContent DEFINE_SIMPLE_STATE_EVENT(RoomNameEvent, "m.room.name", QString, name) DEFINE_EVENTTYPE_ALIAS(RoomName, RoomNameEvent) -DEFINE_SIMPLE_STATE_EVENT(RoomAliasesEvent, "m.room.aliases", QStringList, - aliases) -DEFINE_EVENTTYPE_ALIAS(RoomAliases, RoomAliasesEvent) DEFINE_SIMPLE_STATE_EVENT(RoomCanonicalAliasEvent, "m.room.canonical_alias", QString, alias) DEFINE_EVENTTYPE_ALIAS(RoomCanonicalAlias, RoomCanonicalAliasEvent) DEFINE_SIMPLE_STATE_EVENT(RoomTopicEvent, "m.room.topic", QString, topic) DEFINE_EVENTTYPE_ALIAS(RoomTopic, RoomTopicEvent) -DEFINE_SIMPLE_STATE_EVENT(EncryptionEvent, "m.room.encryption", QString, - algorithm) DEFINE_EVENTTYPE_ALIAS(RoomEncryption, EncryptionEvent) + +class RoomAliasesEvent + : public StateEvent<EventContent::SimpleContent<QStringList>> +{ +public: + DEFINE_EVENT_TYPEID("m.room.aliases", RoomAliasesEvent) + explicit RoomAliasesEvent(const QJsonObject& obj) + : StateEvent(typeId(), obj, QStringLiteral("aliases")) + {} + RoomAliasesEvent(const QString& server, const QStringList& aliases) + : StateEvent(typeId(), matrixTypeId(), server, + QStringLiteral("aliases"), aliases) + {} + QString server() const { return stateKey(); } + QStringList aliases() const { return content().value; } +}; +REGISTER_EVENT_TYPE(RoomAliasesEvent) } // namespace QMatrixClient diff --git a/lib/events/stateevent.cpp b/lib/events/stateevent.cpp index 7fea59a1..bd228abd 100644 --- a/lib/events/stateevent.cpp +++ b/lib/events/stateevent.cpp @@ -26,7 +26,7 @@ using namespace QMatrixClient; [[gnu::unused]] static auto stateEventTypeInitialised = RoomEvent::factory_t::addMethod( [](const QJsonObject& json, const QString& matrixType) -> StateEventPtr { - if (!json.contains("state_key"_ls)) + if (!json.contains(StateKeyKeyL)) return nullptr; if (auto e = StateEventBase::factory_t::make(json, matrixType)) @@ -35,6 +35,12 @@ using namespace QMatrixClient; return makeEvent<StateEventBase>(unknownEventTypeId(), json); }); +StateEventBase::StateEventBase(Event::Type type, event_mtype_t matrixType, + const QString& stateKey, + const QJsonObject& contentJson) + : RoomEvent(type, basicStateEventJson(matrixType, contentJson, stateKey)) +{} + bool StateEventBase::repeatsState() const { const auto prevContentJson = unsignedJson().value(PrevContentKeyL); diff --git a/lib/events/stateevent.h b/lib/events/stateevent.h index 8a89c86c..d1b742ba 100644 --- a/lib/events/stateevent.h +++ b/lib/events/stateevent.h @@ -22,12 +22,29 @@ namespace QMatrixClient { + +/// Make a minimal correct Matrix state event JSON +template <typename StrT> +inline QJsonObject basicStateEventJson(StrT matrixType, + const QJsonObject& content, + const QString& stateKey = {}) +{ + return { { TypeKey, std::forward<StrT>(matrixType) }, + { StateKeyKey, stateKey }, + { ContentKey, content } }; +} + class StateEventBase : public RoomEvent { public: using factory_t = EventFactory<StateEventBase>; - using RoomEvent::RoomEvent; + StateEventBase(Type type, const QJsonObject& json) + : RoomEvent(type, json) + {} + StateEventBase(Type type, event_mtype_t matrixType, + const QString& stateKey = {}, + const QJsonObject& contentJson = {}); ~StateEventBase() override = default; bool isStateEvent() const override { return true; } @@ -87,8 +104,9 @@ public: } template <typename... ContentParamTs> explicit StateEvent(Type type, event_mtype_t matrixType, + const QString& stateKey, ContentParamTs&&... contentParams) - : StateEventBase(type, matrixType) + : StateEventBase(type, matrixType, stateKey) , _content(std::forward<ContentParamTs>(contentParams)...) { editJson().insert(ContentKey, _content.toJson()); |