aboutsummaryrefslogtreecommitdiff
path: root/lib/events/event.h
diff options
context:
space:
mode:
Diffstat (limited to 'lib/events/event.h')
-rw-r--r--lib/events/event.h403
1 files changed, 196 insertions, 207 deletions
diff --git a/lib/events/event.h b/lib/events/event.h
index cbfa06ac..04384aa7 100644
--- a/lib/events/event.h
+++ b/lib/events/event.h
@@ -18,11 +18,14 @@
#pragma once
-#include "converters.h"
#include "util.h"
+#include <QtCore/QJsonObject>
+
namespace QMatrixClient
{
+ // === event_ptr_tt<> and type casting facilities ===
+
template <typename EventT>
using event_ptr_tt = std::unique_ptr<EventT>;
@@ -44,252 +47,238 @@ namespace QMatrixClient
return unique_ptr_cast<TargetT>(ptr);
}
- namespace _impl
+ // === Standard Matrix key names and basicEventJson() ===
+
+ static const auto TypeKey = QStringLiteral("type");
+ static const auto ContentKey = QStringLiteral("content");
+ static const auto EventIdKey = QStringLiteral("event_id");
+ 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;
+
+ // Minimal correct Matrix event JSON
+ template <typename StrT>
+ inline QJsonObject basicEventJson(StrT matrixType,
+ const QJsonObject& content)
+ {
+ return { { TypeKey, std::forward<StrT>(matrixType) },
+ { ContentKey, content } };
+ }
+
+ // === Event factory ===
+
+ using event_type_t = size_t;
+ using event_mtype_t = const char*;
+
+ template <typename EventT>
+ struct EventTypeTraits
+ {
+ static const event_type_t id;
+ };
+
+ template <>
+ struct EventTypeTraits<void>
+ {
+ static constexpr event_type_t id = 0;
+ };
+
+ event_type_t nextTypeId();
+
+ template <typename EventT>
+ const event_type_t EventTypeTraits<EventT>::id = nextTypeId();
+
+ template <typename EventT>
+ inline event_type_t typeId() { return EventTypeTraits<std::decay_t<EventT>>::id; }
+
+ inline event_type_t unknownEventTypeId() { return typeId<void>(); }
+
+ template <typename EventT, typename... ArgTs>
+ inline event_ptr_tt<EventT> makeEvent(ArgTs&&... args)
+ {
+ return std::make_unique<EventT>(std::forward<ArgTs>(args)...);
+ }
+
+ template <typename BaseEventT>
+ class EventFactory
{
- template <typename EventT, typename... ArgTs>
- inline event_ptr_tt<EventT> create(ArgTs&&... args)
- {
- return std::make_unique<EventT>(std::forward<ArgTs>(args)...);
- }
-
- template <typename EventT>
- inline event_ptr_tt<EventT> doMakeEvent(const QJsonObject& obj)
- {
- return create<EventT>(obj);
- }
+ public:
+ template <typename FnT>
+ static void addMethod(FnT&& method)
+ {
+ factories().emplace_back(std::forward<FnT>(method));
+ }
+
+ /** Chain two type factories
+ * Adds the factory class of EventT2 (EventT2::factory_t) to
+ * the list in factory class of EventT1 (EventT1::factory_t) so
+ * that when EventT1::factory_t::make() is invoked, types of
+ * EventT2 factory are looked through as well. This is used
+ * to include RoomEvent types into the more general Event factory,
+ * and state event types into the RoomEvent factory.
+ */
+ template <typename EventT>
+ static auto chainFactory()
+ {
+ addMethod(&EventT::factory_t::make);
+ return 0;
+ }
+
+ static event_ptr_tt<BaseEventT> make(const QJsonObject& json,
+ const QString& matrixType)
+ {
+ for (const auto& f: factories())
+ if (auto e = f(json, matrixType))
+ return e;
+ return makeEvent<BaseEventT>(unknownEventTypeId(), json);
+ }
+
+ private:
+ static auto& factories()
+ {
+ using inner_factory_tt =
+ std::function<event_ptr_tt<BaseEventT>(const QJsonObject&,
+ const QString&)>;
+ static std::vector<inner_factory_tt> _factories {};
+ return _factories;
+ }
+ };
+
+ /** Add a type to its default factory
+ * Adds a standard factory method (via makeEvent<>) for a given
+ * type to EventT::factory_t factory class so that it can be
+ * created dynamically from loadEvent<>().
+ *
+ * \tparam EventT the type to enable dynamic creation of
+ * \return the registered type id
+ * \sa loadEvent, Event::type
+ */
+ template <typename EventT>
+ inline void setupFactory()
+ {
+ EventT::factory_t::addMethod(
+ [] (const QJsonObject& json, const QString& jsonMatrixType)
+ {
+ return EventT::matrixTypeId() == jsonMatrixType
+ ? makeEvent<EventT>(json) : nullptr;
+ });
}
+ // === Event ===
+
class Event
{
Q_GADGET
+ Q_PROPERTY(Type type READ type CONSTANT)
+ Q_PROPERTY(QJsonObject contentJson READ contentJson CONSTANT)
public:
- enum class Type : quint16
- {
- Unknown = 0,
- Typing, Receipt, Tag, DirectChat, ReadMarker,
- RoomEventBase = 0x1000,
- RoomMessage = RoomEventBase + 1,
- RoomEncryptedMessage, Redaction,
- RoomStateEventBase = 0x1800,
- RoomName = RoomStateEventBase + 1,
- RoomAliases, RoomCanonicalAlias, RoomMember, RoomTopic,
- RoomAvatar, RoomEncryption, RoomCreate, RoomJoinRules,
- RoomPowerLevels,
- Reserved = 0x2000
- };
-
- explicit Event(Type type) : _type(type) { }
- Event(Type type, const QJsonObject& rep);
+ using Type = event_type_t;
+ using factory_t = EventFactory<Event>;
+
+ explicit Event(Type type, const QJsonObject& json);
+ explicit Event(Type type, event_mtype_t matrixType,
+ const QJsonObject& contentJson = {});
Event(const Event&) = delete;
+ Event(Event&&) = default;
+ Event& operator=(const Event&) = delete;
+ Event& operator=(Event&&) = delete;
virtual ~Event();
Type type() const { return _type; }
- QString jsonType() const;
- bool isStateEvent() const
- {
- return (quint16(_type) & 0x1800) == 0x1800;
- }
+ QString matrixType() const;
QByteArray originalJson() const;
- QJsonObject originalJsonObject() const;
+ QJsonObject originalJsonObject() const { return fullJson(); }
+
+ const QJsonObject& fullJson() const { return _json; }
// According to the CS API spec, every event also has
// a "content" object; but since its structure is different for
- // different types, we're implementing it per-event type
- // (and in most cases it will be a combination of other fields
- // instead of "content" field).
+ // different types, we're implementing it per-event type.
const QJsonObject contentJson() const;
+ const QJsonObject unsignedJson() const;
+
+ virtual bool isStateEvent() const { return false; }
- virtual QJsonObject toJson() const { Q_ASSERT(false); return {}; }
+ protected:
+ QJsonObject& editJson() { return _json; }
private:
Type _type;
- QJsonObject _originalJson;
-
- REGISTER_ENUM(Type)
- Q_PROPERTY(Type type READ type CONSTANT)
- Q_PROPERTY(QJsonObject contentJson READ contentJson CONSTANT)
+ QJsonObject _json;
};
- using EventType = Event::Type;
using EventPtr = event_ptr_tt<Event>;
- /** 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.
- */
- template <typename EventT>
- inline event_ptr_tt<EventT> makeEvent(const QJsonObject& obj)
- {
- auto e = _impl::doMakeEvent<EventT>(obj);
- if (!e)
- e = _impl::create<EventT>(EventType::Unknown, obj);
- return e;
- }
-
- namespace _impl
- {
- template <>
- EventPtr doMakeEvent<Event>(const QJsonObject& obj);
- }
-
- template <typename EventT> struct FromJson<event_ptr_tt<EventT>>
- {
- auto operator()(const QJsonValue& jv) const
- {
- return makeEvent<EventT>(jv.toObject());
- }
- };
-
template <typename EventT>
using EventsArray = std::vector<event_ptr_tt<EventT>>;
using Events = EventsArray<Event>;
- class RedactionEvent;
+ // === Macros used with event class definitions ===
+
+ // This macro should be used in a public section of an event class to
+ // provide matrixTypeId() and typeId().
+#define DEFINE_EVENT_TYPEID(_Id, _Type) \
+ static constexpr event_mtype_t matrixTypeId() { return _Id; } \
+ static auto typeId() { return QMatrixClient::typeId<_Type>(); } \
+ // End of macro
+
+ // This macro should be put after an event class definition (in .h or .cpp)
+ // to enable its deserialisation from a /sync and other
+ // polymorphic event arrays
+#define REGISTER_EVENT_TYPE(_Type) \
+ namespace { \
+ [[gnu::unused]] \
+ static const auto _factoryAdded##_Type = ( setupFactory<_Type>(), 0); \
+ } \
+ // End of macro
+
+ // This macro provides constants in EventType:: namespace for
+ // back-compatibility with libQMatrixClient 0.3 event type system.
+#define DEFINE_EVENTTYPE_ALIAS(_Id, _Type) \
+ namespace EventType \
+ { \
+ [[deprecated("Use typeId<>(), is<>() or visit<>()")]] \
+ static const auto _Id = typeId<_Type>(); \
+ } \
+ // End of macro
+
+ // === is<>() and visit<>() ===
- /** This class corresponds to m.room.* events */
- class RoomEvent : public Event
- {
- Q_GADGET
- Q_PROPERTY(QString id READ id)
- Q_PROPERTY(QDateTime timestamp READ timestamp CONSTANT)
- Q_PROPERTY(QString roomId READ roomId CONSTANT)
- Q_PROPERTY(QString senderId READ senderId CONSTANT)
- Q_PROPERTY(QString redactionReason READ redactionReason)
- Q_PROPERTY(bool isRedacted READ isRedacted)
- Q_PROPERTY(QString transactionId READ transactionId)
- public:
- // RedactionEvent is an incomplete type here so we cannot inline
- // constructors and destructors
- explicit RoomEvent(Type type);
- RoomEvent(Type type, const QJsonObject& rep);
- ~RoomEvent() override;
-
- QString id() const { return _id; }
- QDateTime timestamp() const;
- QString roomId() const;
- QString senderId() const;
- bool isRedacted() const { return bool(_redactedBecause); }
- const event_ptr_tt<RedactionEvent>& redactedBecause() const
- {
- return _redactedBecause;
- }
- QString redactionReason() const;
- const QString& transactionId() const { return _txnId; }
-
- /**
- * Sets the transaction id for locally created events. This should be
- * done before the event is exposed to any code using the respective
- * Q_PROPERTY.
- *
- * \param txnId - transaction id, normally obtained from
- * Connection::generateTxnId()
- */
- void setTransactionId(const QString& txnId) { _txnId = txnId; }
-
- /**
- * Sets event id for locally created events
- *
- * When a new event is created locally, it has no server id yet.
- * This function allows to add the id once the confirmation from
- * the server is received. There should be no id set previously
- * in the event. It's the responsibility of the code calling addId()
- * to notify clients that use Q_PROPERTY(id) about its change
- */
- void addId(const QString& id);
+ template <typename EventT>
+ inline bool is(const Event& e) { return e.type() == typeId<EventT>(); }
- private:
- QString _id;
- event_ptr_tt<RedactionEvent> _redactedBecause;
- QString _txnId;
- };
- using RoomEventPtr = event_ptr_tt<RoomEvent>;
- using RoomEvents = EventsArray<RoomEvent>;
- using RoomEventsRange = Range<RoomEvents>;
+ inline bool isUnknown(const Event& e) { return e.type() == unknownEventTypeId(); }
- namespace _impl
+ template <typename FnT>
+ inline fn_return_t<FnT> visit(const Event& event, FnT visitor)
{
- template <>
- RoomEventPtr doMakeEvent<RoomEvent>(const QJsonObject& obj);
+ using event_type = fn_arg_t<FnT>;
+ if (is<event_type>(event))
+ return visitor(static_cast<event_type>(event));
+ return fn_return_t<FnT>();
}
- class StateEventBase: public RoomEvent
+ template <typename FnT, typename... FnTs>
+ inline auto visit(const Event& event, FnT visitor1, FnTs&&... visitors)
{
- public:
- explicit StateEventBase(Type type, const QJsonObject& obj)
- : RoomEvent(obj.contains("state_key") ? type : Type::Unknown,
- obj)
- { }
- explicit StateEventBase(Type type)
- : RoomEvent(type)
- { }
- ~StateEventBase() override = default;
-
- virtual bool repeatsState() const;
- };
- using StateEventPtr = event_ptr_tt<StateEventBase>;
- using StateEvents = EventsArray<StateEventBase>;
+ using event_type1 = fn_arg_t<FnT>;
+ if (is<event_type1>(event))
+ return visitor1(static_cast<event_type1&>(event));
- namespace _impl
- {
- template <>
- StateEventPtr doMakeEvent<StateEventBase>(const QJsonObject& obj);
+ return visit(event, std::forward<FnTs>(visitors)...);
}
- template <typename ContentT>
- struct Prev
+ template <typename BaseEventT, typename... FnTs>
+ inline auto visit(const event_ptr_tt<BaseEventT>& eptr, FnTs&&... visitors)
{
- template <typename... ContentParamTs>
- explicit Prev(const QJsonObject& unsignedJson,
- ContentParamTs&&... contentParams)
- : senderId(unsignedJson.value("prev_sender").toString())
- , content(unsignedJson.value("prev_content").toObject(),
- std::forward<ContentParamTs>(contentParams)...)
- { }
-
- QString senderId;
- ContentT content;
- };
-
- template <typename ContentT>
- class StateEvent: public StateEventBase
- {
- public:
- using content_type = ContentT;
-
- template <typename... ContentParamTs>
- explicit StateEvent(Type type, const QJsonObject& obj,
- ContentParamTs&&... contentParams)
- : StateEventBase(type, obj)
- , _content(contentJson(),
- std::forward<ContentParamTs>(contentParams)...)
- {
- auto unsignedData = obj.value("unsigned").toObject();
- if (unsignedData.contains("prev_content"))
- _prev = std::make_unique<Prev<ContentT>>(unsignedData,
- std::forward<ContentParamTs>(contentParams)...);
- }
- template <typename... ContentParamTs>
- explicit StateEvent(Type type, ContentParamTs&&... contentParams)
- : StateEventBase(type)
- , _content(std::forward<ContentParamTs>(contentParams)...)
- { }
-
- QJsonObject toJson() const override { return _content.toJson(); }
-
- const ContentT& content() const { return _content; }
- /** @deprecated Use prevContent instead */
- const ContentT* prev_content() const { return prevContent(); }
- const ContentT* prevContent() const
- { return _prev ? &_prev->content : nullptr; }
- QString prevSenderId() const { return _prev ? _prev->senderId : ""; }
+ using return_type = decltype(visit(*eptr, visitors...));
+ if (eptr)
+ return visit(*eptr, visitors...);
+ return return_type();
+ }
- protected:
- ContentT _content;
- std::unique_ptr<Prev<ContentT>> _prev;
- };
} // namespace QMatrixClient
Q_DECLARE_METATYPE(QMatrixClient::Event*)
-Q_DECLARE_METATYPE(QMatrixClient::RoomEvent*)
Q_DECLARE_METATYPE(const QMatrixClient::Event*)
-Q_DECLARE_METATYPE(const QMatrixClient::RoomEvent*)