Implement alias removal

This commit is contained in:
Brendan Abolivier 2017-07-27 12:08:07 +01:00
parent a5d8facc8d
commit 1ed078ac85
No known key found for this signature in database
GPG key ID: 8EF1500759F70623
4 changed files with 99 additions and 0 deletions

View file

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

View file

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

View file

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

View file

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