From 166ac9d092855652cc01ed57c076ced0d48351df Mon Sep 17 00:00:00 2001 From: Brendan Abolivier Date: Tue, 22 Aug 2017 14:14:37 +0100 Subject: [PATCH] Fix sync not returning on room join (#195) * Use BuildEvent method on room join * Fix building the list of room members in the sync notifier * Fix building the list of room members in the sync notifier * Rephrase comment --- .../dendrite/clientapi/writers/joinroom.go | 51 +++---------------- .../dendrite/syncapi/sync/notifier.go | 3 ++ 2 files changed, 9 insertions(+), 45 deletions(-) diff --git a/src/github.com/matrix-org/dendrite/clientapi/writers/joinroom.go b/src/github.com/matrix-org/dendrite/clientapi/writers/joinroom.go index 6618b0bd..e50f5f27 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/writers/joinroom.go +++ b/src/github.com/matrix-org/dendrite/clientapi/writers/joinroom.go @@ -22,6 +22,7 @@ import ( "github.com/matrix-org/dendrite/clientapi/auth/authtypes" "github.com/matrix-org/dendrite/clientapi/auth/storage/accounts" + "github.com/matrix-org/dendrite/clientapi/events" "github.com/matrix-org/dendrite/clientapi/httputil" "github.com/matrix-org/dendrite/clientapi/jsonerror" "github.com/matrix-org/dendrite/clientapi/producers" @@ -168,52 +169,10 @@ func (r joinRoomReq) joinRoomUsingServers( var eb gomatrixserverlib.EventBuilder r.writeToBuilder(&eb, roomID) - needed, err := gomatrixserverlib.StateNeededForEventBuilder(&eb) - if err != nil { - return httputil.LogThenError(r.req, err) - } - - // Ask the roomserver for information about this room - queryReq := api.QueryLatestEventsAndStateRequest{ - RoomID: roomID, - StateToFetch: needed.Tuples(), - } var queryRes api.QueryLatestEventsAndStateResponse - if queryErr := r.queryAPI.QueryLatestEventsAndState(&queryReq, &queryRes); queryErr != nil { - return httputil.LogThenError(r.req, queryErr) - } - - if queryRes.RoomExists { - // The room exists in the local database, so we just have to send a join - // membership event and return the room ID - // TODO: Check if the user is allowed in the room (has been invited if - // the room is invite-only) - eb.Depth = queryRes.Depth - eb.PrevEvents = queryRes.LatestEvents - - authEvents := gomatrixserverlib.NewAuthEvents(nil) - - for i := range queryRes.StateEvents { - authEvents.AddEvent(&queryRes.StateEvents[i]) - } - - refs, err := needed.AuthEventReferences(&authEvents) - if err != nil { - return httputil.LogThenError(r.req, err) - } - eb.AuthEvents = refs - - now := time.Now() - eventID := fmt.Sprintf("$%s:%s", util.RandomString(16), r.cfg.Matrix.ServerName) - event, err := eb.Build( - eventID, now, r.cfg.Matrix.ServerName, r.cfg.Matrix.KeyID, r.cfg.Matrix.PrivateKey, - ) - if err != nil { - return httputil.LogThenError(r.req, err) - } - - if err := r.producer.SendEvents([]gomatrixserverlib.Event{event}, r.cfg.Matrix.ServerName); err != nil { - return httputil.LogThenError(r.req, err) + if event, err := events.BuildEvent(&eb, r.cfg, r.queryAPI, &queryRes); err == nil { + if sendErr := r.producer.SendEvents([]gomatrixserverlib.Event{*event}, r.cfg.Matrix.ServerName); err != nil { + return httputil.LogThenError(r.req, sendErr) } return util.JSONResponse{ @@ -222,6 +181,8 @@ func (r joinRoomReq) joinRoomUsingServers( RoomID string `json:"room_id"` }{roomID}, } + } else if err != events.ErrRoomNoExists { + return httputil.LogThenError(r.req, err) } if len(servers) == 0 { diff --git a/src/github.com/matrix-org/dendrite/syncapi/sync/notifier.go b/src/github.com/matrix-org/dendrite/syncapi/sync/notifier.go index c2fdd8f0..2fa4279c 100644 --- a/src/github.com/matrix-org/dendrite/syncapi/sync/notifier.go +++ b/src/github.com/matrix-org/dendrite/syncapi/sync/notifier.go @@ -79,6 +79,9 @@ func (n *Notifier) OnNewEvent(ev *gomatrixserverlib.Event, userID string, pos ty case "invite": userIDs = append(userIDs, userID) case "join": + // Manually append the new user's ID so they get notified + // along all members in the room + userIDs = append(userIDs, userID) n.addJoinedUser(ev.RoomID(), userID) case "leave": fallthrough