aboutsummaryrefslogtreecommitdiff
path: root/federationapi
diff options
context:
space:
mode:
authorkegsay <kegan@matrix.org>2023-04-21 17:06:29 +0100
committerGitHub <noreply@github.com>2023-04-21 17:06:29 +0100
commit1647213facae52e2c8889fbc848ffc5d3a5792f0 (patch)
tree684206b99582df20ae144e19db37591cc35b789d /federationapi
parent71eeccf34a2ea4434c315c19778d80a7b2469270 (diff)
Implement new RoomVersionImpl API (#3062)
As outlined in https://github.com/matrix-org/gomatrixserverlib/pull/368 The main change Dendrite side is that `RoomVersion` no longer has any methods on it. Instead, you need to bounce via `gmsl.GetRoomVersion`. It's very interesting to see where exactly Dendrite cares about this. For some places it's creating events (fine) but others are way more specific. Those areas will need to migrate to GMSL at some point.
Diffstat (limited to 'federationapi')
-rw-r--r--federationapi/federationapi_test.go2
-rw-r--r--federationapi/internal/perform.go36
-rw-r--r--federationapi/queue/queue_test.go2
-rw-r--r--federationapi/routing/invite.go8
-rw-r--r--federationapi/routing/join.go19
-rw-r--r--federationapi/routing/leave.go14
-rw-r--r--federationapi/routing/threepid.go7
7 files changed, 62 insertions, 26 deletions
diff --git a/federationapi/federationapi_test.go b/federationapi/federationapi_test.go
index bef17559..70ad2e28 100644
--- a/federationapi/federationapi_test.go
+++ b/federationapi/federationapi_test.go
@@ -321,7 +321,7 @@ func TestRoomsV3URLEscapeDoNot404(t *testing.T) {
)
for _, tc := range testCases {
- ev, err := tc.roomVer.NewEventFromTrustedJSON([]byte(tc.eventJSON), false)
+ ev, err := gomatrixserverlib.MustGetRoomVersion(tc.roomVer).NewEventFromTrustedJSON([]byte(tc.eventJSON), false)
if err != nil {
t.Errorf("failed to parse event: %s", err)
}
diff --git a/federationapi/internal/perform.go b/federationapi/internal/perform.go
index 2ef8b552..c0e0ba14 100644
--- a/federationapi/internal/perform.go
+++ b/federationapi/internal/perform.go
@@ -201,8 +201,9 @@ func (r *FederationInternalAPI) performJoinUsingServer(
if respMakeJoin.RoomVersion == "" {
respMakeJoin.RoomVersion = setDefaultRoomVersionFromJoinEvent(respMakeJoin.JoinEvent)
}
- if _, err = respMakeJoin.RoomVersion.EventFormat(); err != nil {
- return fmt.Errorf("respMakeJoin.RoomVersion.EventFormat: %w", err)
+ verImpl, err := gomatrixserverlib.GetRoomVersion(respMakeJoin.RoomVersion)
+ if err != nil {
+ return err
}
// Build the join event.
@@ -235,7 +236,7 @@ func (r *FederationInternalAPI) performJoinUsingServer(
// contain signatures that we don't know about.
if len(respSendJoin.Event) > 0 {
var remoteEvent *gomatrixserverlib.Event
- remoteEvent, err = respMakeJoin.RoomVersion.NewEventFromUntrustedJSON(respSendJoin.Event)
+ remoteEvent, err = verImpl.NewEventFromUntrustedJSON(respSendJoin.Event)
if err == nil && isWellFormedMembershipEvent(
remoteEvent, roomID, userID,
) {
@@ -464,8 +465,8 @@ func (r *FederationInternalAPI) performOutboundPeekUsingServer(
if respPeek.RoomVersion == "" {
respPeek.RoomVersion = gomatrixserverlib.RoomVersionV1
}
- if _, err = respPeek.RoomVersion.EventFormat(); err != nil {
- return fmt.Errorf("respPeek.RoomVersion.EventFormat: %w", err)
+ if !gomatrixserverlib.KnownRoomVersion(respPeek.RoomVersion) {
+ return fmt.Errorf("unknown room version: %s", respPeek.RoomVersion)
}
// we have the peek state now so let's process regardless of whether upstream gives up
@@ -552,6 +553,13 @@ func (r *FederationInternalAPI) PerformLeave(
continue
}
+ // Work out if we support the room version that has been supplied in
+ // the make_leave response.
+ _, err = gomatrixserverlib.GetRoomVersion(respMakeLeave.RoomVersion)
+ if err != nil {
+ return err
+ }
+
// Set all the fields to be what they should be, this should be a no-op
// but it's possible that the remote server returned us something "odd"
respMakeLeave.LeaveEvent.Type = spec.MRoomMember
@@ -573,12 +581,6 @@ func (r *FederationInternalAPI) PerformLeave(
continue
}
- // Work out if we support the room version that has been supplied in
- // the make_leave response.
- if _, err = respMakeLeave.RoomVersion.EventFormat(); err != nil {
- return gomatrixserverlib.UnsupportedRoomVersionError{}
- }
-
// Build the leave event.
event, err := respMakeLeave.LeaveEvent.Build(
time.Now(),
@@ -659,8 +661,12 @@ func (r *FederationInternalAPI) PerformInvite(
if err != nil {
return fmt.Errorf("r.federation.SendInviteV2: failed to send invite: %w", err)
}
+ verImpl, err := gomatrixserverlib.GetRoomVersion(request.RoomVersion)
+ if err != nil {
+ return err
+ }
- inviteEvent, err := request.RoomVersion.NewEventFromUntrustedJSON(inviteRes.Event)
+ inviteEvent, err := verImpl.NewEventFromUntrustedJSON(inviteRes.Event)
if err != nil {
return fmt.Errorf("r.federation.SendInviteV2 failed to decode event response: %w", err)
}
@@ -779,6 +785,10 @@ func federatedAuthProvider(
// to repeat the entire set of checks just for a missing event or two.
return func(roomVersion gomatrixserverlib.RoomVersion, eventIDs []string) ([]*gomatrixserverlib.Event, error) {
returning := []*gomatrixserverlib.Event{}
+ verImpl, err := gomatrixserverlib.GetRoomVersion(roomVersion)
+ if err != nil {
+ return nil, err
+ }
// See if we have retry entries for each of the supplied event IDs.
for _, eventID := range eventIDs {
@@ -808,7 +818,7 @@ func federatedAuthProvider(
// event ID again.
for _, pdu := range tx.PDUs {
// Try to parse the event.
- ev, everr := roomVersion.NewEventFromUntrustedJSON(pdu)
+ ev, everr := verImpl.NewEventFromUntrustedJSON(pdu)
if everr != nil {
return nil, fmt.Errorf("missingAuth gomatrixserverlib.NewEventFromUntrustedJSON: %w", everr)
}
diff --git a/federationapi/queue/queue_test.go b/federationapi/queue/queue_test.go
index 77b44446..7e71d69d 100644
--- a/federationapi/queue/queue_test.go
+++ b/federationapi/queue/queue_test.go
@@ -105,7 +105,7 @@ func (f *stubFederationClient) P2PSendTransactionToRelay(ctx context.Context, u
func mustCreatePDU(t *testing.T) *gomatrixserverlib.HeaderedEvent {
t.Helper()
content := `{"type":"m.room.message"}`
- ev, err := gomatrixserverlib.RoomVersionV10.NewEventFromTrustedJSON([]byte(content), false)
+ ev, err := gomatrixserverlib.MustGetRoomVersion(gomatrixserverlib.RoomVersionV10).NewEventFromTrustedJSON([]byte(content), false)
if err != nil {
t.Fatalf("failed to create event: %v", err)
}
diff --git a/federationapi/routing/invite.go b/federationapi/routing/invite.go
index e9dbdb7d..b13e59f0 100644
--- a/federationapi/routing/invite.go
+++ b/federationapi/routing/invite.go
@@ -22,7 +22,6 @@ import (
"github.com/matrix-org/dendrite/clientapi/jsonerror"
"github.com/matrix-org/dendrite/roomserver/api"
- roomserverVersion "github.com/matrix-org/dendrite/roomserver/version"
"github.com/matrix-org/dendrite/setup/config"
"github.com/matrix-org/gomatrixserverlib"
"github.com/matrix-org/gomatrixserverlib/fclient"
@@ -78,7 +77,7 @@ func InviteV1(
) util.JSONResponse {
roomVer := gomatrixserverlib.RoomVersionV1
body := request.Content()
- event, err := roomVer.NewEventFromTrustedJSON(body, false)
+ event, err := gomatrixserverlib.MustGetRoomVersion(roomVer).NewEventFromTrustedJSON(body, false)
switch err.(type) {
case gomatrixserverlib.BadJSONError:
return util.JSONResponse{
@@ -116,7 +115,8 @@ func processInvite(
) util.JSONResponse {
// Check that we can accept invites for this room version.
- if _, err := roomserverVersion.SupportedRoomVersion(roomVer); err != nil {
+ verImpl, err := gomatrixserverlib.GetRoomVersion(roomVer)
+ if err != nil {
return util.JSONResponse{
Code: http.StatusBadRequest,
JSON: jsonerror.UnsupportedRoomVersion(
@@ -157,7 +157,7 @@ func processInvite(
}
// Check that the event is signed by the server sending the request.
- redacted, err := event.Version().RedactEventJSON(event.JSON())
+ redacted, err := verImpl.RedactEventJSON(event.JSON())
if err != nil {
return util.JSONResponse{
Code: http.StatusBadRequest,
diff --git a/federationapi/routing/join.go b/federationapi/routing/join.go
index 50a2fd1a..0d83a2af 100644
--- a/federationapi/routing/join.go
+++ b/federationapi/routing/join.go
@@ -206,8 +206,17 @@ func SendJoin(
JSON: jsonerror.InternalServerError(),
}
}
+ verImpl, err := gomatrixserverlib.GetRoomVersion(verRes.RoomVersion)
+ if err != nil {
+ return util.JSONResponse{
+ Code: http.StatusInternalServerError,
+ JSON: jsonerror.UnsupportedRoomVersion(
+ fmt.Sprintf("QueryRoomVersionForRoom returned unknown room version: %s", verRes.RoomVersion),
+ ),
+ }
+ }
- event, err := verRes.RoomVersion.NewEventFromUntrustedJSON(request.Content())
+ event, err := verImpl.NewEventFromUntrustedJSON(request.Content())
if err != nil {
return util.JSONResponse{
Code: http.StatusBadRequest,
@@ -287,7 +296,7 @@ func SendJoin(
}
// Check that the event is signed by the server sending the request.
- redacted, err := event.Version().RedactEventJSON(event.JSON())
+ redacted, err := verImpl.RedactEventJSON(event.JSON())
if err != nil {
logrus.WithError(err).Errorf("XXX: join.go")
return util.JSONResponse{
@@ -461,9 +470,11 @@ func checkRestrictedJoin(
roomVersion gomatrixserverlib.RoomVersion,
roomID, userID string,
) (*util.JSONResponse, string, error) {
- if allowRestricted, err := roomVersion.MayAllowRestrictedJoinsInEventAuth(); err != nil {
+ verImpl, err := gomatrixserverlib.GetRoomVersion(roomVersion)
+ if err != nil {
return nil, "", err
- } else if !allowRestricted {
+ }
+ if !verImpl.MayAllowRestrictedJoinsInEventAuth() {
return nil, "", nil
}
req := &api.QueryRestrictedJoinAllowedRequest{
diff --git a/federationapi/routing/leave.go b/federationapi/routing/leave.go
index b003c1cd..d189cc53 100644
--- a/federationapi/routing/leave.go
+++ b/federationapi/routing/leave.go
@@ -149,8 +149,18 @@ func SendLeave(
}
}
+ verImpl, err := gomatrixserverlib.GetRoomVersion(verRes.RoomVersion)
+ if err != nil {
+ return util.JSONResponse{
+ Code: http.StatusInternalServerError,
+ JSON: jsonerror.UnsupportedRoomVersion(
+ fmt.Sprintf("QueryRoomVersionForRoom returned unknown version: %s", verRes.RoomVersion),
+ ),
+ }
+ }
+
// Decode the event JSON from the request.
- event, err := verRes.RoomVersion.NewEventFromUntrustedJSON(request.Content())
+ event, err := verImpl.NewEventFromUntrustedJSON(request.Content())
switch err.(type) {
case gomatrixserverlib.BadJSONError:
return util.JSONResponse{
@@ -253,7 +263,7 @@ func SendLeave(
}
// Check that the event is signed by the server sending the request.
- redacted, err := event.Version().RedactEventJSON(event.JSON())
+ redacted, err := verImpl.RedactEventJSON(event.JSON())
if err != nil {
logrus.WithError(err).Errorf("XXX: leave.go")
return util.JSONResponse{
diff --git a/federationapi/routing/threepid.go b/federationapi/routing/threepid.go
index 831f3c61..31f29fd3 100644
--- a/federationapi/routing/threepid.go
+++ b/federationapi/routing/threepid.go
@@ -196,7 +196,12 @@ func ExchangeThirdPartyInvite(
util.GetLogger(httpReq.Context()).WithError(err).Error("federation.SendInvite failed")
return jsonerror.InternalServerError()
}
- inviteEvent, err := verRes.RoomVersion.NewEventFromUntrustedJSON(signedEvent.Event)
+ verImpl, err := gomatrixserverlib.GetRoomVersion(verRes.RoomVersion)
+ if err != nil {
+ util.GetLogger(httpReq.Context()).WithError(err).Errorf("unknown room version: %s", verRes.RoomVersion)
+ return jsonerror.InternalServerError()
+ }
+ inviteEvent, err := verImpl.NewEventFromUntrustedJSON(signedEvent.Event)
if err != nil {
util.GetLogger(httpReq.Context()).WithError(err).Error("federation.SendInvite failed")
return jsonerror.InternalServerError()