From 675759c192b90a76c7784cd45a0e8b8a8a5c046f Mon Sep 17 00:00:00 2001 From: Kegsay Date: Mon, 15 May 2017 17:41:54 +0100 Subject: [PATCH] Add invites to /sync responses (#103) --- .../cmd/syncserver-integration-tests/main.go | 29 +++++++++++++++++++ .../dendrite/syncapi/storage/syncserver.go | 25 ++++++++++++---- .../dendrite/syncapi/types/types.go | 2 +- 3 files changed, 50 insertions(+), 6 deletions(-) diff --git a/src/github.com/matrix-org/dendrite/cmd/syncserver-integration-tests/main.go b/src/github.com/matrix-org/dendrite/cmd/syncserver-integration-tests/main.go index 8d04c734..5a1c94e1 100644 --- a/src/github.com/matrix-org/dendrite/cmd/syncserver-integration-tests/main.go +++ b/src/github.com/matrix-org/dendrite/cmd/syncserver-integration-tests/main.go @@ -508,6 +508,35 @@ func main() { // $ curl -XPUT -d '{"name":"A Different Custom Room Name"}' "http://localhost:8009/_matrix/client/r0/rooms/%21PjrbIMW2cIiaYF4t:localhost/state/m.room.name?access_token=@alice:localhost" // $ curl -XPUT -d '{"msgtype":"m.text","body":"hello bob"}' "http://localhost:8009/_matrix/client/r0/rooms/%21PjrbIMW2cIiaYF4t:localhost/send/m.room.message/2?access_token=@alice:localhost" // $ curl -XPUT -d '{"membership":"invite"}' "http://localhost:8009/_matrix/client/r0/rooms/%21PjrbIMW2cIiaYF4t:localhost/state/m.room.member/@charlie:localhost?access_token=@bob:localhost" + if err := exe.WriteToTopic(inputTopic, canonicalJSONInput(outputRoomEventTestData[11:14])); err != nil { + panic(err) + } + + // Make sure charlie sees the invite both with and without a ?since= token + // TODO: Invite state should include the invite event and the room name. + charlieInviteData := `{ + "account_data": { + "events": [] + }, + "next_batch": "14", + "presence": { + "events": [] + }, + "rooms": { + "invite": { + "!PjrbIMW2cIiaYF4t:localhost": { + "invite_state": { + "events": [] + } + } + }, + "join": {}, + "leave": {} + } + }` + testSyncServer(syncServerCmdChan, "@charlie:localhost", "7", charlieInviteData) + testSyncServer(syncServerCmdChan, "@charlie:localhost", "", charlieInviteData) + // $ curl -XPUT -d '{"membership":"join"}' "http://localhost:8009/_matrix/client/r0/rooms/%21PjrbIMW2cIiaYF4t:localhost/state/m.room.member/@charlie:localhost?access_token=@charlie:localhost" // $ curl -XPUT -d '{"msgtype":"m.text","body":"not charlie..."}' "http://localhost:8009/_matrix/client/r0/rooms/%21PjrbIMW2cIiaYF4t:localhost/send/m.room.message/3?access_token=@alice:localhost" // $ curl -XPUT -d '{"membership":"leave"}' "http://localhost:8009/_matrix/client/r0/rooms/%21PjrbIMW2cIiaYF4t:localhost/state/m.room.member/@charlie:localhost?access_token=@alice:localhost" diff --git a/src/github.com/matrix-org/dendrite/syncapi/storage/syncserver.go b/src/github.com/matrix-org/dendrite/syncapi/storage/syncserver.go index 5d0bdacc..83c43167 100644 --- a/src/github.com/matrix-org/dendrite/syncapi/storage/syncserver.go +++ b/src/github.com/matrix-org/dendrite/syncapi/storage/syncserver.go @@ -132,7 +132,7 @@ func (d *SyncServerDatabase) IncrementalSync(userID string, fromPos, toPos types // - For each room which has membership list changes: // * Check if the room is 'newly joined' (insufficient to just check for a join event because we allow dupe joins TODO). // If it is, then we need to send the full room state down (and 'limited' is always true). - // * TODO Check if user is still CURRENTLY invited to the room. If so, add room to 'invited' block. + // * Check if user is still CURRENTLY invited to the room. If so, add room to 'invited' block. // * TODO Check if the user is CURRENTLY left/banned. If so, add room to 'archived' block. // work out which rooms transitioned to 'joined' between the 2 stream positions and add full state where needed. @@ -175,7 +175,8 @@ func (d *SyncServerDatabase) IncrementalSync(userID string, fromPos, toPos types jr.State.Events = gomatrixserverlib.ToClientEvents(state[roomID], gomatrixserverlib.FormatSync) res.Rooms.Join[roomID] = *jr } - return nil + + return d.addInvitesToResponse(txn, userID, res) }) return } @@ -200,7 +201,7 @@ func (d *SyncServerDatabase) CompleteSync(userID string, numRecentEventsPerRoom return err } - // Build up a /sync response + // Build up a /sync response. Add joined rooms. res = types.NewResponse(pos) for _, roomID := range roomIDs { stateEvents, err := d.roomstate.CurrentState(txn, roomID) @@ -221,12 +222,26 @@ func (d *SyncServerDatabase) CompleteSync(userID string, numRecentEventsPerRoom jr.State.Events = gomatrixserverlib.ToClientEvents(stateEvents, gomatrixserverlib.FormatSync) res.Rooms.Join[roomID] = *jr } - // TODO: Add invites! - return nil + + return d.addInvitesToResponse(txn, userID, res) }) return } +func (d *SyncServerDatabase) addInvitesToResponse(txn *sql.Tx, userID string, res *types.Response) error { + // Add invites - TODO: This will break over federation as they won't be in the current state table according to Mark. + roomIDs, err := d.roomstate.SelectRoomIDsWithMembership(txn, userID, "invite") + if err != nil { + return err + } + for _, roomID := range roomIDs { + ir := types.NewInviteResponse() + // TODO: invite_state. The state won't be in the current state table in cases where you get invited over federation + res.Rooms.Invite[roomID] = *ir + } + return nil +} + // There may be some overlap where events in stateEvents are already in recentEvents, so filter // them out so we don't include them twice in the /sync response. They should be in recentEvents // only, so clients get to the correct state once they have rolled forward. diff --git a/src/github.com/matrix-org/dendrite/syncapi/types/types.go b/src/github.com/matrix-org/dendrite/syncapi/types/types.go index 588737de..a1c3e3c7 100644 --- a/src/github.com/matrix-org/dendrite/syncapi/types/types.go +++ b/src/github.com/matrix-org/dendrite/syncapi/types/types.go @@ -97,7 +97,7 @@ func NewJoinResponse() *JoinResponse { // InviteResponse represents a /sync response for a room which is under the 'invite' key. type InviteResponse struct { InviteState struct { - Events []gomatrixserverlib.ClientEvent + Events []gomatrixserverlib.ClientEvent `json:"events"` } `json:"invite_state"` }