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

This commit is contained in:
Kegsay 2017-03-07 16:11:08 +00:00 committed by GitHub
parent 5552e1f3a8
commit 8ba9d4af04
4 changed files with 67 additions and 0 deletions

View File

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

View File

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

View File

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

View File

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