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"
|
|
|
|
|
|
|
|
"github.com/matrix-org/dendrite/clientapi/httputil"
|
|
|
|
"github.com/matrix-org/dendrite/clientapi/jsonerror"
|
|
|
|
"github.com/matrix-org/dendrite/common/config"
|
2019-12-20 07:24:57 -06:00
|
|
|
federationSenderAPI "github.com/matrix-org/dendrite/federationsender/api"
|
2018-08-08 10:17:09 -05:00
|
|
|
roomserverAPI "github.com/matrix-org/dendrite/roomserver/api"
|
2017-11-20 08:33:49 -06: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,
|
|
|
|
federation *gomatrixserverlib.FederationClient,
|
|
|
|
cfg config.Dendrite,
|
2018-08-08 10:17:09 -05:00
|
|
|
aliasAPI roomserverAPI.RoomserverAliasAPI,
|
2019-12-20 07:24:57 -06:00
|
|
|
senderAPI federationSenderAPI.FederationSenderQueryAPI,
|
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,
|
2017-11-20 08:33:49 -06:00
|
|
|
JSON: jsonerror.BadJSON("Must supply room alias parameter."),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_, domain, err := gomatrixserverlib.SplitID('#', roomAlias)
|
|
|
|
if err != nil {
|
|
|
|
return util.JSONResponse{
|
2018-03-13 10:55:45 -05:00
|
|
|
Code: http.StatusBadRequest,
|
2017-11-20 08:33:49 -06:00
|
|
|
JSON: jsonerror.BadJSON("Room alias must be in the form '#localpart:domain'"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var resp gomatrixserverlib.RespDirectory
|
|
|
|
|
|
|
|
if domain == cfg.Matrix.ServerName {
|
2018-08-08 10:17:09 -05:00
|
|
|
queryReq := roomserverAPI.GetRoomIDForAliasRequest{Alias: roomAlias}
|
|
|
|
var queryRes roomserverAPI.GetRoomIDForAliasResponse
|
2018-05-30 07:43:13 -05:00
|
|
|
if err = aliasAPI.GetRoomIDForAlias(httpReq.Context(), &queryReq, &queryRes); err != nil {
|
2017-11-20 08:33:49 -06:00
|
|
|
return httputil.LogThenError(httpReq, err)
|
|
|
|
}
|
|
|
|
|
2018-08-07 08:41:50 -05:00
|
|
|
if queryRes.RoomID != "" {
|
2019-12-20 07:24:57 -06:00
|
|
|
serverQueryReq := federationSenderAPI.QueryJoinedHostServerNamesInRoomRequest{RoomID: queryRes.RoomID}
|
|
|
|
var serverQueryRes federationSenderAPI.QueryJoinedHostServerNamesInRoomResponse
|
|
|
|
if err = senderAPI.QueryJoinedHostServerNamesInRoom(httpReq.Context(), &serverQueryReq, &serverQueryRes); err != nil {
|
|
|
|
return httputil.LogThenError(httpReq, err)
|
|
|
|
}
|
|
|
|
|
2017-11-20 08:33:49 -06:00
|
|
|
resp = gomatrixserverlib.RespDirectory{
|
|
|
|
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,
|
2017-11-20 08:33:49 -06: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 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,
|
2017-11-20 08:33:49 -06: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.
|
|
|
|
return httputil.LogThenError(httpReq, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return util.JSONResponse{
|
2018-03-13 10:55:45 -05:00
|
|
|
Code: http.StatusOK,
|
2017-11-20 08:33:49 -06:00
|
|
|
JSON: resp,
|
|
|
|
}
|
|
|
|
}
|