aboutsummaryrefslogtreecommitdiff
path: root/lib/events
diff options
context:
space:
mode:
Diffstat (limited to 'lib/events')
-rw-r--r--lib/events/encryptionevent.cpp53
-rw-r--r--lib/events/encryptionevent.h78
-rw-r--r--lib/events/event.h6
-rw-r--r--lib/events/eventcontent.h8
-rw-r--r--lib/events/eventloader.h20
-rw-r--r--lib/events/roomevent.cpp12
-rw-r--r--lib/events/roomevent.h3
-rw-r--r--lib/events/roommemberevent.h10
-rw-r--r--lib/events/simplestateevents.h26
-rw-r--r--lib/events/stateevent.cpp8
-rw-r--r--lib/events/stateevent.h21
11 files changed, 226 insertions, 19 deletions
diff --git a/lib/events/encryptionevent.cpp b/lib/events/encryptionevent.cpp
new file mode 100644
index 00000000..b8e2b575
--- /dev/null
+++ b/lib/events/encryptionevent.cpp
@@ -0,0 +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
new file mode 100644
index 00000000..b9e108f0
--- /dev/null
+++ b/lib/events/encryptionevent.h
@@ -0,0 +1,78 @@
+/******************************************************************************
+ * 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 "stateevent.h"
+#include "eventcontent.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 b7bbd83e..b3a58806 100644
--- a/lib/events/event.h
+++ b/lib/events/event.h
@@ -60,14 +60,16 @@ namespace QMatrixClient
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)
@@ -259,7 +261,7 @@ namespace QMatrixClient
}
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 ab31a75d..254eb9a9 100644
--- a/lib/events/eventcontent.h
+++ b/lib/events/eventcontent.h
@@ -53,6 +53,9 @@ namespace QMatrixClient
QJsonObject originalJson;
protected:
+ Base(const Base&) = default;
+ Base(Base&&) = default;
+
virtual void fillJson(QJsonObject* o) const = 0;
};
@@ -167,11 +170,14 @@ namespace QMatrixClient
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 da663392..d0fa60a2 100644
--- a/lib/events/eventloader.h
+++ b/lib/events/eventloader.h
@@ -32,7 +32,8 @@ namespace QMatrixClient {
}
}
- /** 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.
@@ -44,7 +45,8 @@ namespace QMatrixClient {
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.
@@ -57,6 +59,20 @@ namespace QMatrixClient {
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>>
{
static auto load(const QJsonValue& jv)
diff --git a/lib/events/roomevent.cpp b/lib/events/roomevent.cpp
index 3d03509f..f1e563ff 100644
--- a/lib/events/roomevent.cpp
+++ b/lib/events/roomevent.cpp
@@ -78,7 +78,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 ce96174e..e26a7135 100644
--- a/lib/events/roomevent.h
+++ b/lib/events/roomevent.h
@@ -60,6 +60,9 @@ namespace QMatrixClient
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 b8224033..39aa280c 100644
--- a/lib/events/roommemberevent.h
+++ b/lib/events/roommemberevent.h
@@ -56,8 +56,14 @@ namespace QMatrixClient
explicit RoomMemberEvent(const QJsonObject& obj)
: StateEvent(typeId(), obj)
{ }
+ [[deprecated("Use RoomMemberEvent(userId, contentArgs) instead")]]
RoomMemberEvent(MemberEventContent&& c)
- : StateEvent(typeId(), matrixTypeId(), 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
@@ -76,7 +82,7 @@ namespace QMatrixClient
MembershipType membership() const { return content().membership; }
QString userId() const
- { return fullJson()["state_key"_ls].toString(); }
+ { 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 2c23d9ca..ef56c7b2 100644
--- a/lib/events/simplestateevents.h
+++ b/lib/events/simplestateevents.h
@@ -20,8 +20,6 @@
#include "stateevent.h"
-#include "converters.h"
-
namespace QMatrixClient
{
namespace EventContent
@@ -63,7 +61,7 @@ namespace QMatrixClient
explicit _Name() : _Name(value_type()) { } \
template <typename T> \
explicit _Name(T&& value) \
- : StateEvent(typeId(), matrixTypeId(), \
+ : StateEvent(typeId(), matrixTypeId(), QString(), \
QStringLiteral(#_ContentKey), \
std::forward<T>(value)) \
{ } \
@@ -78,15 +76,27 @@ namespace QMatrixClient
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 a84f302b..6a6e7782 100644
--- a/lib/events/stateevent.cpp
+++ b/lib/events/stateevent.cpp
@@ -27,7 +27,7 @@ using namespace QMatrixClient;
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))
@@ -36,6 +36,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 3f54f7bf..3b56a265 100644
--- a/lib/events/stateevent.h
+++ b/lib/events/stateevent.h
@@ -21,12 +21,28 @@
#include "roomevent.h"
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; }
@@ -83,8 +99,9 @@ namespace QMatrixClient {
}
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());