From 8ba9d4af048167dfb56e2feb8c2a1b9561fbca9f Mon Sep 17 00:00:00 2001 From: Kegsay Date: Tue, 7 Mar 2017 16:11:08 +0000 Subject: [PATCH] Add /createRoom endpoint and unmarshal HTTP body (#27) --- .../dendrite/clientapi/auth/auth.go | 1 + .../dendrite/clientapi/common/common.go | 21 ++++++++++ .../dendrite/clientapi/routing/routing.go | 3 ++ .../dendrite/clientapi/writers/createroom.go | 42 +++++++++++++++++++ 4 files changed, 67 insertions(+) create mode 100644 src/github.com/matrix-org/dendrite/clientapi/common/common.go create mode 100644 src/github.com/matrix-org/dendrite/clientapi/writers/createroom.go diff --git a/src/github.com/matrix-org/dendrite/clientapi/auth/auth.go b/src/github.com/matrix-org/dendrite/clientapi/auth/auth.go index e3d8e6fa..1363040c 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/auth/auth.go +++ b/src/github.com/matrix-org/dendrite/clientapi/auth/auth.go @@ -26,6 +26,7 @@ func VerifyAccessToken(req *http.Request) (userID string, resErr *util.JSONRespo resErr = &res } // TODO: Check the token against the database + userID = token return } diff --git a/src/github.com/matrix-org/dendrite/clientapi/common/common.go b/src/github.com/matrix-org/dendrite/clientapi/common/common.go new file mode 100644 index 00000000..72ccfb06 --- /dev/null +++ b/src/github.com/matrix-org/dendrite/clientapi/common/common.go @@ -0,0 +1,21 @@ +package common + +import ( + "encoding/json" + "github.com/matrix-org/dendrite/clientapi/jsonerror" + "github.com/matrix-org/util" + "net/http" +) + +// UnmarshalJSONRequest into the given interface pointer. Returns an error JSON response if +// there was a problem unmarshalling. Calling this function consumes the request body. +func UnmarshalJSONRequest(req *http.Request, iface interface{}) *util.JSONResponse { + defer req.Body.Close() + if err := json.NewDecoder(req.Body).Decode(iface); err != nil { + return &util.JSONResponse{ + Code: 400, + JSON: jsonerror.NotJSON("The request body was not JSON"), + } + } + return nil +} 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 6e830e03..22fcfbd6 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/routing/routing.go +++ b/src/github.com/matrix-org/dendrite/clientapi/routing/routing.go @@ -17,6 +17,9 @@ const pathPrefixR0 = "/_matrix/client/r0" func Setup(servMux *http.ServeMux, httpClient *http.Client) { apiMux := mux.NewRouter() r0mux := apiMux.PathPrefix(pathPrefixR0).Subrouter() + r0mux.Handle("/createRoom", make("createRoom", wrap(func(req *http.Request) util.JSONResponse { + return writers.CreateRoom(req) + }))) r0mux.Handle("/sync", make("sync", wrap(func(req *http.Request) util.JSONResponse { return readers.Sync(req) }))) diff --git a/src/github.com/matrix-org/dendrite/clientapi/writers/createroom.go b/src/github.com/matrix-org/dendrite/clientapi/writers/createroom.go new file mode 100644 index 00000000..945eae7b --- /dev/null +++ b/src/github.com/matrix-org/dendrite/clientapi/writers/createroom.go @@ -0,0 +1,42 @@ +package writers + +import ( + "encoding/json" + "net/http" + + log "github.com/Sirupsen/logrus" + "github.com/matrix-org/dendrite/clientapi/auth" + "github.com/matrix-org/dendrite/clientapi/common" + "github.com/matrix-org/util" +) + +// https://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-createroom +type createRoomRequest struct { + Invite []string `json:"invite"` + Name string `json:"name"` + Visibility string `json:"visibility"` + Topic string `json:"topic"` + Preset string `json:"preset"` + CreationContent map[string]interface{} `json:"creation_content"` + InitialState json.RawMessage `json:"initial_state"` // TODO + RoomAliasName string `json:"room_alias_name"` +} + +// CreateRoom implements /createRoom +func CreateRoom(req *http.Request) util.JSONResponse { + logger := util.GetLogger(req.Context()) + userID, resErr := auth.VerifyAccessToken(req) + if resErr != nil { + return *resErr + } + var r createRoomRequest + resErr = common.UnmarshalJSONRequest(req, &r) + if resErr != nil { + return *resErr + } + + logger.WithFields(log.Fields{ + "userID": userID, + }).Info("Creating room") + return util.MessageResponse(404, "Not implemented yet") +}