From 3c61d0344fc0b8b51378832d40e9ec24811bb5b3 Mon Sep 17 00:00:00 2001 From: nxshock Date: Thu, 29 Aug 2019 21:05:40 +0500 Subject: [PATCH] Add createRoom handler --- internal/handlers.go | 33 +++++++++++++++++++ .../createroom/{reply.go => Response.go} | 2 +- internal/server.go | 1 + 3 files changed, 35 insertions(+), 1 deletion(-) rename internal/models/createroom/{reply.go => Response.go} (85%) diff --git a/internal/handlers.go b/internal/handlers.go index 669f7a3..35d205a 100644 --- a/internal/handlers.go +++ b/internal/handlers.go @@ -8,6 +8,8 @@ import ( "strconv" "strings" + "github.com/signaller-matrix/signaller/internal/models/createroom" + "github.com/gorilla/mux" "github.com/signaller-matrix/signaller/internal/models" @@ -542,6 +544,37 @@ func publicRoomsHandler(w http.ResponseWriter, r *http.Request) { sendJsonResponse(w, http.StatusOK, response) } +// https://matrix.org/docs/spec/client_server/latest#post-matrix-client-r0-createroom +func createRoomHandler(w http.ResponseWriter, r *http.Request) { + token := getTokenFromResponse(r) + if token == "" { + errorResponse(w, models.M_FORBIDDEN, http.StatusForbidden, "") + return + } + + user := currServer.Backend.GetUserByToken(token) + if user == nil { + errorResponse(w, models.M_UNKNOWN_TOKEN, http.StatusBadRequest, "") + return + } + + var request createroom.Request + err := getRequest(r, &request) + if err != nil { + errorResponse(w, models.M_BAD_JSON, http.StatusBadRequest, err.Error()) + return + } + + room, apiErr := user.CreateRoom(request) + if err != nil { + errorResponse(w, apiErr, http.StatusBadRequest, apiErr.Error()) + return + } + + response := createroom.Response{RoomID: room.ID()} + sendJsonResponse(w, http.StatusOK, response) +} + // https://matrix.org/docs/spec/client_server/latest#put-matrix-client-r0-directory-room-roomalias // https://matrix.org/docs/spec/client_server/latest#get-matrix-client-r0-directory-room-roomalias // https://matrix.org/docs/spec/client_server/latest#delete-matrix-client-r0-directory-room-roomalias diff --git a/internal/models/createroom/reply.go b/internal/models/createroom/Response.go similarity index 85% rename from internal/models/createroom/reply.go rename to internal/models/createroom/Response.go index ccf714a..139881a 100644 --- a/internal/models/createroom/reply.go +++ b/internal/models/createroom/Response.go @@ -1,6 +1,6 @@ package createroom // https://matrix.org/docs/spec/client_server/latest#post-matrix-client-r0-createroom -type Reply struct { +type Response struct { RoomID string `json:"room_id"` } diff --git a/internal/server.go b/internal/server.go index 79726f6..56dd112 100644 --- a/internal/server.go +++ b/internal/server.go @@ -36,6 +36,7 @@ func NewServer(port int) (*Server, error) { router.HandleFunc("/_matrix/client/r0/sync", SyncHandler) router.HandleFunc("/_matrix/client/r0/capabilities", CapabilitiesHandler) router.HandleFunc("/_matrix/client/r0/devices", DevicesHandler) + router.HandleFunc("/_matrix/client/r0/createRoom", createRoomHandler).Methods(http.MethodPost) router.HandleFunc("/_matrix/client/r0/directory/list/room/{roomID}", listRoomHandler) router.HandleFunc("/_matrix/client/r0/rooms/{roomId}/leave", leaveRoomHandler) router.HandleFunc("/_matrix/client/r0/register/available", registerAvailableHandler)