SendLeave implements the /send_leave API
( httpReq *http.Request, request *gomatrixserverlib.FederationRequest, cfg *config.FederationAPI, rsAPI api.FederationRoomserverAPI, keys gomatrixserverlib.JSONVerifier, roomID, eventID string, )
| 119 | |
| 120 | // SendLeave implements the /send_leave API |
| 121 | func SendLeave( |
| 122 | httpReq *http.Request, |
| 123 | request *gomatrixserverlib.FederationRequest, |
| 124 | cfg *config.FederationAPI, |
| 125 | rsAPI api.FederationRoomserverAPI, |
| 126 | keys gomatrixserverlib.JSONVerifier, |
| 127 | roomID, eventID string, |
| 128 | ) util.JSONResponse { |
| 129 | verReq := api.QueryRoomVersionForRoomRequest{RoomID: roomID} |
| 130 | verRes := api.QueryRoomVersionForRoomResponse{} |
| 131 | if err := rsAPI.QueryRoomVersionForRoom(httpReq.Context(), &verReq, &verRes); err != nil { |
| 132 | return util.JSONResponse{ |
| 133 | Code: http.StatusBadRequest, |
| 134 | JSON: jsonerror.UnsupportedRoomVersion(err.Error()), |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | // Decode the event JSON from the request. |
| 139 | event, err := gomatrixserverlib.NewEventFromUntrustedJSON(request.Content(), verRes.RoomVersion) |
| 140 | switch err.(type) { |
| 141 | case gomatrixserverlib.BadJSONError: |
| 142 | return util.JSONResponse{ |
| 143 | Code: http.StatusBadRequest, |
| 144 | JSON: jsonerror.BadJSON(err.Error()), |
| 145 | } |
| 146 | case nil: |
| 147 | default: |
| 148 | return util.JSONResponse{ |
| 149 | Code: http.StatusBadRequest, |
| 150 | JSON: jsonerror.NotJSON("The request body could not be decoded into valid JSON. " + err.Error()), |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | // Check that the room ID is correct. |
| 155 | if event.RoomID() != roomID { |
| 156 | return util.JSONResponse{ |
| 157 | Code: http.StatusBadRequest, |
| 158 | JSON: jsonerror.BadJSON("The room ID in the request path must match the room ID in the leave event JSON"), |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | // Check that the event ID is correct. |
| 163 | if event.EventID() != eventID { |
| 164 | return util.JSONResponse{ |
| 165 | Code: http.StatusBadRequest, |
| 166 | JSON: jsonerror.BadJSON("The event ID in the request path must match the event ID in the leave event JSON"), |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | // Check that the event is from the server sending the request. |
| 171 | if event.Origin() != request.Origin() { |
| 172 | return util.JSONResponse{ |
| 173 | Code: http.StatusForbidden, |
| 174 | JSON: jsonerror.Forbidden("The leave must be sent by the server it originated on"), |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | if event.StateKey() == nil || event.StateKeyEquals("") { |
no test coverage detected