From 389aabfd9a0901a71cb4a630b055523c1b1b369d Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Thu, 2 Apr 2020 11:40:29 +0100 Subject: [PATCH] Remodel a bit, don't reflect received invites --- federationsender/consumers/roomserver.go | 78 +++++++++++++++++------- go.mod | 2 +- go.sum | 16 +---- 3 files changed, 59 insertions(+), 37 deletions(-) diff --git a/federationsender/consumers/roomserver.go b/federationsender/consumers/roomserver.go index 2e14b859e..3dc32d31c 100644 --- a/federationsender/consumers/roomserver.go +++ b/federationsender/consumers/roomserver.go @@ -32,6 +32,7 @@ import ( // OutputRoomEventConsumer consumes events that originated in the room server. type OutputRoomEventConsumer struct { + cfg *config.Dendrite roomServerConsumer *common.ContinualConsumer db storage.Database queues *queue.OutgoingQueues @@ -52,6 +53,7 @@ func NewOutputRoomEventConsumer( PartitionStore: store, } s := &OutputRoomEventConsumer{ + cfg: cfg, roomServerConsumer: &consumer, db: store, queues: queues, @@ -128,6 +130,11 @@ func (s *OutputRoomEventConsumer) onMessage(msg *sarama.ConsumerMessage) error { // processMessage updates the list of currently joined hosts in the room // and then sends the event to the hosts that were joined before the event. func (s *OutputRoomEventConsumer) processMessage(ore api.OutputNewRoomEvent) error { + if ore.SendAsServer == api.DoNotSendToOtherServers { + // Ignore event that we don't need to send anywhere. + return nil + } + addsStateEvents, err := s.lookupStateEvents(ore.AddsStateEventIDs, ore.Event.Event) if err != nil { return err @@ -161,11 +168,6 @@ func (s *OutputRoomEventConsumer) processMessage(ore api.OutputNewRoomEvent) err return nil } - if ore.SendAsServer == api.DoNotSendToOtherServers { - // Ignore event that we don't need to send anywhere. - return nil - } - // Work out which hosts were joined at the event itself. joinedHostsAtEvent, err := s.joinedHostsAtEvent(ore, oldJoinedHosts) if err != nil { @@ -180,29 +182,61 @@ func (s *OutputRoomEventConsumer) processMessage(ore api.OutputNewRoomEvent) err // processInvite handles an invite event for sending over federation. func (s *OutputRoomEventConsumer) processInvite(oie api.OutputNewInviteEvent) error { - queryReq := api.QueryLatestEventsAndStateRequest{ - RoomID: oie.Event.RoomID(), - StateToFetch: []gomatrixserverlib.StateKeyTuple{ - gomatrixserverlib.StateKeyTuple{EventType: gomatrixserverlib.MRoomName, StateKey: ""}, - gomatrixserverlib.StateKeyTuple{EventType: gomatrixserverlib.MRoomCanonicalAlias, StateKey: ""}, - gomatrixserverlib.StateKeyTuple{EventType: gomatrixserverlib.MRoomAliases, StateKey: ""}, - gomatrixserverlib.StateKeyTuple{EventType: gomatrixserverlib.MRoomJoinRules, StateKey: ""}, - }, - } - queryRes := api.QueryLatestEventsAndStateResponse{} - if err := s.query.QueryLatestEventsAndState(context.TODO(), &queryReq, &queryRes); err != nil { - return err + // Don't try to reflect and resend invites that didn't originate from us. + if s.cfg.Matrix.ServerName != oie.Event.Origin() { + return nil } - var strippedState []gomatrixserverlib.InviteV2StrippedState - for _, state := range queryRes.StateEvents { - event := state.Unwrap() - strippedState = append(strippedState, gomatrixserverlib.NewInviteV2StrippedState(&event)) + // When sending a v2 invite, the inviting server should try and include + // a "stripped down" version of the room state. This is pretty much just + // enough information for the remote side to show something useful to the + // user, like the room name, aliases etc. + strippedState := []gomatrixserverlib.InviteV2StrippedState{} + stateWanted := []string{ + gomatrixserverlib.MRoomName, gomatrixserverlib.MRoomCanonicalAlias, + gomatrixserverlib.MRoomAliases, gomatrixserverlib.MRoomJoinRules, } + // For each of the state keys that we want to try and send, ask the + // roomserver if we have a state event for that room that matches the + // state key. + for _, wanted := range stateWanted { + queryReq := api.QueryLatestEventsAndStateRequest{ + RoomID: oie.Event.RoomID(), + StateToFetch: []gomatrixserverlib.StateKeyTuple{ + gomatrixserverlib.StateKeyTuple{ + EventType: wanted, + StateKey: "", + }, + }, + } + // If this fails then we just move onto the next event - we don't + // actually know at this point whether the room even has that type + // of state. + queryRes := api.QueryLatestEventsAndStateResponse{} + if err := s.query.QueryLatestEventsAndState(context.TODO(), &queryReq, &queryRes); err != nil { + log.WithFields(log.Fields{ + "room_id": queryReq.RoomID, + "event_type": wanted, + }).WithError(err).Info("couldn't find state to strip") + continue + } + // Append the stripped down copy of the state to our list. + for _, headeredEvent := range queryRes.StateEvents { + event := headeredEvent.Unwrap() + strippedState = append(strippedState, gomatrixserverlib.NewInviteV2StrippedState(&event)) + + log.WithFields(log.Fields{ + "room_id": queryReq.RoomID, + "event_type": event.Type(), + }).Info("adding stripped state") + } + } + + // Build the invite request with the info we've got. inviteReq, err := gomatrixserverlib.NewInviteV2Request(&oie.Event, strippedState) if err != nil { - return err + return fmt.Errorf("gomatrixserverlib.NewInviteV2Request: %w", err) } // Send the event. diff --git a/go.mod b/go.mod index 802d9499e..2c8c6d37d 100644 --- a/go.mod +++ b/go.mod @@ -9,7 +9,7 @@ require ( github.com/matrix-org/go-http-js-libp2p v0.0.0-20200318135427-31631a9ef51f github.com/matrix-org/go-sqlite3-js v0.0.0-20200325174927-327088cdef10 github.com/matrix-org/gomatrix v0.0.0-20190528120928-7df988a63f26 - github.com/matrix-org/gomatrixserverlib v0.0.0-20200402094320-cc9719221ef6 + github.com/matrix-org/gomatrixserverlib v0.0.0-20200402100901-4537e9f1c07f github.com/matrix-org/naffka v0.0.0-20200127221512-0716baaabaf1 github.com/matrix-org/util v0.0.0-20190711121626-527ce5ddefc7 github.com/mattn/go-sqlite3 v2.0.2+incompatible diff --git a/go.sum b/go.sum index 10a909a9b..82e781eef 100644 --- a/go.sum +++ b/go.sum @@ -130,20 +130,8 @@ github.com/matrix-org/gomatrix v0.0.0-20190528120928-7df988a63f26 h1:Hr3zjRsq2bh github.com/matrix-org/gomatrix v0.0.0-20190528120928-7df988a63f26/go.mod h1:3fxX6gUjWyI/2Bt7J1OLhpCzOfO/bB3AiX0cJtEKud0= github.com/matrix-org/gomatrixserverlib v0.0.0-20200124100636-0c2ec91d1df5 h1:kmRjpmFOenVpOaV/DRlo9p6z/IbOKlUC+hhKsAAh8Qg= github.com/matrix-org/gomatrixserverlib v0.0.0-20200124100636-0c2ec91d1df5/go.mod h1:FsKa2pWE/bpQql9H7U4boOPXFoJX/QcqaZZ6ijLkaZI= -github.com/matrix-org/gomatrixserverlib v0.0.0-20200401160213-1316eff9cef1 h1:7U5P1WA8mMmbQ2CKAttco7Pmls2Rs2Sj2HjqgRWwVDk= -github.com/matrix-org/gomatrixserverlib v0.0.0-20200401160213-1316eff9cef1/go.mod h1:FsKa2pWE/bpQql9H7U4boOPXFoJX/QcqaZZ6ijLkaZI= -github.com/matrix-org/gomatrixserverlib v0.0.0-20200401161903-a3e8d6416da9 h1:67k9RvLu4hss1gaBOmW1RDlEa3uVA63ffWdgpCjD4xk= -github.com/matrix-org/gomatrixserverlib v0.0.0-20200401161903-a3e8d6416da9/go.mod h1:FsKa2pWE/bpQql9H7U4boOPXFoJX/QcqaZZ6ijLkaZI= -github.com/matrix-org/gomatrixserverlib v0.0.0-20200402085213-cc2e2618557a h1:Y62i3CUxY0sc4u0bJ14nJcyre57oyQkhgNJcyYCpgZY= -github.com/matrix-org/gomatrixserverlib v0.0.0-20200402085213-cc2e2618557a/go.mod h1:FsKa2pWE/bpQql9H7U4boOPXFoJX/QcqaZZ6ijLkaZI= -github.com/matrix-org/gomatrixserverlib v0.0.0-20200402085826-4984d4aff1fe h1:kjWJTx0of+DHkCRF74vdLRKpGexMVas2Wo3lBn2vgmA= -github.com/matrix-org/gomatrixserverlib v0.0.0-20200402085826-4984d4aff1fe/go.mod h1:FsKa2pWE/bpQql9H7U4boOPXFoJX/QcqaZZ6ijLkaZI= -github.com/matrix-org/gomatrixserverlib v0.0.0-20200402091320-7d0d154abbc0 h1:D6rx1SGsGr76s9JaQ3OkNkA8wvJvSWOwAx3s5iEHnVg= -github.com/matrix-org/gomatrixserverlib v0.0.0-20200402091320-7d0d154abbc0/go.mod h1:FsKa2pWE/bpQql9H7U4boOPXFoJX/QcqaZZ6ijLkaZI= -github.com/matrix-org/gomatrixserverlib v0.0.0-20200402093729-4bed8c617258 h1:qm7odcVT00ifuD7H11GU1ZNCAmIIn4tKLBtQxAFMu18= -github.com/matrix-org/gomatrixserverlib v0.0.0-20200402093729-4bed8c617258/go.mod h1:FsKa2pWE/bpQql9H7U4boOPXFoJX/QcqaZZ6ijLkaZI= -github.com/matrix-org/gomatrixserverlib v0.0.0-20200402094320-cc9719221ef6 h1:y7C0BFmD5PONJqW0ohx5LsoCDcfXr5D378ottjv8iCY= -github.com/matrix-org/gomatrixserverlib v0.0.0-20200402094320-cc9719221ef6/go.mod h1:FsKa2pWE/bpQql9H7U4boOPXFoJX/QcqaZZ6ijLkaZI= +github.com/matrix-org/gomatrixserverlib v0.0.0-20200402100901-4537e9f1c07f h1:3flspujT88u6NgS0jgo/IypwT7TsvIXCvasFwzwh9WQ= +github.com/matrix-org/gomatrixserverlib v0.0.0-20200402100901-4537e9f1c07f/go.mod h1:FsKa2pWE/bpQql9H7U4boOPXFoJX/QcqaZZ6ijLkaZI= github.com/matrix-org/naffka v0.0.0-20200127221512-0716baaabaf1 h1:osLoFdOy+ChQqVUn2PeTDETFftVkl4w9t/OW18g3lnk= github.com/matrix-org/naffka v0.0.0-20200127221512-0716baaabaf1/go.mod h1:cXoYQIENbdWIQHt1SyCo6Bl3C3raHwJ0wgVrXHSqf+A= github.com/matrix-org/util v0.0.0-20171127121716-2e2df66af2f5 h1:W7l5CP4V7wPyPb4tYE11dbmeAOwtFQBTW0rf4OonOS8=