diff options
author | Kitsune Ral <Kitsune-Ral@users.sf.net> | 2017-03-09 13:13:48 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-03-09 13:13:48 +0900 |
commit | af8f95fa0446bf377321d9f285b0e55c08fd91da (patch) | |
tree | cdd07d0df71e2f7baa87d1be73fb4e3c3473d3eb | |
parent | ba5ca3c88a20926f2be06872f530d9da12d5a062 (diff) | |
parent | 3524342faacd12b00fa4c3a3ebe6a68782975eac (diff) | |
download | libquotient-af8f95fa0446bf377321d9f285b0e55c08fd91da.tar.gz libquotient-af8f95fa0446bf377321d9f285b0e55c08fd91da.zip |
Merge pull request #54 from Fxrh/kitsune-call-server
Connection::callApi + Room::getPreviousMessages(limit)
-rw-r--r-- | connection.cpp | 17 | ||||
-rw-r--r-- | connection.h | 8 | ||||
-rw-r--r-- | jobs/postmessagejob.cpp | 6 | ||||
-rw-r--r-- | jobs/postmessagejob.h | 4 | ||||
-rw-r--r-- | jobs/roommessagesjob.cpp | 9 | ||||
-rw-r--r-- | jobs/roommessagesjob.h | 9 | ||||
-rw-r--r-- | room.cpp | 21 | ||||
-rw-r--r-- | room.h | 3 |
8 files changed, 43 insertions, 34 deletions
diff --git a/connection.cpp b/connection.cpp index 0f115e3a..dff88bef 100644 --- a/connection.cpp +++ b/connection.cpp @@ -58,8 +58,6 @@ class Connection::Private QString userId; SyncJob* syncJob; - - SyncJob* startSyncJob(const QString& filter, int timeout); }; Connection::Connection(QUrl server, QObject* parent) @@ -167,7 +165,8 @@ void Connection::sync(int timeout) return; const QString filter = "{\"room\": { \"timeline\": { \"limit\": 100 } } }"; - auto job = d->startSyncJob(filter, timeout); + auto job = d->syncJob = + callApi<SyncJob>(d->data->lastEvent(), filter, timeout); connect( job, &SyncJob::success, [=] () { d->data->setLastEvent(job->nextBatch()); for( auto& roomData: job->roomData() ) @@ -187,17 +186,9 @@ void Connection::sync(int timeout) }); } -SyncJob* Connection::Private::startSyncJob(const QString& filter, int timeout) -{ - syncJob = new SyncJob(data, data->lastEvent(), filter, timeout); - syncJob->start(); - return syncJob; - -} - void Connection::postMessage(Room* room, QString type, QString message) { - PostMessageJob* job = new PostMessageJob(d->data, room, type, message); + PostMessageJob* job = new PostMessageJob(d->data, room->id(), type, message); job->start(); } @@ -226,7 +217,7 @@ void Connection::leaveRoom(Room* room) RoomMessagesJob* Connection::getMessages(Room* room, QString from) { - RoomMessagesJob* job = new RoomMessagesJob(d->data, room, from); + RoomMessagesJob* job = new RoomMessagesJob(d->data, room->id(), from); job->start(); return job; } diff --git a/connection.h b/connection.h index 0e25d695..1ce3e0de 100644 --- a/connection.h +++ b/connection.h @@ -69,6 +69,14 @@ namespace QMatrixClient Q_INVOKABLE QString token() const; Q_INVOKABLE QString accessToken() const; + template <typename JobT, typename... JobArgTs> + JobT* callApi(JobArgTs... jobArgs) + { + auto job = new JobT(connectionData(), jobArgs...); + job->start(); + return job; + } + signals: void resolved(); void connected(); diff --git a/jobs/postmessagejob.cpp b/jobs/postmessagejob.cpp index 9903bec6..a835cd9f 100644 --- a/jobs/postmessagejob.cpp +++ b/jobs/postmessagejob.cpp @@ -17,7 +17,6 @@ */ #include "postmessagejob.h" -#include "../room.h" #include "../connectiondata.h" #include <QtNetwork/QNetworkReply> @@ -32,9 +31,10 @@ class PostMessageJob::Private QString eventId; // unused yet }; -PostMessageJob::PostMessageJob(ConnectionData* connection, Room* room, QString type, QString message) +PostMessageJob::PostMessageJob(ConnectionData* connection, QString roomId, + QString type, QString message) : BaseJob(connection, JobHttpType::PostJob, "PostMessageJob", - QString("_matrix/client/r0/rooms/%1/send/m.room.message").arg(room->id()), + QString("_matrix/client/r0/rooms/%1/send/m.room.message").arg(roomId), Query(), Data({ { "msgtype", type }, { "body", message } })) , d(new Private) diff --git a/jobs/postmessagejob.h b/jobs/postmessagejob.h index 2e1989fd..eba3da51 100644 --- a/jobs/postmessagejob.h +++ b/jobs/postmessagejob.h @@ -22,11 +22,11 @@ namespace QMatrixClient { - class Room; class PostMessageJob: public BaseJob { public: - PostMessageJob(ConnectionData* connection, Room* room, QString type, QString message); + PostMessageJob(ConnectionData* connection, QString roomId, + QString type, QString message); virtual ~PostMessageJob(); //bool success(); diff --git a/jobs/roommessagesjob.cpp b/jobs/roommessagesjob.cpp index 7a0bc756..fd972eaf 100644 --- a/jobs/roommessagesjob.cpp +++ b/jobs/roommessagesjob.cpp @@ -17,7 +17,7 @@ */ #include "roommessagesjob.h" -#include "../room.h" +#include "../util.h" #include <QtCore/QJsonArray> @@ -32,12 +32,13 @@ class RoomMessagesJob::Private QString end; }; -RoomMessagesJob::RoomMessagesJob(ConnectionData* data, Room* room, QString from, FetchDirectory dir, int limit) +RoomMessagesJob::RoomMessagesJob(ConnectionData* data, QString roomId, + QString from, int limit, FetchDirection dir) : BaseJob(data, JobHttpType::GetJob, "RoomMessagesJob", - QString("/_matrix/client/r0/rooms/%1/messages").arg(room->id()), + QString("/_matrix/client/r0/rooms/%1/messages").arg(roomId), Query( { { "from", from } - , { "dir", dir == FetchDirectory::Backwards ? "b" : "f" } + , { "dir", dir == FetchDirection::Backward ? "b" : "f" } , { "limit", QString::number(limit) } })) , d(new Private) diff --git a/jobs/roommessagesjob.h b/jobs/roommessagesjob.h index fd6a131d..af7d65df 100644 --- a/jobs/roommessagesjob.h +++ b/jobs/roommessagesjob.h @@ -24,15 +24,14 @@ namespace QMatrixClient { - class Room; - - enum class FetchDirectory { Backwards, Forward }; + enum class FetchDirection { Backward, Forward }; class RoomMessagesJob: public BaseJob { public: - RoomMessagesJob(ConnectionData* data, Room* room, QString from, - FetchDirectory dir = FetchDirectory::Backwards, int limit=10); + RoomMessagesJob(ConnectionData* data, QString roomId, + QString from, int limit = 10, + FetchDirection dir = FetchDirection::Backward); virtual ~RoomMessagesJob(); Events releaseEvents(); @@ -36,7 +36,9 @@ #include "events/roommemberevent.h" #include "events/typingevent.h" #include "events/receiptevent.h" +#include "jobs/postmessagejob.h" #include "jobs/roommessagesjob.h" +#include "jobs/postreceiptjob.h" using namespace QMatrixClient; @@ -90,7 +92,7 @@ class Room::Private void renameMember(User* u, QString oldName); void removeMember(User* u); - void getPreviousContent(); + void getPreviousContent(int limit = 10); bool isEventNotable(const Event* e) const; @@ -269,7 +271,8 @@ void Room::markMessagesAsRead(QString uptoEventId) { if ((*markers.second)->senderId() != connection()->userId()) { - connection()->postReceipt(this, *markers.second); + connection()->callApi<PostReceiptJob>(this->id(), + (*markers.second)->id()); break; } } @@ -490,16 +493,22 @@ void Room::updateData(SyncRoomData& data) } } -void Room::getPreviousContent() +void Room::postMessage(QString type, QString content) { - d->getPreviousContent(); + connection()->callApi<PostMessageJob>(id(), type, content); } -void Room::Private::getPreviousContent() +void Room::getPreviousContent(int limit) +{ + d->getPreviousContent(limit); +} + +void Room::Private::getPreviousContent(int limit) { if( !roomMessagesJob ) { - roomMessagesJob = connection->getMessages(q, prevBatch); + roomMessagesJob = + connection->callApi<RoomMessagesJob>(id, prevBatch, limit); connect( roomMessagesJob, &RoomMessagesJob::result, [=]() { if( !roomMessagesJob->error() ) { @@ -93,7 +93,8 @@ namespace QMatrixClient MemberSorter memberSorter() const; public slots: - void getPreviousContent(); + void postMessage(QString msgType, QString msgContent); + void getPreviousContent(int limit = 10); void userRenamed(User* user, QString oldName); signals: |