summary refs log tree commit diff
path: root/src/MatrixClient.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/MatrixClient.cc')
-rw-r--r--src/MatrixClient.cc78
1 files changed, 77 insertions, 1 deletions
diff --git a/src/MatrixClient.cc b/src/MatrixClient.cc

index 981a30c2..bd43efd8 100644 --- a/src/MatrixClient.cc +++ b/src/MatrixClient.cc
@@ -463,6 +463,40 @@ MatrixClient::onMessagesResponse(QNetworkReply *reply) } 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) { switch (static_cast<Endpoint>(reply->property("endpoint").toInt())) { @@ -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<int>(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<int>(Endpoint::LeaveRoom)); +}