From 1ed078ac85210818dbfb8ebb531d5a806cca58cc Mon Sep 17 00:00:00 2001 From: Brendan Abolivier Date: Thu, 27 Jul 2017 12:08:07 +0100 Subject: [PATCH] Implement alias removal --- .../dendrite/clientapi/readers/directory.go | 25 ++++++++++++ .../dendrite/clientapi/routing/routing.go | 7 ++++ .../dendrite/roomserver/api/query.go | 29 ++++++++++++++ .../dendrite/roomserver/query/query.go | 38 +++++++++++++++++++ 4 files changed, 99 insertions(+) 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 fd5fc778f..11ed843e4 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/readers/directory.go +++ b/src/github.com/matrix-org/dendrite/clientapi/readers/directory.go @@ -95,6 +95,7 @@ func DirectoryRoom( } // SetLocalAlias implements PUT /directory/room/{roomAlias} +// TODO: Check if the user has the power level to set an alias func SetLocalAlias( req *http.Request, device *authtypes.Device, @@ -144,3 +145,27 @@ func SetLocalAlias( JSON: struct{}{}, } } + +// RemoveLocalAlias implements DELETE /directory/room/{roomAlias} +// TODO: Check if the user has the power level to remove an alias +func RemoveLocalAlias( + req *http.Request, + device *authtypes.Device, + alias string, + cfg *config.Dendrite, + queryAPI api.RoomserverQueryAPI, +) util.JSONResponse { + queryReq := api.RemoveRoomAliasRequest{ + Alias: alias, + UserID: device.UserID, + } + var queryRes api.RemoveRoomAliasResponse + if err := queryAPI.RemoveRoomAlias(&queryReq, &queryRes); err != nil { + return httputil.LogThenError(req, err) + } + + return util.JSONResponse{ + Code: 200, + JSON: struct{}{}, + } +} 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 217ba4158..c6ea524b6 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/routing/routing.go +++ b/src/github.com/matrix-org/dendrite/clientapi/routing/routing.go @@ -120,6 +120,13 @@ func Setup( }), ).Methods("PUT", "OPTIONS") + r0mux.Handle("/directory/room/{roomAlias}", + common.MakeAuthAPI("directory_room", deviceDB, func(req *http.Request, device *authtypes.Device) util.JSONResponse { + vars := mux.Vars(req) + return readers.RemoveLocalAlias(req, device, vars["roomAlias"], &cfg, queryAPI) + }), + ).Methods("DELETE") + r0mux.Handle("/logout", common.MakeAuthAPI("logout", deviceDB, func(req *http.Request, device *authtypes.Device) util.JSONResponse { return readers.Logout(req, deviceDB, device) 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 cae7ec1ae..7bfc90b8c 100644 --- a/src/github.com/matrix-org/dendrite/roomserver/api/query.go +++ b/src/github.com/matrix-org/dendrite/roomserver/api/query.go @@ -128,6 +128,17 @@ type GetAliasRoomIDResponse struct { RoomID string `json:"room_id"` } +// RemoveRoomAliasRequest is a request to RemoveRoomAlias +type RemoveRoomAliasRequest struct { + // ID of the user removing the alias + UserID string `json:"user_id"` + // The room alias to remove + Alias string `json:"alias"` +} + +// RemoveRoomAliasResponse is a response to RemoveRoomAlias +type RemoveRoomAliasResponse struct{} + // RoomserverQueryAPI is used to query information from the room server. type RoomserverQueryAPI interface { // Query the latest events and state for a room from the room server. @@ -159,6 +170,12 @@ type RoomserverQueryAPI interface { request *GetAliasRoomIDRequest, response *GetAliasRoomIDResponse, ) error + + // Remove a room alias + RemoveRoomAlias( + request *RemoveRoomAliasRequest, + response *RemoveRoomAliasResponse, + ) error } // RoomserverQueryLatestEventsAndStatePath is the HTTP path for the QueryLatestEventsAndState API. @@ -176,6 +193,9 @@ const RoomserverSetRoomAliasPath = "/api/roomserver/setRoomAlias" // RoomserverGetAliasRoomIDPath is the HTTP path for the GetAliasRoomID API. const RoomserverGetAliasRoomIDPath = "/api/roomserver/getAliasRoomID" +// RoomserverRemoveRoomAliasPath is the HTTP path for the RemoveRoomAlias API. +const RoomserverRemoveRoomAliasPath = "/api/roomserver/removeRoomAlias" + // NewRoomserverQueryAPIHTTP creates a RoomserverQueryAPI implemented by talking to a HTTP POST API. // If httpClient is nil then it uses the http.DefaultClient func NewRoomserverQueryAPIHTTP(roomserverURL string, httpClient *http.Client) RoomserverQueryAPI { @@ -235,6 +255,15 @@ func (h *httpRoomserverQueryAPI) GetAliasRoomID( return postJSON(h.httpClient, apiURL, request, response) } +// RemoveRoomAlias implements RoomserverQueryAPI +func (h *httpRoomserverQueryAPI) RemoveRoomAlias( + request *RemoveRoomAliasRequest, + response *RemoveRoomAliasResponse, +) error { + apiURL := h.roomserverURL + RoomserverRemoveRoomAliasPath + return postJSON(h.httpClient, apiURL, request, response) +} + func postJSON(httpClient *http.Client, apiURL string, request, response interface{}) error { jsonBytes, err := json.Marshal(request) if err != nil { 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 a58b586bf..0112f0951 100644 --- a/src/github.com/matrix-org/dendrite/roomserver/query/query.go +++ b/src/github.com/matrix-org/dendrite/roomserver/query/query.go @@ -212,6 +212,30 @@ func (r *RoomserverQueryAPI) GetAliasRoomID( return nil } +// RemoveRoomAlias implements api.RoomserverQueryAPI +func (r *RoomserverQueryAPI) RemoveRoomAlias( + request *api.RemoveRoomAliasRequest, + response *api.RemoveRoomAliasResponse, +) error { + // Lookup the room ID in the database + roomID, err := r.DB.GetRoomIDFromAlias(request.Alias) + if err != nil { + return err + } + + // Remove the dalias from the database + if err := r.DB.RemoveRoomAlias(request.Alias); err != nil { + return err + } + + // Send an updated m.room.aliases event + if err := r.sendUpdatedAliasesEvent(request.UserID, roomID); err != nil { + return err + } + + return nil +} + type roomAliasesContent struct { Aliases []string `json:"aliases"` } @@ -392,4 +416,18 @@ func (r *RoomserverQueryAPI) SetupHTTP(servMux *http.ServeMux) { return util.JSONResponse{Code: 200, JSON: &response} }), ) + servMux.Handle( + api.RoomserverRemoveRoomAliasPath, + common.MakeAPI("removeRoomAlias", func(req *http.Request) util.JSONResponse { + var request api.RemoveRoomAliasRequest + var response api.RemoveRoomAliasResponse + if err := json.NewDecoder(req.Body).Decode(&request); err != nil { + return util.ErrorResponse(err) + } + if err := r.RemoveRoomAlias(&request, &response); err != nil { + return util.ErrorResponse(err) + } + return util.JSONResponse{Code: 200, JSON: &response} + }), + ) }