check that user leaves the room

This commit is contained in:
Anant Prakash 2018-07-11 02:55:20 +05:30
parent 1241cf66d8
commit 227d53dff4
No known key found for this signature in database
GPG key ID: C5D399F626523045
2 changed files with 30 additions and 1 deletions

View file

@ -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{}{},

View file

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