2017-11-20 15:33:49 +01: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"
|
|
|
|
|
|
|
|
"github.com/matrix-org/dendrite/clientapi/jsonerror"
|
2021-11-24 11:45:23 +01:00
|
|
|
federationAPI "github.com/matrix-org/dendrite/federationapi/api"
|
2018-08-08 17:17:09 +02:00
|
|
|
roomserverAPI "github.com/matrix-org/dendrite/roomserver/api"
|
2020-12-02 18:41:00 +01:00
|
|
|
"github.com/matrix-org/dendrite/setup/config"
|
2017-11-20 15:33:49 +01:00
|
|
|
"github.com/matrix-org/gomatrix"
|
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
|
|
|
"github.com/matrix-org/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
// RoomAliasToID converts the queried alias into a room ID and returns it
|
|
|
|
func RoomAliasToID(
|
|
|
|
httpReq *http.Request,
|
2022-05-17 14:23:35 +02:00
|
|
|
federation federationAPI.FederationClient,
|
2020-08-10 15:18:04 +02:00
|
|
|
cfg *config.FederationAPI,
|
2022-05-05 20:30:38 +02:00
|
|
|
rsAPI roomserverAPI.FederationRoomserverAPI,
|
2021-11-24 11:45:23 +01:00
|
|
|
senderAPI federationAPI.FederationInternalAPI,
|
2017-11-20 15:33:49 +01:00
|
|
|
) util.JSONResponse {
|
2018-08-07 15:41:50 +02:00
|
|
|
roomAlias := httpReq.FormValue("room_alias")
|
2017-11-20 15:33:49 +01:00
|
|
|
if roomAlias == "" {
|
|
|
|
return util.JSONResponse{
|
2018-03-13 16:55:45 +01:00
|
|
|
Code: http.StatusBadRequest,
|
2017-11-20 15:33:49 +01:00
|
|
|
JSON: jsonerror.BadJSON("Must supply room alias parameter."),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_, domain, err := gomatrixserverlib.SplitID('#', roomAlias)
|
|
|
|
if err != nil {
|
|
|
|
return util.JSONResponse{
|
2018-03-13 16:55:45 +01:00
|
|
|
Code: http.StatusBadRequest,
|
2017-11-20 15:33:49 +01:00
|
|
|
JSON: jsonerror.BadJSON("Room alias must be in the form '#localpart:domain'"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var resp gomatrixserverlib.RespDirectory
|
|
|
|
|
|
|
|
if domain == cfg.Matrix.ServerName {
|
2021-03-03 18:00:31 +01:00
|
|
|
queryReq := &roomserverAPI.GetRoomIDForAliasRequest{
|
|
|
|
Alias: roomAlias,
|
|
|
|
IncludeAppservices: true,
|
|
|
|
}
|
|
|
|
queryRes := &roomserverAPI.GetRoomIDForAliasResponse{}
|
|
|
|
if err = rsAPI.GetRoomIDForAlias(httpReq.Context(), queryReq, queryRes); err != nil {
|
2020-03-02 17:20:44 +01:00
|
|
|
util.GetLogger(httpReq.Context()).WithError(err).Error("aliasAPI.GetRoomIDForAlias failed")
|
|
|
|
return jsonerror.InternalServerError()
|
2017-11-20 15:33:49 +01:00
|
|
|
}
|
|
|
|
|
2018-08-07 15:41:50 +02:00
|
|
|
if queryRes.RoomID != "" {
|
2021-11-24 11:45:23 +01:00
|
|
|
serverQueryReq := federationAPI.QueryJoinedHostServerNamesInRoomRequest{RoomID: queryRes.RoomID}
|
|
|
|
var serverQueryRes federationAPI.QueryJoinedHostServerNamesInRoomResponse
|
2019-12-20 14:24:57 +01:00
|
|
|
if err = senderAPI.QueryJoinedHostServerNamesInRoom(httpReq.Context(), &serverQueryReq, &serverQueryRes); err != nil {
|
2020-03-02 17:20:44 +01:00
|
|
|
util.GetLogger(httpReq.Context()).WithError(err).Error("senderAPI.QueryJoinedHostServerNamesInRoom failed")
|
|
|
|
return jsonerror.InternalServerError()
|
2019-12-20 14:24:57 +01:00
|
|
|
}
|
|
|
|
|
2017-11-20 15:33:49 +01:00
|
|
|
resp = gomatrixserverlib.RespDirectory{
|
|
|
|
RoomID: queryRes.RoomID,
|
2019-12-20 14:24:57 +01:00
|
|
|
Servers: serverQueryRes.ServerNames,
|
2017-11-20 15:33:49 +01:00
|
|
|
}
|
|
|
|
} else {
|
2018-08-07 15:41:50 +02:00
|
|
|
// If no alias was found, return an error
|
2017-11-20 15:33:49 +01:00
|
|
|
return util.JSONResponse{
|
2018-03-13 16:55:45 +01:00
|
|
|
Code: http.StatusNotFound,
|
2017-11-20 15:33:49 +01:00
|
|
|
JSON: jsonerror.NotFound(fmt.Sprintf("Room alias %s not found", roomAlias)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
resp, err = federation.LookupRoomAlias(httpReq.Context(), domain, roomAlias)
|
|
|
|
if err != nil {
|
|
|
|
switch x := err.(type) {
|
|
|
|
case gomatrix.HTTPError:
|
2018-03-13 16:55:45 +01:00
|
|
|
if x.Code == http.StatusNotFound {
|
2017-11-20 15:33:49 +01:00
|
|
|
return util.JSONResponse{
|
2018-03-13 16:55:45 +01:00
|
|
|
Code: http.StatusNotFound,
|
2017-11-20 15:33:49 +01:00
|
|
|
JSON: jsonerror.NotFound("Room alias not found"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// TODO: Return 502 if the remote server errored.
|
|
|
|
// TODO: Return 504 if the remote server timed out.
|
2020-03-02 17:20:44 +01:00
|
|
|
util.GetLogger(httpReq.Context()).WithError(err).Error("federation.LookupRoomAlias failed")
|
|
|
|
return jsonerror.InternalServerError()
|
2017-11-20 15:33:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return util.JSONResponse{
|
2018-03-13 16:55:45 +01:00
|
|
|
Code: http.StatusOK,
|
2017-11-20 15:33:49 +01:00
|
|
|
JSON: resp,
|
|
|
|
}
|
|
|
|
}
|