Implement invite v1 for sytest mainly

This commit is contained in:
Kegan Dougal 2020-06-25 16:24:38 +01:00
parent 67f7a53f12
commit d687f0d640
2 changed files with 71 additions and 14 deletions

View file

@ -15,6 +15,7 @@
package routing package routing
import ( import (
"context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"net/http" "net/http"
@ -27,8 +28,8 @@ import (
"github.com/matrix-org/util" "github.com/matrix-org/util"
) )
// Invite implements /_matrix/federation/v2/invite/{roomID}/{eventID} // InviteV2 implements /_matrix/federation/v2/invite/{roomID}/{eventID}
func Invite( func InviteV2(
httpReq *http.Request, httpReq *http.Request,
request *gomatrixserverlib.FederationRequest, request *gomatrixserverlib.FederationRequest,
roomID string, roomID string,
@ -44,14 +45,58 @@ func Invite(
JSON: jsonerror.NotJSON("The request body could not be decoded into an invite request. " + err.Error()), 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. // 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{ return util.JSONResponse{
Code: http.StatusBadRequest, Code: http.StatusBadRequest,
JSON: jsonerror.UnsupportedRoomVersion( 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(), AtTS: event.OriginServerTS(),
StrictValidityChecking: true, StrictValidityChecking: true,
}} }}
verifyResults, err := keys.VerifyJSONs(httpReq.Context(), verifyRequests) verifyResults, err := keys.VerifyJSONs(ctx, verifyRequests)
if err != nil { 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() return jsonerror.InternalServerError()
} }
if verifyResults[0].Error != nil { if verifyResults[0].Error != nil {
@ -99,13 +144,9 @@ func Invite(
// Add the invite event to the roomserver. // Add the invite event to the roomserver.
if perr := api.SendInvite( if perr := api.SendInvite(
httpReq.Context(), rsAPI, ctx, rsAPI, signedEvent.Headered(roomVer), strippedState, event.Origin(), nil,
signedEvent.Headered(inviteReq.RoomVersion()),
inviteReq.InviteRoomState(),
event.Origin(),
nil,
); perr != 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() return perr.JSONResponse()
} }

View file

@ -83,10 +83,26 @@ func Setup(
}, },
)).Methods(http.MethodPut, http.MethodOptions) )).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( v2fedmux.Handle("/invite/{roomID}/{eventID}", httputil.MakeFedAPI(
"federation_invite", cfg.Matrix.ServerName, keys, wakeup, "federation_invite", cfg.Matrix.ServerName, keys, wakeup,
func(httpReq *http.Request, request *gomatrixserverlib.FederationRequest, vars map[string]string) util.JSONResponse { func(httpReq *http.Request, request *gomatrixserverlib.FederationRequest, vars map[string]string) util.JSONResponse {
return Invite( return InviteV2(
httpReq, request, vars["roomID"], vars["eventID"], httpReq, request, vars["roomID"], vars["eventID"],
cfg, rsAPI, keys, cfg, rsAPI, keys,
) )