From 564d518c086f2aeab0f0466b7cd1915e20edc7da Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Thu, 3 May 2018 21:23:28 +0900 Subject: GetRoomEventsJob (replaces RoomMessagesJob) + refactoring 1. Updates in this commit (see further) allow to generate and build GetRoomEventsJob from message_pagination.yaml; this job completely preempts RoomMessagesJob. 2. EventsBatch<> is no more a thing; there's EventsArray<> to replace it but it's loaded from a JSON array rather than an event batch (a JSON array inside another JSON object). SyncJob that used it extensively has been moved to "conventional" containers (Events, RoomEvents and the newly introduced StateEvents). RoomMessagesJob that also used EventsBatch<> is decommissioned (see above). 3. RoomEventsRange is now an alias for Range, defined in util.h (otherwise almost the same). 4. Connection::getMessages() is no more. Use Room::getPreviousContent() and Connection::callApi() instead. 5. Moving things around in Room, since SyncJob now supplies state events in more specific StateEvents, rather than RoomEvents. --- lib/events/event.h | 86 +++++++++++++----------------------------------------- 1 file changed, 21 insertions(+), 65 deletions(-) (limited to 'lib/events/event.h') diff --git a/lib/events/event.h b/lib/events/event.h index 396406f1..8449c2ec 100644 --- a/lib/events/event.h +++ b/lib/events/event.h @@ -53,7 +53,10 @@ namespace QMatrixClient } template - event_ptr_tt doMakeEvent(const QJsonObject& obj); + inline event_ptr_tt doMakeEvent(const QJsonObject& obj) + { + return create(obj); + } } class Event @@ -114,7 +117,7 @@ namespace QMatrixClient * parameter type) and create an event object of that type. */ template - event_ptr_tt makeEvent(const QJsonObject& obj) + inline event_ptr_tt makeEvent(const QJsonObject& obj) { auto e = _impl::doMakeEvent(obj); if (!e) @@ -128,50 +131,17 @@ namespace QMatrixClient EventPtr doMakeEvent(const QJsonObject& obj); } - template <> struct FromJson + template struct FromJson> { - EventPtr operator()(const QJsonValue& jv) const + auto operator()(const QJsonValue& jv) const { - return makeEvent(jv.toObject()); + return makeEvent(jv.toObject()); } }; - /** - * \brief A vector of pointers to events with deserialisation capabilities - * - * This is a simple wrapper over a generic vector type that adds - * a convenience method to deserialise events from QJsonArray. - * \tparam EventT base type of all events in the vector - */ template - class EventsBatch : public std::vector> - { - public: - /** - * \brief Deserialise events from an array - * - * Given the following JSON construct, creates events from - * the array stored at key "node": - * \code - * "container": { - * "node": [ { "event_id": "!evt1:srv.org", ... }, ... ] - * } - * \endcode - * \param container - the wrapping JSON object - * \param node - the key in container that holds the array of events - */ - void fromJson(const QJsonObject& container, const QString& node) - { - const auto objs = container.value(node).toArray(); - using size_type = typename std::vector>::size_type; - // The below line accommodates the difference in size types of - // STL and Qt containers. - this->reserve(static_cast(objs.size())); - for (const auto& objValue: objs) - this->emplace_back(makeEvent(objValue.toObject())); - } - }; - using Events = EventsBatch; + using EventsArray = std::vector>; + using Events = EventsArray; class RedactionEvent; @@ -231,8 +201,9 @@ namespace QMatrixClient event_ptr_tt _redactedBecause; QString _txnId; }; - using RoomEvents = EventsBatch; using RoomEventPtr = event_ptr_tt; + using RoomEvents = EventsArray; + using RoomEventsRange = Range; namespace _impl { @@ -240,29 +211,6 @@ namespace QMatrixClient RoomEventPtr doMakeEvent(const QJsonObject& obj); } - /** - * Conceptually similar to QStringView (but much more primitive), it's a - * simple abstraction over a pair of RoomEvents::const_iterator values - * referring to the beginning and the end of a range in a RoomEvents - * container. - */ - struct RoomEventsRange - { - RoomEvents::iterator from; - RoomEvents::iterator to; - - RoomEvents::size_type size() const - { - Q_ASSERT(std::distance(from, to) >= 0); - return RoomEvents::size_type(std::distance(from, to)); - } - bool empty() const { return from == to; } - RoomEvents::const_iterator begin() const { return from; } - RoomEvents::const_iterator end() const { return to; } - RoomEvents::iterator begin() { return from; } - RoomEvents::iterator end() { return to; } - }; - class StateEventBase: public RoomEvent { public: @@ -273,10 +221,18 @@ namespace QMatrixClient explicit StateEventBase(Type type) : RoomEvent(type) { } - ~StateEventBase() override = 0; + ~StateEventBase() override = default; virtual bool repeatsState() const; }; + using StateEventPtr = event_ptr_tt; + using StateEvents = EventsArray; + + namespace _impl + { + template <> + StateEventPtr doMakeEvent(const QJsonObject& obj); + } template struct Prev -- cgit v1.2.3