2017-11-20 08:33:49 -06:00
|
|
|
// Copyright 2017 New Vector Ltd
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package routing
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
2021-11-24 04:45:23 -06:00
|
|
|
federationAPI "github.com/matrix-org/dendrite/federationapi/api"
|
2018-08-08 10:17:09 -05:00
|
|
|
roomserverAPI "github.com/matrix-org/dendrite/roomserver/api"
|
2023-07-20 09:06:05 -05:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/types"
|
2020-12-02 11:41:00 -06:00
|
|
|
"github.com/matrix-org/dendrite/setup/config"
|
2017-11-20 08:33:49 -06:00
|
|
|
"github.com/matrix-org/gomatrix"
|
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2023-04-06 03:55:01 -05:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/fclient"
|
2023-05-09 17:46:49 -05:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/spec"
|
2017-11-20 08:33:49 -06:00
|
|
|
"github.com/matrix-org/util"
|
2023-07-20 09:06:05 -05:00
|
|
|
log "github.com/sirupsen/logrus"
|
2017-11-20 08:33:49 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// RoomAliasToID converts the queried alias into a room ID and returns it
|
|
|
|
func RoomAliasToID(
|
|
|
|
httpReq *http.Request,
|
2023-04-24 11:23:25 -05:00
|
|
|
federation fclient.FederationClient,
|
2020-08-10 08:18:04 -05:00
|
|
|
cfg *config.FederationAPI,
|
2022-05-05 13:30:38 -05:00
|
|
|
rsAPI roomserverAPI.FederationRoomserverAPI,
|
2021-11-24 04:45:23 -06:00
|
|
|
senderAPI federationAPI.FederationInternalAPI,
|
2017-11-20 08:33:49 -06:00
|
|
|
) util.JSONResponse {
|
2018-08-07 08:41:50 -05:00
|
|
|
roomAlias := httpReq.FormValue("room_alias")
|
2017-11-20 08:33:49 -06:00
|
|
|
if roomAlias == "" {
|
|
|
|
return util.JSONResponse{
|
2018-03-13 10:55:45 -05:00
|
|
|
Code: http.StatusBadRequest,
|
2023-05-09 17:46:49 -05:00
|
|
|
JSON: spec.BadJSON("Must supply room alias parameter."),
|
2017-11-20 08:33:49 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_, domain, err := gomatrixserverlib.SplitID('#', roomAlias)
|
|
|
|
if err != nil {
|
|
|
|
return util.JSONResponse{
|
2018-03-13 10:55:45 -05:00
|
|
|
Code: http.StatusBadRequest,
|
2023-05-09 17:46:49 -05:00
|
|
|
JSON: spec.BadJSON("Room alias must be in the form '#localpart:domain'"),
|
2017-11-20 08:33:49 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-06 03:55:01 -05:00
|
|
|
var resp fclient.RespDirectory
|
2017-11-20 08:33:49 -06:00
|
|
|
|
|
|
|
if domain == cfg.Matrix.ServerName {
|
2021-03-03 11:00:31 -06:00
|
|
|
queryReq := &roomserverAPI.GetRoomIDForAliasRequest{
|
|
|
|
Alias: roomAlias,
|
|
|
|
IncludeAppservices: true,
|
|
|
|
}
|
|
|
|
queryRes := &roomserverAPI.GetRoomIDForAliasResponse{}
|
|
|
|
if err = rsAPI.GetRoomIDForAlias(httpReq.Context(), queryReq, queryRes); err != nil {
|
2020-03-02 10:20:44 -06:00
|
|
|
util.GetLogger(httpReq.Context()).WithError(err).Error("aliasAPI.GetRoomIDForAlias failed")
|
2023-05-16 19:33:27 -05:00
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusInternalServerError,
|
|
|
|
JSON: spec.InternalServerError{},
|
|
|
|
}
|
2017-11-20 08:33:49 -06:00
|
|
|
}
|
|
|
|
|
2018-08-07 08:41:50 -05:00
|
|
|
if queryRes.RoomID != "" {
|
2021-11-24 04:45:23 -06:00
|
|
|
serverQueryReq := federationAPI.QueryJoinedHostServerNamesInRoomRequest{RoomID: queryRes.RoomID}
|
|
|
|
var serverQueryRes federationAPI.QueryJoinedHostServerNamesInRoomResponse
|
2019-12-20 07:24:57 -06:00
|
|
|
if err = senderAPI.QueryJoinedHostServerNamesInRoom(httpReq.Context(), &serverQueryReq, &serverQueryRes); err != nil {
|
2020-03-02 10:20:44 -06:00
|
|
|
util.GetLogger(httpReq.Context()).WithError(err).Error("senderAPI.QueryJoinedHostServerNamesInRoom failed")
|
2023-05-16 19:33:27 -05:00
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusInternalServerError,
|
|
|
|
JSON: spec.InternalServerError{},
|
|
|
|
}
|
2019-12-20 07:24:57 -06:00
|
|
|
}
|
|
|
|
|
2023-04-06 03:55:01 -05:00
|
|
|
resp = fclient.RespDirectory{
|
2017-11-20 08:33:49 -06:00
|
|
|
RoomID: queryRes.RoomID,
|
2019-12-20 07:24:57 -06:00
|
|
|
Servers: serverQueryRes.ServerNames,
|
2017-11-20 08:33:49 -06:00
|
|
|
}
|
|
|
|
} else {
|
2018-08-07 08:41:50 -05:00
|
|
|
// If no alias was found, return an error
|
2017-11-20 08:33:49 -06:00
|
|
|
return util.JSONResponse{
|
2018-03-13 10:55:45 -05:00
|
|
|
Code: http.StatusNotFound,
|
2023-05-09 17:46:49 -05:00
|
|
|
JSON: spec.NotFound(fmt.Sprintf("Room alias %s not found", roomAlias)),
|
2017-11-20 08:33:49 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2022-11-15 09:05:23 -06:00
|
|
|
resp, err = federation.LookupRoomAlias(httpReq.Context(), domain, cfg.Matrix.ServerName, roomAlias)
|
2017-11-20 08:33:49 -06:00
|
|
|
if err != nil {
|
|
|
|
switch x := err.(type) {
|
|
|
|
case gomatrix.HTTPError:
|
2018-03-13 10:55:45 -05:00
|
|
|
if x.Code == http.StatusNotFound {
|
2017-11-20 08:33:49 -06:00
|
|
|
return util.JSONResponse{
|
2018-03-13 10:55:45 -05:00
|
|
|
Code: http.StatusNotFound,
|
2023-05-09 17:46:49 -05:00
|
|
|
JSON: spec.NotFound("Room alias not found"),
|
2017-11-20 08:33:49 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// TODO: Return 502 if the remote server errored.
|
|
|
|
// TODO: Return 504 if the remote server timed out.
|
2020-03-02 10:20:44 -06:00
|
|
|
util.GetLogger(httpReq.Context()).WithError(err).Error("federation.LookupRoomAlias failed")
|
2023-05-16 19:33:27 -05:00
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusInternalServerError,
|
|
|
|
JSON: spec.InternalServerError{},
|
|
|
|
}
|
2017-11-20 08:33:49 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return util.JSONResponse{
|
2018-03-13 10:55:45 -05:00
|
|
|
Code: http.StatusOK,
|
2017-11-20 08:33:49 -06:00
|
|
|
JSON: resp,
|
|
|
|
}
|
|
|
|
}
|
2023-07-20 09:06:05 -05:00
|
|
|
|
|
|
|
// Query the immediate children of a room/space
|
|
|
|
//
|
|
|
|
// Implements /_matrix/federation/v1/hierarchy/{roomID}
|
|
|
|
func QueryRoomHierarchy(httpReq *http.Request, request *fclient.FederationRequest, roomIDStr string, rsAPI roomserverAPI.FederationRoomserverAPI) util.JSONResponse {
|
|
|
|
parsedRoomID, err := spec.NewRoomID(roomIDStr)
|
|
|
|
if err != nil {
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusNotFound,
|
|
|
|
JSON: spec.InvalidParam("room is unknown/forbidden"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
roomID := *parsedRoomID
|
|
|
|
|
|
|
|
suggestedOnly := false // Defaults to false (spec-defined)
|
|
|
|
switch httpReq.URL.Query().Get("suggested_only") {
|
|
|
|
case "true":
|
|
|
|
suggestedOnly = true
|
|
|
|
case "false":
|
|
|
|
case "": // Empty string is returned when query param is not set
|
|
|
|
default:
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
JSON: spec.InvalidParam("query parameter 'suggested_only', if set, must be 'true' or 'false'"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
walker := roomserverAPI.NewRoomHierarchyWalker(types.NewServerNameNotDevice(request.Origin()), roomID, suggestedOnly, 1)
|
2024-03-28 14:40:45 -05:00
|
|
|
discoveredRooms, inaccessibleRooms, _, err := rsAPI.QueryNextRoomHierarchyPage(httpReq.Context(), walker, -1)
|
2023-07-20 09:06:05 -05:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
switch err.(type) {
|
|
|
|
case roomserverAPI.ErrRoomUnknownOrNotAllowed:
|
|
|
|
util.GetLogger(httpReq.Context()).WithError(err).Debugln("room unknown/forbidden when handling SS room hierarchy request")
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusNotFound,
|
|
|
|
JSON: spec.NotFound("room is unknown/forbidden"),
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
log.WithError(err).Errorf("failed to fetch next page of room hierarchy (SS API)")
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: http.StatusInternalServerError,
|
|
|
|
JSON: spec.Unknown("internal server error"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(discoveredRooms) == 0 {
|
|
|
|
util.GetLogger(httpReq.Context()).Debugln("no rooms found when handling SS room hierarchy request")
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: 404,
|
|
|
|
JSON: spec.NotFound("room is unknown/forbidden"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return util.JSONResponse{
|
|
|
|
Code: 200,
|
|
|
|
JSON: fclient.RoomHierarchyResponse{
|
2024-03-28 14:40:45 -05:00
|
|
|
Room: discoveredRooms[0],
|
|
|
|
Children: discoveredRooms[1:],
|
|
|
|
InaccessibleChildren: inaccessibleRooms,
|
2023-07-20 09:06:05 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|