aboutsummaryrefslogtreecommitdiff
path: root/lib/events
diff options
context:
space:
mode:
Diffstat (limited to 'lib/events')
-rw-r--r--lib/events/event.cpp5
-rw-r--r--lib/events/event.h12
-rw-r--r--lib/events/eventloader.h1
-rw-r--r--lib/events/simplestateevents.h14
-rw-r--r--lib/events/stateevent.cpp15
-rw-r--r--lib/events/stateevent.h20
6 files changed, 59 insertions, 8 deletions
diff --git a/lib/events/event.cpp b/lib/events/event.cpp
index fd6e3939..c98dfbb6 100644
--- a/lib/events/event.cpp
+++ b/lib/events/event.cpp
@@ -77,3 +77,8 @@ const QJsonObject Event::unsignedJson() const
{
return fullJson()[UnsignedKeyL].toObject();
}
+
+void Event::dumpTo(QDebug dbg) const
+{
+ dbg << QJsonDocument(contentJson()).toJson(QJsonDocument::Compact);
+}
diff --git a/lib/events/event.h b/lib/events/event.h
index 5b33628f..c51afcc4 100644
--- a/lib/events/event.h
+++ b/lib/events/event.h
@@ -209,7 +209,7 @@ namespace QMatrixClient
inline auto registerEventType()
{
static const auto _ = setupFactory<EventT>();
- return _;
+ return _; // Only to facilitate usage in static initialisation
}
// === Event ===
@@ -257,8 +257,18 @@ namespace QMatrixClient
return fromJson<T>(contentJson()[key]);
}
+ friend QDebug operator<<(QDebug dbg, const Event& e)
+ {
+ QDebugStateSaver _dss { dbg };
+ dbg.noquote().nospace()
+ << e.matrixType() << '(' << e.type() << "): ";
+ e.dumpTo(dbg);
+ return dbg;
+ }
+
virtual bool isStateEvent() const { return false; }
virtual bool isCallEvent() const { return false; }
+ virtual void dumpTo(QDebug dbg) const;
protected:
QJsonObject& editJson() { return _json; }
diff --git a/lib/events/eventloader.h b/lib/events/eventloader.h
index 3ee9a181..cd2f9149 100644
--- a/lib/events/eventloader.h
+++ b/lib/events/eventloader.h
@@ -19,7 +19,6 @@
#pragma once
#include "stateevent.h"
-#include "converters.h"
namespace QMatrixClient {
namespace _impl {
diff --git a/lib/events/simplestateevents.h b/lib/events/simplestateevents.h
index 56be947c..5aa24c15 100644
--- a/lib/events/simplestateevents.h
+++ b/lib/events/simplestateevents.h
@@ -59,21 +59,23 @@ namespace QMatrixClient
};
} // namespace EventContent
-#define DEFINE_SIMPLE_STATE_EVENT(_Name, _TypeId, _ContentType, _ContentKey) \
- class _Name : public StateEvent<EventContent::SimpleContent<_ContentType>> \
+#define DEFINE_SIMPLE_STATE_EVENT(_Name, _TypeId, _ValueType, _ContentKey) \
+ class _Name : public StateEvent<EventContent::SimpleContent<_ValueType>> \
{ \
public: \
- using content_type = _ContentType; \
+ using value_type = content_type::value_type; \
DEFINE_EVENT_TYPEID(_TypeId, _Name) \
- explicit _Name(const QJsonObject& obj) \
- : StateEvent(typeId(), obj, QStringLiteral(#_ContentKey)) \
- { } \
+ explicit _Name() : _Name(value_type()) { } \
template <typename T> \
explicit _Name(T&& value) \
: StateEvent(typeId(), matrixTypeId(), \
QStringLiteral(#_ContentKey), \
std::forward<T>(value)) \
{ } \
+ explicit _Name(QJsonObject obj) \
+ : StateEvent(typeId(), std::move(obj), \
+ QStringLiteral(#_ContentKey)) \
+ { } \
auto _ContentKey() const { return content().value; } \
}; \
REGISTER_EVENT_TYPE(_Name) \
diff --git a/lib/events/stateevent.cpp b/lib/events/stateevent.cpp
index fd5d2642..fd8079be 100644
--- a/lib/events/stateevent.cpp
+++ b/lib/events/stateevent.cpp
@@ -28,3 +28,18 @@ bool StateEventBase::repeatsState() const
const auto prevContentJson = unsignedJson().value(PrevContentKeyL);
return fullJson().value(ContentKeyL) == prevContentJson;
}
+
+QString StateEventBase::replacedState() const
+{
+ return unsignedJson().value("replaces_state"_ls).toString();
+}
+
+void StateEventBase::dumpTo(QDebug dbg) const
+{
+ if (!stateKey().isEmpty())
+ dbg << '<' << stateKey() << "> ";
+ if (unsignedJson().contains(PrevContentKeyL))
+ dbg << QJsonDocument(unsignedJson()[PrevContentKeyL].toObject())
+ .toJson(QJsonDocument::Compact) << " -> ";
+ RoomEvent::dumpTo(dbg);
+}
diff --git a/lib/events/stateevent.h b/lib/events/stateevent.h
index 6032132e..d4a7e8b3 100644
--- a/lib/events/stateevent.h
+++ b/lib/events/stateevent.h
@@ -30,11 +30,21 @@ namespace QMatrixClient {
~StateEventBase() override = default;
bool isStateEvent() const override { return true; }
+ QString replacedState() const;
+ void dumpTo(QDebug dbg) const override;
+
virtual bool repeatsState() const;
};
using StateEventPtr = event_ptr_tt<StateEventBase>;
using StateEvents = EventsArray<StateEventBase>;
+ /**
+ * A combination of event type and state key uniquely identifies a piece
+ * of state in Matrix.
+ * \sa https://matrix.org/docs/spec/client_server/unstable.html#types-of-room-events
+ */
+ using StateEventKey = std::pair<QString, QString>;
+
template <typename ContentT>
struct Prev
{
@@ -90,3 +100,13 @@ namespace QMatrixClient {
std::unique_ptr<Prev<ContentT>> _prev;
};
} // namespace QMatrixClient
+
+namespace std {
+ template <> struct hash<QMatrixClient::StateEventKey>
+ {
+ size_t operator()(const QMatrixClient::StateEventKey& k) const Q_DECL_NOEXCEPT
+ {
+ return qHash(k);
+ }
+ };
+}