diff --git a/STATUS.md b/STATUS.md index 10e484a..a084648 100644 --- a/STATUS.md +++ b/STATUS.md @@ -11,6 +11,7 @@ Implemented from [Client-Server API](https://matrix.org/docs/spec/client_server/ - [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) +- [x] [10.4.3.1 POST /_matrix/client/r0/rooms/{roomId}/leave](https://matrix.org/docs/spec/client_server/latest#post-matrix-client-r0-rooms-roomid-leave) - [x] [10.5.1 GET /_matrix/client/r0/directory/list/room/{roomId}](https://matrix.org/docs/spec/client_server/latest#get-matrix-client-r0-directory-list-room-roomid) - [x] [10.5.2 PUT /_matrix/client/r0/directory/list/room/{roomId}](https://matrix.org/docs/spec/client_server/latest#put-matrix-client-r0-directory-list-room-roomid) - [x] [13.10.1.1 GET /_matrix/client/r0/devices](https://matrix.org/docs/spec/client_server/latest#get-matrix-client-r0-devices) diff --git a/internal/handlers.go b/internal/handlers.go index 2beacaa..98e0508 100644 --- a/internal/handlers.go +++ b/internal/handlers.go @@ -82,6 +82,41 @@ func LoginHandler(w http.ResponseWriter, r *http.Request) { } } +// https://matrix.org/docs/spec/client_server/latest#post-matrix-client-r0-rooms-roomid-leave +func leaveRoomHandler(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodPost { + errorResponse(w, models.M_UNKNOWN, http.StatusBadRequest, "wrong method: "+r.Method) + return + } + + token := getTokenFromResponse(r) + + if token == "" { + errorResponse(w, models.M_MISSING_TOKEN, http.StatusBadRequest, "") + return + } + + user := currServer.Backend.GetUserByToken(token) + if user == nil { + errorResponse(w, models.M_UNKNOWN_TOKEN, http.StatusBadRequest, "") + return + } + + room := currServer.Backend.GetRoomByID(mux.Vars(r)["roomID"]) // TODO: can ["roomID"] throw panic? + if room == nil { + errorResponse(w, models.M_NOT_FOUND, http.StatusBadRequest, "room not found") + return + } + + err := user.LeaveRoom(room) + if err != nil { + errorResponse(w, *err, http.StatusBadRequest, "") + return + } + + sendJsonResponse(w, http.StatusOK, struct{}{}) +} + // https://models.org/docs/spec/client_server/latest#post-models-client-r0-logout func LogoutHandler(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodPost { diff --git a/internal/server.go b/internal/server.go index 1367950..b947fcb 100644 --- a/internal/server.go +++ b/internal/server.go @@ -37,6 +37,7 @@ func NewServer(port int) (*Server, error) { router.HandleFunc("/_matrix/client/r0/capabilities", CapabilitiesHandler) router.HandleFunc("/_matrix/client/r0/devices", DevicesHandler) router.HandleFunc("/_matrix/client/r0/directory/list/room/{roomID}", listRoomHandler) + router.HandleFunc("/_matrix/client/r0/rooms/{roomId}/leave", leaveRoomHandler) router.HandleFunc("/", RootHandler) if port <= 0 || port > 65535 {