Skip to content
This repository has been archived by the owner on Nov 25, 2024. It is now read-only.

Commit

Permalink
check that user leaves the room
Browse files Browse the repository at this point in the history
  • Loading branch information
APwhitehat committed Jul 10, 2018
1 parent 1241cf6 commit 227d53d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
29 changes: 29 additions & 0 deletions src/github.com/matrix-org/dendrite/federationapi/routing/leave.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ package routing

import (
"encoding/json"
"fmt"
"net/http"

"github.com/matrix-org/dendrite/clientapi/httputil"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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{}{},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 227d53d

Please sign in to comment.