aboutsummaryrefslogtreecommitdiff
path: root/federationapi
diff options
context:
space:
mode:
authorkegsay <kegan@matrix.org>2023-05-02 15:03:16 +0100
committerGitHub <noreply@github.com>2023-05-02 15:03:16 +0100
commitf5b3144dc33ddcb2ab161323d422cab257d04b4c (patch)
tree07d3b03944d2529d96c29674a96c3e76a76ae3b9 /federationapi
parent696cbb70b8cc8d663f7bb7c7a5f25cd57ea6803e (diff)
Use PDU not *Event in HeaderedEvent (#3073)
Requires https://github.com/matrix-org/gomatrixserverlib/pull/376 This has numerous upsides: - Less type casting to `*Event` is required. - Making Dendrite work with `PDU` interfaces means we can swap out Event impls more easily. - Tests which represent weird event shapes are easier to write. Part of a series of refactors on GMSL.
Diffstat (limited to 'federationapi')
-rw-r--r--federationapi/consumers/roomserver.go22
-rw-r--r--federationapi/federationapi_test.go4
-rw-r--r--federationapi/internal/federationclient.go2
-rw-r--r--federationapi/internal/perform.go10
-rw-r--r--federationapi/queue/queue_test.go2
-rw-r--r--federationapi/routing/backfill.go6
-rw-r--r--federationapi/routing/events.go4
-rw-r--r--federationapi/routing/invite.go4
-rw-r--r--federationapi/routing/join.go10
-rw-r--r--federationapi/routing/leave.go10
-rw-r--r--federationapi/routing/peek.go2
-rw-r--r--federationapi/routing/threepid.go6
12 files changed, 41 insertions, 41 deletions
diff --git a/federationapi/consumers/roomserver.go b/federationapi/consumers/roomserver.go
index d0aa0640..c6ad3f74 100644
--- a/federationapi/consumers/roomserver.go
+++ b/federationapi/consumers/roomserver.go
@@ -187,9 +187,9 @@ func (s *OutputRoomEventConsumer) processMessage(ore api.OutputNewRoomEvent, rew
addsStateEvents = append(addsStateEvents, eventsRes.Events...)
}
- evs := make([]*gomatrixserverlib.Event, len(addsStateEvents))
+ evs := make([]gomatrixserverlib.PDU, len(addsStateEvents))
for i := range evs {
- evs[i] = addsStateEvents[i].Event
+ evs[i] = addsStateEvents[i].PDU
}
addsJoinedHosts, err := JoinedHostsFromEvents(evs)
@@ -340,7 +340,7 @@ func (s *OutputRoomEventConsumer) joinedHostsAtEvent(
ore.AddsStateEventIDs, ore.RemovesStateEventIDs,
ore.StateBeforeAddsEventIDs, ore.StateBeforeRemovesEventIDs,
)
- combinedAddsEvents, err := s.lookupStateEvents(combinedAdds, ore.Event.Event)
+ combinedAddsEvents, err := s.lookupStateEvents(combinedAdds, ore.Event.PDU)
if err != nil {
return nil, err
}
@@ -374,7 +374,7 @@ func (s *OutputRoomEventConsumer) joinedHostsAtEvent(
}
// handle peeking hosts
- inboundPeeks, err := s.db.GetInboundPeeks(s.ctx, ore.Event.Event.RoomID())
+ inboundPeeks, err := s.db.GetInboundPeeks(s.ctx, ore.Event.PDU.RoomID())
if err != nil {
return nil, err
}
@@ -394,7 +394,7 @@ func (s *OutputRoomEventConsumer) joinedHostsAtEvent(
// JoinedHostsFromEvents turns a list of state events into a list of joined hosts.
// This errors if one of the events was invalid.
// It should be impossible for an invalid event to get this far in the pipeline.
-func JoinedHostsFromEvents(evs []*gomatrixserverlib.Event) ([]types.JoinedHost, error) {
+func JoinedHostsFromEvents(evs []gomatrixserverlib.PDU) ([]types.JoinedHost, error) {
var joinedHosts []types.JoinedHost
for _, ev := range evs {
if ev.Type() != "m.room.member" || ev.StateKey() == nil {
@@ -459,8 +459,8 @@ func combineDeltas(adds1, removes1, adds2, removes2 []string) (adds, removes []s
// lookupStateEvents looks up the state events that are added by a new event.
func (s *OutputRoomEventConsumer) lookupStateEvents(
- addsStateEventIDs []string, event *gomatrixserverlib.Event,
-) ([]*gomatrixserverlib.Event, error) {
+ addsStateEventIDs []string, event gomatrixserverlib.PDU,
+) ([]gomatrixserverlib.PDU, error) {
// Fast path if there aren't any new state events.
if len(addsStateEventIDs) == 0 {
return nil, nil
@@ -468,11 +468,11 @@ func (s *OutputRoomEventConsumer) lookupStateEvents(
// Fast path if the only state event added is the event itself.
if len(addsStateEventIDs) == 1 && addsStateEventIDs[0] == event.EventID() {
- return []*gomatrixserverlib.Event{event}, nil
+ return []gomatrixserverlib.PDU{event}, nil
}
missing := addsStateEventIDs
- var result []*gomatrixserverlib.Event
+ var result []gomatrixserverlib.PDU
// Check if event itself is being added.
for _, eventID := range missing {
@@ -497,7 +497,7 @@ func (s *OutputRoomEventConsumer) lookupStateEvents(
}
for _, headeredEvent := range eventResp.Events {
- result = append(result, headeredEvent.Event)
+ result = append(result, headeredEvent.PDU)
}
missing = missingEventsFrom(result, addsStateEventIDs)
@@ -511,7 +511,7 @@ func (s *OutputRoomEventConsumer) lookupStateEvents(
return result, nil
}
-func missingEventsFrom(events []*gomatrixserverlib.Event, required []string) []string {
+func missingEventsFrom(events []gomatrixserverlib.PDU, required []string) []string {
have := map[string]bool{}
for _, event := range events {
have[event.EventID()] = true
diff --git a/federationapi/federationapi_test.go b/federationapi/federationapi_test.go
index de65f348..c21e7ecb 100644
--- a/federationapi/federationapi_test.go
+++ b/federationapi/federationapi_test.go
@@ -133,12 +133,12 @@ func (f *fedClient) MakeJoin(ctx context.Context, origin, s spec.ServerName, roo
}
return
}
-func (f *fedClient) SendJoin(ctx context.Context, origin, s spec.ServerName, event *gomatrixserverlib.Event) (res fclient.RespSendJoin, err error) {
+func (f *fedClient) SendJoin(ctx context.Context, origin, s spec.ServerName, event gomatrixserverlib.PDU) (res fclient.RespSendJoin, err error) {
f.fedClientMutex.Lock()
defer f.fedClientMutex.Unlock()
for _, r := range f.allowJoins {
if r.ID == event.RoomID() {
- r.InsertEvent(f.t, &types.HeaderedEvent{Event: event})
+ r.InsertEvent(f.t, &types.HeaderedEvent{PDU: event})
f.t.Logf("Join event: %v", event.EventID())
res.StateEvents = types.NewEventJSONsFromHeaderedEvents(r.CurrentState())
res.AuthEvents = types.NewEventJSONsFromHeaderedEvents(r.Events())
diff --git a/federationapi/internal/federationclient.go b/federationapi/internal/federationclient.go
index dd329057..d4d7269d 100644
--- a/federationapi/internal/federationclient.go
+++ b/federationapi/internal/federationclient.go
@@ -27,7 +27,7 @@ func (a *FederationInternalAPI) MakeJoin(
}
func (a *FederationInternalAPI) SendJoin(
- ctx context.Context, origin, s spec.ServerName, event *gomatrixserverlib.Event,
+ ctx context.Context, origin, s spec.ServerName, event gomatrixserverlib.PDU,
) (res gomatrixserverlib.SendJoinResponse, err error) {
ctx, cancel := context.WithTimeout(ctx, defaultTimeout)
defer cancel()
diff --git a/federationapi/internal/perform.go b/federationapi/internal/perform.go
index 9dbc0e27..f55995ca 100644
--- a/federationapi/internal/perform.go
+++ b/federationapi/internal/perform.go
@@ -204,7 +204,7 @@ func (r *FederationInternalAPI) performJoinUsingServer(
user.Domain(),
roomserverAPI.KindNew,
response.StateSnapshot,
- &types.HeaderedEvent{Event: response.JoinEvent},
+ &types.HeaderedEvent{PDU: response.JoinEvent},
serverName,
nil,
false,
@@ -389,7 +389,7 @@ func (r *FederationInternalAPI) performOutboundPeekUsingServer(
StateEvents: gomatrixserverlib.NewEventJSONsFromEvents(stateEvents),
AuthEvents: gomatrixserverlib.NewEventJSONsFromEvents(authEvents),
},
- &types.HeaderedEvent{Event: respPeek.LatestEvent},
+ &types.HeaderedEvent{PDU: respPeek.LatestEvent},
serverName,
nil,
false,
@@ -536,7 +536,7 @@ func (r *FederationInternalAPI) PerformInvite(
"destination": destination,
}).Info("Sending invite")
- inviteReq, err := fclient.NewInviteV2Request(request.Event.Event, request.InviteRoomState)
+ inviteReq, err := fclient.NewInviteV2Request(request.Event.PDU, request.InviteRoomState)
if err != nil {
return fmt.Errorf("gomatrixserverlib.NewInviteV2Request: %w", err)
}
@@ -554,7 +554,7 @@ func (r *FederationInternalAPI) PerformInvite(
if err != nil {
return fmt.Errorf("r.federation.SendInviteV2 failed to decode event response: %w", err)
}
- response.Event = &types.HeaderedEvent{Event: inviteEvent}
+ response.Event = &types.HeaderedEvent{PDU: inviteEvent}
return nil
}
@@ -603,7 +603,7 @@ func (r *FederationInternalAPI) MarkServersAlive(destinations []spec.ServerName)
}
}
-func checkEventsContainCreateEvent(events []*gomatrixserverlib.Event) error {
+func checkEventsContainCreateEvent(events []gomatrixserverlib.PDU) error {
// sanity check we have a create event and it has a known room version
for _, ev := range events {
if ev.Type() == spec.MRoomCreate && ev.StateKeyEquals("") {
diff --git a/federationapi/queue/queue_test.go b/federationapi/queue/queue_test.go
index c1923911..cc38e136 100644
--- a/federationapi/queue/queue_test.go
+++ b/federationapi/queue/queue_test.go
@@ -109,7 +109,7 @@ func mustCreatePDU(t *testing.T) *types.HeaderedEvent {
if err != nil {
t.Fatalf("failed to create event: %v", err)
}
- return &types.HeaderedEvent{Event: ev}
+ return &types.HeaderedEvent{PDU: ev}
}
func mustCreateEDU(t *testing.T) *gomatrixserverlib.EDU {
diff --git a/federationapi/routing/backfill.go b/federationapi/routing/backfill.go
index 06685387..05488af6 100644
--- a/federationapi/routing/backfill.go
+++ b/federationapi/routing/backfill.go
@@ -103,18 +103,18 @@ func Backfill(
}
// Filter any event that's not from the requested room out.
- evs := make([]*gomatrixserverlib.Event, 0)
+ evs := make([]gomatrixserverlib.PDU, 0)
var ev *types.HeaderedEvent
for _, ev = range res.Events {
if ev.RoomID() == roomID {
- evs = append(evs, ev.Event)
+ evs = append(evs, ev.PDU)
}
}
eventJSONs := []json.RawMessage{}
for _, e := range gomatrixserverlib.ReverseTopologicalOrdering(
- gomatrixserverlib.ToPDUs(evs),
+ evs,
gomatrixserverlib.TopologicalOrderByPrevEvents,
) {
eventJSONs = append(eventJSONs, e.JSON())
diff --git a/federationapi/routing/events.go b/federationapi/routing/events.go
index e2547389..efd64dce 100644
--- a/federationapi/routing/events.go
+++ b/federationapi/routing/events.go
@@ -80,7 +80,7 @@ func allowedToSeeEvent(
}
// fetchEvent fetches the event without auth checks. Returns an error if the event cannot be found.
-func fetchEvent(ctx context.Context, rsAPI api.FederationRoomserverAPI, roomID, eventID string) (*gomatrixserverlib.Event, *util.JSONResponse) {
+func fetchEvent(ctx context.Context, rsAPI api.FederationRoomserverAPI, roomID, eventID string) (gomatrixserverlib.PDU, *util.JSONResponse) {
var eventsResponse api.QueryEventsByIDResponse
err := rsAPI.QueryEventsByID(
ctx,
@@ -99,5 +99,5 @@ func fetchEvent(ctx context.Context, rsAPI api.FederationRoomserverAPI, roomID,
}
}
- return eventsResponse.Events[0].Event, nil
+ return eventsResponse.Events[0].PDU, nil
}
diff --git a/federationapi/routing/invite.go b/federationapi/routing/invite.go
index 6fa37f4a..0fcb6414 100644
--- a/federationapi/routing/invite.go
+++ b/federationapi/routing/invite.go
@@ -107,7 +107,7 @@ func InviteV1(
func processInvite(
ctx context.Context,
isInviteV2 bool,
- event *gomatrixserverlib.Event,
+ event gomatrixserverlib.PDU,
roomVer gomatrixserverlib.RoomVersion,
strippedState []fclient.InviteV2StrippedState,
roomID string,
@@ -198,7 +198,7 @@ func processInvite(
)
// Add the invite event to the roomserver.
- inviteEvent := &types.HeaderedEvent{Event: &signedEvent}
+ inviteEvent := &types.HeaderedEvent{PDU: signedEvent}
request := &api.PerformInviteRequest{
Event: inviteEvent,
InviteRoomState: strippedState,
diff --git a/federationapi/routing/join.go b/federationapi/routing/join.go
index a6a7511c..a67262da 100644
--- a/federationapi/routing/join.go
+++ b/federationapi/routing/join.go
@@ -163,13 +163,13 @@ func MakeJoin(
}
// Check that the join is allowed or not
- stateEvents := make([]*gomatrixserverlib.Event, len(queryRes.StateEvents))
+ stateEvents := make([]gomatrixserverlib.PDU, len(queryRes.StateEvents))
for i := range queryRes.StateEvents {
- stateEvents[i] = queryRes.StateEvents[i].Event
+ stateEvents[i] = queryRes.StateEvents[i].PDU
}
- provider := gomatrixserverlib.NewAuthEvents(gomatrixserverlib.ToPDUs(stateEvents))
- if err = gomatrixserverlib.Allowed(event.Event, &provider); err != nil {
+ provider := gomatrixserverlib.NewAuthEvents(stateEvents)
+ if err = gomatrixserverlib.Allowed(event.PDU, &provider); err != nil {
return util.JSONResponse{
Code: http.StatusForbidden,
JSON: jsonerror.Forbidden(err.Error()),
@@ -414,7 +414,7 @@ func SendJoin(
InputRoomEvents: []api.InputRoomEvent{
{
Kind: api.KindNew,
- Event: &types.HeaderedEvent{Event: &signed},
+ Event: &types.HeaderedEvent{PDU: signed},
SendAsServer: string(cfg.Matrix.ServerName),
TransactionID: nil,
},
diff --git a/federationapi/routing/leave.go b/federationapi/routing/leave.go
index c9b13b98..da677d5d 100644
--- a/federationapi/routing/leave.go
+++ b/federationapi/routing/leave.go
@@ -110,12 +110,12 @@ func MakeLeave(
}
// Check that the leave is allowed or not
- stateEvents := make([]*gomatrixserverlib.Event, len(queryRes.StateEvents))
+ stateEvents := make([]gomatrixserverlib.PDU, len(queryRes.StateEvents))
for i := range queryRes.StateEvents {
- stateEvents[i] = queryRes.StateEvents[i].Event
+ stateEvents[i] = queryRes.StateEvents[i].PDU
}
- provider := gomatrixserverlib.NewAuthEvents(gomatrixserverlib.ToPDUs(stateEvents))
- if err = gomatrixserverlib.Allowed(event.Event, &provider); err != nil {
+ provider := gomatrixserverlib.NewAuthEvents(stateEvents)
+ if err = gomatrixserverlib.Allowed(event, &provider); err != nil {
return util.JSONResponse{
Code: http.StatusForbidden,
JSON: jsonerror.Forbidden(err.Error()),
@@ -313,7 +313,7 @@ func SendLeave(
InputRoomEvents: []api.InputRoomEvent{
{
Kind: api.KindNew,
- Event: &types.HeaderedEvent{Event: event},
+ Event: &types.HeaderedEvent{PDU: event},
SendAsServer: string(cfg.Matrix.ServerName),
TransactionID: nil,
},
diff --git a/federationapi/routing/peek.go b/federationapi/routing/peek.go
index 45dc7f8c..efc46146 100644
--- a/federationapi/routing/peek.go
+++ b/federationapi/routing/peek.go
@@ -91,7 +91,7 @@ func Peek(
StateEvents: types.NewEventJSONsFromHeaderedEvents(response.StateEvents),
AuthEvents: types.NewEventJSONsFromHeaderedEvents(response.AuthChainEvents),
RoomVersion: response.RoomVersion,
- LatestEvent: response.LatestEvent.Event,
+ LatestEvent: response.LatestEvent.PDU,
RenewalInterval: renewalInterval,
}
diff --git a/federationapi/routing/threepid.go b/federationapi/routing/threepid.go
index 1b854501..fdfc7bce 100644
--- a/federationapi/routing/threepid.go
+++ b/federationapi/routing/threepid.go
@@ -86,7 +86,7 @@ func CreateInvitesFrom3PIDInvites(
return jsonerror.InternalServerError()
}
if event != nil {
- evs = append(evs, &types.HeaderedEvent{Event: event})
+ evs = append(evs, &types.HeaderedEvent{PDU: event})
}
}
@@ -210,7 +210,7 @@ func ExchangeThirdPartyInvite(
httpReq.Context(), rsAPI,
api.KindNew,
[]*types.HeaderedEvent{
- {Event: inviteEvent},
+ {PDU: inviteEvent},
},
request.Destination(),
request.Origin(),
@@ -325,7 +325,7 @@ func buildMembershipEvent(
authEvents := gomatrixserverlib.NewAuthEvents(nil)
for i := range queryRes.StateEvents {
- err = authEvents.AddEvent(queryRes.StateEvents[i].Event)
+ err = authEvents.AddEvent(queryRes.StateEvents[i].PDU)
if err != nil {
return nil, err
}