From 853fda2e7942ffb3bc8051e6411faa23cff6f3c2 Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Thu, 3 May 2018 12:14:10 +0900 Subject: Cleanup --- lib/events/roommemberevent.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'lib/events') diff --git a/lib/events/roommemberevent.h b/lib/events/roommemberevent.h index 89b970c9..5f1e578d 100644 --- a/lib/events/roommemberevent.h +++ b/lib/events/roommemberevent.h @@ -56,12 +56,14 @@ namespace QMatrixClient using MembershipType = MemberEventContent::MembershipType; + explicit RoomMemberEvent(Type type, const QJsonObject& obj) + : StateEvent(type, obj) + { } RoomMemberEvent(MemberEventContent&& c) : StateEvent(Type::RoomMember, c) { } explicit RoomMemberEvent(const QJsonObject& obj) - : StateEvent(Type::RoomMember, obj) -// , _userId(obj["state_key"].toString()) + : RoomMemberEvent(Type::RoomMember, obj) { } MembershipType membership() const { return content().membership; } @@ -72,7 +74,6 @@ namespace QMatrixClient QUrl avatarUrl() const { return content().avatarUrl; } private: -// QString _userId; REGISTER_ENUM(MembershipType) }; } // namespace QMatrixClient -- cgit v1.2.3 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.cpp | 23 +++++++++----- lib/events/event.h | 86 +++++++++++++--------------------------------------- 2 files changed, 37 insertions(+), 72 deletions(-) (limited to 'lib/events') diff --git a/lib/events/event.cpp b/lib/events/event.cpp index 193250de..57049671 100644 --- a/lib/events/event.cpp +++ b/lib/events/event.cpp @@ -160,14 +160,13 @@ void RoomEvent::addId(const QString& id) template <> RoomEventPtr _impl::doMakeEvent(const QJsonObject& obj) { - return RoomEventPtr { makeIfMatches - (obj, obj["type"].toString()) }; -} + // Check more specific event types first + if (auto e = doMakeEvent(obj)) + return e; -StateEventBase::~StateEventBase() = default; + return makeIfMatches(obj, obj["type"].toString()); +} bool StateEventBase::repeatsState() const { @@ -176,3 +175,13 @@ bool StateEventBase::repeatsState() const .toObject().value("prev_content"); return contentJson == prevContentJson; } + +template<> +StateEventPtr _impl::doMakeEvent(const QJsonObject& obj) +{ + return makeIfMatches(obj, obj["type"].toString()); + +} 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 From 28a0d70164e2596d306521cd18d25c0e8c0b5336 Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Fri, 4 May 2018 12:13:43 +0900 Subject: EvT::TypeId: Use a member function instead of a variable The latter one causes linkage errors when used from a template method (but not from a template class, puzzlingly). --- lib/events/accountdataevents.h | 3 +-- lib/events/directchatevent.h | 2 +- lib/events/event.cpp | 2 +- lib/events/receiptevent.cpp | 2 +- lib/events/receiptevent.h | 2 +- lib/events/redactionevent.h | 2 +- lib/events/roomavatarevent.h | 2 +- lib/events/roommemberevent.h | 2 +- lib/events/roommessageevent.h | 2 +- lib/events/simplestateevents.h | 2 +- lib/events/typingevent.h | 2 +- 11 files changed, 11 insertions(+), 12 deletions(-) (limited to 'lib/events') diff --git a/lib/events/accountdataevents.h b/lib/events/accountdataevents.h index f3ba27bb..11667172 100644 --- a/lib/events/accountdataevents.h +++ b/lib/events/accountdataevents.h @@ -54,8 +54,7 @@ namespace QMatrixClient class _Name : public Event \ { \ public: \ - static constexpr const char* TypeId = _TypeId; \ - static const char* typeId() { return TypeId; } \ + static constexpr const char* typeId() { return _TypeId; } \ explicit _Name(const QJsonObject& obj) \ : Event((_EnumType), obj) \ , _content(contentJson(), QStringLiteral(#_ContentKey)) \ diff --git a/lib/events/directchatevent.h b/lib/events/directchatevent.h index 2b0ad0a0..bd8f2d35 100644 --- a/lib/events/directchatevent.h +++ b/lib/events/directchatevent.h @@ -29,6 +29,6 @@ namespace QMatrixClient QMultiHash usersToDirectChats() const; - static constexpr const char * TypeId = "m.direct"; + static constexpr const char* typeId() { return "m.direct"; } }; } diff --git a/lib/events/event.cpp b/lib/events/event.cpp index 57049671..c2b92a50 100644 --- a/lib/events/event.cpp +++ b/lib/events/event.cpp @@ -76,7 +76,7 @@ template inline event_ptr_tt makeIfMatches(const QJsonObject& o, const QString& selector) { - if (selector == EventT::TypeId) + if (selector == EventT::typeId()) return _impl::create(o); return makeIfMatches(o, selector); diff --git a/lib/events/receiptevent.cpp b/lib/events/receiptevent.cpp index 7555db82..a12f4c05 100644 --- a/lib/events/receiptevent.cpp +++ b/lib/events/receiptevent.cpp @@ -43,7 +43,7 @@ using namespace QMatrixClient; ReceiptEvent::ReceiptEvent(const QJsonObject& obj) : Event(Type::Receipt, obj) { - Q_ASSERT(obj["type"].toString() == TypeId); + Q_ASSERT(obj["type"].toString() == typeId()); const QJsonObject contents = contentJson(); _eventsWithReceipts.reserve(contents.size()); diff --git a/lib/events/receiptevent.h b/lib/events/receiptevent.h index 5b99ae3f..e1d2d1ec 100644 --- a/lib/events/receiptevent.h +++ b/lib/events/receiptevent.h @@ -42,7 +42,7 @@ namespace QMatrixClient EventsWithReceipts eventsWithReceipts() const { return _eventsWithReceipts; } - static constexpr const char* const TypeId = "m.receipt"; + static constexpr const char* typeId() { return "m.receipt"; } private: EventsWithReceipts _eventsWithReceipts; diff --git a/lib/events/redactionevent.h b/lib/events/redactionevent.h index 829b9085..dad54788 100644 --- a/lib/events/redactionevent.h +++ b/lib/events/redactionevent.h @@ -25,7 +25,7 @@ namespace QMatrixClient class RedactionEvent : public RoomEvent { public: - static constexpr const char* const TypeId = "m.room.redaction"; + static constexpr const char* typeId() { return "m.room.redaction"; } explicit RedactionEvent(const QJsonObject& obj) : RoomEvent(Type::Redaction, obj) diff --git a/lib/events/roomavatarevent.h b/lib/events/roomavatarevent.h index ccfe8fbf..0e44ad7c 100644 --- a/lib/events/roomavatarevent.h +++ b/lib/events/roomavatarevent.h @@ -37,7 +37,7 @@ namespace QMatrixClient : StateEvent(Type::RoomAvatar, obj) { } - static constexpr const char* TypeId = "m.room.avatar"; + static constexpr const char* typeId() { return "m.room.avatar"; } }; } // namespace QMatrixClient diff --git a/lib/events/roommemberevent.h b/lib/events/roommemberevent.h index 5f1e578d..8e0cc0a4 100644 --- a/lib/events/roommemberevent.h +++ b/lib/events/roommemberevent.h @@ -52,7 +52,7 @@ namespace QMatrixClient { Q_GADGET public: - static constexpr const char* TypeId = "m.room.member"; + static constexpr const char* typeId() { return "m.room.member"; } using MembershipType = MemberEventContent::MembershipType; diff --git a/lib/events/roommessageevent.h b/lib/events/roommessageevent.h index a55564ed..dc734b6e 100644 --- a/lib/events/roommessageevent.h +++ b/lib/events/roommessageevent.h @@ -65,7 +65,7 @@ namespace QMatrixClient QJsonObject toJson() const; - static constexpr const char* TypeId = "m.room.message"; + static constexpr const char* typeId() { return "m.room.message"; } private: QString _msgtype; diff --git a/lib/events/simplestateevents.h b/lib/events/simplestateevents.h index 6b0cd51a..d9f403e8 100644 --- a/lib/events/simplestateevents.h +++ b/lib/events/simplestateevents.h @@ -28,7 +28,7 @@ namespace QMatrixClient : public StateEvent> \ { \ public: \ - static constexpr const char* TypeId = _TypeId; \ + static constexpr const char* typeId() { return _TypeId; } \ explicit _Name(const QJsonObject& obj) \ : StateEvent(_EnumType, obj, QStringLiteral(#_ContentKey)) \ { } \ diff --git a/lib/events/typingevent.h b/lib/events/typingevent.h index 8c9551a4..6ccbc1c8 100644 --- a/lib/events/typingevent.h +++ b/lib/events/typingevent.h @@ -27,7 +27,7 @@ namespace QMatrixClient class TypingEvent: public Event { public: - static constexpr const char* const TypeId = "m.typing"; + static constexpr const char* typeId() { return "m.typing"; } TypingEvent(const QJsonObject& obj); -- cgit v1.2.3 From f70ec41accd6da9f223bc41b446ad1d2d77de3f4 Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Tue, 1 May 2018 21:08:06 +0900 Subject: Fix building on OSX --- lib/events/event.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib/events') diff --git a/lib/events/event.cpp b/lib/events/event.cpp index c2b92a50..576e9426 100644 --- a/lib/events/event.cpp +++ b/lib/events/event.cpp @@ -87,7 +87,7 @@ EventPtr _impl::doMakeEvent(const QJsonObject& obj) { // Check more specific event types first if (auto e = doMakeEvent(obj)) - return e; + return ptrCast(move(e)); return makeIfMatches( @@ -162,7 +162,7 @@ RoomEventPtr _impl::doMakeEvent(const QJsonObject& obj) { // Check more specific event types first if (auto e = doMakeEvent(obj)) - return e; + return ptrCast(move(e)); return makeIfMatches(obj, obj["type"].toString()); -- cgit v1.2.3