From 227d53dff4cea8e93d8cb1347a8e17912d3b96af Mon Sep 17 00:00:00 2001 From: Anant Prakash Date: Wed, 11 Jul 2018 02:55:20 +0530 Subject: [PATCH] check that user leaves the room --- .../dendrite/federationapi/routing/leave.go | 29 +++++++++++++++++++ .../dendrite/federationapi/routing/routing.go | 2 +- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/github.com/matrix-org/dendrite/federationapi/routing/leave.go b/src/github.com/matrix-org/dendrite/federationapi/routing/leave.go index 44bfab9806..101d544462 100644 --- a/src/github.com/matrix-org/dendrite/federationapi/routing/leave.go +++ b/src/github.com/matrix-org/dendrite/federationapi/routing/leave.go @@ -14,6 +14,7 @@ package routing import ( "encoding/json" + "fmt" "net/http" "github.com/matrix-org/dendrite/clientapi/httputil" @@ -96,6 +97,7 @@ func SendLeave( request *gomatrixserverlib.FederationRequest, cfg config.Dendrite, producer *producers.RoomserverProducer, + query api.RoomserverQueryAPI, keys gomatrixserverlib.KeyRing, roomID, eventID string, ) util.JSONResponse { @@ -148,6 +150,16 @@ func SendLeave( } } + var userID string + if stateKey := event.StateKey(); stateKey != nil { + userID = *stateKey + } else { + return util.JSONResponse{ + Code: http.StatusBadRequest, + JSON: jsonerror.BadJSON("The event JSON should contain the stateKey"), + } + } + // Send the events to the room server. // We are responsible for notifying other servers that the user has left // the room, so set SendAsServer to cfg.Matrix.ServerName @@ -156,6 +168,23 @@ func SendLeave( return httputil.LogThenError(httpReq, err) } + // Check that the leave has taken effect + var response api.QueryMembershipForUserResponse + if err = query.QueryMembershipForUser( + httpReq.Context(), + &api.QueryMembershipForUserRequest{ + RoomID: event.RoomID(), + Sender: userID, + }, + &response, + ); err != nil { + return httputil.LogThenError(httpReq, err) + } + + if response.IsInRoom { + return httputil.LogThenError(httpReq, fmt.Errorf("user (user ID: %s) is still in room", userID)) + } + return util.JSONResponse{ Code: http.StatusOK, JSON: struct{}{}, diff --git a/src/github.com/matrix-org/dendrite/federationapi/routing/routing.go b/src/github.com/matrix-org/dendrite/federationapi/routing/routing.go index d52f3e63c5..fd81b0b644 100644 --- a/src/github.com/matrix-org/dendrite/federationapi/routing/routing.go +++ b/src/github.com/matrix-org/dendrite/federationapi/routing/routing.go @@ -201,7 +201,7 @@ func Setup( roomID := vars["roomID"] userID := vars["userID"] return SendLeave( - httpReq, request, cfg, producer, keys, roomID, userID, + httpReq, request, cfg, producer, query, keys, roomID, userID, ) }, )).Methods(http.MethodPut)