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

Function SendJoin

federationapi/routing/join.go:180–433  ·  view source on GitHub ↗

SendJoin implements the /send_join API The make-join send-join dance makes much more sense as a single flow so the cyclomatic complexity is high: nolint:gocyclo

(
	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

178// flow so the cyclomatic complexity is high:
179// nolint:gocyclo
180func SendJoin(
181 httpReq *http.Request,
182 request *gomatrixserverlib.FederationRequest,
183 cfg *config.FederationAPI,
184 rsAPI api.FederationRoomserverAPI,
185 keys gomatrixserverlib.JSONVerifier,
186 roomID, eventID string,
187) util.JSONResponse {
188 verReq := api.QueryRoomVersionForRoomRequest{RoomID: roomID}
189 verRes := api.QueryRoomVersionForRoomResponse{}
190 if err := rsAPI.QueryRoomVersionForRoom(httpReq.Context(), &verReq, &verRes); err != nil {
191 util.GetLogger(httpReq.Context()).WithError(err).Error("rsAPI.QueryRoomVersionForRoom failed")
192 return util.JSONResponse{
193 Code: http.StatusInternalServerError,
194 JSON: jsonerror.InternalServerError(),
195 }
196 }
197
198 event, err := gomatrixserverlib.NewEventFromUntrustedJSON(request.Content(), verRes.RoomVersion)
199 if err != nil {
200 return util.JSONResponse{
201 Code: http.StatusBadRequest,
202 JSON: jsonerror.BadJSON("The request body could not be decoded into valid JSON: " + err.Error()),
203 }
204 }
205
206 // Check that the event is from the server sending the request.
207 if event.Origin() != request.Origin() {
208 return util.JSONResponse{
209 Code: http.StatusForbidden,
210 JSON: jsonerror.Forbidden("The join must be sent by the server it originated on"),
211 }
212 }
213
214 // Check that a state key is provided.
215 if event.StateKey() == nil || event.StateKeyEquals("") {
216 return util.JSONResponse{
217 Code: http.StatusBadRequest,
218 JSON: jsonerror.BadJSON("No state key was provided in the join event."),
219 }
220 }
221 if !event.StateKeyEquals(event.Sender()) {
222 return util.JSONResponse{
223 Code: http.StatusBadRequest,
224 JSON: jsonerror.BadJSON("Event state key must match the event sender."),
225 }
226 }
227
228 // Check that the sender belongs to the server that is sending us
229 // the request. By this point we've already asserted that the sender
230 // and the state key are equal so we don't need to check both.
231 var domain gomatrixserverlib.ServerName
232 if _, domain, err = gomatrixserverlib.SplitID('@', event.Sender()); err != nil {
233 return util.JSONResponse{
234 Code: http.StatusForbidden,
235 JSON: jsonerror.Forbidden("The sender of the join is invalid"),
236 }
237 } else if domain != request.Origin() {

Callers 1

SetupFunction · 0.85

Calls 8

ContextMethod · 0.80
VerifyJSONsMethod · 0.80
PrevEventIDsMethod · 0.80
eventsByDepthTypeAlias · 0.70
InputRoomEventsMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected