diff options
Diffstat (limited to 'lib/events')
-rw-r--r-- | lib/events/encryptionevent.cpp | 48 | ||||
-rw-r--r-- | lib/events/encryptionevent.h | 47 | ||||
-rw-r--r-- | lib/events/simplestateevents.h | 2 |
3 files changed, 88 insertions, 9 deletions
diff --git a/lib/events/encryptionevent.cpp b/lib/events/encryptionevent.cpp index 7b620bce..b8e2b575 100644 --- a/lib/events/encryptionevent.cpp +++ b/lib/events/encryptionevent.cpp @@ -1,5 +1,53 @@ // // 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; + } + }; +} + +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 index b44e0eeb..6a4a1c67 100644 --- a/lib/events/encryptionevent.h +++ b/lib/events/encryptionevent.h @@ -18,25 +18,58 @@ #pragma once -#include "roomevent.h" +#include "stateevent.h" +#include "eventcontent.h" namespace QMatrixClient { - class EncryptionEvent : public RoomEvent + 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) - explicit EncryptionEvent(const QJsonObject& obj) - : RoomEvent(typeId(), obj) - , _algorithm(contentJson()["algorithm"].toString()) + using EncryptionType = EncryptionEventContent::EncryptionType; + + explicit EncryptionEvent(const QJsonObject& obj = {}) // TODO: apropriate default value + : StateEvent(typeId(), obj) { } + EncryptionEvent(EncryptionEventContent&& c) + : StateEvent(typeId(), matrixTypeId(), c) + { } + + EncryptionType encryption() const { return content().encryption; } - QString algorithm() const { return _algorithm; } + QString algorithm() const { return content().algorithm; } + int rotationPeriodMs() const { return content().rotationPeriodMs; } + int rotationPeriodMsgs() const { return content().rotationPeriodMsgs; } private: - QString _algorithm; + REGISTER_ENUM(EncryptionType) }; + REGISTER_EVENT_TYPE(EncryptionEvent) DEFINE_EVENTTYPE_ALIAS(Encryption, EncryptionEvent) } // namespace QMatrixClient diff --git a/lib/events/simplestateevents.h b/lib/events/simplestateevents.h index 2c23d9ca..81401532 100644 --- a/lib/events/simplestateevents.h +++ b/lib/events/simplestateevents.h @@ -86,7 +86,5 @@ namespace QMatrixClient 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) } // namespace QMatrixClient |