diff options
author | Rohit Mohan <rohitmohan96@gmail.com> | 2020-09-01 14:56:34 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-09-01 10:26:34 +0100 |
commit | 3f9b829bc570d5f6353eda21ecf3d0088e4d9c50 (patch) | |
tree | 6ce9e11eb72e88fdd4404cabfbb572fce8bd2c0e /clientapi/routing/directory_public.go | |
parent | b0d2b39739551a46698222ec62a3c5fb11efd7e5 (diff) |
Public room client API changes (#1368)
Signed-off-by: Rohit Mohan <rohitmohan96@gmail.com>
Diffstat (limited to 'clientapi/routing/directory_public.go')
-rw-r--r-- | clientapi/routing/directory_public.go | 42 |
1 files changed, 42 insertions, 0 deletions
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 |