mirror of
https://github.com/matrix-org/dendrite.git
synced 2025-12-13 18:03:10 -06:00
Invite events filtering
Signed-off-by: Thibaut CHARLES cromfr@gmail.com
This commit is contained in:
parent
4ef80a4e48
commit
a8daf97a13
|
|
@ -4,7 +4,9 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
|
||||||
|
"github.com/lib/pq"
|
||||||
"github.com/matrix-org/dendrite/common"
|
"github.com/matrix-org/dendrite/common"
|
||||||
|
"github.com/matrix-org/gomatrix"
|
||||||
"github.com/matrix-org/gomatrixserverlib"
|
"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'),
|
id BIGINT PRIMARY KEY DEFAULT nextval('syncapi_stream_id'),
|
||||||
event_id TEXT NOT NULL,
|
event_id TEXT NOT NULL,
|
||||||
room_id TEXT NOT NULL,
|
room_id TEXT NOT NULL,
|
||||||
|
type TEXT NOT NULL,
|
||||||
|
sender TEXT NOT NULL,
|
||||||
target_user_id TEXT NOT NULL,
|
target_user_id TEXT NOT NULL,
|
||||||
event_json TEXT NOT NULL
|
event_json TEXT NOT NULL
|
||||||
);
|
);
|
||||||
|
|
||||||
-- For looking up the invites for a given user.
|
-- For looking up the invites for a given user.
|
||||||
CREATE INDEX IF NOT EXISTS syncapi_invites_target_user_id_idx
|
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
|
-- For deleting old invites
|
||||||
CREATE INDEX IF NOT EXISTS syncapi_invites_event_id_idx
|
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 = "" +
|
const insertInviteEventSQL = "" +
|
||||||
"INSERT INTO syncapi_invite_events (" +
|
"INSERT INTO syncapi_invite_events (" +
|
||||||
" room_id, event_id, target_user_id, event_json" +
|
" room_id, event_id, type, sender, target_user_id, event_json" +
|
||||||
") VALUES ($1, $2, $3, $4) RETURNING id"
|
") VALUES ($1, $2, $3, $4, $5, $6) RETURNING id"
|
||||||
|
|
||||||
const deleteInviteEventSQL = "" +
|
const deleteInviteEventSQL = "" +
|
||||||
"DELETE FROM syncapi_invite_events WHERE event_id = $1"
|
"DELETE FROM syncapi_invite_events WHERE event_id = $1"
|
||||||
|
|
@ -37,6 +41,12 @@ const deleteInviteEventSQL = "" +
|
||||||
const selectInviteEventsInRangeSQL = "" +
|
const selectInviteEventsInRangeSQL = "" +
|
||||||
"SELECT room_id, event_json FROM syncapi_invite_events" +
|
"SELECT room_id, event_json FROM syncapi_invite_events" +
|
||||||
" WHERE target_user_id = $1 AND id > $2 AND id <= $3" +
|
" 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"
|
" ORDER BY id DESC"
|
||||||
|
|
||||||
const selectMaxInviteIDSQL = "" +
|
const selectMaxInviteIDSQL = "" +
|
||||||
|
|
@ -76,6 +86,8 @@ func (s *inviteEventsStatements) insertInviteEvent(
|
||||||
ctx,
|
ctx,
|
||||||
inviteEvent.RoomID(),
|
inviteEvent.RoomID(),
|
||||||
inviteEvent.EventID(),
|
inviteEvent.EventID(),
|
||||||
|
inviteEvent.Type(),
|
||||||
|
inviteEvent.Sender(),
|
||||||
*inviteEvent.StateKey(),
|
*inviteEvent.StateKey(),
|
||||||
inviteEvent.JSON(),
|
inviteEvent.JSON(),
|
||||||
).Scan(&streamPos)
|
).Scan(&streamPos)
|
||||||
|
|
@ -92,10 +104,18 @@ func (s *inviteEventsStatements) deleteInviteEvent(
|
||||||
// selectInviteEventsInRange returns a map of room ID to invite event for the
|
// selectInviteEventsInRange returns a map of room ID to invite event for the
|
||||||
// active invites for the target user ID in the supplied range.
|
// active invites for the target user ID in the supplied range.
|
||||||
func (s *inviteEventsStatements) selectInviteEventsInRange(
|
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) {
|
) (map[string]gomatrixserverlib.Event, error) {
|
||||||
stmt := common.TxStmt(txn, s.selectInviteEventsInRangeStmt)
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -255,7 +255,7 @@ func (d *SyncServerDatabase) IncrementalSync(
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: This should be done in getStateDeltas
|
// 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
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -338,8 +338,7 @@ func (d *SyncServerDatabase) CompleteSync(
|
||||||
res.Rooms.Join[roomID] = *jr
|
res.Rooms.Join[roomID] = *jr
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO: filter Invite events
|
if err = d.addInvitesToResponse(ctx, txn, userID, 0, posTo, filter, res); err != nil {
|
||||||
if err = d.addInvitesToResponse(ctx, txn, userID, 0, posTo, res); err != nil {
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -408,10 +407,11 @@ func (d *SyncServerDatabase) addInvitesToResponse(
|
||||||
ctx context.Context, txn *sql.Tx,
|
ctx context.Context, txn *sql.Tx,
|
||||||
userID string,
|
userID string,
|
||||||
fromPos, toPos types.StreamPosition,
|
fromPos, toPos types.StreamPosition,
|
||||||
|
filter *gomatrix.Filter,
|
||||||
res *types.Response,
|
res *types.Response,
|
||||||
) error {
|
) error {
|
||||||
invites, err := d.invites.selectInviteEventsInRange(
|
invites, err := d.invites.selectInviteEventsInRange(
|
||||||
ctx, txn, userID, int64(fromPos), int64(toPos),
|
ctx, txn, userID, int64(fromPos), int64(toPos), filter,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue