From a8daf97a138f8194ce9eb41eb616dc82522eb254 Mon Sep 17 00:00:00 2001 From: "Crom (Thibaut CHARLES)" Date: Sun, 14 Jan 2018 14:16:31 +0100 Subject: [PATCH] Invite events filtering Signed-off-by: Thibaut CHARLES cromfr@gmail.com --- .../dendrite/syncapi/storage/invites_table.go | 30 +++++++++++++++---- .../dendrite/syncapi/storage/syncserver.go | 8 ++--- 2 files changed, 29 insertions(+), 9 deletions(-) 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 88c98f7e3..4f80adf25 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,7 +4,9 @@ import ( "context" "database/sql" + "github.com/lib/pq" "github.com/matrix-org/dendrite/common" + "github.com/matrix-org/gomatrix" "github.com/matrix-org/gomatrixserverlib" ) @@ -13,13 +15,15 @@ CREATE TABLE IF NOT EXISTS syncapi_invite_events ( id BIGINT PRIMARY KEY DEFAULT nextval('syncapi_stream_id'), event_id TEXT NOT NULL, room_id TEXT NOT NULL, + type TEXT NOT NULL, + sender TEXT NOT NULL, target_user_id TEXT NOT NULL, event_json TEXT NOT NULL ); -- For looking up the invites for a given user. CREATE INDEX IF NOT EXISTS syncapi_invites_target_user_id_idx - ON syncapi_invite_events (target_user_id, id); + ON syncapi_invite_events (target_user_id, id, room_id, type, sender); -- For deleting old invites CREATE INDEX IF NOT EXISTS syncapi_invites_event_id_idx @@ -28,8 +32,8 @@ CREATE INDEX IF NOT EXISTS syncapi_invites_event_id_idx const insertInviteEventSQL = "" + "INSERT INTO syncapi_invite_events (" + - " room_id, event_id, target_user_id, event_json" + - ") VALUES ($1, $2, $3, $4) RETURNING id" + " room_id, event_id, type, sender, target_user_id, event_json" + + ") VALUES ($1, $2, $3, $4, $5, $6) RETURNING id" const deleteInviteEventSQL = "" + "DELETE FROM syncapi_invite_events WHERE event_id = $1" @@ -37,6 +41,12 @@ const deleteInviteEventSQL = "" + const selectInviteEventsInRangeSQL = "" + "SELECT room_id, event_json FROM syncapi_invite_events" + " WHERE target_user_id = $1 AND id > $2 AND id <= $3" + + " AND ( $4::text[] IS NULL OR sender = ANY($4) )" + + " AND ( $5::text[] IS NULL OR NOT(sender = ANY($5)) )" + + " AND ( $6::text[] IS NULL OR type LIKE ANY($6) )" + + " AND ( $7::text[] IS NULL OR NOT(type LIKE ANY($7)) )" + + " AND ( $8::text[] IS NULL OR room_id = ANY($8) )" + + " AND ( $9::text[] IS NULL OR NOT(room_id = ANY($9)) )" + " ORDER BY id DESC" const selectMaxInviteIDSQL = "" + @@ -76,6 +86,8 @@ func (s *inviteEventsStatements) insertInviteEvent( ctx, inviteEvent.RoomID(), inviteEvent.EventID(), + inviteEvent.Type(), + inviteEvent.Sender(), *inviteEvent.StateKey(), inviteEvent.JSON(), ).Scan(&streamPos) @@ -92,10 +104,18 @@ func (s *inviteEventsStatements) deleteInviteEvent( // 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, txn *sql.Tx, targetUserID string, startPos, endPos int64, + ctx context.Context, txn *sql.Tx, targetUserID string, startPos, endPos int64, filter *gomatrix.Filter, ) (map[string]gomatrixserverlib.Event, error) { stmt := common.TxStmt(txn, s.selectInviteEventsInRangeStmt) - rows, err := stmt.QueryContext(ctx, targetUserID, startPos, endPos) + + rows, err := stmt.QueryContext(ctx, targetUserID, startPos, endPos, + pq.StringArray(filterConvertWildcardToSQL(filter.Room.State.Types)), + pq.StringArray(filterConvertWildcardToSQL(filter.Room.State.NotTypes)), + pq.StringArray(filter.Room.State.Senders), + pq.StringArray(filter.Room.State.NotSenders), + pq.StringArray(append(filter.Room.Rooms, filter.Room.State.Rooms...)), + pq.StringArray(append(filter.Room.NotRooms, filter.Room.State.NotRooms...)), + ) 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 5622ec9aa..bfec0be30 100644 --- a/src/github.com/matrix-org/dendrite/syncapi/storage/syncserver.go +++ b/src/github.com/matrix-org/dendrite/syncapi/storage/syncserver.go @@ -255,7 +255,7 @@ func (d *SyncServerDatabase) IncrementalSync( } // TODO: This should be done in getStateDeltas - if err = d.addInvitesToResponse(ctx, txn, device.UserID, fromPos, toPos, res); err != nil { + if err = d.addInvitesToResponse(ctx, txn, device.UserID, fromPos, toPos, filter, res); err != nil { return nil, err } @@ -338,8 +338,7 @@ func (d *SyncServerDatabase) CompleteSync( res.Rooms.Join[roomID] = *jr } - //TODO: filter Invite events - if err = d.addInvitesToResponse(ctx, txn, userID, 0, posTo, res); err != nil { + if err = d.addInvitesToResponse(ctx, txn, userID, 0, posTo, filter, res); err != nil { return nil, err } @@ -408,10 +407,11 @@ func (d *SyncServerDatabase) addInvitesToResponse( ctx context.Context, txn *sql.Tx, userID string, fromPos, toPos types.StreamPosition, + filter *gomatrix.Filter, res *types.Response, ) error { invites, err := d.invites.selectInviteEventsInRange( - ctx, txn, userID, int64(fromPos), int64(toPos), + ctx, txn, userID, int64(fromPos), int64(toPos), filter, ) if err != nil { return err