mirror of
https://github.com/matrix-org/dendrite.git
synced 2025-12-13 01:43:09 -06:00
Implement alias removal
This commit is contained in:
parent
a5d8facc8d
commit
1ed078ac85
|
|
@ -95,6 +95,7 @@ func DirectoryRoom(
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetLocalAlias implements PUT /directory/room/{roomAlias}
|
// SetLocalAlias implements PUT /directory/room/{roomAlias}
|
||||||
|
// TODO: Check if the user has the power level to set an alias
|
||||||
func SetLocalAlias(
|
func SetLocalAlias(
|
||||||
req *http.Request,
|
req *http.Request,
|
||||||
device *authtypes.Device,
|
device *authtypes.Device,
|
||||||
|
|
@ -144,3 +145,27 @@ func SetLocalAlias(
|
||||||
JSON: struct{}{},
|
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{}{},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -120,6 +120,13 @@ func Setup(
|
||||||
}),
|
}),
|
||||||
).Methods("PUT", "OPTIONS")
|
).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",
|
r0mux.Handle("/logout",
|
||||||
common.MakeAuthAPI("logout", deviceDB, func(req *http.Request, device *authtypes.Device) util.JSONResponse {
|
common.MakeAuthAPI("logout", deviceDB, func(req *http.Request, device *authtypes.Device) util.JSONResponse {
|
||||||
return readers.Logout(req, deviceDB, device)
|
return readers.Logout(req, deviceDB, device)
|
||||||
|
|
|
||||||
|
|
@ -128,6 +128,17 @@ type GetAliasRoomIDResponse struct {
|
||||||
RoomID string `json:"room_id"`
|
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.
|
// RoomserverQueryAPI is used to query information from the room server.
|
||||||
type RoomserverQueryAPI interface {
|
type RoomserverQueryAPI interface {
|
||||||
// Query the latest events and state for a room from the room server.
|
// Query the latest events and state for a room from the room server.
|
||||||
|
|
@ -159,6 +170,12 @@ type RoomserverQueryAPI interface {
|
||||||
request *GetAliasRoomIDRequest,
|
request *GetAliasRoomIDRequest,
|
||||||
response *GetAliasRoomIDResponse,
|
response *GetAliasRoomIDResponse,
|
||||||
) error
|
) error
|
||||||
|
|
||||||
|
// Remove a room alias
|
||||||
|
RemoveRoomAlias(
|
||||||
|
request *RemoveRoomAliasRequest,
|
||||||
|
response *RemoveRoomAliasResponse,
|
||||||
|
) error
|
||||||
}
|
}
|
||||||
|
|
||||||
// RoomserverQueryLatestEventsAndStatePath is the HTTP path for the QueryLatestEventsAndState API.
|
// 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.
|
// RoomserverGetAliasRoomIDPath is the HTTP path for the GetAliasRoomID API.
|
||||||
const RoomserverGetAliasRoomIDPath = "/api/roomserver/getAliasRoomID"
|
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.
|
// NewRoomserverQueryAPIHTTP creates a RoomserverQueryAPI implemented by talking to a HTTP POST API.
|
||||||
// If httpClient is nil then it uses the http.DefaultClient
|
// If httpClient is nil then it uses the http.DefaultClient
|
||||||
func NewRoomserverQueryAPIHTTP(roomserverURL string, httpClient *http.Client) RoomserverQueryAPI {
|
func NewRoomserverQueryAPIHTTP(roomserverURL string, httpClient *http.Client) RoomserverQueryAPI {
|
||||||
|
|
@ -235,6 +255,15 @@ func (h *httpRoomserverQueryAPI) GetAliasRoomID(
|
||||||
return postJSON(h.httpClient, apiURL, request, response)
|
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 {
|
func postJSON(httpClient *http.Client, apiURL string, request, response interface{}) error {
|
||||||
jsonBytes, err := json.Marshal(request)
|
jsonBytes, err := json.Marshal(request)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
|
|
@ -212,6 +212,30 @@ func (r *RoomserverQueryAPI) GetAliasRoomID(
|
||||||
return nil
|
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 {
|
type roomAliasesContent struct {
|
||||||
Aliases []string `json:"aliases"`
|
Aliases []string `json:"aliases"`
|
||||||
}
|
}
|
||||||
|
|
@ -392,4 +416,18 @@ func (r *RoomserverQueryAPI) SetupHTTP(servMux *http.ServeMux) {
|
||||||
return util.JSONResponse{Code: 200, JSON: &response}
|
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}
|
||||||
|
}),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue