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/room.h | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'lib/room.h') diff --git a/lib/room.h b/lib/room.h index 288db5fb..7b4b3578 100644 --- a/lib/room.h +++ b/lib/room.h @@ -408,13 +408,14 @@ namespace QMatrixClient void fileTransferCancelled(QString id); protected: - virtual void processStateEvents(const RoomEvents& events); + /// Returns true if any of room names/aliases has changed + virtual bool processStateEvent(const RoomEvent& e); virtual void processEphemeralEvent(EventPtr&& event); virtual void processAccountDataEvent(EventPtr&& event); - virtual void onAddNewTimelineEvents(timeline_iter_t from) { } - virtual void onAddHistoricalTimelineEvents(rev_iter_t from) { } - virtual void onRedaction(const RoomEvent& prevEvent, - const RoomEvent& after) { } + virtual void onAddNewTimelineEvents(timeline_iter_t /*from*/) { } + virtual void onAddHistoricalTimelineEvents(rev_iter_t /*from*/) { } + virtual void onRedaction(const RoomEvent& /*prevEvent*/, + const RoomEvent& /*after*/) { } private: class Private; -- cgit v1.2.3 From 4cbe1a5fe9b0ae17e89425c3127db2af9b328320 Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Fri, 4 May 2018 12:12:07 +0900 Subject: Preempt job/setroomstatejob.* with jobs/generated/room_state.* The template part (not exposed in the auto-generated class) goes to Room::Private::requestSetState(). Also, Room::setMemberState() to interface with User class. --- CMakeLists.txt | 1 - lib/jobs/generated/room_state.cpp | 70 +++++++++++++++++++++++++++++++++++++++ lib/jobs/generated/room_state.h | 47 ++++++++++++++++++++++++++ lib/jobs/setroomstatejob.cpp | 32 ------------------ lib/jobs/setroomstatejob.h | 64 ----------------------------------- lib/room.cpp | 29 ++++++++++++---- lib/room.h | 4 +++ lib/user.cpp | 5 ++- libqmatrixclient.pri | 2 -- 9 files changed, 146 insertions(+), 108 deletions(-) create mode 100644 lib/jobs/generated/room_state.cpp create mode 100644 lib/jobs/generated/room_state.h delete mode 100644 lib/jobs/setroomstatejob.cpp delete mode 100644 lib/jobs/setroomstatejob.h (limited to 'lib/room.h') diff --git a/CMakeLists.txt b/CMakeLists.txt index 5da87063..96bca983 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -83,7 +83,6 @@ set(libqmatrixclient_SRCS lib/jobs/basejob.cpp lib/jobs/checkauthmethods.cpp lib/jobs/sendeventjob.cpp - lib/jobs/setroomstatejob.cpp lib/jobs/syncjob.cpp lib/jobs/mediathumbnailjob.cpp lib/jobs/downloadfilejob.cpp diff --git a/lib/jobs/generated/room_state.cpp b/lib/jobs/generated/room_state.cpp new file mode 100644 index 00000000..39f36afb --- /dev/null +++ b/lib/jobs/generated/room_state.cpp @@ -0,0 +1,70 @@ +/****************************************************************************** + * THIS FILE IS GENERATED - ANY EDITS WILL BE OVERWRITTEN + */ + +#include "room_state.h" + +#include "converters.h" + +#include + +using namespace QMatrixClient; + +static const auto basePath = QStringLiteral("/_matrix/client/r0"); + +class SetRoomStateWithKeyJob::Private +{ + public: + QString eventId; +}; + +SetRoomStateWithKeyJob::SetRoomStateWithKeyJob(const QString& roomId, const QString& eventType, const QString& stateKey, const QJsonObject& body) + : BaseJob(HttpVerb::Put, "SetRoomStateWithKeyJob", + basePath % "/rooms/" % roomId % "/state/" % eventType % "/" % stateKey) + , d(new Private) +{ + setRequestData(Data(toJson(body))); +} + +SetRoomStateWithKeyJob::~SetRoomStateWithKeyJob() = default; + +const QString& SetRoomStateWithKeyJob::eventId() const +{ + return d->eventId; +} + +BaseJob::Status SetRoomStateWithKeyJob::parseJson(const QJsonDocument& data) +{ + auto json = data.object(); + d->eventId = fromJson(json.value("event_id")); + return Success; +} + +class SetRoomStateJob::Private +{ + public: + QString eventId; +}; + +SetRoomStateJob::SetRoomStateJob(const QString& roomId, const QString& eventType, const QJsonObject& body) + : BaseJob(HttpVerb::Put, "SetRoomStateJob", + basePath % "/rooms/" % roomId % "/state/" % eventType) + , d(new Private) +{ + setRequestData(Data(toJson(body))); +} + +SetRoomStateJob::~SetRoomStateJob() = default; + +const QString& SetRoomStateJob::eventId() const +{ + return d->eventId; +} + +BaseJob::Status SetRoomStateJob::parseJson(const QJsonDocument& data) +{ + auto json = data.object(); + d->eventId = fromJson(json.value("event_id")); + return Success; +} + diff --git a/lib/jobs/generated/room_state.h b/lib/jobs/generated/room_state.h new file mode 100644 index 00000000..5d16b2a6 --- /dev/null +++ b/lib/jobs/generated/room_state.h @@ -0,0 +1,47 @@ +/****************************************************************************** + * THIS FILE IS GENERATED - ANY EDITS WILL BE OVERWRITTEN + */ + +#pragma once + +#include "../basejob.h" + +#include + + +namespace QMatrixClient +{ + // Operations + + class SetRoomStateWithKeyJob : public BaseJob + { + public: + explicit SetRoomStateWithKeyJob(const QString& roomId, const QString& eventType, const QString& stateKey, const QJsonObject& body = {}); + ~SetRoomStateWithKeyJob() override; + + const QString& eventId() const; + + protected: + Status parseJson(const QJsonDocument& data) override; + + private: + class Private; + QScopedPointer d; + }; + + class SetRoomStateJob : public BaseJob + { + public: + explicit SetRoomStateJob(const QString& roomId, const QString& eventType, const QJsonObject& body = {}); + ~SetRoomStateJob() override; + + const QString& eventId() const; + + protected: + Status parseJson(const QJsonDocument& data) override; + + private: + class Private; + QScopedPointer d; + }; +} // namespace QMatrixClient diff --git a/lib/jobs/setroomstatejob.cpp b/lib/jobs/setroomstatejob.cpp deleted file mode 100644 index c2beb87b..00000000 --- a/lib/jobs/setroomstatejob.cpp +++ /dev/null @@ -1,32 +0,0 @@ -/****************************************************************************** - * Copyright (C) 2015 Felix Rohrbach - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - */ - -#include "setroomstatejob.h" - -using namespace QMatrixClient; - -BaseJob::Status SetRoomStateJob::parseJson(const QJsonDocument& data) -{ - _eventId = data.object().value("event_id").toString(); - if (!_eventId.isEmpty()) - return Success; - - qCDebug(JOBS) << data; - return { UserDefinedError, "No event_id in the JSON response" }; -} - diff --git a/lib/jobs/setroomstatejob.h b/lib/jobs/setroomstatejob.h deleted file mode 100644 index 36047667..00000000 --- a/lib/jobs/setroomstatejob.h +++ /dev/null @@ -1,64 +0,0 @@ -/****************************************************************************** - * Copyright (C) 2015 Felix Rohrbach - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - */ - -#pragma once - -#include "basejob.h" - -#include "connectiondata.h" - -namespace QMatrixClient -{ - class SetRoomStateJob: public BaseJob - { - public: - /** - * Constructs a job that sets a state using an arbitrary room event - * with a state key. - */ - template - SetRoomStateJob(const QString& roomId, const QString& stateKey, - const EvT& event) - : BaseJob(HttpVerb::Put, "SetRoomStateJob", - QStringLiteral("_matrix/client/r0/rooms/%1/state/%2/%3") - .arg(roomId, EvT::typeId(), stateKey), - Query(), - Data(event.toJson())) - { } - /** - * Constructs a job that sets a state using an arbitrary room event - * without a state key. - */ - template - SetRoomStateJob(const QString& roomId, const EvT& event) - : BaseJob(HttpVerb::Put, "SetRoomStateJob", - QStringLiteral("_matrix/client/r0/rooms/%1/state/%2") - .arg(roomId, EvT::typeId()), - Query(), - Data(event.toJson())) - { } - - QString eventId() const { return _eventId; } - - protected: - Status parseJson(const QJsonDocument& data) override; - - private: - QString _eventId; - }; -} // namespace QMatrixClient diff --git a/lib/room.cpp b/lib/room.cpp index f8f195e1..2ec5a591 100644 --- a/lib/room.cpp +++ b/lib/room.cpp @@ -26,7 +26,7 @@ #include "jobs/generated/redaction.h" #include "jobs/generated/account-data.h" #include "jobs/generated/message_pagination.h" -#include "jobs/setroomstatejob.h" +#include "jobs/generated/room_state.h" #include "events/simplestateevents.h" #include "events/roomavatarevent.h" #include "events/roommemberevent.h" @@ -199,6 +199,20 @@ class Room::Private void markMessagesAsRead(rev_iter_t upToMarker); + template + auto requestSetState(const QString& stateKey, const EvT& event) + { + return connection->callApi( + id, EvT::typeId(), stateKey, event.toJson()); + } + + template + auto requestSetState(const EvT& event) + { + return connection->callApi( + id, EvT::typeId(), event.toJson()); + } + /** * @brief Apply redaction to the timeline * @@ -1110,19 +1124,17 @@ void Room::postMessage(const RoomMessageEvent& event) void Room::setName(const QString& newName) { - connection()->callApi(id(), RoomNameEvent(newName)); + d->requestSetState(RoomNameEvent(newName)); } void Room::setCanonicalAlias(const QString& newAlias) { - connection()->callApi(id(), - RoomCanonicalAliasEvent(newAlias)); + d->requestSetState(RoomCanonicalAliasEvent(newAlias)); } void Room::setTopic(const QString& newTopic) { - RoomTopicEvent evt(newTopic); - connection()->callApi(id(), evt); + d->requestSetState(RoomTopicEvent(newTopic)); } void Room::getPreviousContent(int limit) @@ -1153,6 +1165,11 @@ LeaveRoomJob* Room::leaveRoom() return connection()->callApi(id()); } +SetRoomStateWithKeyJob*Room::setMemberState(const QString& memberId, const RoomMemberEvent& event) const +{ + return d->requestSetState(memberId, event); +} + void Room::kickMember(const QString& memberId, const QString& reason) { connection()->callApi(id(), memberId, reason); diff --git a/lib/room.h b/lib/room.h index 7b4b3578..0d629e20 100644 --- a/lib/room.h +++ b/lib/room.h @@ -32,10 +32,12 @@ namespace QMatrixClient { class Event; + class RoomMemberEvent; class Connection; class User; class MemberSorter; class LeaveRoomJob; + class SetRoomStateWithKeyJob; class RedactEventJob; class TimelineItem @@ -344,6 +346,8 @@ namespace QMatrixClient void inviteToRoom(const QString& memberId); LeaveRoomJob* leaveRoom(); + SetRoomStateWithKeyJob* setMemberState( + const QString& memberId, const RoomMemberEvent& event) const; void kickMember(const QString& memberId, const QString& reason = {}); void ban(const QString& userId, const QString& reason = {}); void unban(const QString& userId); diff --git a/lib/user.cpp b/lib/user.cpp index 89e324f9..5c380424 100644 --- a/lib/user.cpp +++ b/lib/user.cpp @@ -23,7 +23,7 @@ #include "avatar.h" #include "events/event.h" #include "events/roommemberevent.h" -#include "jobs/setroomstatejob.h" +#include "jobs/generated/room_state.h" #include "jobs/generated/profile.h" #include "jobs/generated/content-repo.h" @@ -277,8 +277,7 @@ void User::rename(const QString& newName, const Room* r) "Attempt to rename a user that's not a room member"); MemberEventContent evtC; evtC.displayName = newName; - auto job = d->connection->callApi( - r->id(), id(), RoomMemberEvent(move(evtC))); + auto job = r->setMemberState(id(), RoomMemberEvent(move(evtC))); connect(job, &BaseJob::success, this, [=] { updateName(newName, r); }); } diff --git a/libqmatrixclient.pri b/libqmatrixclient.pri index 39e153a2..3aa419bb 100644 --- a/libqmatrixclient.pri +++ b/libqmatrixclient.pri @@ -36,7 +36,6 @@ HEADERS += \ $$SRCPATH/jobs/postreceiptjob.h \ $$SRCPATH/jobs/syncjob.h \ $$SRCPATH/jobs/mediathumbnailjob.h \ - $$SRCPATH/jobs/setroomstatejob.h \ $$SRCPATH/jobs/downloadfilejob.h \ $$SRCPATH/jobs/postreadmarkersjob.h \ $$files($$SRCPATH/jobs/generated/*.h, false) \ @@ -67,7 +66,6 @@ SOURCES += \ $$SRCPATH/jobs/postreceiptjob.cpp \ $$SRCPATH/jobs/syncjob.cpp \ $$SRCPATH/jobs/mediathumbnailjob.cpp \ - $$SRCPATH/jobs/setroomstatejob.cpp \ $$SRCPATH/jobs/downloadfilejob.cpp \ $$files($$SRCPATH/jobs/generated/*.cpp, false) \ $$files($$SRCPATH/jobs/generated/definitions/*.cpp, false) \ -- cgit v1.2.3