diff options
author | Alex Chen <Cnly@users.noreply.github.com> | 2019-10-02 00:09:47 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-10-02 00:09:47 +0800 |
commit | e239fb10f36662d38ab9d1cad5a0bc65518c1704 (patch) | |
tree | 8cbaad35e3ea31bd6198e8da923e0b918d73e6b9 /clientapi | |
parent | 49fd47c86313f036da3a9549e7d14def166f4ea7 (diff) |
Add missing servers field in /directory/room/:alias response (#732)
Diffstat (limited to 'clientapi')
-rw-r--r-- | clientapi/clientapi.go | 4 | ||||
-rw-r--r-- | clientapi/routing/directory.go | 78 | ||||
-rw-r--r-- | clientapi/routing/routing.go | 4 |
3 files changed, 54 insertions, 32 deletions
diff --git a/clientapi/clientapi.go b/clientapi/clientapi.go index 5b6e21c8..f3f3e08c 100644 --- a/clientapi/clientapi.go +++ b/clientapi/clientapi.go @@ -23,6 +23,7 @@ import ( "github.com/matrix-org/dendrite/clientapi/routing" "github.com/matrix-org/dendrite/common/basecomponent" "github.com/matrix-org/dendrite/common/transactions" + federationSenderAPI "github.com/matrix-org/dendrite/federationsender/api" roomserverAPI "github.com/matrix-org/dendrite/roomserver/api" typingServerAPI "github.com/matrix-org/dendrite/typingserver/api" "github.com/matrix-org/gomatrixserverlib" @@ -43,6 +44,7 @@ func SetupClientAPIComponent( typingInputAPI typingServerAPI.TypingServerInputAPI, asAPI appserviceAPI.AppServiceQueryAPI, transactionsCache *transactions.Cache, + fedSenderAPI federationSenderAPI.FederationSenderQueryAPI, ) { roomserverProducer := producers.NewRoomserverProducer(inputAPI) typingProducer := producers.NewTypingServerProducer(typingInputAPI) @@ -67,6 +69,6 @@ func SetupClientAPIComponent( routing.Setup( base.APIMux, *base.Cfg, roomserverProducer, queryAPI, aliasAPI, asAPI, accountsDB, deviceDB, federation, *keyRing, userUpdateProducer, - syncProducer, typingProducer, transactionsCache, + syncProducer, typingProducer, transactionsCache, fedSenderAPI, ) } diff --git a/clientapi/routing/directory.go b/clientapi/routing/directory.go index 0d91d042..574b275d 100644 --- a/clientapi/routing/directory.go +++ b/clientapi/routing/directory.go @@ -22,12 +22,24 @@ import ( "github.com/matrix-org/dendrite/clientapi/httputil" "github.com/matrix-org/dendrite/clientapi/jsonerror" "github.com/matrix-org/dendrite/common/config" + federationSenderAPI "github.com/matrix-org/dendrite/federationsender/api" roomserverAPI "github.com/matrix-org/dendrite/roomserver/api" - "github.com/matrix-org/gomatrix" "github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/util" ) +type roomDirectoryResponse struct { + RoomID string `json:"room_id"` + Servers []string `json:"servers"` +} + +func (r *roomDirectoryResponse) fillServers(servers []gomatrixserverlib.ServerName) { + r.Servers = make([]string, len(servers)) + for i, s := range servers { + r.Servers[i] = string(s) + } +} + // DirectoryRoom looks up a room alias func DirectoryRoom( req *http.Request, @@ -35,6 +47,7 @@ func DirectoryRoom( federation *gomatrixserverlib.FederationClient, cfg *config.Dendrite, rsAPI roomserverAPI.RoomserverAliasAPI, + fedSenderAPI federationSenderAPI.FederationSenderQueryAPI, ) util.JSONResponse { _, domain, err := gomatrixserverlib.SplitID('#', roomAlias) if err != nil { @@ -44,46 +57,51 @@ func DirectoryRoom( } } - if domain == cfg.Matrix.ServerName { - // Query the roomserver API to check if the alias exists locally - queryReq := roomserverAPI.GetRoomIDForAliasRequest{Alias: roomAlias} - var queryRes roomserverAPI.GetRoomIDForAliasResponse - if err = rsAPI.GetRoomIDForAlias(req.Context(), &queryReq, &queryRes); err != nil { - return httputil.LogThenError(req, err) - } + var res roomDirectoryResponse - // List any roomIDs found associated with this alias - if len(queryRes.RoomID) > 0 { - return util.JSONResponse{ - Code: http.StatusOK, - JSON: queryRes, - } - } - } else { - // Query the federation for this room alias - resp, err := federation.LookupRoomAlias(req.Context(), domain, roomAlias) - if err != nil { - switch err.(type) { - case gomatrix.HTTPError: - default: + // Query the roomserver API to check if the alias exists locally. + queryReq := roomserverAPI.GetRoomIDForAliasRequest{Alias: roomAlias} + var queryRes roomserverAPI.GetRoomIDForAliasResponse + if err = rsAPI.GetRoomIDForAlias(req.Context(), &queryReq, &queryRes); err != nil { + return httputil.LogThenError(req, err) + } + + res.RoomID = queryRes.RoomID + + if res.RoomID == "" { + // If we don't know it locally, do a federation query. + // But don't send the query to ourselves. + if domain != cfg.Matrix.ServerName { + fedRes, fedErr := federation.LookupRoomAlias(req.Context(), domain, roomAlias) + if fedErr != nil { // TODO: Return 502 if the remote server errored. // TODO: Return 504 if the remote server timed out. - return httputil.LogThenError(req, err) + return httputil.LogThenError(req, fedErr) } + res.RoomID = fedRes.RoomID + res.fillServers(fedRes.Servers) } - if len(resp.RoomID) > 0 { + + if res.RoomID == "" { return util.JSONResponse{ - Code: http.StatusOK, - JSON: resp, + Code: http.StatusNotFound, + JSON: jsonerror.NotFound( + fmt.Sprintf("Room alias %s not found", roomAlias), + ), } } + } else { + joinedHostsReq := federationSenderAPI.QueryJoinedHostServerNamesInRoomRequest{RoomID: res.RoomID} + var joinedHostsRes federationSenderAPI.QueryJoinedHostServerNamesInRoomResponse + if err = fedSenderAPI.QueryJoinedHostServerNamesInRoom(req.Context(), &joinedHostsReq, &joinedHostsRes); err != nil { + return httputil.LogThenError(req, err) + } + res.fillServers(joinedHostsRes.ServerNames) } return util.JSONResponse{ - Code: http.StatusNotFound, - JSON: jsonerror.NotFound( - fmt.Sprintf("Room alias %s not found", roomAlias), - ), + Code: http.StatusOK, + JSON: res, } } diff --git a/clientapi/routing/routing.go b/clientapi/routing/routing.go index d4b323a2..4a36661d 100644 --- a/clientapi/routing/routing.go +++ b/clientapi/routing/routing.go @@ -30,6 +30,7 @@ import ( "github.com/matrix-org/dendrite/common" "github.com/matrix-org/dendrite/common/config" "github.com/matrix-org/dendrite/common/transactions" + federationSenderAPI "github.com/matrix-org/dendrite/federationsender/api" roomserverAPI "github.com/matrix-org/dendrite/roomserver/api" "github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/util" @@ -59,6 +60,7 @@ func Setup( syncProducer *producers.SyncAPIProducer, typingProducer *producers.TypingServerProducer, transactionsCache *transactions.Cache, + federationSender federationSenderAPI.FederationSenderQueryAPI, ) { apiMux.Handle("/_matrix/client/versions", @@ -185,7 +187,7 @@ func Setup( if err != nil { return util.ErrorResponse(err) } - return DirectoryRoom(req, vars["roomAlias"], federation, &cfg, aliasAPI) + return DirectoryRoom(req, vars["roomAlias"], federation, &cfg, aliasAPI, federationSender) }), ).Methods(http.MethodGet, http.MethodOptions) |