From 26b050fad7c70fd203932da80cec7749ec04e6ad Mon Sep 17 00:00:00 2001 From: Brendan Abolivier Date: Thu, 27 Jul 2017 10:55:42 +0100 Subject: [PATCH] Check if alias already exists --- .../dendrite/clientapi/readers/directory.go | 7 +++++++ .../matrix-org/dendrite/roomserver/api/query.go | 5 ++++- .../matrix-org/dendrite/roomserver/query/query.go | 14 ++++++++++++-- 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/github.com/matrix-org/dendrite/clientapi/readers/directory.go b/src/github.com/matrix-org/dendrite/clientapi/readers/directory.go index 68f59a1a5..1651fa889 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/readers/directory.go +++ b/src/github.com/matrix-org/dendrite/clientapi/readers/directory.go @@ -113,6 +113,13 @@ func SetLocalAlias( return httputil.LogThenError(req, err) } + if queryRes.AliasExists { + return util.JSONResponse{ + Code: 409, + JSON: jsonerror.Unknown("The alias " + alias + " already exists."), + } + } + return util.JSONResponse{ Code: 200, JSON: struct{}{}, 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 8f77b07bf..dc2acaacf 100644 --- a/src/github.com/matrix-org/dendrite/roomserver/api/query.go +++ b/src/github.com/matrix-org/dendrite/roomserver/api/query.go @@ -111,7 +111,10 @@ type SetRoomAliasRequest struct { } // SetRoomAliasResponse is a response to SetRoomAlias -type SetRoomAliasResponse struct{} +type SetRoomAliasResponse struct { + // Does the alias already refer to a room? + AliasExists bool `json:"alias_exists"` +} // RoomserverQueryAPI is used to query information from the room server. type RoomserverQueryAPI interface { diff --git a/src/github.com/matrix-org/dendrite/roomserver/query/query.go b/src/github.com/matrix-org/dendrite/roomserver/query/query.go index deb339830..0b72f43aa 100644 --- a/src/github.com/matrix-org/dendrite/roomserver/query/query.go +++ b/src/github.com/matrix-org/dendrite/roomserver/query/query.go @@ -172,8 +172,19 @@ func (r *RoomserverQueryAPI) SetRoomAlias( request *api.SetRoomAliasRequest, response *api.SetRoomAliasResponse, ) error { + // Check if the alias isn't already referring to a room + roomID, err := r.DB.GetRoomIDFromAlias(request.Alias) + if err != nil { + return err + } + if len(roomID) > 0 { + // If the alias already exists, stop the process + response.AliasExists = true + return nil + } + response.AliasExists = false + // Save the new alias - // TODO: Check if alias already exists if err := r.DB.SetRoomAlias(request.Alias, request.RoomID); err != nil { return err } @@ -183,7 +194,6 @@ func (r *RoomserverQueryAPI) SetRoomAlias( return err } - // We don't need to return anything in the response, so we don't edit it return nil }