From 1dce783c4ac9ca37343648114885d332bdfe7fa1 Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Wed, 27 Dec 2017 19:28:38 +0900 Subject: Introduce StateEventBase - a non-template base for StateEvent<> This will hold common logic for all state events, including the newly introduced repeatsState() that returns true when prev_content repeats content. This will be used to address QMatrixClient/Quaternion#245. --- events/event.cpp | 10 ++++++++++ events/event.h | 22 ++++++++++++++++++---- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/events/event.cpp b/events/event.cpp index e74bc114..c7345a13 100644 --- a/events/event.cpp +++ b/events/event.cpp @@ -144,3 +144,13 @@ RoomEventPtr _impl::doMakeEvent(const QJsonObject& obj) RoomAvatarEvent, EncryptionEvent, RedactionEvent> (obj, obj["type"].toString()) }; } + +StateEventBase::~StateEventBase() = default; + +bool StateEventBase::repeatsState() const +{ + auto contentJson = originalJsonObject().value("content"); + auto prevContentJson = originalJsonObject().value("unsigned") + .toObject().value("prev_content"); + return contentJson == prevContentJson; +} diff --git a/events/event.h b/events/event.h index 00dc4d5c..319c3443 100644 --- a/events/event.h +++ b/events/event.h @@ -240,8 +240,23 @@ namespace QMatrixClient RoomEvents::iterator end() { return to; } }; + class StateEventBase: public RoomEvent + { + 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 = 0; + + virtual bool repeatsState() const; + }; + template - class StateEvent: public RoomEvent + class StateEvent: public StateEventBase { public: using content_type = ContentT; @@ -249,8 +264,7 @@ namespace QMatrixClient template explicit StateEvent(Type type, const QJsonObject& obj, ContentParamTs&&... contentParams) - : RoomEvent(obj.contains("state_key") ? type : Type::Unknown, - obj) + : StateEventBase(type, obj) , _content(contentJson(), std::forward(contentParams)...) { @@ -262,7 +276,7 @@ namespace QMatrixClient } template explicit StateEvent(Type type, ContentParamTs&&... contentParams) - : RoomEvent(type) + : StateEventBase(type) , _content(std::forward(contentParams)...) { } -- cgit v1.2.3