From f4a53ee54bb4c8129fdfc32cfc46287296863853 Mon Sep 17 00:00:00 2001 From: Brendan Abolivier Date: Thu, 10 Aug 2017 12:07:28 +0100 Subject: [PATCH] Support the since and limit parameters --- .../dendrite/clientapi/routing/routing.go | 2 +- .../clientapi/writers/room_directory.go | 26 ++++++++++++++++++- .../dendrite/roomserver/api/public_room.go | 11 ++++++-- .../roomserver/publicroom/public_room.go | 22 ++++++++++++++-- 4 files changed, 55 insertions(+), 6 deletions(-) diff --git a/src/github.com/matrix-org/dendrite/clientapi/routing/routing.go b/src/github.com/matrix-org/dendrite/clientapi/routing/routing.go index 410c9f7db..0ce65e544 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/routing/routing.go +++ b/src/github.com/matrix-org/dendrite/clientapi/routing/routing.go @@ -278,7 +278,7 @@ func Setup( // TODO: Return a list of public rooms return writers.GetPublicRooms(req, publicRoomAPI) }), - ) + ).Methods("GET", "POST", "OPTIONS") unstableMux.Handle("/thirdparty/protocols", common.MakeAPI("thirdparty_protocols", func(req *http.Request) util.JSONResponse { diff --git a/src/github.com/matrix-org/dendrite/clientapi/writers/room_directory.go b/src/github.com/matrix-org/dendrite/clientapi/writers/room_directory.go index 49a73750c..a40e8a288 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/writers/room_directory.go +++ b/src/github.com/matrix-org/dendrite/clientapi/writers/room_directory.go @@ -16,6 +16,7 @@ package writers import ( "net/http" + "strconv" "github.com/matrix-org/dendrite/clientapi/auth/authtypes" "github.com/matrix-org/dendrite/clientapi/httputil" @@ -69,11 +70,14 @@ func SetVisibility( } } -// GetPublicRooms implements GET /publicRooms +// GetPublicRooms implements GET and POST /publicRooms func GetPublicRooms( req *http.Request, publicRoomAPI api.RoomserverPublicRoomAPI, ) util.JSONResponse { queryReq := api.GetPublicRoomsRequest{} + if fillErr := fillPublicRoomsReq(req, &queryReq); fillErr != nil { + return *fillErr + } var queryRes api.GetPublicRoomsResponse if err := publicRoomAPI.GetPublicRooms(&queryReq, &queryRes); err != nil { return httputil.LogThenError(req, err) @@ -84,3 +88,23 @@ func GetPublicRooms( JSON: queryRes, } } + +func fillPublicRoomsReq(httpReq *http.Request, queryReq *api.GetPublicRoomsRequest) *util.JSONResponse { + if httpReq.Method == "GET" { + limit, err := strconv.Atoi(httpReq.FormValue("limit")) + // Atoi returns 0 and an error when trying to parse an empty string + // In that case, we want to assign 0 so we ignore the error + if err != nil && len(httpReq.FormValue("limit")) > 0 { + reqErr := httputil.LogThenError(httpReq, err) + return &reqErr + } + queryReq.Limit = int16(limit) + queryReq.Since = httpReq.FormValue("since") + } else if httpReq.Method == "POST" { + if reqErr := httputil.UnmarshalJSONRequest(httpReq, queryReq); reqErr != nil { + return reqErr + } + } + + return nil +} diff --git a/src/github.com/matrix-org/dendrite/roomserver/api/public_room.go b/src/github.com/matrix-org/dendrite/roomserver/api/public_room.go index 8f7302954..63f7afc7b 100644 --- a/src/github.com/matrix-org/dendrite/roomserver/api/public_room.go +++ b/src/github.com/matrix-org/dendrite/roomserver/api/public_room.go @@ -42,9 +42,16 @@ type GetRoomVisibilityResponse struct { } // GetPublicRoomsRequest is a request to GetPublicRooms +// TODO: Support the "server" request parameter type GetPublicRoomsRequest struct { - Limit int16 `json:"limit"` - Since string `json:"since"` + Limit int16 `json:"limit"` + Since string `json:"since"` + Filter Filter `json:"filter"` +} + +// Filter implements the Filter structure from the Matrix spec +type Filter struct { + SearchTerm string `json:"generic_search_term"` } // GetPublicRoomsResponse is a response to GetPublicRooms diff --git a/src/github.com/matrix-org/dendrite/roomserver/publicroom/public_room.go b/src/github.com/matrix-org/dendrite/roomserver/publicroom/public_room.go index 6ee6f9e9b..1598d2efc 100644 --- a/src/github.com/matrix-org/dendrite/roomserver/publicroom/public_room.go +++ b/src/github.com/matrix-org/dendrite/roomserver/publicroom/public_room.go @@ -18,6 +18,7 @@ import ( "encoding/json" "errors" "net/http" + "strconv" "github.com/matrix-org/dendrite/common" "github.com/matrix-org/dendrite/roomserver/api" @@ -108,7 +109,18 @@ func (r *RoomserverPublicRoomAPI) GetPublicRooms( req *api.GetPublicRoomsRequest, response *api.GetPublicRoomsResponse, ) error { - // TODO: Limit by req.Limit and offset by req.Since + var limit int16 + var offset int64 + + limit = req.Limit + ofst, err := strconv.Atoi(req.Since) + // Atoi returns 0 and an error when trying to parse an empty string + // In that case, we want to assign 0 so we ignore the error + if err != nil && len(req.Since) > 0 { + return err + } + offset = int64(ofst) + roomIDs, err := r.DB.GetPublicRoomIDs() if err != nil { return err @@ -133,7 +145,13 @@ func (r *RoomserverPublicRoomAPI) GetPublicRooms( chunks = append(chunks, chunk) } - response.Chunks = chunks + if limit == 0 { + // If limit is 0, don't limit the results + response.Chunks = chunks[offset:] + } else { + response.Chunks = chunks[offset:limit] + } + return nil }