From cc972a8b941a4ca668509177a0116773e7b8ab1b Mon Sep 17 00:00:00 2001 From: Prateek Sachan Date: Thu, 26 Mar 2020 12:50:16 +0530 Subject: [PATCH] Add support for server query param in publicrooms --- publicroomsapi/directory/public_rooms.go | 17 ++++++++++++++++- publicroomsapi/publicroomsapi.go | 2 +- publicroomsapi/routing/routing.go | 9 ++++++--- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/publicroomsapi/directory/public_rooms.go b/publicroomsapi/directory/public_rooms.go index 7bd6740eb..65fac10de 100644 --- a/publicroomsapi/directory/public_rooms.go +++ b/publicroomsapi/directory/public_rooms.go @@ -21,6 +21,8 @@ import ( "sync" "time" + "github.com/matrix-org/dendrite/common/config" + "github.com/matrix-org/dendrite/clientapi/httputil" "github.com/matrix-org/dendrite/clientapi/jsonerror" "github.com/matrix-org/dendrite/publicroomsapi/storage" @@ -41,12 +43,25 @@ type filter struct { // GetPostPublicRooms implements GET and POST /publicRooms func GetPostPublicRooms( - req *http.Request, publicRoomDatabase storage.Database, + req *http.Request, cfg *config.Dendrite, server gomatrixserverlib.ServerName, + publicRoomDatabase storage.Database, fedClient *gomatrixserverlib.FederationClient, ) util.JSONResponse { var request PublicRoomReq if fillErr := fillPublicRoomsReq(req, &request); fillErr != nil { return *fillErr } + if server != "" && server != cfg.Matrix.ServerName { + //TODO Authenticate user before serving rooms from other server + fres, err := fedClient.GetPublicRooms(req.Context(), server, int(request.Limit), + request.Since, false, "") + if err != nil { + return jsonerror.InternalServerError() + } + return util.JSONResponse{ + Code: http.StatusOK, + JSON: fres, + } + } response, err := publicRooms(req.Context(), request, publicRoomDatabase) if err != nil { return jsonerror.InternalServerError() diff --git a/publicroomsapi/publicroomsapi.go b/publicroomsapi/publicroomsapi.go index 399c0cc57..3e4949d84 100644 --- a/publicroomsapi/publicroomsapi.go +++ b/publicroomsapi/publicroomsapi.go @@ -47,5 +47,5 @@ func SetupPublicRoomsAPIComponent( logrus.WithError(err).Panic("failed to start public rooms server consumer") } - routing.Setup(base.APIMux, deviceDB, publicRoomsDB, fedClient, extRoomsProvider) + routing.Setup(base.APIMux, base.Cfg, deviceDB, publicRoomsDB, fedClient, extRoomsProvider) } diff --git a/publicroomsapi/routing/routing.go b/publicroomsapi/routing/routing.go index 321b61b89..6a9d0e605 100644 --- a/publicroomsapi/routing/routing.go +++ b/publicroomsapi/routing/routing.go @@ -17,6 +17,8 @@ package routing import ( "net/http" + "github.com/matrix-org/dendrite/common/config" + "github.com/gorilla/mux" "github.com/matrix-org/dendrite/clientapi/auth" "github.com/matrix-org/dendrite/clientapi/auth/authtypes" @@ -37,7 +39,7 @@ const pathPrefixR0 = "/_matrix/client/r0" // applied: // nolint: gocyclo func Setup( - apiMux *mux.Router, deviceDB devices.Database, publicRoomsDB storage.Database, + apiMux *mux.Router, cfg *config.Dendrite, deviceDB devices.Database, publicRoomsDB storage.Database, fedClient *gomatrixserverlib.FederationClient, extRoomsProvider types.ExternalPublicRoomsProvider, ) { r0mux := apiMux.PathPrefix(pathPrefixR0).Subrouter() @@ -72,14 +74,15 @@ func Setup( if extRoomsProvider != nil { return directory.GetPostPublicRoomsWithExternal(req, publicRoomsDB, fedClient, extRoomsProvider) } - return directory.GetPostPublicRooms(req, publicRoomsDB) + server := gomatrixserverlib.ServerName(req.URL.Query().Get("server")) + return directory.GetPostPublicRooms(req, cfg, server, publicRoomsDB, fedClient) }), ).Methods(http.MethodGet, http.MethodPost, http.MethodOptions) // Federation - TODO: should this live here or in federation API? It's sure easier if it's here so here it is. apiMux.Handle("/_matrix/federation/v1/publicRooms", common.MakeExternalAPI("federation_public_rooms", func(req *http.Request) util.JSONResponse { - return directory.GetPostPublicRooms(req, publicRoomsDB) + return directory.GetPostPublicRooms(req, cfg, cfg.Matrix.ServerName, publicRoomsDB, fedClient) }), ).Methods(http.MethodGet) }