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. --- 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 ++- 7 files changed, 146 insertions(+), 105 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') 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); }); } -- cgit v1.2.3