diff options
Diffstat (limited to 'jobs')
-rw-r--r-- | jobs/basejob.h | 9 | ||||
-rw-r--r-- | jobs/postmessagejob.cpp | 64 | ||||
-rw-r--r-- | jobs/sendeventjob.cpp | 41 | ||||
-rw-r--r-- | jobs/sendeventjob.h (renamed from jobs/postmessagejob.h) | 35 |
4 files changed, 71 insertions, 78 deletions
diff --git a/jobs/basejob.h b/jobs/basejob.h index 2be4577f..0ec40a7a 100644 --- a/jobs/basejob.h +++ b/jobs/basejob.h @@ -63,7 +63,7 @@ namespace QMatrixClient public: using QUrlQuery::QUrlQuery; Query() = default; - explicit Query(const QList< QPair<QString, QString> >& l) + explicit Query(const std::initializer_list< QPair<QString, QString> >& l) { setQueryItems(l); } @@ -78,11 +78,16 @@ namespace QMatrixClient { public: Data() = default; - explicit Data(const QList< QPair<QString, QString> >& l) + Data(const QJsonObject& o) : QJsonObject(o) { } + Data(QJsonObject&& o) : QJsonObject(std::move(o)) { } +#if (QT_VERSION < QT_VERSION_CHECK(5, 4, 0)) + // This method exists in QJsonObject of newer Qt versions + explicit Data(const std::initializer_list< QPair<QString, QString> >& l) { for (auto i: l) insert(i.first, i.second); } +#endif QByteArray serialize() const { return QJsonDocument(*this).toJson(); diff --git a/jobs/postmessagejob.cpp b/jobs/postmessagejob.cpp deleted file mode 100644 index df30614c..00000000 --- a/jobs/postmessagejob.cpp +++ /dev/null @@ -1,64 +0,0 @@ -/****************************************************************************** - * Copyright (C) 2015 Felix Rohrbach <kde@fxrh.de> - * - * 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 "postmessagejob.h" -#include "util.h" - -using namespace QMatrixClient; - -class PostMessageJob::Private -{ - public: - QString eventId; // unused yet -}; - -PostMessageJob::PostMessageJob(const ConnectionData* connection, - const QString& roomId, const QString& type, - const QString& plainText) - : BaseJob(connection, HttpVerb::Post, "PostMessageJob", - QStringLiteral("_matrix/client/r0/rooms/%1/send/m.room.message").arg(roomId), - Query(), - Data({ { "msgtype", type }, { "body", plainText } }) ) - , d(new Private) -{ } - -PostMessageJob::PostMessageJob(const ConnectionData* connection, - const QString& roomId, const QString& type, - const QString& plainText, const QString& richText) - : BaseJob(connection, HttpVerb::Post, "PostMessageJob", - QStringLiteral("_matrix/client/r0/rooms/%1/send/m.room.message").arg(roomId), - Query(), - Data({ { "msgtype", type }, { "body", plainText } - , { "format", QStringLiteral("org.matrix.custom.html") } - , { "formatted_body", richText } }) ) - , d(new Private) -{ } - -PostMessageJob::~PostMessageJob() -{ - delete d; -} - -BaseJob::Status PostMessageJob::parseJson(const QJsonDocument& data) -{ - if( data.object().contains("event_id") ) - return Success; - - qCDebug(JOBS) << data; - return { UserDefinedError, "No event_id in the JSON response" }; -} diff --git a/jobs/sendeventjob.cpp b/jobs/sendeventjob.cpp new file mode 100644 index 00000000..f3c95fe8 --- /dev/null +++ b/jobs/sendeventjob.cpp @@ -0,0 +1,41 @@ +/****************************************************************************** + * Copyright (C) 2015 Felix Rohrbach <kde@fxrh.de> + * + * 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 "sendeventjob.h" + +#include "events/roommessageevent.h" + +using namespace QMatrixClient; + +SendEventJob::SendEventJob(const ConnectionData* connection, + const QString& roomId, const QString& type, + const QString& plainText) + : SendEventJob(connection, roomId, + new RoomMessageEvent(plainText, type)) +{ } + +BaseJob::Status SendEventJob::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/jobs/postmessagejob.h b/jobs/sendeventjob.h index f4ae809b..180fdd19 100644 --- a/jobs/postmessagejob.h +++ b/jobs/sendeventjob.h @@ -20,27 +20,38 @@ #include "basejob.h" +#include "connectiondata.h" + namespace QMatrixClient { - class PostMessageJob: public BaseJob + class SendEventJob: public BaseJob { public: - /** Constructs a plain text message job */ - PostMessageJob(const ConnectionData* connection, const QString& roomId, - const QString& type, const QString& plainText); - /** Constructs a rich text message job */ - PostMessageJob(const ConnectionData* connection, const QString& roomId, - const QString& type, const QString& plainText, - const QString& richText); - virtual ~PostMessageJob(); + /** Constructs a job that sends an arbitrary room event */ + template <typename EvT> + SendEventJob(const ConnectionData* connection, const QString& roomId, + const EvT* event) + : BaseJob(connection, HttpVerb::Put, "SendEventJob", + QStringLiteral("_matrix/client/r0/rooms/%1/send/%2/%3") + .arg(roomId).arg(EvT::TypeId) + .arg(event->transactionId()), + Query(), + Data(event->toJson())) + { } + + /** + * Constructs a plain text message job (for compatibility with + * the old PostMessageJob API). + */ + SendEventJob(const ConnectionData* connection, const QString& roomId, + const QString& type, const QString& plainText); - //bool success(); + QString eventId() const { return _eventId; } protected: Status parseJson(const QJsonDocument& data) override; private: - class Private; - Private* d; + QString _eventId; }; } // namespace QMatrixClient |