MCPcopy Create free account
hub / github.com/daodst/chat / SendLeave

Function SendLeave

federationapi/routing/leave.go:121–306  ·  view source on GitHub ↗

SendLeave implements the /send_leave API

(
	httpReq *http.Request,
	request *gomatrixserverlib.FederationRequest,
	cfg *config.FederationAPI,
	rsAPI api.FederationRoomserverAPI,
	keys gomatrixserverlib.JSONVerifier,
	roomID, eventID string,
)

Source from the content-addressed store, hash-verified

119
120// SendLeave implements the /send_leave API
121func 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("") {

Callers 1

SetupFunction · 0.85

Calls 6

ContextMethod · 0.80
VerifyJSONsMethod · 0.80
InputRoomEventsMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected