diff options
author | KitsuneRal <Kitsune-Ral@users.sf.net> | 2016-10-17 07:25:30 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-10-17 07:25:30 +0900 |
commit | b44ad3573d21c456014d7983669acc31dd6eebdf (patch) | |
tree | 9281918022181ec6eb3b28b0fe6dcbef572233e1 /jobs | |
parent | 6536124821c1df791a69a58de21a71322d9363f6 (diff) | |
parent | f2cfd4691d4830f57ebaea08b9aa9a4db3aad537 (diff) | |
download | libquotient-b44ad3573d21c456014d7983669acc31dd6eebdf.tar.gz libquotient-b44ad3573d21c456014d7983669acc31dd6eebdf.zip |
Merge pull request #43 from Fxrh/kitsune-request-params
Merged as amended.
Diffstat (limited to 'jobs')
-rw-r--r-- | jobs/basejob.cpp | 74 | ||||
-rw-r--r-- | jobs/basejob.h | 53 | ||||
-rw-r--r-- | jobs/checkauthmethods.cpp | 8 | ||||
-rw-r--r-- | jobs/checkauthmethods.h | 1 | ||||
-rw-r--r-- | jobs/joinroomjob.cpp | 10 | ||||
-rw-r--r-- | jobs/joinroomjob.h | 1 | ||||
-rw-r--r-- | jobs/leaveroomjob.cpp | 27 | ||||
-rw-r--r-- | jobs/leaveroomjob.h | 7 | ||||
-rw-r--r-- | jobs/logoutjob.cpp | 7 | ||||
-rw-r--r-- | jobs/logoutjob.h | 3 | ||||
-rw-r--r-- | jobs/mediathumbnailjob.cpp | 35 | ||||
-rw-r--r-- | jobs/mediathumbnailjob.h | 3 | ||||
-rw-r--r-- | jobs/passwordlogin.cpp | 31 | ||||
-rw-r--r-- | jobs/passwordlogin.h | 2 | ||||
-rw-r--r-- | jobs/postmessagejob.cpp | 28 | ||||
-rw-r--r-- | jobs/postmessagejob.h | 2 | ||||
-rw-r--r-- | jobs/postreceiptjob.cpp | 10 | ||||
-rw-r--r-- | jobs/postreceiptjob.h | 3 | ||||
-rw-r--r-- | jobs/roommessagesjob.cpp | 36 | ||||
-rw-r--r-- | jobs/roommessagesjob.h | 5 | ||||
-rw-r--r-- | jobs/syncjob.cpp | 66 | ||||
-rw-r--r-- | jobs/syncjob.h | 10 |
22 files changed, 157 insertions, 265 deletions
diff --git a/jobs/basejob.cpp b/jobs/basejob.cpp index a3b69922..a50e9f84 100644 --- a/jobs/basejob.cpp +++ b/jobs/basejob.cpp @@ -41,13 +41,22 @@ struct NetworkReplyDeleter : public QScopedPointerDeleteLater class BaseJob::Private { public: - Private(ConnectionData* c, JobHttpType t, bool nt) - : connection(c), type(t), needsToken(nt) + Private(ConnectionData* c, JobHttpType t, QString endpoint, + const QUrlQuery& q, const Data& data, bool nt) + : connection(c), type(t), apiEndpoint(endpoint), requestQuery(q) + , requestData(data), needsToken(nt) , reply(nullptr), status(NoError) - {} + { } + inline void sendRequest(); + ConnectionData* connection; + + // Contents for the network request JobHttpType type; + QString apiEndpoint; + QUrlQuery requestQuery; + Data requestData; bool needsToken; QScopedPointer<QNetworkReply, NetworkReplyDeleter> reply; @@ -61,8 +70,10 @@ inline QDebug operator<<(QDebug dbg, BaseJob* j) return dbg << "Job" << j->objectName(); } -BaseJob::BaseJob(ConnectionData* connection, JobHttpType type, QString name, bool needsToken) - : d(new Private(connection, type, needsToken)) +BaseJob::BaseJob(ConnectionData* connection, JobHttpType type, QString name, + QString endpoint, BaseJob::Query query, BaseJob::Data data, + bool needsToken) + : d(new Private(connection, type, endpoint, query, data, needsToken)) { setObjectName(name); connect (&d->timer, &QTimer::timeout, this, &BaseJob::timeout); @@ -79,48 +90,63 @@ ConnectionData* BaseJob::connection() const return d->connection; } -QJsonObject BaseJob::data() const +const QUrlQuery&BaseJob::query() const { - return QJsonObject(); + return d->requestQuery; } -QUrlQuery BaseJob::query() const +void BaseJob::setRequestQuery(const QUrlQuery& query) { - return QUrlQuery(); + d->requestQuery = query; } -void BaseJob::start() +const BaseJob::Data&BaseJob::requestData() const +{ + return d->requestData; +} + +void BaseJob::setRequestData(const BaseJob::Data& data) { - QUrl url = d->connection->baseUrl(); - url.setPath( url.path() + "/" + apiPath() ); - QUrlQuery query = this->query(); - if( d->needsToken ) - query.addQueryItem("access_token", connection()->accessToken()); - url.setQuery(query); - QNetworkRequest req = QNetworkRequest(url); + d->requestData = data; +} + +void BaseJob::Private::sendRequest() +{ + QUrl url = connection->baseUrl(); + url.setPath( url.path() + "/" + apiEndpoint ); + if (needsToken) + requestQuery.addQueryItem("access_token", connection->accessToken()); + url.setQuery(requestQuery); + + QNetworkRequest req {url}; req.setHeader(QNetworkRequest::ContentTypeHeader, "application/json"); #if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)) req.setAttribute(QNetworkRequest::FollowRedirectsAttribute, true); req.setMaximumRedirectsAllowed(10); #endif - QJsonDocument data = QJsonDocument(this->data()); - switch( d->type ) + switch( type ) { case JobHttpType::GetJob: - d->reply.reset( d->connection->nam()->get(req) ); + reply.reset( connection->nam()->get(req) ); break; case JobHttpType::PostJob: - d->reply.reset( d->connection->nam()->post(req, data.toJson()) ); + reply.reset( connection->nam()->post(req, requestData.serialize()) ); break; case JobHttpType::PutJob: - d->reply.reset( d->connection->nam()->put(req, data.toJson()) ); + reply.reset( connection->nam()->put(req, requestData.serialize()) ); + break; + case JobHttpType::DeleteJob: + reply.reset( connection->nam()->deleteResource(req) ); break; } +} + +void BaseJob::start() +{ + d->sendRequest(); connect( d->reply.data(), &QNetworkReply::sslErrors, this, &BaseJob::sslErrors ); connect( d->reply.data(), &QNetworkReply::finished, this, &BaseJob::gotReply ); d->timer.start( 120*1000 ); -// connect( d->reply, static_cast<void(QNetworkReply::*)(QNetworkReply::NetworkError)>(&QNetworkReply::error), -// this, &BaseJob::networkError ); // http://doc.qt.io/qt-5/qnetworkreply.html#error-1 } void BaseJob::gotReply() diff --git a/jobs/basejob.h b/jobs/basejob.h index 07b1f1dd..f7e4b948 100644 --- a/jobs/basejob.h +++ b/jobs/basejob.h @@ -32,8 +32,8 @@ namespace QMatrixClient { class ConnectionData; - enum class JobHttpType { GetJob, PutJob, PostJob }; - + enum class JobHttpType { GetJob, PutJob, PostJob, DeleteJob }; + class BaseJob: public QObject { Q_OBJECT @@ -51,6 +51,42 @@ namespace QMatrixClient }; /** + * A simple wrapper around QUrlQuery that allows its creation from + * a list of string pairs + */ + class Query : public QUrlQuery + { + public: + using QUrlQuery::QUrlQuery; + Query() = default; + Query(const QList< QPair<QString, QString> >& l) + { + setQueryItems(l); + } + }; + /** + * A simple wrapper around QJsonObject that represents a JSON data + * section of an HTTP request to a Matrix server. Facilitates + * creation from a list of key-value string pairs and dumping of + * a resulting JSON to a QByteArray. + */ + class Data : public QJsonObject + { + public: + using QJsonObject::QJsonObject; + Data() = default; + Data(const QList< QPair<QString, QString> >& l) + { + for (auto i: l) + insert(i.first, i.second); + } + QByteArray serialize() const + { + return QJsonDocument(*this).toJson(); + } + }; + + /** * This structure stores the status of a server call job. The status consists * of a code, that is described (but not delimited) by the respective enum, * and a freeform message. @@ -72,8 +108,9 @@ namespace QMatrixClient }; public: - BaseJob(ConnectionData* connection, JobHttpType type, - QString name, bool needsToken=true); + BaseJob(ConnectionData* connection, JobHttpType type, QString name, + QString endpoint, Query query = Query(), Data data = Data(), + bool needsToken = true); virtual ~BaseJob(); void start(); @@ -138,10 +175,10 @@ namespace QMatrixClient protected: ConnectionData* connection() const; - // to implement - virtual QString apiPath() const = 0; - virtual QUrlQuery query() const; - virtual QJsonObject data() const; + const QUrlQuery& query() const; + void setRequestQuery(const QUrlQuery& query); + const Data& requestData() const; + void setRequestData(const Data& data); /** * Used by gotReply() slot to check the received reply for general diff --git a/jobs/checkauthmethods.cpp b/jobs/checkauthmethods.cpp index c93bf802..28646822 100644 --- a/jobs/checkauthmethods.cpp +++ b/jobs/checkauthmethods.cpp @@ -37,7 +37,8 @@ class CheckAuthMethods::Private }; CheckAuthMethods::CheckAuthMethods(ConnectionData* connection) - : BaseJob(connection, JobHttpType::GetJob, "CheckAuthMethods", false) + : BaseJob(connection, JobHttpType::GetJob, "CheckAuthMethods", + "_matrix/client/r0/login", Query(), Data(), false) , d(new Private) { } @@ -52,11 +53,6 @@ QString CheckAuthMethods::session() return d->session; } -QString CheckAuthMethods::apiPath() const -{ - return "_matrix/client/r0/login"; -} - BaseJob::Status CheckAuthMethods::parseJson(const QJsonDocument& data) { // TODO diff --git a/jobs/checkauthmethods.h b/jobs/checkauthmethods.h index 36eaa01a..e6aeba69 100644 --- a/jobs/checkauthmethods.h +++ b/jobs/checkauthmethods.h @@ -34,7 +34,6 @@ namespace QMatrixClient QString session(); protected: - QString apiPath() const override; Status parseJson(const QJsonDocument& data) override; private: diff --git a/jobs/joinroomjob.cpp b/jobs/joinroomjob.cpp index 8848aa70..d443acd7 100644 --- a/jobs/joinroomjob.cpp +++ b/jobs/joinroomjob.cpp @@ -29,14 +29,13 @@ class JoinRoomJob::Private { public: QString roomId; - QString roomAlias; }; JoinRoomJob::JoinRoomJob(ConnectionData* data, QString roomAlias) - : BaseJob(data, JobHttpType::PostJob, "JoinRoomJob") + : BaseJob(data, JobHttpType::PostJob, "JoinRoomJob", + QString("_matrix/client/r0/join/%1").arg(roomAlias)) , d(new Private) { - d->roomAlias = roomAlias; } JoinRoomJob::~JoinRoomJob() @@ -49,11 +48,6 @@ QString JoinRoomJob::roomId() return d->roomId; } -QString JoinRoomJob::apiPath() const -{ - return QString("_matrix/client/r0/join/%1").arg(d->roomAlias); -} - BaseJob::Status JoinRoomJob::parseJson(const QJsonDocument& data) { QJsonObject json = data.object(); diff --git a/jobs/joinroomjob.h b/jobs/joinroomjob.h index a6f4af21..c1eebbef 100644 --- a/jobs/joinroomjob.h +++ b/jobs/joinroomjob.h @@ -34,7 +34,6 @@ namespace QMatrixClient QString roomId(); protected: - QString apiPath() const override; Status parseJson(const QJsonDocument& data) override; private: diff --git a/jobs/leaveroomjob.cpp b/jobs/leaveroomjob.cpp index b29c80ed..554bb990 100644 --- a/jobs/leaveroomjob.cpp +++ b/jobs/leaveroomjob.cpp @@ -18,33 +18,14 @@ #include "leaveroomjob.h" -#include <QtNetwork/QNetworkReply> - #include "../room.h" -#include "../connectiondata.h" using namespace QMatrixClient; -class LeaveRoomJob::Private -{ - public: - Private(Room* r) : room(r) {} - - Room* room; -}; - LeaveRoomJob::LeaveRoomJob(ConnectionData* data, Room* room) - : BaseJob(data, JobHttpType::PostJob, "LeaveRoomJob") - , d(new Private(room)) -{ -} + : BaseJob(data, JobHttpType::PostJob, "LeaveRoomJob", + QString("_matrix/client/r0/rooms/%1/leave").arg(room->id())) +{ } LeaveRoomJob::~LeaveRoomJob() -{ - delete d; -} - -QString LeaveRoomJob::apiPath() const -{ - return QString("_matrix/client/r0/rooms/%1/leave").arg(d->room->id()); -} +{ } diff --git a/jobs/leaveroomjob.h b/jobs/leaveroomjob.h index 9ce9bf4a..492233f6 100644 --- a/jobs/leaveroomjob.h +++ b/jobs/leaveroomjob.h @@ -31,13 +31,6 @@ namespace QMatrixClient public: LeaveRoomJob(ConnectionData* data, Room* room); virtual ~LeaveRoomJob(); - - protected: - QString apiPath() const override; - - private: - class Private; - Private* d; }; } diff --git a/jobs/logoutjob.cpp b/jobs/logoutjob.cpp index 88767e4c..25dee174 100644 --- a/jobs/logoutjob.cpp +++ b/jobs/logoutjob.cpp @@ -21,15 +21,10 @@ using namespace QMatrixClient; LogoutJob::LogoutJob(ConnectionData* connection) - : BaseJob(connection, JobHttpType::PostJob, "LogoutJob") + : BaseJob(connection, JobHttpType::PostJob, "LogoutJob", "/_matrix/client/r0/logout") { } LogoutJob::~LogoutJob() { } - -QString LogoutJob::apiPath() const -{ - return "/_matrix/client/r0/logout"; -} diff --git a/jobs/logoutjob.h b/jobs/logoutjob.h index faa1e2cb..7d70a74c 100644 --- a/jobs/logoutjob.h +++ b/jobs/logoutjob.h @@ -27,8 +27,5 @@ namespace QMatrixClient public: LogoutJob(ConnectionData* connection); virtual ~LogoutJob(); - - protected: - QString apiPath() const override; }; } diff --git a/jobs/mediathumbnailjob.cpp b/jobs/mediathumbnailjob.cpp index 975a533d..dee06372 100644 --- a/jobs/mediathumbnailjob.cpp +++ b/jobs/mediathumbnailjob.cpp @@ -25,21 +25,21 @@ using namespace QMatrixClient; class MediaThumbnailJob::Private { public: - QUrl url; QPixmap thumbnail; - QSize requestedSize; - ThumbnailType thumbnailType; }; MediaThumbnailJob::MediaThumbnailJob(ConnectionData* data, QUrl url, QSize requestedSize, ThumbnailType thumbnailType) - : BaseJob(data, JobHttpType::GetJob, "MediaThumbnailJob") + : BaseJob(data, JobHttpType::GetJob, "MediaThumbnailJob", + QString("/_matrix/media/v1/thumbnail/%1%2").arg(url.host(), url.path()), + Query( + { { "width", QString::number(requestedSize.width()) } + , { "height", QString::number(requestedSize.height()) } + , { "method", + thumbnailType == ThumbnailType::Scale ? "scale" : "crop" } + })) , d(new Private) -{ - d->url = url; - d->requestedSize = requestedSize; - d->thumbnailType = thumbnailType; -} +{ } MediaThumbnailJob::~MediaThumbnailJob() { @@ -51,23 +51,6 @@ QPixmap MediaThumbnailJob::thumbnail() return d->thumbnail; } -QString MediaThumbnailJob::apiPath() const -{ - return QString("/_matrix/media/v1/thumbnail/%1%2").arg(d->url.host()).arg(d->url.path()); -} - -QUrlQuery MediaThumbnailJob::query() const -{ - QUrlQuery query; - query.addQueryItem("width", QString::number(d->requestedSize.width())); - query.addQueryItem("height", QString::number(d->requestedSize.height())); - if( d->thumbnailType == ThumbnailType::Scale ) - query.addQueryItem("method", "scale"); - else - query.addQueryItem("method", "crop"); - return query; -} - BaseJob::Status MediaThumbnailJob::parseReply(QByteArray data) { if( !d->thumbnail.loadFromData(data) ) diff --git a/jobs/mediathumbnailjob.h b/jobs/mediathumbnailjob.h index 8a6cdce2..15bec9a1 100644 --- a/jobs/mediathumbnailjob.h +++ b/jobs/mediathumbnailjob.h @@ -37,9 +37,6 @@ namespace QMatrixClient QPixmap thumbnail(); protected: - QString apiPath() const override; - QUrlQuery query() const override; - Status parseReply(QByteArray data) override; private: diff --git a/jobs/passwordlogin.cpp b/jobs/passwordlogin.cpp index c85e4c13..a7a405aa 100644 --- a/jobs/passwordlogin.cpp +++ b/jobs/passwordlogin.cpp @@ -29,21 +29,24 @@ using namespace QMatrixClient; class PasswordLogin::Private { public: - Private() {} - - QString user; - QString password; QString returned_id; QString returned_server; QString returned_token; }; PasswordLogin::PasswordLogin(ConnectionData* connection, QString user, QString password) - : BaseJob(connection, JobHttpType::PostJob, "PasswordLogin", false) + : BaseJob(connection, JobHttpType::PostJob, "PasswordLogin" + , "_matrix/client/r0/login" + , Query() + , Data( + { { "type", "m.login.password" } + , { "user", user } + , { "password", password } + }) + , false + ) , d(new Private) { - d->user = user; - d->password = password; } PasswordLogin::~PasswordLogin() @@ -66,20 +69,6 @@ QString PasswordLogin::server() return d->returned_server; } -QString PasswordLogin::apiPath() const -{ - return "_matrix/client/r0/login"; -} - -QJsonObject PasswordLogin::data() const -{ - QJsonObject json; - json.insert("type", QLatin1String("m.login.password")); - json.insert("user", d->user); - json.insert("password", d->password); - return json; -} - BaseJob::Status PasswordLogin::parseJson(const QJsonDocument& data) { QJsonObject json = data.object(); diff --git a/jobs/passwordlogin.h b/jobs/passwordlogin.h index d7e42725..156865a3 100644 --- a/jobs/passwordlogin.h +++ b/jobs/passwordlogin.h @@ -36,8 +36,6 @@ namespace QMatrixClient QString server(); protected: - QString apiPath() const override; - QJsonObject data() const override; Status parseJson(const QJsonDocument& data) override; private: diff --git a/jobs/postmessagejob.cpp b/jobs/postmessagejob.cpp index 0a38da62..9903bec6 100644 --- a/jobs/postmessagejob.cpp +++ b/jobs/postmessagejob.cpp @@ -29,38 +29,22 @@ class PostMessageJob::Private public: Private() {} - QString type; - QString message; - Room* room; + QString eventId; // unused yet }; PostMessageJob::PostMessageJob(ConnectionData* connection, Room* room, QString type, QString message) - : BaseJob(connection, JobHttpType::PostJob, "PostMessageJob") + : BaseJob(connection, JobHttpType::PostJob, "PostMessageJob", + QString("_matrix/client/r0/rooms/%1/send/m.room.message").arg(room->id()), + Query(), + Data({ { "msgtype", type }, { "body", message } })) , d(new Private) -{ - d->type = type; - d->message = message; - d->room = room; -} +{ } PostMessageJob::~PostMessageJob() { delete d; } -QString PostMessageJob::apiPath() const -{ - return QString("_matrix/client/r0/rooms/%1/send/m.room.message").arg(d->room->id()); -} - -QJsonObject PostMessageJob::data() const -{ - QJsonObject json; - json.insert("msgtype", d->type); - json.insert("body", d->message); - return json; -} - BaseJob::Status PostMessageJob::parseJson(const QJsonDocument& data) { if( data.object().contains("event_id") ) diff --git a/jobs/postmessagejob.h b/jobs/postmessagejob.h index 73d72020..9d52ae8d 100644 --- a/jobs/postmessagejob.h +++ b/jobs/postmessagejob.h @@ -33,8 +33,6 @@ namespace QMatrixClient //bool success(); protected: - QString apiPath() const override; - QJsonObject data() const override; Status parseJson(const QJsonDocument& data) override; private: diff --git a/jobs/postreceiptjob.cpp b/jobs/postreceiptjob.cpp index f99b6869..d0c82b4f 100644 --- a/jobs/postreceiptjob.cpp +++ b/jobs/postreceiptjob.cpp @@ -34,19 +34,13 @@ class PostReceiptJob::Private }; PostReceiptJob::PostReceiptJob(ConnectionData* connection, QString roomId, QString eventId) - : BaseJob(connection, JobHttpType::PostJob, "PostReceiptJob") + : BaseJob(connection, JobHttpType::PostJob, "PostReceiptJob", + QString("/_matrix/client/r0/rooms/%1/receipt/m.read/%2").arg(roomId, eventId)) , d(new Private) { - d->roomId = roomId; - d->eventId = eventId; } PostReceiptJob::~PostReceiptJob() { delete d; } - -QString PostReceiptJob::apiPath() const -{ - return QString("/_matrix/client/r0/rooms/%1/receipt/m.read/%2").arg(d->roomId).arg(d->eventId); -} diff --git a/jobs/postreceiptjob.h b/jobs/postreceiptjob.h index 15963bf7..4e047b3a 100644 --- a/jobs/postreceiptjob.h +++ b/jobs/postreceiptjob.h @@ -30,9 +30,6 @@ namespace QMatrixClient PostReceiptJob(ConnectionData* connection, QString roomId, QString eventId); virtual ~PostReceiptJob(); - protected: - QString apiPath() const override; - private: class Private; Private* d; diff --git a/jobs/roommessagesjob.cpp b/jobs/roommessagesjob.cpp index f880713d..d67ffc2a 100644 --- a/jobs/roommessagesjob.cpp +++ b/jobs/roommessagesjob.cpp @@ -29,23 +29,20 @@ class RoomMessagesJob::Private public: Private() {} - Room* room; - QString from; - FetchDirectory dir; - int limit; - Events events; QString end; }; RoomMessagesJob::RoomMessagesJob(ConnectionData* data, Room* room, QString from, FetchDirectory dir, int limit) - : BaseJob(data, JobHttpType::GetJob, "RoomMessagesJob") + : BaseJob(data, JobHttpType::GetJob, "RoomMessagesJob", + QString("/_matrix/client/r0/rooms/%1/messages").arg(room->id()), + Query( + { { "from", from } + , { "dir", dir == FetchDirectory::Backwards ? "b" : "f" } + , { "limit", QString::number(limit) } + })) + , d(new Private) { - d = new Private(); - d->room = room; - d->from = from; - d->dir = dir; - d->limit = limit; } RoomMessagesJob::~RoomMessagesJob() @@ -63,23 +60,6 @@ QString RoomMessagesJob::end() return d->end; } -QString RoomMessagesJob::apiPath() const -{ - return QString("/_matrix/client/r0/rooms/%1/messages").arg(d->room->id()); -} - -QUrlQuery RoomMessagesJob::query() const -{ - QUrlQuery query; - query.addQueryItem("from", d->from); - if( d->dir == FetchDirectory::Backwards ) - query.addQueryItem("dir", "b"); - else - query.addQueryItem("dir", "f"); - query.addQueryItem("limit", QString::number(d->limit)); - return query; -} - BaseJob::Status RoomMessagesJob::parseJson(const QJsonDocument& data) { QJsonObject obj = data.object(); diff --git a/jobs/roommessagesjob.h b/jobs/roommessagesjob.h index 9bedcad9..227ffac2 100644 --- a/jobs/roommessagesjob.h +++ b/jobs/roommessagesjob.h @@ -32,15 +32,14 @@ namespace QMatrixClient class RoomMessagesJob: public BaseJob { public: - RoomMessagesJob(ConnectionData* data, Room* room, QString from, FetchDirectory dir = FetchDirectory::Backwards, int limit=10); + RoomMessagesJob(ConnectionData* data, Room* room, QString from, + FetchDirectory dir = FetchDirectory::Backwards, int limit=10); virtual ~RoomMessagesJob(); Events events(); QString end(); protected: - QString apiPath() const override; - QUrlQuery query() const override; Status parseJson(const QJsonDocument& data) override; private: diff --git a/jobs/syncjob.cpp b/jobs/syncjob.cpp index 59c40785..521f829e 100644 --- a/jobs/syncjob.cpp +++ b/jobs/syncjob.cpp @@ -32,25 +32,28 @@ using namespace QMatrixClient; class SyncJob::Private { public: - QString since; - QString filter; - bool fullState; - QString presence; - int timeout; QString nextBatch; - SyncData roomData; }; static size_t jobId = 0; -SyncJob::SyncJob(ConnectionData* connection, QString since) - : BaseJob(connection, JobHttpType::GetJob, QString("SyncJob-%1").arg(++jobId)) +SyncJob::SyncJob(ConnectionData* connection, + QString since, QString filter, int timeout, QString presence) + : BaseJob(connection, JobHttpType::GetJob, QString("SyncJob-%1").arg(++jobId), + "_matrix/client/r0/sync") , d(new Private) { - d->since = since; - d->fullState = false; - d->timeout = -1; + QUrlQuery query; + if( !filter.isEmpty() ) + query.addQueryItem("filter", filter); + if( !presence.isEmpty() ) + query.addQueryItem("set_presence", presence); + if( timeout >= 0 ) + query.addQueryItem("timeout", QString::number(timeout)); + if( !since.isEmpty() ) + query.addQueryItem("since", since); + setRequestQuery(query); } SyncJob::~SyncJob() @@ -58,26 +61,6 @@ SyncJob::~SyncJob() delete d; } -void SyncJob::setFilter(QString filter) -{ - d->filter = filter; -} - -void SyncJob::setFullState(bool full) -{ - d->fullState = full; -} - -void SyncJob::setPresence(QString presence) -{ - d->presence = presence; -} - -void SyncJob::setTimeout(int timeout) -{ - d->timeout = timeout; -} - QString SyncJob::nextBatch() const { return d->nextBatch; @@ -88,27 +71,6 @@ SyncData& SyncJob::roomData() return d->roomData; } -QString SyncJob::apiPath() const -{ - return "_matrix/client/r0/sync"; -} - -QUrlQuery SyncJob::query() const -{ - QUrlQuery query; - if( !d->filter.isEmpty() ) - query.addQueryItem("filter", d->filter); - if( d->fullState ) - query.addQueryItem("full_state", "true"); - if( !d->presence.isEmpty() ) - query.addQueryItem("set_presence", d->presence); - if( d->timeout >= 0 ) - query.addQueryItem("timeout", QString::number(d->timeout)); - if( !d->since.isEmpty() ) - query.addQueryItem("since", d->since); - return query; -} - BaseJob::Status SyncJob::parseJson(const QJsonDocument& data) { QJsonObject json = data.object(); diff --git a/jobs/syncjob.h b/jobs/syncjob.h index a9138a65..e7b23d16 100644 --- a/jobs/syncjob.h +++ b/jobs/syncjob.h @@ -99,20 +99,14 @@ namespace QMatrixClient class SyncJob: public BaseJob { public: - SyncJob(ConnectionData* connection, QString since=QString()); + SyncJob(ConnectionData* connection, QString since = {}, QString filter = {}, + int timeout = -1, QString presence = {}); virtual ~SyncJob(); - void setFilter(QString filter); - void setFullState(bool full); - void setPresence(QString presence); - void setTimeout(int timeout); - SyncData& roomData(); QString nextBatch() const; protected: - QString apiPath() const override; - QUrlQuery query() const override; Status parseJson(const QJsonDocument& data) override; private: |