Check if alias already exists

This commit is contained in:
Brendan Abolivier 2017-07-27 10:55:42 +01:00
parent 61adfa125c
commit 26b050fad7
No known key found for this signature in database
GPG key ID: 8EF1500759F70623
3 changed files with 23 additions and 3 deletions

View file

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

View file

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

View file

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