From 7251d6856993a08dd8ec1d4965a310e4cf8e97d3 Mon Sep 17 00:00:00 2001 From: Alexey Rusakov Date: Wed, 10 Aug 2022 21:54:15 +0200 Subject: StateEventBase -> StateEvent Now that StateEvent name is vacated, the naming for event core classes can be completely unified: Event, RoomEvent, CallEvent, StateEvent. --- lib/eventitem.h | 5 ++--- lib/events/event.cpp | 2 +- lib/events/event.h | 4 ++-- lib/events/roomcreateevent.h | 4 ++-- lib/events/roomtombstoneevent.h | 4 ++-- lib/events/stateevent.cpp | 13 ++++++------- lib/events/stateevent.h | 42 +++++++++++++++++++++-------------------- lib/room.cpp | 15 +++++++-------- lib/room.h | 20 ++++++++++++-------- lib/roomstateview.cpp | 10 +++++----- lib/roomstateview.h | 13 ++++++------- 11 files changed, 67 insertions(+), 65 deletions(-) (limited to 'lib') diff --git a/lib/eventitem.h b/lib/eventitem.h index 445f8265..96e45b38 100644 --- a/lib/eventitem.h +++ b/lib/eventitem.h @@ -95,10 +95,9 @@ private: }; template <> -inline const StateEventBase* EventItemBase::viewAs() const +inline const StateEvent* EventItemBase::viewAs() const { - return evt->isStateEvent() ? weakPtrCast(evt) - : nullptr; + return evt->isStateEvent() ? weakPtrCast(evt) : nullptr; } template <> diff --git a/lib/events/event.cpp b/lib/events/event.cpp index ca751081..da7de919 100644 --- a/lib/events/event.cpp +++ b/lib/events/event.cpp @@ -73,7 +73,7 @@ const QJsonObject Event::unsignedJson() const return fullJson()[UnsignedKeyL].toObject(); } -bool Event::isStateEvent() const { return is(); } +bool Event::isStateEvent() const { return is(); } bool Event::isCallEvent() const { return is(); } diff --git a/lib/events/event.h b/lib/events/event.h index d0b63085..c8ef5acb 100644 --- a/lib/events/event.h +++ b/lib/events/event.h @@ -125,7 +125,7 @@ inline bool operator==(const AbstractEventMetaType& lhs, //! //! TL;DR for the loadFrom() story: //! - for base event types, use QUO_BASE_EVENT and, if you have additional -//! validation (e.g., JSON has to contain a certain key - see StateEventBase +//! validation (e.g., JSON has to contain a certain key - see StateEvent //! for a real example), define it in the static EventT::isValid() member //! function accepting QJsonObject and returning bool. //! - for leaf (specific) event types - simply use QUO_EVENT and it will do @@ -153,7 +153,7 @@ public: //! any of its base event types) has a static isValid() predicate and //! the event JSON does not satisfy it, nullptr is immediately returned //! to the upper level or to the loadFrom() caller. This is how existence - //! of `state_key` is checked in any type derived from StateEventBase. + //! of `state_key` is checked in any type derived from StateEvent. //! 3. If step 1b above returned non-nullptr, immediately return it. //! 4. //! a. If EventT::isValid() or EventT::TypeId (either, or both) exist and diff --git a/lib/events/roomcreateevent.h b/lib/events/roomcreateevent.h index 2709258f..5968e187 100644 --- a/lib/events/roomcreateevent.h +++ b/lib/events/roomcreateevent.h @@ -7,11 +7,11 @@ #include "quotient_common.h" namespace Quotient { -class QUOTIENT_API RoomCreateEvent : public StateEventBase { +class QUOTIENT_API RoomCreateEvent : public StateEvent { public: QUO_EVENT(RoomCreateEvent, "m.room.create") - using StateEventBase::StateEventBase; + using StateEvent::StateEvent; struct Predecessor { QString roomId; diff --git a/lib/events/roomtombstoneevent.h b/lib/events/roomtombstoneevent.h index 95743e32..c85b4dfd 100644 --- a/lib/events/roomtombstoneevent.h +++ b/lib/events/roomtombstoneevent.h @@ -6,11 +6,11 @@ #include "stateevent.h" namespace Quotient { -class QUOTIENT_API RoomTombstoneEvent : public StateEventBase { +class QUOTIENT_API RoomTombstoneEvent : public StateEvent { public: QUO_EVENT(RoomTombstoneEvent, "m.room.tombstone") - using StateEventBase::StateEventBase; + using StateEvent::StateEvent; QString serverMessage() const; QString successorRoomId() const; diff --git a/lib/events/stateevent.cpp b/lib/events/stateevent.cpp index 204044bb..72ecd5ad 100644 --- a/lib/events/stateevent.cpp +++ b/lib/events/stateevent.cpp @@ -6,30 +6,29 @@ using namespace Quotient; -StateEventBase::StateEventBase(const QJsonObject& json) +StateEvent::StateEvent(const QJsonObject& json) : RoomEvent(json) { Q_ASSERT_X(json.contains(StateKeyKeyL), __FUNCTION__, "Attempt to create a state event without state key"); } -StateEventBase::StateEventBase(Event::Type type, const QString& stateKey, +StateEvent::StateEvent(Event::Type type, const QString& stateKey, const QJsonObject& contentJson) : RoomEvent(basicJson(type, stateKey, contentJson)) {} -bool StateEventBase::repeatsState() const +bool StateEvent::repeatsState() const { - const auto prevContentJson = unsignedPart(PrevContentKeyL); - return fullJson().value(ContentKeyL) == prevContentJson; + return contentJson() == unsignedPart(PrevContentKeyL); } -QString StateEventBase::replacedState() const +QString StateEvent::replacedState() const { return unsignedPart("replaces_state"_ls); } -void StateEventBase::dumpTo(QDebug dbg) const +void StateEvent::dumpTo(QDebug dbg) const { if (!stateKey().isEmpty()) dbg << '<' << stateKey() << "> "; diff --git a/lib/events/stateevent.h b/lib/events/stateevent.h index ffbce76e..992ec2e2 100644 --- a/lib/events/stateevent.h +++ b/lib/events/stateevent.h @@ -7,10 +7,10 @@ namespace Quotient { -class QUOTIENT_API StateEventBase : public RoomEvent { +class QUOTIENT_API StateEvent : public RoomEvent { public: - QUO_BASE_EVENT(StateEventBase, "json.contains('state_key')"_ls, - RoomEvent::BaseMetaType) + QUO_BASE_EVENT(StateEvent, "json.contains('state_key')"_ls, + RoomEvent::BaseMetaType) static bool isValid(const QJsonObject& fullJson) { return fullJson.contains(StateKeyKeyL); @@ -24,8 +24,8 @@ public: //! constructors and calls in, e.g., RoomStateView don't include it. static constexpr auto needsStateKey = false; - explicit StateEventBase(Type type, const QString& stateKey = {}, - const QJsonObject& contentJson = {}); + explicit StateEvent(Type type, const QString& stateKey = {}, + const QJsonObject& contentJson = {}); //! Make a minimal correct Matrix state event JSON static QJsonObject basicJson(const QString& matrixTypeId, @@ -41,18 +41,20 @@ public: virtual bool repeatsState() const; protected: - explicit StateEventBase(const QJsonObject& json); + explicit StateEvent(const QJsonObject& json); void dumpTo(QDebug dbg) const override; }; -using StateEventPtr = event_ptr_tt; -using StateEvents = EventsArray; +using StateEventBase + [[deprecated("StateEventBase is StateEvent now")]] = StateEvent; +using StateEventPtr = event_ptr_tt; +using StateEvents = EventsArray; -[[deprecated("Use StateEventBase::basicJson() instead")]] +[[deprecated("Use StateEvent::basicJson() instead")]] inline QJsonObject basicStateEventJson(const QString& matrixTypeId, const QJsonObject& content, const QString& stateKey = {}) { - return StateEventBase::basicJson(matrixTypeId, stateKey, content); + return StateEvent::basicJson(matrixTypeId, stateKey, content); } /** @@ -64,8 +66,8 @@ inline QJsonObject basicStateEventJson(const QString& matrixTypeId, using StateEventKey = std::pair; template -class EventTemplate - : public StateEventBase { +class EventTemplate + : public StateEvent { public: using content_type = ContentT; @@ -82,14 +84,14 @@ public: }; explicit EventTemplate(const QJsonObject& fullJson) - : StateEventBase(fullJson) + : StateEvent(fullJson) , _content(fromJson(Event::contentJson())) , _prev(unsignedJson()) {} template explicit EventTemplate(const QString& stateKey, ContentParamTs&&... contentParams) - : StateEventBase(EventT::TypeId, stateKey) + : StateEvent(EventT::TypeId, stateKey) , _content { std::forward(contentParams)... } { editJson().insert(ContentKey, toJson(_content)); @@ -113,11 +115,11 @@ private: template class KeyedStateEventBase - : public EventTemplate { + : public EventTemplate { public: static constexpr auto needsStateKey = true; - using EventTemplate::EventTemplate; + using EventTemplate::EventTemplate; }; template @@ -125,9 +127,9 @@ concept Keyed_State_Event = EvT::needsStateKey; template class KeylessStateEventBase - : public EventTemplate { + : public EventTemplate { private: - using base_type = EventTemplate; + using base_type = EventTemplate; public: template @@ -145,5 +147,5 @@ template concept Keyless_State_Event = !EvT::needsStateKey; } // namespace Quotient -Q_DECLARE_METATYPE(Quotient::StateEventBase*) -Q_DECLARE_METATYPE(const Quotient::StateEventBase*) +Q_DECLARE_METATYPE(Quotient::StateEvent*) +Q_DECLARE_METATYPE(const Quotient::StateEvent*) diff --git a/lib/room.cpp b/lib/room.cpp index 24939b55..6bed9b56 100644 --- a/lib/room.cpp +++ b/lib/room.cpp @@ -208,7 +208,7 @@ public: void getPreviousContent(int limit = 10, const QString &filter = {}); - const StateEventBase* getCurrentState(const StateEventKey& evtKey) const + const StateEvent* getCurrentState(const StateEventKey& evtKey) const { const auto* evt = currentState.value(evtKey, nullptr); if (!evt) { @@ -216,9 +216,8 @@ public: // In the absence of a real event, make a stub as-if an event // with empty content has been received. Event classes should be // prepared for empty/invalid/malicious content anyway. - stubbedState.emplace(evtKey, - loadEvent(evtKey.first, - evtKey.second)); + stubbedState.emplace( + evtKey, loadEvent(evtKey.first, evtKey.second)); qCDebug(STATE) << "A new stub event created for key {" << evtKey.first << evtKey.second << "}"; qCDebug(STATE) << "Stubbed state size:" << stubbedState.size(); @@ -1565,8 +1564,8 @@ bool Room::usesEncryption() const .isEmpty(); } -const StateEventBase* Room::getCurrentState(const QString& evtType, - const QString& stateKey) const +const StateEvent* Room::getCurrentState(const QString& evtType, + const QString& stateKey) const { return d->getCurrentState({ evtType, stateKey }); } @@ -2279,7 +2278,7 @@ QString Room::postJson(const QString& matrixType, return d->sendEvent(loadEvent(matrixType, eventContent)); } -SetRoomStateWithKeyJob* Room::setState(const StateEventBase& evt) +SetRoomStateWithKeyJob* Room::setState(const StateEvent& evt) { return setState(evt.matrixType(), evt.stateKey(), evt.contentJson()); } @@ -3095,7 +3094,7 @@ Room::Changes Room::processStateEvent(const RoomEvent& e) // Change the state const auto* const oldStateEvent = - std::exchange(curStateEvent, static_cast(&e)); + std::exchange(curStateEvent, static_cast(&e)); Q_ASSERT(!oldStateEvent || (oldStateEvent->matrixType() == e.matrixType() && oldStateEvent->stateKey() == e.stateKey())); diff --git a/lib/room.h b/lib/room.h index b454bfbc..5eb66b8b 100644 --- a/lib/room.h +++ b/lib/room.h @@ -757,9 +757,8 @@ public: */ [[deprecated("Use currentState().get() instead; " "make sure to check its result for nullptrs")]] // - const Quotient::StateEventBase* - getCurrentState(const QString& evtType, - const QString& stateKey = {}) const; + const StateEvent* getCurrentState(const QString& evtType, + const QString& stateKey = {}) const; /// Get a state event with the given event type and state key /*! This is a typesafe overload that accepts a C++ event type instead of @@ -782,16 +781,21 @@ public: RoomStateView currentState() const; //! Send a request to update the room state with the given event - SetRoomStateWithKeyJob* setState(const StateEventBase& evt); + SetRoomStateWithKeyJob* setState(const StateEvent& evt); //! \brief Set a state event of the given type with the given arguments //! //! This typesafe overload attempts to send a state event with the type //! \p EvT and the content defined by \p args. Specifically, the function - //! creates a temporary object of type \p EvT passing \p args to - //! the constructor, and sends a request to the homeserver using - //! the Matrix event type defined by \p EvT and the event content produced - //! via EvT::contentJson(). + //! constructs a temporary object of type \p EvT with its content + //! list-initialised from \p args, and sends a request to the homeserver + //! using the Matrix event type defined by \p EvT and the event content + //! produced via EvT::contentJson(). + //! + //! \note This call is not suitable for events that assume non-empty + //! stateKey, such as member events; for those you have to create + //! a temporary event object yourself and use the setState() overload + //! that accepts StateEvent const-ref. template auto setState(ArgTs&&... args) { diff --git a/lib/roomstateview.cpp b/lib/roomstateview.cpp index 94c88eee..be0f7c6c 100644 --- a/lib/roomstateview.cpp +++ b/lib/roomstateview.cpp @@ -5,8 +5,8 @@ using namespace Quotient; -const StateEventBase* RoomStateView::get(const QString& evtType, - const QString& stateKey) const +const StateEvent* RoomStateView::get(const QString& evtType, + const QString& stateKey) const { return value({ evtType, stateKey }); } @@ -23,10 +23,10 @@ QJsonObject RoomStateView::contentJson(const QString& evtType, return queryOr(evtType, stateKey, &Event::contentJson, QJsonObject()); } -const QVector -RoomStateView::eventsOfType(const QString& evtType) const +const QVector RoomStateView::eventsOfType( + const QString& evtType) const { - auto vals = QVector(); + auto vals = QVector(); for (auto it = cbegin(); it != cend(); ++it) if (it.key().first == evtType) vals.append(it.value()); diff --git a/lib/roomstateview.h b/lib/roomstateview.h index 119c24cf..c5261a1e 100644 --- a/lib/roomstateview.h +++ b/lib/roomstateview.h @@ -22,10 +22,10 @@ template >> concept Keyless_State_Fn = !EvT::needsStateKey; class QUOTIENT_API RoomStateView - : private QHash { + : private QHash { Q_GADGET public: - const QHash& events() const + const QHash& events() const { return *this; } @@ -40,8 +40,8 @@ public: //! have to check that it has_value() before using. Alternatively //! you can now use queryCurrentState() to access state safely. //! \sa getCurrentStateContentJson - const StateEventBase* get(const QString& evtType, - const QString& stateKey = {}) const; + const StateEvent* get(const QString& evtType, + const QString& stateKey = {}) const; //! \brief Get a state event with the given event type and state key //! @@ -94,7 +94,7 @@ public: auto content(const QString& stateKey, typename EvT::content_type defaultValue = {}) const { - // StateEvent<>::content is special in that it returns a const-ref, + // EventBase<>::content is special in that it returns a const-ref, // and lift() inside queryOr() can't wrap that in a temporary Omittable. if (const auto evt = get(stateKey)) return evt->content(); @@ -122,8 +122,7 @@ public: //! //! This method returns all known state events that have occured in //! the room of the given type. - const QVector - eventsOfType(const QString& evtType) const; + const QVector eventsOfType(const QString& evtType) const; //! \brief Run a function on a state event with the given type and key //! -- cgit v1.2.3