From 33bb853438cc2abb40db54a41cd1c1555f7ac93b Mon Sep 17 00:00:00 2001 From: nxshock Date: Thu, 25 Jul 2019 20:14:37 +0500 Subject: [PATCH] Add capabilities method --- README.md | 1 + internal/handlers.go | 25 ++++++++++++++++++ internal/models/capabilities/capabilities.go | 27 ++++++++++++++++++++ internal/server.go | 7 ++++- 4 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 internal/models/capabilities/capabilities.go diff --git a/README.md b/README.md index 0a82b24..84a90c7 100644 --- a/README.md +++ b/README.md @@ -15,4 +15,5 @@ Implemented from [Client-Server API](https://matrix.org/docs/spec/client_server/ - [x] [5.4.3 POST /_matrix/client/r0/logout](https://matrix.org/docs/spec/client_server/latest#post-matrix-client-r0-logout) - [x] [5.4.4 POST /_matrix/client/r0/logout/all](https://matrix.org/docs/spec/client_server/latest#post-matrix-client-r0-logout-all) - [x] [5.7.1 GET /_matrix/client/r0/account/whoami](https://matrix.org/docs/spec/client_server/latest#get-matrix-client-r0-account-whoami) +- [x] [6.1 GET /_matrix/client/r0/capabilities](https://matrix.org/docs/spec/client_server/latest#get-matrix-client-r0-capabilities) - [x] [10.4.1 GET /_matrix/client/r0/joined_rooms](https://matrix.org/docs/spec/client_server/latest#get-matrix-client-r0-joined-rooms) diff --git a/internal/handlers.go b/internal/handlers.go index c6807df..d0a487f 100644 --- a/internal/handlers.go +++ b/internal/handlers.go @@ -11,6 +11,7 @@ import ( "github.com/nxshock/signaller/internal/models/common" "github.com/nxshock/signaller/internal/models" + "github.com/nxshock/signaller/internal/models/capabilities" "github.com/nxshock/signaller/internal/models/joinedrooms" login "github.com/nxshock/signaller/internal/models/login" register "github.com/nxshock/signaller/internal/models/register" @@ -224,6 +225,30 @@ func SyncHandler(w http.ResponseWriter, r *http.Request) { sendJsonResponse(w, http.StatusOK, response) } +// https://matrix.org/docs/spec/client_server/latest#get-matrix-client-r0-capabilities +func CapabilitiesHandler(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodGet { + errorResponse(w, models.M_UNKNOWN, http.StatusBadRequest, "wrong method: "+r.Method) + return + } + + token := getTokenFromResponse(r) + if token == "" { + errorResponse(w, models.M_FORBIDDEN, http.StatusForbidden, "") + } + + user := currServer.Backend.GetUserByToken(token) + if user == nil { + errorResponse(w, models.M_UNKNOWN_TOKEN, http.StatusBadRequest, "") + return + } + + var response capabilities.Response + response.Capabilities = currServer.Capabilities + + sendJsonResponse(w, http.StatusOK, response) +} + func sendJsonResponse(w http.ResponseWriter, httpStatus int, data interface{}) error { b, err := json.Marshal(data) if err != nil { diff --git a/internal/models/capabilities/capabilities.go b/internal/models/capabilities/capabilities.go new file mode 100644 index 0000000..a337355 --- /dev/null +++ b/internal/models/capabilities/capabilities.go @@ -0,0 +1,27 @@ +package capabilities + +// https://matrix.org/docs/spec/client_server/latest#get-matrix-client-r0-capabilities +type Response struct { + Capabilities Capabilities `json:"capabilities"` // Required. The custom capabilities the server supports, using the Java package naming convention. +} + +type Capabilities struct { + ChangePassword ChangePasswordCapability `json:"m.change_password"` // Capability to indicate if the user can change their password. + RoomVersions RoomVersionsCapability `json:"m.room_versions"` // The room versions the server supports. +} + +type ChangePasswordCapability struct { + Enabled bool `json:"enabled"` // Required. True if the user can change their password, false otherwise. +} + +type RoomVersionsCapability struct { + Default string `json:"default"` // Required. The default room version the server is using for new rooms. + Available map[string]Stability `json:"available"` // Required. A detailed description of the room versions the server supports. +} + +type Stability string + +const ( + Stable Stability = "stable" + Unstable Stability = "unstable" +) diff --git a/internal/server.go b/internal/server.go index bc10e6d..a5d2379 100644 --- a/internal/server.go +++ b/internal/server.go @@ -4,6 +4,8 @@ import ( "net/http" "github.com/gorilla/mux" + + "github.com/nxshock/signaller/internal/models/capabilities" ) var currServer *Server @@ -13,7 +15,9 @@ type Server struct { router *mux.Router Address string - Backend Backend + + Capabilities capabilities.Capabilities + Backend Backend } func New() *Server { @@ -26,6 +30,7 @@ func New() *Server { router.HandleFunc("/_matrix/client/r0/account/whoami", WhoAmIHandler) router.HandleFunc("/_matrix/client/r0/joined_rooms", JoinedRoomsHandler) router.HandleFunc("/_matrix/client/r0/sync", SyncHandler) + router.HandleFunc("/_matrix/client/r0/capabilities", CapabilitiesHandler) router.HandleFunc("/", RootHandler) httpServer := new(http.Server)