From 3f9b829bc570d5f6353eda21ecf3d0088e4d9c50 Mon Sep 17 00:00:00 2001 From: Rohit Mohan Date: Tue, 1 Sep 2020 14:56:34 +0530 Subject: [PATCH] Public room client API changes (#1368) Signed-off-by: Rohit Mohan --- clientapi/routing/directory_public.go | 42 +++++++++++++++++++++++++++ clientapi/routing/routing.go | 2 +- sytest-whitelist | 3 ++ 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/clientapi/routing/directory_public.go b/clientapi/routing/directory_public.go index fcf3f656..bae7e49b 100644 --- a/clientapi/routing/directory_public.go +++ b/clientapi/routing/directory_public.go @@ -27,6 +27,7 @@ import ( "github.com/matrix-org/dendrite/clientapi/httputil" "github.com/matrix-org/dendrite/clientapi/jsonerror" currentstateAPI "github.com/matrix-org/dendrite/currentstateserver/api" + "github.com/matrix-org/dendrite/internal/config" roomserverAPI "github.com/matrix-org/dendrite/roomserver/api" "github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/util" @@ -41,6 +42,7 @@ type PublicRoomReq struct { Since string `json:"since,omitempty"` Limit int16 `json:"limit,omitempty"` Filter filter `json:"filter,omitempty"` + Server string `json:"server,omitempty"` } type filter struct { @@ -51,11 +53,28 @@ type filter struct { func GetPostPublicRooms( req *http.Request, rsAPI roomserverAPI.RoomserverInternalAPI, stateAPI currentstateAPI.CurrentStateInternalAPI, extRoomsProvider api.ExtraPublicRoomsProvider, + federation *gomatrixserverlib.FederationClient, + cfg *config.ClientAPI, ) util.JSONResponse { var request PublicRoomReq if fillErr := fillPublicRoomsReq(req, &request); fillErr != nil { return *fillErr } + + serverName := gomatrixserverlib.ServerName(request.Server) + + if serverName != "" && serverName != cfg.Matrix.ServerName { + res, err := federation.GetPublicRooms(req.Context(), serverName, int(request.Limit), request.Since, false, "") + if err != nil { + util.GetLogger(req.Context()).WithError(err).Error("failed to get public rooms") + return jsonerror.InternalServerError() + } + return util.JSONResponse{ + Code: http.StatusOK, + JSON: res, + } + } + response, err := publicRooms(req.Context(), request, rsAPI, stateAPI, extRoomsProvider) if err != nil { util.GetLogger(req.Context()).WithError(err).Errorf("failed to work out public rooms") @@ -98,6 +117,8 @@ func publicRooms(ctx context.Context, request PublicRoomReq, rsAPI roomserverAPI response.TotalRoomCountEstimate = len(rooms) + rooms = filterRooms(rooms, request.Filter.SearchTerms) + chunk, prev, next := sliceInto(rooms, offset, limit) if prev >= 0 { response.PrevBatch = "T" + strconv.Itoa(prev) @@ -111,6 +132,25 @@ func publicRooms(ctx context.Context, request PublicRoomReq, rsAPI roomserverAPI return &response, err } +func filterRooms(rooms []gomatrixserverlib.PublicRoom, searchTerm string) []gomatrixserverlib.PublicRoom { + if searchTerm == "" { + return rooms + } + + normalizedTerm := strings.ToLower(searchTerm) + + result := make([]gomatrixserverlib.PublicRoom, 0) + for _, room := range rooms { + if strings.Contains(strings.ToLower(room.Name), normalizedTerm) || + strings.Contains(strings.ToLower(room.Topic), normalizedTerm) || + strings.Contains(strings.ToLower(room.CanonicalAlias), normalizedTerm) { + result = append(result, room) + } + } + + return result +} + // fillPublicRoomsReq fills the Limit, Since and Filter attributes of a GET or POST request // on /publicRooms by parsing the incoming HTTP request // Filter is only filled for POST requests @@ -134,11 +174,13 @@ func fillPublicRoomsReq(httpReq *http.Request, request *PublicRoomReq) *util.JSO } request.Limit = int16(limit) request.Since = httpReq.FormValue("since") + request.Server = httpReq.FormValue("server") } else { resErr := httputil.UnmarshalJSONRequest(httpReq, request) if resErr != nil { return resErr } + request.Server = httpReq.FormValue("server") } // strip the 'T' which is only required because when sytest does pagination tests it stops diff --git a/clientapi/routing/routing.go b/clientapi/routing/routing.go index f2494dc7..24343ee1 100644 --- a/clientapi/routing/routing.go +++ b/clientapi/routing/routing.go @@ -314,7 +314,7 @@ func Setup( ).Methods(http.MethodPut, http.MethodOptions) r0mux.Handle("/publicRooms", httputil.MakeExternalAPI("public_rooms", func(req *http.Request) util.JSONResponse { - return GetPostPublicRooms(req, rsAPI, stateAPI, extRoomsProvider) + return GetPostPublicRooms(req, rsAPI, stateAPI, extRoomsProvider, federation, cfg) }), ).Methods(http.MethodGet, http.MethodPost, http.MethodOptions) diff --git a/sytest-whitelist b/sytest-whitelist index a17ed840..7ce59fef 100644 --- a/sytest-whitelist +++ b/sytest-whitelist @@ -457,3 +457,6 @@ Inbound /v1/send_leave rejects leaves from other servers Guest users can accept invites to private rooms over federation AS user (not ghost) can join room without registering If user leaves room, remote user changes device and rejoins we see update in /sync and /keys/changes +Can search public room list +Can get remote public room list +Asking for a remote rooms list, but supplying the local server's name, returns the local rooms list