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.
This commit is contained in:
Max Sandholm 2017-10-01 19:49:36 +03:00 committed by mujx
parent ea296321c9
commit 7ad45d8d64
23 changed files with 571 additions and 41 deletions

View file

@ -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<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));
}