diff options
author | Alexey Rusakov <Kitsune-Ral@users.sf.net> | 2022-08-10 21:54:15 +0200 |
---|---|---|
committer | Alexey Rusakov <Kitsune-Ral@users.sf.net> | 2022-09-04 18:42:11 +0200 |
commit | 7251d6856993a08dd8ec1d4965a310e4cf8e97d3 (patch) | |
tree | d5647a3990f6f00c4d1ef459ac9c6f54e87f042a | |
parent | bde38f86337d6f49b34b38016ab088d2f48ec371 (diff) | |
download | libquotient-7251d6856993a08dd8ec1d4965a310e4cf8e97d3.tar.gz libquotient-7251d6856993a08dd8ec1d4965a310e4cf8e97d3.zip |
StateEventBase -> StateEvent
Now that StateEvent name is vacated, the naming for event core classes
can be completely unified: Event, RoomEvent, CallEvent, StateEvent.
-rw-r--r-- | lib/eventitem.h | 5 | ||||
-rw-r--r-- | lib/events/event.cpp | 2 | ||||
-rw-r--r-- | lib/events/event.h | 4 | ||||
-rw-r--r-- | lib/events/roomcreateevent.h | 4 | ||||
-rw-r--r-- | lib/events/roomtombstoneevent.h | 4 | ||||
-rw-r--r-- | lib/events/stateevent.cpp | 13 | ||||
-rw-r--r-- | lib/events/stateevent.h | 42 | ||||
-rw-r--r-- | lib/room.cpp | 15 | ||||
-rw-r--r-- | lib/room.h | 20 | ||||
-rw-r--r-- | lib/roomstateview.cpp | 10 | ||||
-rw-r--r-- | lib/roomstateview.h | 13 |
11 files changed, 67 insertions, 65 deletions
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<StateEventBase>() const +inline const StateEvent* EventItemBase::viewAs<StateEvent>() const { - return evt->isStateEvent() ? weakPtrCast<const StateEventBase>(evt) - : nullptr; + return evt->isStateEvent() ? weakPtrCast<const StateEvent>(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<StateEventBase>(); } +bool Event::isStateEvent() const { return is<StateEvent>(); } bool Event::isCallEvent() const { return is<CallEvent>(); } 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<QJsonObject>(PrevContentKeyL); - return fullJson().value(ContentKeyL) == prevContentJson; + return contentJson() == unsignedPart<QJsonObject>(PrevContentKeyL); } -QString StateEventBase::replacedState() const +QString StateEvent::replacedState() const { return unsignedPart<QString>("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<StateEventBase>; -using StateEvents = EventsArray<StateEventBase>; +using StateEventBase + [[deprecated("StateEventBase is StateEvent now")]] = StateEvent; +using StateEventPtr = event_ptr_tt<StateEvent>; +using StateEvents = EventsArray<StateEvent>; -[[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<QString, QString>; template <typename EventT, typename ContentT> -class EventTemplate<EventT, StateEventBase, ContentT> - : public StateEventBase { +class EventTemplate<EventT, StateEvent, ContentT> + : public StateEvent { public: using content_type = ContentT; @@ -82,14 +84,14 @@ public: }; explicit EventTemplate(const QJsonObject& fullJson) - : StateEventBase(fullJson) + : StateEvent(fullJson) , _content(fromJson<ContentT>(Event::contentJson())) , _prev(unsignedJson()) {} template <typename... ContentParamTs> explicit EventTemplate(const QString& stateKey, ContentParamTs&&... contentParams) - : StateEventBase(EventT::TypeId, stateKey) + : StateEvent(EventT::TypeId, stateKey) , _content { std::forward<ContentParamTs>(contentParams)... } { editJson().insert(ContentKey, toJson(_content)); @@ -113,11 +115,11 @@ private: template <typename EventT, typename ContentT> class KeyedStateEventBase - : public EventTemplate<EventT, StateEventBase, ContentT> { + : public EventTemplate<EventT, StateEvent, ContentT> { public: static constexpr auto needsStateKey = true; - using EventTemplate<EventT, StateEventBase, ContentT>::EventTemplate; + using EventTemplate<EventT, StateEvent, ContentT>::EventTemplate; }; template <typename EvT> @@ -125,9 +127,9 @@ concept Keyed_State_Event = EvT::needsStateKey; template <typename EventT, typename ContentT> class KeylessStateEventBase - : public EventTemplate<EventT, StateEventBase, ContentT> { + : public EventTemplate<EventT, StateEvent, ContentT> { private: - using base_type = EventTemplate<EventT, StateEventBase, ContentT>; + using base_type = EventTemplate<EventT, StateEvent, ContentT>; public: template <typename... ContentParamTs> @@ -145,5 +147,5 @@ template <typename EvT> 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<StateEventBase>(evtKey.first, - evtKey.second)); + stubbedState.emplace( + evtKey, loadEvent<StateEvent>(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<RoomEvent>(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<const StateEventBase*>(&e)); + std::exchange(curStateEvent, static_cast<const StateEvent*>(&e)); Q_ASSERT(!oldStateEvent || (oldStateEvent->matrixType() == e.matrixType() && oldStateEvent->stateKey() == e.stateKey())); @@ -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 <typename EvT, typename... ArgTs> 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<const StateEventBase*> -RoomStateView::eventsOfType(const QString& evtType) const +const QVector<const StateEvent*> RoomStateView::eventsOfType( + const QString& evtType) const { - auto vals = QVector<const StateEventBase*>(); + auto vals = QVector<const StateEvent*>(); 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 <typename FnT, class EvT = std::decay_t<fn_arg_t<FnT>>> concept Keyless_State_Fn = !EvT::needsStateKey; class QUOTIENT_API RoomStateView - : private QHash<StateEventKey, const StateEventBase*> { + : private QHash<StateEventKey, const StateEvent*> { Q_GADGET public: - const QHash<StateEventKey, const StateEventBase*>& events() const + const QHash<StateEventKey, const StateEvent*>& 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<EvT>(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<const StateEventBase*> - eventsOfType(const QString& evtType) const; + const QVector<const StateEvent*> eventsOfType(const QString& evtType) const; //! \brief Run a function on a state event with the given type and key //! |