Implement leaving room method

This commit is contained in:
nxshock 2019-08-01 19:29:43 +05:00
parent 490bd56e2d
commit 2cffa574fa
3 changed files with 37 additions and 0 deletions

View File

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

View File

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

View File

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