From a6d47d2fa97745718b9d3a99c39d3516b9aa06f0 Mon Sep 17 00:00:00 2001 From: Remi Reuvekamp Date: Wed, 15 Nov 2017 21:57:13 +0100 Subject: [PATCH] Implement new ReserveRoomID in CreateRoom --- .../dendrite/clientapi/routing/createroom.go | 22 +++++++++++++++---- .../dendrite/clientapi/routing/routing.go | 2 +- .../dendrite/roomserver/api/query.go | 7 ++++++ 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/src/github.com/matrix-org/dendrite/clientapi/routing/createroom.go b/src/github.com/matrix-org/dendrite/clientapi/routing/createroom.go index 078a7319b..17941d202 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/routing/createroom.go +++ b/src/github.com/matrix-org/dendrite/clientapi/routing/createroom.go @@ -23,7 +23,6 @@ import ( "github.com/matrix-org/dendrite/roomserver/api" - log "github.com/sirupsen/logrus" "github.com/matrix-org/dendrite/clientapi/auth/authtypes" "github.com/matrix-org/dendrite/clientapi/auth/storage/accounts" "github.com/matrix-org/dendrite/clientapi/httputil" @@ -33,6 +32,7 @@ import ( "github.com/matrix-org/dendrite/common/config" "github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/util" + log "github.com/sirupsen/logrus" ) // https://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-createroom @@ -90,10 +90,24 @@ type fledglingEvent struct { func CreateRoom(req *http.Request, device *authtypes.Device, cfg config.Dendrite, producer *producers.RoomserverProducer, accountDB *accounts.Database, aliasAPI api.RoomserverAliasAPI, + queryAPI api.RoomserverQueryAPI, ) util.JSONResponse { - // TODO (#267): Check room ID doesn't clash with an existing one, and we - // probably shouldn't be using pseudo-random strings, maybe GUIDs? - roomID := fmt.Sprintf("!%s:%s", util.RandomString(16), cfg.Matrix.ServerName) + // Generate a room ID and reserve it. + // Keep trying until we have one which is unused. + var roomID string + for roomID == "" { + checkRoomID := util.RandomString(16) + checkRoomID = fmt.Sprintf("!%s:%s", checkRoomID, cfg.Matrix.ServerName) + + queryReq := api.QueryReserveRoomIDRequest{RoomID: checkRoomID} + var queryResp api.QueryReserveRoomIDResponse + queryAPI.QueryReserveRoomID(req.Context(), &queryReq, &queryResp) + + if queryResp.Success { + roomID = checkRoomID + } + } + return createRoom(req, device, cfg, roomID, producer, accountDB, aliasAPI) } diff --git a/src/github.com/matrix-org/dendrite/clientapi/routing/routing.go b/src/github.com/matrix-org/dendrite/clientapi/routing/routing.go index 0b9e4172a..586646937 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/routing/routing.go +++ b/src/github.com/matrix-org/dendrite/clientapi/routing/routing.go @@ -71,7 +71,7 @@ func Setup( r0mux.Handle("/createRoom", common.MakeAuthAPI("createRoom", deviceDB, func(req *http.Request, device *authtypes.Device) util.JSONResponse { - return CreateRoom(req, device, cfg, producer, accountDB, aliasAPI) + return CreateRoom(req, device, cfg, producer, accountDB, aliasAPI, queryAPI) }), ).Methods("POST", "OPTIONS") r0mux.Handle("/join/{roomIDOrAlias}", diff --git a/src/github.com/matrix-org/dendrite/roomserver/api/query.go b/src/github.com/matrix-org/dendrite/roomserver/api/query.go index 36961d383..578cfe444 100644 --- a/src/github.com/matrix-org/dendrite/roomserver/api/query.go +++ b/src/github.com/matrix-org/dendrite/roomserver/api/query.go @@ -209,6 +209,13 @@ type RoomserverQueryAPI interface { request *QueryServerAllowedToSeeEventRequest, response *QueryServerAllowedToSeeEventResponse, ) error + + // Query if a room ID is available and reserve it. + QueryReserveRoomID( + ctx context.Context, + request *QueryReserveRoomIDRequest, + response *QueryReserveRoomIDResponse, + ) error } // RoomserverQueryLatestEventsAndStatePath is the HTTP path for the QueryLatestEventsAndState API.