Create roomserver QueryReserveRoomID

TODO: Query prefix is not really correct for this specific query because
it will not do readonly stuff.
Maybe rename it?
This commit is contained in:
Remi Reuvekamp 2017-10-19 13:00:31 +02:00
parent 32a2b3a5c0
commit be7a785e52
2 changed files with 60 additions and 2 deletions

View file

@ -21,10 +21,9 @@ import (
"fmt"
"net/http"
opentracing "github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
"github.com/opentracing/opentracing-go"
"github.com/matrix-org/gomatrixserverlib"
)
@ -155,6 +154,18 @@ type QueryServerAllowedToSeeEventResponse struct {
AllowedToSeeEvent bool `json:"can_see_event"`
}
// QueryReserveRoomIDRequest is a request to QueryServerAllowedToSeeEvent
type QueryReserveRoomIDRequest struct {
// TODO: Docs
RoomID string `json:"room_id"`
}
// QueryReserveRoomIDResponse is a response to QueryServerAllowedToSeeEvent
type QueryReserveRoomIDResponse struct {
// TODO: Docs
Success bool `json:"success"`
}
// RoomserverQueryAPI is used to query information from the room server.
type RoomserverQueryAPI interface {
// Query the latest events and state for a room from the room server.
@ -218,6 +229,9 @@ const RoomserverQueryInvitesForUserPath = "/api/roomserver/queryInvitesForUser"
// RoomserverQueryServerAllowedToSeeEventPath is the HTTP path for the QueryServerAllowedToSeeEvent API
const RoomserverQueryServerAllowedToSeeEventPath = "/api/roomserver/queryServerAllowedToSeeEvent"
// RoomserverQueryReserveRoomIDPath is the HTTP path for the QueryReserveRoomID API
const RoomserverQueryReserveRoomIDPath = "/api/roomserver/queryReserveRoomID"
// NewRoomserverQueryAPIHTTP creates a RoomserverQueryAPI implemented by talking to a HTTP POST API.
// If httpClient is nil then it uses the http.DefaultClient
func NewRoomserverQueryAPIHTTP(roomserverURL string, httpClient *http.Client) RoomserverQueryAPI {
@ -310,6 +324,19 @@ func (h *httpRoomserverQueryAPI) QueryServerAllowedToSeeEvent(
return postJSON(ctx, span, h.httpClient, apiURL, request, response)
}
// TODO: Docs
func (h *httpRoomserverQueryAPI) QueryReserveRoomID(
ctx context.Context,
request *QueryReserveRoomIDRequest,
response *QueryReserveRoomIDResponse,
) (err error) {
span, ctx := opentracing.StartSpanFromContext(ctx, "QueryReserveRoomID")
defer span.Finish()
apiURL := h.roomserverURL + RoomserverQueryReserveRoomIDPath
return postJSON(ctx, span, h.httpClient, apiURL, request, response)
}
func postJSON(
ctx context.Context, span opentracing.Span, httpClient *http.Client,
apiURL string, request, response interface{},

View file

@ -418,6 +418,23 @@ func (r *RoomserverQueryAPI) QueryServerAllowedToSeeEvent(
return nil
}
// QueryReserveRoomID implements api.RoomserverQueryAPI
func (r *RoomserverQueryAPI) QueryReserveRoomID(
ctx context.Context,
request *api.QueryReserveRoomIDRequest,
response *api.QueryReserveRoomIDResponse,
) error {
nid, err := r.DB.RoomNID(ctx, request.RoomID)
if nid != 0 {
response.Success = false
return nil
}
response.Success = true
return err
}
// SetupHTTP adds the RoomserverQueryAPI handlers to the http.ServeMux.
// nolint: gocyclo
func (r *RoomserverQueryAPI) SetupHTTP(servMux *http.ServeMux) {
@ -505,4 +522,18 @@ func (r *RoomserverQueryAPI) SetupHTTP(servMux *http.ServeMux) {
return util.JSONResponse{Code: 200, JSON: &response}
}),
)
servMux.Handle(
api.RoomserverQueryReserveRoomIDPath,
common.MakeInternalAPI("queryReserveRoomID", func(req *http.Request) util.JSONResponse {
var request api.QueryReserveRoomIDRequest
var response api.QueryReserveRoomIDResponse
if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
return util.ErrorResponse(err)
}
if err := r.QueryReserveRoomID(req.Context(), &request, &response); err != nil {
return util.ErrorResponse(err)
}
return util.JSONResponse{Code: 200, JSON: &response}
}),
)
}