From 7ad45d8d6448c60b4c81c80fa8d6d81afd6e4a02 Mon Sep 17 00:00:00 2001 From: Max Sandholm Date: Sun, 1 Oct 2017 19:49:36 +0300 Subject: React to externally left and joined rooms, and add "leave room" button in room menu (#75) * Initial "join room" feature. * React correctly to remotely joined rooms. * Leaving rooms implemented both locally using the room menu in nheko, and reacting properly when leaving a room remotely from another client. --- src/MatrixClient.cc | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 77 insertions(+), 1 deletion(-) (limited to 'src/MatrixClient.cc') diff --git a/src/MatrixClient.cc b/src/MatrixClient.cc index 981a30c2..bd43efd8 100644 --- a/src/MatrixClient.cc +++ b/src/MatrixClient.cc @@ -462,6 +462,40 @@ MatrixClient::onMessagesResponse(QNetworkReply *reply) emit messagesRetrieved(room_id, msgs); } +void +MatrixClient::onJoinRoomResponse(QNetworkReply *reply) +{ + reply->deleteLater(); + + int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); + + if (status == 0 || status >= 400) { + qWarning() << reply->errorString(); + return; + } + + auto data = reply->readAll(); + QJsonDocument response = QJsonDocument::fromJson(data); + QString room_id = response.object()["room_id"].toString(); + emit joinedRoom(room_id); +} + +void +MatrixClient::onLeaveRoomResponse(QNetworkReply *reply) +{ + reply->deleteLater(); + + int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); + + if (status == 0 || status >= 400) { + qWarning() << reply->errorString(); + return; + } + + QString room_id = reply->property("room_id").toString(); + emit leftRoom(room_id); +} + void MatrixClient::onResponse(QNetworkReply *reply) { @@ -508,6 +542,12 @@ MatrixClient::onResponse(QNetworkReply *reply) case Endpoint::Messages: onMessagesResponse(reply); break; + case Endpoint::JoinRoom: + onJoinRoomResponse(reply); + break; + case Endpoint::LeaveRoom: + onLeaveRoomResponse(reply); + break; default: break; } @@ -571,7 +611,8 @@ void MatrixClient::sync() noexcept { QJsonObject filter{ { "room", - QJsonObject{ { "ephemeral", QJsonObject{ { "limit", 0 } } } } }, + QJsonObject{ { "include_leave", true }, + { "ephemeral", QJsonObject{ { "limit", 0 } } } } }, { "presence", QJsonObject{ { "limit", 0 } } } }; QUrlQuery query; @@ -842,3 +883,38 @@ MatrixClient::uploadImage(const QString &roomid, const QString &filename) reply->setProperty("room_id", roomid); reply->setProperty("filename", filename); } + +void +MatrixClient::joinRoom(const QString &roomIdOrAlias) +{ + QUrlQuery query; + query.addQueryItem("access_token", token_); + + QUrl endpoint(server_); + endpoint.setPath(clientApiUrl_ + QString("/join/%1").arg(roomIdOrAlias)); + endpoint.setQuery(query); + + QNetworkRequest request(endpoint); + request.setHeader(QNetworkRequest::KnownHeaders::ContentTypeHeader, "application/json"); + + QNetworkReply *reply = post(request, "{}"); + reply->setProperty("endpoint", static_cast(Endpoint::JoinRoom)); +} + +void +MatrixClient::leaveRoom(const QString &roomId) +{ + QUrlQuery query; + query.addQueryItem("access_token", token_); + + QUrl endpoint(server_); + endpoint.setPath(clientApiUrl_ + QString("/rooms/%1/leave").arg(roomId)); + endpoint.setQuery(query); + + QNetworkRequest request(endpoint); + request.setHeader(QNetworkRequest::KnownHeaders::ContentTypeHeader, "application/json"); + + QNetworkReply *reply = post(request, "{}"); + reply->setProperty("room_id", roomId); + reply->setProperty("endpoint", static_cast(Endpoint::LeaveRoom)); +} -- cgit 1.5.1