Support the since and limit parameters

This commit is contained in:
Brendan Abolivier 2017-08-10 12:07:28 +01:00
parent d9913c303f
commit f4a53ee54b
No known key found for this signature in database
GPG key ID: 8EF1500759F70623
4 changed files with 55 additions and 6 deletions

View file

@ -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 {

View file

@ -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
}

View file

@ -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

View file

@ -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
}