Populate invite request with stripped state

This commit is contained in:
Neil Alexander 2020-04-02 10:46:31 +01:00
parent 2122f9a7cb
commit 520464e154
6 changed files with 42 additions and 30 deletions

View file

@ -180,8 +180,33 @@ 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
}
var strippedState []gomatrixserverlib.InviteV2StrippedState
for _, state := range queryRes.StateEvents {
event := state.Unwrap()
strippedState = append(strippedState, gomatrixserverlib.NewInviteV2StrippedState(&event))
}
inviteReq, err := gomatrixserverlib.NewInviteV2Request(&oie.Event, strippedState)
if err != nil {
return err
}
// Send the event.
return s.queues.SendInvite(&oie.Event)
return s.queues.SendInvite(&inviteReq)
}
// joinedHostsAtEvent works out a list of matrix servers that were joined to

View file

@ -42,7 +42,7 @@ type destinationQueue struct {
lastTransactionIDs []gomatrixserverlib.TransactionID
pendingEvents []*gomatrixserverlib.HeaderedEvent
pendingEDUs []*gomatrixserverlib.EDU
pendingInvites []*gomatrixserverlib.HeaderedEvent
pendingInvites []*gomatrixserverlib.InviteV2Request
}
// Send event adds the event to the pending queue for the destination.
@ -71,7 +71,7 @@ func (oq *destinationQueue) sendEDU(e *gomatrixserverlib.EDU) {
}
}
func (oq *destinationQueue) sendInvite(ev *gomatrixserverlib.HeaderedEvent) {
func (oq *destinationQueue) sendInvite(ev *gomatrixserverlib.InviteV2Request) {
oq.runningMutex.Lock()
defer oq.runningMutex.Unlock()
oq.pendingInvites = append(oq.pendingInvites, ev)
@ -113,36 +113,17 @@ func (oq *destinationQueue) next() *gomatrixserverlib.Transaction {
defer oq.runningMutex.Unlock()
if len(oq.pendingInvites) > 0 {
for _, invite := range oq.pendingInvites {
// TODO: Get the correct stripped state here
inviteReq, err := gomatrixserverlib.NewInviteV2Request(
invite,
[]gomatrixserverlib.InviteV2StrippedState{},
)
if err != nil {
log.WithFields(log.Fields{
"event_id": invite.EventID(),
"state_key": invite.StateKey(),
"destination": oq.destination,
}).WithError(err).Error("failed to create invite request")
continue
}
for _, inviteReq := range oq.pendingInvites {
ev := inviteReq.Event()
log.WithFields(log.Fields{
"event_id": ev.EventID(),
"state_key": ev.StateKey(),
"room_version": inviteReq.RoomVersion(),
}).WithError(err).Info("created invite request")
if _, err := oq.client.SendInviteV2(
context.TODO(),
oq.destination,
inviteReq,
*inviteReq,
); err != nil {
log.WithFields(log.Fields{
"event_id": invite.EventID(),
"state_key": invite.StateKey(),
"event_id": ev.EventID(),
"state_key": ev.StateKey(),
"destination": oq.destination,
}).WithError(err).Error("failed to send invite")
}

View file

@ -82,8 +82,9 @@ func (oqs *OutgoingQueues) SendEvent(
// SendEvent sends an event to the destinations
func (oqs *OutgoingQueues) SendInvite(
ev *gomatrixserverlib.HeaderedEvent,
inviteReq *gomatrixserverlib.InviteV2Request,
) error {
ev := inviteReq.Event()
stateKey := ev.StateKey()
if stateKey == nil {
log.WithFields(log.Fields{
@ -117,7 +118,7 @@ func (oqs *OutgoingQueues) SendInvite(
oqs.queues[destination] = oq
}
oq.sendInvite(ev)
oq.sendInvite(inviteReq)
return nil
}

2
go.mod
View file

@ -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-20200402091320-7d0d154abbc0
github.com/matrix-org/gomatrixserverlib v0.0.0-20200402094320-cc9719221ef6
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

4
go.sum
View file

@ -140,6 +140,10 @@ github.com/matrix-org/gomatrixserverlib v0.0.0-20200402085826-4984d4aff1fe h1:kj
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/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=

View file

@ -231,3 +231,4 @@ remote user can join room with version 2
User can invite local user to room with version 2
Remote user can backfill in a room with version 2
Inbound federation accepts attempts to join v2 rooms from servers with support
Outbound federation can send invites via v2 API