mirror of
https://github.com/matrix-org/dendrite
synced 2024-11-05 23:48:58 +01:00
4b09f445c9
* Initial pass at refactoring config (not finished) * Don't forget current state and EDU servers * More shifting around * Update server key API tests * Fix roomserver test * Fix more tests * Further tweaks * Fix current state server test (sort of) * Maybe fix appservices * Fix client API test * Include database connection string in database options * Fix sync API build * Update config test * Fix unit tests * Fix federation sender build * Fix gobind build * Set Listen address for all services in HTTP monolith mode * Validate config, reinstate appservice derived in directory, tweaks * Tweak federation API test * Set MaxOpenConnections/MaxIdleConnections to previous values * Update generate-config
105 lines
3.6 KiB
Go
105 lines
3.6 KiB
Go
// 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"
|
|
federationSenderAPI "github.com/matrix-org/dendrite/federationsender/api"
|
|
"github.com/matrix-org/dendrite/internal/config"
|
|
roomserverAPI "github.com/matrix-org/dendrite/roomserver/api"
|
|
"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.FederationAPI,
|
|
rsAPI roomserverAPI.RoomserverInternalAPI,
|
|
senderAPI federationSenderAPI.FederationSenderInternalAPI,
|
|
) util.JSONResponse {
|
|
roomAlias := httpReq.FormValue("room_alias")
|
|
if roomAlias == "" {
|
|
return util.JSONResponse{
|
|
Code: http.StatusBadRequest,
|
|
JSON: jsonerror.BadJSON("Must supply room alias parameter."),
|
|
}
|
|
}
|
|
_, domain, err := gomatrixserverlib.SplitID('#', roomAlias)
|
|
if err != nil {
|
|
return util.JSONResponse{
|
|
Code: http.StatusBadRequest,
|
|
JSON: jsonerror.BadJSON("Room alias must be in the form '#localpart:domain'"),
|
|
}
|
|
}
|
|
|
|
var resp gomatrixserverlib.RespDirectory
|
|
|
|
if domain == cfg.Matrix.ServerName {
|
|
queryReq := roomserverAPI.GetRoomIDForAliasRequest{Alias: roomAlias}
|
|
var queryRes roomserverAPI.GetRoomIDForAliasResponse
|
|
if err = rsAPI.GetRoomIDForAlias(httpReq.Context(), &queryReq, &queryRes); err != nil {
|
|
util.GetLogger(httpReq.Context()).WithError(err).Error("aliasAPI.GetRoomIDForAlias failed")
|
|
return jsonerror.InternalServerError()
|
|
}
|
|
|
|
if queryRes.RoomID != "" {
|
|
serverQueryReq := federationSenderAPI.QueryJoinedHostServerNamesInRoomRequest{RoomID: queryRes.RoomID}
|
|
var serverQueryRes federationSenderAPI.QueryJoinedHostServerNamesInRoomResponse
|
|
if err = senderAPI.QueryJoinedHostServerNamesInRoom(httpReq.Context(), &serverQueryReq, &serverQueryRes); err != nil {
|
|
util.GetLogger(httpReq.Context()).WithError(err).Error("senderAPI.QueryJoinedHostServerNamesInRoom failed")
|
|
return jsonerror.InternalServerError()
|
|
}
|
|
|
|
resp = gomatrixserverlib.RespDirectory{
|
|
RoomID: queryRes.RoomID,
|
|
Servers: serverQueryRes.ServerNames,
|
|
}
|
|
} else {
|
|
// If no alias was found, return an error
|
|
return util.JSONResponse{
|
|
Code: http.StatusNotFound,
|
|
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:
|
|
if x.Code == http.StatusNotFound {
|
|
return util.JSONResponse{
|
|
Code: http.StatusNotFound,
|
|
JSON: jsonerror.NotFound("Room alias not found"),
|
|
}
|
|
}
|
|
}
|
|
// TODO: Return 502 if the remote server errored.
|
|
// TODO: Return 504 if the remote server timed out.
|
|
util.GetLogger(httpReq.Context()).WithError(err).Error("federation.LookupRoomAlias failed")
|
|
return jsonerror.InternalServerError()
|
|
}
|
|
}
|
|
|
|
return util.JSONResponse{
|
|
Code: http.StatusOK,
|
|
JSON: resp,
|
|
}
|
|
}
|