From 749def2b983d2338272c0891d15de20df22e2eea Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Thu, 22 Jun 2017 16:38:29 +0900 Subject: Enable sending RoomMessageEvents 1. PostMessageJob is now SendEventJob, which reflects two things: first, it's a PUT instead of a POST (POST for /send is not supported by the latest spec anyway), so that we could enable tracking transaction ids for local echo in the near future; second, it's no more just about messages, the job can support sending any room events (topic changes etc.). 2. Room::postMessage() now uses the new RoomMessageEvent API to send m.room.message events. --- jobs/postmessagejob.cpp | 64 ------------------------------------------------- jobs/postmessagejob.h | 46 ----------------------------------- jobs/sendeventjob.cpp | 41 +++++++++++++++++++++++++++++++ jobs/sendeventjob.h | 57 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 98 insertions(+), 110 deletions(-) delete mode 100644 jobs/postmessagejob.cpp delete mode 100644 jobs/postmessagejob.h create mode 100644 jobs/sendeventjob.cpp create mode 100644 jobs/sendeventjob.h (limited to 'jobs') 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 - * - * 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/postmessagejob.h b/jobs/postmessagejob.h deleted file mode 100644 index f4ae809b..00000000 --- a/jobs/postmessagejob.h +++ /dev/null @@ -1,46 +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" - -namespace QMatrixClient -{ - class PostMessageJob: 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(); - - //bool success(); - - protected: - Status parseJson(const QJsonDocument& data) override; - - private: - class Private; - Private* d; - }; -} // namespace QMatrixClient 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 + * + * 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/sendeventjob.h b/jobs/sendeventjob.h new file mode 100644 index 00000000..180fdd19 --- /dev/null +++ b/jobs/sendeventjob.h @@ -0,0 +1,57 @@ +/****************************************************************************** + * 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 SendEventJob: public BaseJob + { + public: + /** Constructs a job that sends an arbitrary room event */ + template + 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); + + QString eventId() const { return _eventId; } + + protected: + Status parseJson(const QJsonDocument& data) override; + + private: + QString _eventId; + }; +} // namespace QMatrixClient -- cgit v1.2.3