From ddd9841cb219ba7c4353eba96960dfb9f3c7fbbf Mon Sep 17 00:00:00 2001 From: Mark Haines Date: Wed, 20 Sep 2017 11:29:00 +0100 Subject: [PATCH] Use the invite table to list the active invites for a user --- .../syncapi/storage/current_room_state_table.go | 5 ++++- .../dendrite/syncapi/storage/invites_table.go | 9 +++++---- .../dendrite/syncapi/storage/syncserver.go | 12 ++++++++---- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/github.com/matrix-org/dendrite/syncapi/storage/current_room_state_table.go b/src/github.com/matrix-org/dendrite/syncapi/storage/current_room_state_table.go index e5430d14d..4ea06808a 100644 --- a/src/github.com/matrix-org/dendrite/syncapi/storage/current_room_state_table.go +++ b/src/github.com/matrix-org/dendrite/syncapi/storage/current_room_state_table.go @@ -140,7 +140,10 @@ func (s *currentRoomStateStatements) selectJoinedUsers( // SelectRoomIDsWithMembership returns the list of room IDs which have the given user in the given membership state. func (s *currentRoomStateStatements) selectRoomIDsWithMembership( - ctx context.Context, txn *sql.Tx, userID, membership string, + ctx context.Context, + txn *sql.Tx, + userID string, + membership string, // nolint: unparam ) ([]string, error) { stmt := common.TxStmt(txn, s.selectRoomIDsWithMembershipStmt) rows, err := stmt.QueryContext(ctx, userID, membership) diff --git a/src/github.com/matrix-org/dendrite/syncapi/storage/invites_table.go b/src/github.com/matrix-org/dendrite/syncapi/storage/invites_table.go index 2b937fec0..48b9e4aba 100644 --- a/src/github.com/matrix-org/dendrite/syncapi/storage/invites_table.go +++ b/src/github.com/matrix-org/dendrite/syncapi/storage/invites_table.go @@ -4,6 +4,8 @@ import ( "context" "database/sql" + "github.com/matrix-org/dendrite/common" + "github.com/matrix-org/gomatrixserverlib" ) @@ -67,11 +69,10 @@ func (s *inviteEventsStatements) insertInviteEvent( // selectInviteEventsInRange returns a map of room ID to invite event for the // active invites for the target user ID in the supplied range. func (s *inviteEventsStatements) selectInviteEventsInRange( - ctx context.Context, targetUserID string, startPos, endPos int64, + ctx context.Context, txn *sql.Tx, targetUserID string, startPos, endPos int64, ) (map[string]gomatrixserverlib.Event, error) { - rows, err := s.selectInviteEventsInRangeStmt.QueryContext( - ctx, targetUserID, startPos, endPos, - ) + stmt := common.TxStmt(txn, s.selectInviteEventsInRangeStmt) + rows, err := stmt.QueryContext(ctx, targetUserID, startPos, endPos) if err != nil { return nil, err } 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 9fc65ad0b..f8dca4d94 100644 --- a/src/github.com/matrix-org/dendrite/syncapi/storage/syncserver.go +++ b/src/github.com/matrix-org/dendrite/syncapi/storage/syncserver.go @@ -367,16 +367,20 @@ func (d *SyncServerDatabase) UpsertAccountData( func (d *SyncServerDatabase) addInvitesToResponse( ctx context.Context, txn *sql.Tx, userID string, - _, _ types.StreamPosition, + fromPos, toPos types.StreamPosition, 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(ctx, txn, userID, "invite") + invites, err := d.invites.selectInviteEventsInRange( + ctx, txn, userID, int64(fromPos), int64(toPos), + ) if err != nil { return err } - for _, roomID := range roomIDs { + for roomID, inviteEvent := range invites { ir := types.NewInviteResponse() + ir.InviteState.Events = gomatrixserverlib.ToClientEvents( + []gomatrixserverlib.Event{inviteEvent}, gomatrixserverlib.FormatSync, + ) // 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 }