Add /createRoom endpoint and unmarshal HTTP body
MODIFY
src/github.com/matrix-org/dendrite/clientapi/auth/auth.go
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
}

ADD
src/github.com/matrix-org/dendrite/clientapi/common/common.go
+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
+}

MODIFY
src/github.com/matrix-org/dendrite/clientapi/routing/routing.go
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)
})))

ADD
src/github.com/matrix-org/dendrite/clientapi/writers/createroom.go
+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")
+}

Add /createRoom endpoint and unmarshal HTTP body (#27)