aboutsummaryrefslogtreecommitdiff
path: root/lib/events/event.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'lib/events/event.cpp')
-rw-r--r--lib/events/event.cpp161
1 files changed, 21 insertions, 140 deletions
diff --git a/lib/events/event.cpp b/lib/events/event.cpp
index 576e9426..44bf79a1 100644
--- a/lib/events/event.cpp
+++ b/lib/events/event.cpp
@@ -18,170 +18,51 @@
#include "event.h"
-#include "roommessageevent.h"
-#include "simplestateevents.h"
-#include "roommemberevent.h"
-#include "roomavatarevent.h"
-#include "typingevent.h"
-#include "receiptevent.h"
-#include "accountdataevents.h"
-#include "directchatevent.h"
-#include "redactionevent.h"
#include "logging.h"
#include <QtCore/QJsonDocument>
using namespace QMatrixClient;
-Event::Event(Type type, const QJsonObject& rep)
- : _type(type), _originalJson(rep)
+event_type_t QMatrixClient::nextTypeId()
{
- if (!rep.contains("content") &&
- !rep.value("unsigned").toObject().contains("redacted_because"))
- {
- qCWarning(EVENTS) << "Event without 'content' node";
- qCWarning(EVENTS) << formatJson << rep;
- }
+ static event_type_t _id = EventTypeTraits<void>::id;
+ return ++_id;
}
-Event::~Event() = default;
-
-QString Event::jsonType() const
+Event::Event(Type type, const QJsonObject& json)
+ : _type(type), _json(json)
{
- return originalJsonObject().value("type").toString();
-}
-
-QByteArray Event::originalJson() const
-{
- return QJsonDocument(_originalJson).toJson();
-}
-
-QJsonObject Event::originalJsonObject() const
-{
- return _originalJson;
-}
-
-const QJsonObject Event::contentJson() const
-{
- return _originalJson["content"].toObject();
-}
-
-template <typename BaseEventT>
-inline event_ptr_tt<BaseEventT> makeIfMatches(const QJsonObject&, const QString&)
-{
- return nullptr;
-}
-
-template <typename BaseEventT, typename EventT, typename... EventTs>
-inline event_ptr_tt<BaseEventT> makeIfMatches(const QJsonObject& o,
- const QString& selector)
-{
- if (selector == EventT::typeId())
- return _impl::create<EventT>(o);
-
- return makeIfMatches<BaseEventT, EventTs...>(o, selector);
-}
-
-template <>
-EventPtr _impl::doMakeEvent<Event>(const QJsonObject& obj)
-{
- // Check more specific event types first
- if (auto e = doMakeEvent<RoomEvent>(obj))
- return ptrCast<Event>(move(e));
-
- return makeIfMatches<Event,
- TypingEvent, ReceiptEvent, TagEvent, ReadMarkerEvent, DirectChatEvent>(
- obj, obj["type"].toString());
-}
-
-RoomEvent::RoomEvent(Event::Type type) : Event(type) { }
-
-RoomEvent::RoomEvent(Type type, const QJsonObject& rep)
- : Event(type, rep)
- , _id(rep["event_id"].toString())
-{
-// if (_id.isEmpty())
-// {
-// qCWarning(EVENTS) << "Can't find event_id in a room event";
-// qCWarning(EVENTS) << formatJson << rep;
-// }
-// if (!rep.contains("origin_server_ts"))
-// {
-// qCWarning(EVENTS) << "Can't find server timestamp in a room event";
-// qCWarning(EVENTS) << formatJson << rep;
-// }
-// if (_senderId.isEmpty())
-// {
-// qCWarning(EVENTS) << "Can't find sender in a room event";
-// qCWarning(EVENTS) << formatJson << rep;
-// }
- auto unsignedData = rep["unsigned"].toObject();
- auto redaction = unsignedData.value("redacted_because");
- if (redaction.isObject())
+ if (!json.contains(ContentKeyL) &&
+ !json.value(UnsignedKeyL).toObject().contains(RedactedCauseKeyL))
{
- _redactedBecause = _impl::create<RedactionEvent>(redaction.toObject());
- return;
+ qCWarning(EVENTS) << "Event without 'content' node";
+ qCWarning(EVENTS) << formatJson << json;
}
-
- _txnId = unsignedData.value("transactionId").toString();
- if (!_txnId.isEmpty())
- qCDebug(EVENTS) << "Event transactionId:" << _txnId;
-}
-
-RoomEvent::~RoomEvent() = default; // Let the smart pointer do its job
-
-QDateTime RoomEvent::timestamp() const
-{
- return QMatrixClient::fromJson<QDateTime>(
- originalJsonObject().value("origin_server_ts"));
}
-QString RoomEvent::roomId() const
-{
- return originalJsonObject().value("room_id").toString();
-}
-
-QString RoomEvent::senderId() const
-{
- return originalJsonObject().value("sender").toString();
-}
+Event::Event(Type type, event_mtype_t matrixType, const QJsonObject& contentJson)
+ : Event(type, basicEventJson(matrixType, contentJson))
+{ }
-QString RoomEvent::redactionReason() const
-{
- return isRedacted() ? _redactedBecause->reason() : QString{};
-}
+Event::~Event() = default;
-void RoomEvent::addId(const QString& id)
+QString Event::matrixType() const
{
- Q_ASSERT(_id.isEmpty()); Q_ASSERT(!id.isEmpty());
- _id = id;
+ return fullJson()[TypeKeyL].toString();
}
-template <>
-RoomEventPtr _impl::doMakeEvent(const QJsonObject& obj)
+QByteArray Event::originalJson() const
{
- // Check more specific event types first
- if (auto e = doMakeEvent<StateEventBase>(obj))
- return ptrCast<RoomEvent>(move(e));
-
- return makeIfMatches<RoomEvent,
- RoomMessageEvent, RedactionEvent>(obj, obj["type"].toString());
+ return QJsonDocument(_json).toJson();
}
-bool StateEventBase::repeatsState() const
+const QJsonObject Event::contentJson() const
{
- auto contentJson = originalJsonObject().value("content");
- auto prevContentJson = originalJsonObject().value("unsigned")
- .toObject().value("prev_content");
- return contentJson == prevContentJson;
+ return fullJson()[ContentKeyL].toObject();
}
-template<>
-StateEventPtr _impl::doMakeEvent<StateEventBase>(const QJsonObject& obj)
+const QJsonObject Event::unsignedJson() const
{
- return makeIfMatches<StateEventBase,
- RoomNameEvent, RoomAliasesEvent,
- RoomCanonicalAliasEvent, RoomMemberEvent, RoomTopicEvent,
- RoomAvatarEvent, EncryptionEvent>(obj, obj["type"].toString());
-
+ return fullJson()[UnsignedKeyL].toObject();
}