diff --git a/federationapi/routing/invite.go b/federationapi/routing/invite.go index b1d84f254..4a49463a2 100644 --- a/federationapi/routing/invite.go +++ b/federationapi/routing/invite.go @@ -15,6 +15,7 @@ package routing import ( + "context" "encoding/json" "fmt" "net/http" @@ -27,8 +28,8 @@ import ( "github.com/matrix-org/util" ) -// Invite implements /_matrix/federation/v2/invite/{roomID}/{eventID} -func Invite( +// InviteV2 implements /_matrix/federation/v2/invite/{roomID}/{eventID} +func InviteV2( httpReq *http.Request, request *gomatrixserverlib.FederationRequest, roomID string, @@ -44,14 +45,58 @@ func Invite( JSON: jsonerror.NotJSON("The request body could not be decoded into an invite request. " + err.Error()), } } - event := inviteReq.Event() + return processInvite( + httpReq.Context(), inviteReq.Event(), inviteReq.RoomVersion(), inviteReq.InviteRoomState(), roomID, eventID, cfg, rsAPI, keys, + ) +} + +// InviteV1 implements /_matrix/federation/v1/invite/{roomID}/{eventID} +func InviteV1( + httpReq *http.Request, + request *gomatrixserverlib.FederationRequest, + roomID string, + eventID string, + cfg *config.Dendrite, + rsAPI api.RoomserverInternalAPI, + keys gomatrixserverlib.JSONVerifier, +) util.JSONResponse { + roomVer := gomatrixserverlib.RoomVersionV1 + body := request.Content() + event, err := gomatrixserverlib.NewEventFromTrustedJSON(body, false, roomVer) + if err != nil { + return util.JSONResponse{ + Code: http.StatusBadRequest, + JSON: jsonerror.NotJSON("The request body could not be decoded into an invite v1 request: " + err.Error()), + } + } + var strippedState []gomatrixserverlib.InviteV2StrippedState + if err := json.Unmarshal(event.Unsigned(), &strippedState); err != nil { + // just warn, they may not have added any. + util.GetLogger(httpReq.Context()).Warnf("failed to extract stripped state from invite event") + } + return processInvite( + httpReq.Context(), event, roomVer, strippedState, roomID, eventID, cfg, rsAPI, keys, + ) +} + +func processInvite( + ctx context.Context, + event gomatrixserverlib.Event, + roomVer gomatrixserverlib.RoomVersion, + strippedState []gomatrixserverlib.InviteV2StrippedState, + roomID string, + eventID string, + cfg *config.Dendrite, + rsAPI api.RoomserverInternalAPI, + keys gomatrixserverlib.JSONVerifier, +) util.JSONResponse { // Check that we can accept invites for this room version. - if _, err := roomserverVersion.SupportedRoomVersion(inviteReq.RoomVersion()); err != nil { + if _, err := roomserverVersion.SupportedRoomVersion(roomVer); err != nil { return util.JSONResponse{ Code: http.StatusBadRequest, JSON: jsonerror.UnsupportedRoomVersion( - fmt.Sprintf("Room version %q is not supported by this server.", inviteReq.RoomVersion()), + fmt.Sprintf("Room version %q is not supported by this server.", roomVer), ), } } @@ -80,9 +125,9 @@ func Invite( AtTS: event.OriginServerTS(), StrictValidityChecking: true, }} - verifyResults, err := keys.VerifyJSONs(httpReq.Context(), verifyRequests) + verifyResults, err := keys.VerifyJSONs(ctx, verifyRequests) if err != nil { - util.GetLogger(httpReq.Context()).WithError(err).Error("keys.VerifyJSONs failed") + util.GetLogger(ctx).WithError(err).Error("keys.VerifyJSONs failed") return jsonerror.InternalServerError() } if verifyResults[0].Error != nil { @@ -99,13 +144,9 @@ func Invite( // Add the invite event to the roomserver. if perr := api.SendInvite( - httpReq.Context(), rsAPI, - signedEvent.Headered(inviteReq.RoomVersion()), - inviteReq.InviteRoomState(), - event.Origin(), - nil, + ctx, rsAPI, signedEvent.Headered(roomVer), strippedState, event.Origin(), nil, ); perr != nil { - util.GetLogger(httpReq.Context()).WithError(err).Error("producer.SendInvite failed") + util.GetLogger(ctx).WithError(err).Error("producer.SendInvite failed") return perr.JSONResponse() } diff --git a/federationapi/routing/routing.go b/federationapi/routing/routing.go index 645f397de..0afea7d04 100644 --- a/federationapi/routing/routing.go +++ b/federationapi/routing/routing.go @@ -83,10 +83,26 @@ func Setup( }, )).Methods(http.MethodPut, http.MethodOptions) + v1fedmux.Handle("/invite/{roomID}/{eventID}", httputil.MakeFedAPI( + "federation_invite", cfg.Matrix.ServerName, keys, wakeup, + func(httpReq *http.Request, request *gomatrixserverlib.FederationRequest, vars map[string]string) util.JSONResponse { + res := InviteV1( + httpReq, request, vars["roomID"], vars["eventID"], + cfg, rsAPI, keys, + ) + return util.JSONResponse{ + Code: res.Code, + JSON: []interface{}{ + res.Code, res.JSON, + }, + } + }, + )).Methods(http.MethodPut, http.MethodOptions) + v2fedmux.Handle("/invite/{roomID}/{eventID}", httputil.MakeFedAPI( "federation_invite", cfg.Matrix.ServerName, keys, wakeup, func(httpReq *http.Request, request *gomatrixserverlib.FederationRequest, vars map[string]string) util.JSONResponse { - return Invite( + return InviteV2( httpReq, request, vars["roomID"], vars["eventID"], cfg, rsAPI, keys, )