Track child event types, more updates

This commit is contained in:
Neil Alexander 2022-10-13 13:07:37 +01:00
parent 32b8a5b06d
commit 3a3b252b31
No known key found for this signature in database
GPG key ID: A02A2019A2BB0944
6 changed files with 59 additions and 97 deletions

View file

@ -32,6 +32,7 @@ CREATE TABLE IF NOT EXISTS syncapi_relations (
room_id TEXT NOT NULL, room_id TEXT NOT NULL,
event_id TEXT NOT NULL, event_id TEXT NOT NULL,
child_event_id TEXT NOT NULL, child_event_id TEXT NOT NULL,
child_event_type TEXT NOT NULL,
rel_type TEXT NOT NULL, rel_type TEXT NOT NULL,
CONSTRAINT syncapi_relations_unique UNIQUE (room_id, event_id, child_event_id, rel_type) CONSTRAINT syncapi_relations_unique UNIQUE (room_id, event_id, child_event_id, rel_type)
); );
@ -39,8 +40,8 @@ CREATE TABLE IF NOT EXISTS syncapi_relations (
const insertRelationSQL = "" + const insertRelationSQL = "" +
"INSERT INTO syncapi_relations (" + "INSERT INTO syncapi_relations (" +
" room_id, event_id, child_event_id, rel_type" + " room_id, event_id, child_event_id, child_event_type, rel_type" +
") VALUES ($1, $2, $3, $4) " + ") VALUES ($1, $2, $3, $4, $5) " +
" ON CONFLICT DO NOTHING" " ON CONFLICT DO NOTHING"
const deleteRelationSQL = "" + const deleteRelationSQL = "" +
@ -48,23 +49,19 @@ const deleteRelationSQL = "" +
const selectRelationsInRangeAscSQL = "" + const selectRelationsInRangeAscSQL = "" +
"SELECT id, child_event_id, rel_type FROM syncapi_relations" + "SELECT id, child_event_id, rel_type FROM syncapi_relations" +
" WHERE room_id = $1 AND event_id = $2 AND id > $3 AND id <= $4" + " WHERE room_id = $1 AND event_id = $2" +
" ORDER BY id ASC LIMIT $5" " AND ( $3 = '' OR rel_type = $3 )" +
" AND ( $4 = '' OR child_event_type = $4 )" +
" AND id > $5 AND id <= $6" +
" ORDER BY id ASC LIMIT $7"
const selectRelationsInRangeDescSQL = "" + const selectRelationsInRangeDescSQL = "" +
"SELECT id, child_event_id, rel_type FROM syncapi_relations" + "SELECT id, child_event_id, rel_type FROM syncapi_relations" +
" WHERE room_id = $1 AND event_id = $2 AND id >= $3 AND id < $4" + " WHERE room_id = $1 AND event_id = $2" +
" ORDER BY id DESC LIMIT $5" " AND ( $3 = '' OR rel_type = $3 )" +
" AND ( $4 = '' OR child_event_type = $4 )" +
const selectRelationsByTypeInRangeAscSQL = "" + " AND id >= $5 AND id < $6" +
"SELECT id, child_event_id, rel_type FROM syncapi_relations" + " ORDER BY id DESC LIMIT $7"
" WHERE room_id = $1 AND event_id = $2 AND rel_type = $3 AND id > $4 AND id <= $5" +
" ORDER BY id ASC LIMIT $6"
const selectRelationsByTypeInRangeDescSQL = "" +
"SELECT id, child_event_id, rel_type FROM syncapi_relations" +
" WHERE room_id = $1 AND event_id = $2 AND rel_type = $3 AND id >= $4 AND id < $5" +
" ORDER BY id DESC LIMIT $6"
const selectMaxRelationIDSQL = "" + const selectMaxRelationIDSQL = "" +
"SELECT COALESCE(MAX(id), 0) FROM syncapi_relations" "SELECT COALESCE(MAX(id), 0) FROM syncapi_relations"
@ -73,8 +70,6 @@ type relationsStatements struct {
insertRelationStmt *sql.Stmt insertRelationStmt *sql.Stmt
selectRelationsInRangeAscStmt *sql.Stmt selectRelationsInRangeAscStmt *sql.Stmt
selectRelationsInRangeDescStmt *sql.Stmt selectRelationsInRangeDescStmt *sql.Stmt
selectRelationsByTypeInRangeAscStmt *sql.Stmt
selectRelationsByTypeInRangeDescStmt *sql.Stmt
deleteRelationStmt *sql.Stmt deleteRelationStmt *sql.Stmt
selectMaxRelationIDStmt *sql.Stmt selectMaxRelationIDStmt *sql.Stmt
} }
@ -89,18 +84,16 @@ func NewPostgresRelationsTable(db *sql.DB) (tables.Relations, error) {
{&s.insertRelationStmt, insertRelationSQL}, {&s.insertRelationStmt, insertRelationSQL},
{&s.selectRelationsInRangeAscStmt, selectRelationsInRangeAscSQL}, {&s.selectRelationsInRangeAscStmt, selectRelationsInRangeAscSQL},
{&s.selectRelationsInRangeDescStmt, selectRelationsInRangeDescSQL}, {&s.selectRelationsInRangeDescStmt, selectRelationsInRangeDescSQL},
{&s.selectRelationsByTypeInRangeAscStmt, selectRelationsByTypeInRangeAscSQL},
{&s.selectRelationsByTypeInRangeDescStmt, selectRelationsByTypeInRangeDescSQL},
{&s.deleteRelationStmt, deleteRelationSQL}, {&s.deleteRelationStmt, deleteRelationSQL},
{&s.selectMaxRelationIDStmt, selectMaxRelationIDSQL}, {&s.selectMaxRelationIDStmt, selectMaxRelationIDSQL},
}.Prepare(db) }.Prepare(db)
} }
func (s *relationsStatements) InsertRelation( func (s *relationsStatements) InsertRelation(
ctx context.Context, txn *sql.Tx, roomID, eventID, childEventID, relType string, ctx context.Context, txn *sql.Tx, roomID, eventID, childEventID, childEventType, relType string,
) (err error) { ) (err error) {
_, err = sqlutil.TxStmt(txn, s.insertRelationStmt).ExecContext( _, err = sqlutil.TxStmt(txn, s.insertRelationStmt).ExecContext(
ctx, roomID, eventID, childEventID, relType, ctx, roomID, eventID, childEventID, childEventType, relType,
) )
return return
} }
@ -117,28 +110,17 @@ func (s *relationsStatements) DeleteRelation(
// SelectRelationsInRange returns a map rel_type -> []child_event_id // SelectRelationsInRange returns a map rel_type -> []child_event_id
func (s *relationsStatements) SelectRelationsInRange( func (s *relationsStatements) SelectRelationsInRange(
ctx context.Context, txn *sql.Tx, roomID, eventID, relType string, ctx context.Context, txn *sql.Tx, roomID, eventID, relType, eventType string,
r types.Range, limit int, r types.Range, limit int,
) (map[string][]types.RelationEntry, types.StreamPosition, error) { ) (map[string][]types.RelationEntry, types.StreamPosition, error) {
var lastPos types.StreamPosition var lastPos types.StreamPosition
var stmt *sql.Stmt var stmt *sql.Stmt
var rows *sql.Rows
var err error
if relType != "" {
if r.Backwards {
stmt = sqlutil.TxStmt(txn, s.selectRelationsByTypeInRangeDescStmt)
} else {
stmt = sqlutil.TxStmt(txn, s.selectRelationsByTypeInRangeAscStmt)
}
rows, err = stmt.QueryContext(ctx, roomID, eventID, relType, r.Low(), r.High(), limit)
} else {
if r.Backwards { if r.Backwards {
stmt = sqlutil.TxStmt(txn, s.selectRelationsInRangeDescStmt) stmt = sqlutil.TxStmt(txn, s.selectRelationsInRangeDescStmt)
} else { } else {
stmt = sqlutil.TxStmt(txn, s.selectRelationsInRangeAscStmt) stmt = sqlutil.TxStmt(txn, s.selectRelationsInRangeAscStmt)
} }
rows, err = stmt.QueryContext(ctx, roomID, eventID, r.Low(), r.High(), limit) rows, err := stmt.QueryContext(ctx, roomID, eventID, relType, eventType, r.Low(), r.High(), limit)
}
if err != nil { if err != nil {
return nil, lastPos, err return nil, lastPos, err
} }

View file

@ -606,7 +606,7 @@ func (d *Database) UpdateRelations(ctx context.Context, event *gomatrixserverlib
return d.Writer.Do(d.DB, nil, func(txn *sql.Tx) error { return d.Writer.Do(d.DB, nil, func(txn *sql.Tx) error {
return d.Relations.InsertRelation( return d.Relations.InsertRelation(
ctx, txn, event.RoomID(), content.Relations.EventID, ctx, txn, event.RoomID(), content.Relations.EventID,
event.EventID(), content.Relations.RelationType, event.EventID(), event.Type(), content.Relations.RelationType,
) )
}) })
} }

View file

@ -626,7 +626,7 @@ func (d *DatabaseTransaction) RelationsFor(ctx context.Context, roomID, eventID,
// First look up any relations from the database. We add one to the limit here // First look up any relations from the database. We add one to the limit here
// so that we can tell if we're overflowing, as we will only set the "next_batch" // so that we can tell if we're overflowing, as we will only set the "next_batch"
// in the response if we are. // in the response if we are.
relations, _, err := d.Relations.SelectRelationsInRange(ctx, d.txn, roomID, eventID, relType, r, limit+1) relations, _, err := d.Relations.SelectRelationsInRange(ctx, d.txn, roomID, eventID, relType, eventType, r, limit+1)
if err != nil { if err != nil {
return nil, "", "", fmt.Errorf("d.Relations.SelectRelationsInRange: %w", err) return nil, "", "", fmt.Errorf("d.Relations.SelectRelationsInRange: %w", err)
} }
@ -672,9 +672,6 @@ func (d *DatabaseTransaction) RelationsFor(ctx context.Context, roomID, eventID,
// type if it was specified. // type if it was specified.
clientEvents = make([]gomatrixserverlib.ClientEvent, 0, len(events)) clientEvents = make([]gomatrixserverlib.ClientEvent, 0, len(events))
for _, event := range events { for _, event := range events {
if eventType != "" && event.Type() != eventType {
continue
}
clientEvents = append( clientEvents = append(
clientEvents, clientEvents,
gomatrixserverlib.ToClientEvent(event.Event, gomatrixserverlib.FormatAll), gomatrixserverlib.ToClientEvent(event.Event, gomatrixserverlib.FormatAll),

View file

@ -30,6 +30,7 @@ CREATE TABLE IF NOT EXISTS syncapi_relations (
room_id TEXT NOT NULL, room_id TEXT NOT NULL,
event_id TEXT NOT NULL, event_id TEXT NOT NULL,
child_event_id TEXT NOT NULL, child_event_id TEXT NOT NULL,
child_event_type TEXT NOT NULL,
rel_type TEXT NOT NULL, rel_type TEXT NOT NULL,
UNIQUE (room_id, event_id, child_event_id, rel_type) UNIQUE (room_id, event_id, child_event_id, rel_type)
); );
@ -37,8 +38,8 @@ CREATE TABLE IF NOT EXISTS syncapi_relations (
const insertRelationSQL = "" + const insertRelationSQL = "" +
"INSERT INTO syncapi_relations (" + "INSERT INTO syncapi_relations (" +
" id, room_id, event_id, child_event_id, rel_type" + " id, room_id, event_id, child_event_id, child_event_type, rel_type" +
") VALUES ($1, $2, $3, $4, $5) " + ") VALUES ($1, $2, $3, $4, $5, $6) " +
" ON CONFLICT DO NOTHING" " ON CONFLICT DO NOTHING"
const deleteRelationSQL = "" + const deleteRelationSQL = "" +
@ -46,23 +47,19 @@ const deleteRelationSQL = "" +
const selectRelationsInRangeAscSQL = "" + const selectRelationsInRangeAscSQL = "" +
"SELECT id, child_event_id, rel_type FROM syncapi_relations" + "SELECT id, child_event_id, rel_type FROM syncapi_relations" +
" WHERE room_id = $1 AND event_id = $2 AND id > $3 AND id <= $4" + " WHERE room_id = $1 AND event_id = $2" +
" ORDER BY id ASC LIMIT $5" " AND ( $3 = '' OR rel_type = $3 )" +
" AND ( $4 = '' OR child_event_type = $4 )" +
" AND id > $5 AND id <= $6" +
" ORDER BY id ASC LIMIT $7"
const selectRelationsInRangeDescSQL = "" + const selectRelationsInRangeDescSQL = "" +
"SELECT id, child_event_id, rel_type FROM syncapi_relations" + "SELECT id, child_event_id, rel_type FROM syncapi_relations" +
" WHERE room_id = $1 AND event_id = $2 AND id >= $3 AND id < $4" + " WHERE room_id = $1 AND event_id = $2" +
" ORDER BY id DESC LIMIT $5" " AND ( $3 = '' OR rel_type = $3 )" +
" AND ( $4 = '' OR child_event_type = $4 )" +
const selectRelationsByTypeInRangeAscSQL = "" + " AND id >= $5 AND id < $6" +
"SELECT id, child_event_id, rel_type FROM syncapi_relations" + " ORDER BY id DESC LIMIT $7"
" WHERE room_id = $1 AND event_id = $2 AND rel_type = $3 AND id > $4 AND id <= $5" +
" ORDER BY id ASC LIMIT $6"
const selectRelationsByTypeInRangeDescSQL = "" +
"SELECT id, child_event_id, rel_type FROM syncapi_relations" +
" WHERE room_id = $1 AND event_id = $2 AND rel_type = $3 AND id >= $4 AND id < $5" +
" ORDER BY id DESC LIMIT $6"
const selectMaxRelationIDSQL = "" + const selectMaxRelationIDSQL = "" +
"SELECT COALESCE(MAX(id), 0) FROM syncapi_relations" "SELECT COALESCE(MAX(id), 0) FROM syncapi_relations"
@ -72,8 +69,6 @@ type relationsStatements struct {
insertRelationStmt *sql.Stmt insertRelationStmt *sql.Stmt
selectRelationsInRangeAscStmt *sql.Stmt selectRelationsInRangeAscStmt *sql.Stmt
selectRelationsInRangeDescStmt *sql.Stmt selectRelationsInRangeDescStmt *sql.Stmt
selectRelationsByTypeInRangeAscStmt *sql.Stmt
selectRelationsByTypeInRangeDescStmt *sql.Stmt
deleteRelationStmt *sql.Stmt deleteRelationStmt *sql.Stmt
selectMaxRelationIDStmt *sql.Stmt selectMaxRelationIDStmt *sql.Stmt
} }
@ -90,22 +85,20 @@ func NewSqliteRelationsTable(db *sql.DB, streamID *StreamIDStatements) (tables.R
{&s.insertRelationStmt, insertRelationSQL}, {&s.insertRelationStmt, insertRelationSQL},
{&s.selectRelationsInRangeAscStmt, selectRelationsInRangeAscSQL}, {&s.selectRelationsInRangeAscStmt, selectRelationsInRangeAscSQL},
{&s.selectRelationsInRangeDescStmt, selectRelationsInRangeDescSQL}, {&s.selectRelationsInRangeDescStmt, selectRelationsInRangeDescSQL},
{&s.selectRelationsByTypeInRangeAscStmt, selectRelationsByTypeInRangeAscSQL},
{&s.selectRelationsByTypeInRangeDescStmt, selectRelationsByTypeInRangeDescSQL},
{&s.deleteRelationStmt, deleteRelationSQL}, {&s.deleteRelationStmt, deleteRelationSQL},
{&s.selectMaxRelationIDStmt, selectMaxRelationIDSQL}, {&s.selectMaxRelationIDStmt, selectMaxRelationIDSQL},
}.Prepare(db) }.Prepare(db)
} }
func (s *relationsStatements) InsertRelation( func (s *relationsStatements) InsertRelation(
ctx context.Context, txn *sql.Tx, roomID, eventID, childEventID, relType string, ctx context.Context, txn *sql.Tx, roomID, eventID, childEventID, childEventType, relType string,
) (err error) { ) (err error) {
var streamPos types.StreamPosition var streamPos types.StreamPosition
if streamPos, err = s.streamIDStatements.nextRelationID(ctx, txn); err != nil { if streamPos, err = s.streamIDStatements.nextRelationID(ctx, txn); err != nil {
return return
} }
_, err = sqlutil.TxStmt(txn, s.insertRelationStmt).ExecContext( _, err = sqlutil.TxStmt(txn, s.insertRelationStmt).ExecContext(
ctx, streamPos, roomID, eventID, childEventID, relType, ctx, streamPos, roomID, eventID, childEventID, childEventType, relType,
) )
return return
} }
@ -122,28 +115,17 @@ func (s *relationsStatements) DeleteRelation(
// SelectRelationsInRange returns a map rel_type -> []child_event_id // SelectRelationsInRange returns a map rel_type -> []child_event_id
func (s *relationsStatements) SelectRelationsInRange( func (s *relationsStatements) SelectRelationsInRange(
ctx context.Context, txn *sql.Tx, roomID, eventID, relType string, ctx context.Context, txn *sql.Tx, roomID, eventID, relType, eventType string,
r types.Range, limit int, r types.Range, limit int,
) (map[string][]types.RelationEntry, types.StreamPosition, error) { ) (map[string][]types.RelationEntry, types.StreamPosition, error) {
var lastPos types.StreamPosition var lastPos types.StreamPosition
var stmt *sql.Stmt var stmt *sql.Stmt
var rows *sql.Rows
var err error
if relType != "" {
if r.Backwards {
stmt = sqlutil.TxStmt(txn, s.selectRelationsByTypeInRangeDescStmt)
} else {
stmt = sqlutil.TxStmt(txn, s.selectRelationsByTypeInRangeAscStmt)
}
rows, err = stmt.QueryContext(ctx, roomID, eventID, relType, r.Low(), r.High(), limit)
} else {
if r.Backwards { if r.Backwards {
stmt = sqlutil.TxStmt(txn, s.selectRelationsInRangeDescStmt) stmt = sqlutil.TxStmt(txn, s.selectRelationsInRangeDescStmt)
} else { } else {
stmt = sqlutil.TxStmt(txn, s.selectRelationsInRangeAscStmt) stmt = sqlutil.TxStmt(txn, s.selectRelationsInRangeAscStmt)
} }
rows, err = stmt.QueryContext(ctx, roomID, eventID, r.Low(), r.High(), limit) rows, err := stmt.QueryContext(ctx, roomID, eventID, relType, eventType, r.Low(), r.High(), limit)
}
if err != nil { if err != nil {
return nil, lastPos, err return nil, lastPos, err
} }

View file

@ -210,7 +210,7 @@ type Presence interface {
type Relations interface { type Relations interface {
// Inserts a relation which refers from the child event ID to the event ID in the given room. // Inserts a relation which refers from the child event ID to the event ID in the given room.
// If the relation already exists then this function will do nothing and return no error. // If the relation already exists then this function will do nothing and return no error.
InsertRelation(ctx context.Context, txn *sql.Tx, roomID, eventID, childEventID, relType string) (err error) InsertRelation(ctx context.Context, txn *sql.Tx, roomID, eventID, childEventID, childEventType, relType string) (err error)
// Deletes a relation which already exists as the result of an event redaction. If the relation // Deletes a relation which already exists as the result of an event redaction. If the relation
// does not exist then this function will do nothing and return no error. // does not exist then this function will do nothing and return no error.
DeleteRelation(ctx context.Context, txn *sql.Tx, roomID, childEventID string) error DeleteRelation(ctx context.Context, txn *sql.Tx, roomID, childEventID string) error
@ -219,7 +219,7 @@ type Relations interface {
// contain relations of that type, otherwise if "" is specified then all relations in the range // contain relations of that type, otherwise if "" is specified then all relations in the range
// will be returned, inclusive of the "to" position but excluding the "from" position. The stream // will be returned, inclusive of the "to" position but excluding the "from" position. The stream
// position returned is the maximum position of the returned results. // position returned is the maximum position of the returned results.
SelectRelationsInRange(ctx context.Context, txn *sql.Tx, roomID, eventID, relType string, r types.Range, limit int) (map[string][]types.RelationEntry, types.StreamPosition, error) SelectRelationsInRange(ctx context.Context, txn *sql.Tx, roomID, eventID, relType, eventType string, r types.Range, limit int) (map[string][]types.RelationEntry, types.StreamPosition, error)
// SelectMaxRelationID returns the maximum ID of all relations, used to determine what the boundaries // SelectMaxRelationID returns the maximum ID of all relations, used to determine what the boundaries
// should be if there are no boundaries supplied (i.e. we want to work backwards but don't have a // should be if there are no boundaries supplied (i.e. we want to work backwards but don't have a
// "from" or want to work forwards and don't have a "to"). // "from" or want to work forwards and don't have a "to").

View file

@ -43,7 +43,7 @@ func newRelationsTable(t *testing.T, dbType test.DBType) (tables.Relations, *sql
func compareRelationsToExpected(t *testing.T, tab tables.Relations, r types.Range, expected []types.RelationEntry) { func compareRelationsToExpected(t *testing.T, tab tables.Relations, r types.Range, expected []types.RelationEntry) {
ctx := context.Background() ctx := context.Background()
relations, _, err := tab.SelectRelationsInRange(ctx, nil, roomID, "a", "", r, 50) relations, _, err := tab.SelectRelationsInRange(ctx, nil, roomID, "a", "", "", r, 50)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -60,6 +60,7 @@ func compareRelationsToExpected(t *testing.T, tab tables.Relations, r types.Rang
} }
const roomID = "!roomid:server" const roomID = "!roomid:server"
const childType = "m.room.something"
const relType = "m.reaction" const relType = "m.reaction"
func TestRelationsTable(t *testing.T) { func TestRelationsTable(t *testing.T) {
@ -70,7 +71,7 @@ func TestRelationsTable(t *testing.T) {
// Insert some relations // Insert some relations
for _, child := range []string{"b", "c", "d"} { for _, child := range []string{"b", "c", "d"} {
if err := tab.InsertRelation(ctx, nil, roomID, "a", child, relType); err != nil { if err := tab.InsertRelation(ctx, nil, roomID, "a", child, childType, relType); err != nil {
t.Fatal(err) t.Fatal(err)
} }
} }
@ -138,7 +139,7 @@ func TestRelationsTable(t *testing.T) {
// Insert some new relations // Insert some new relations
for _, child := range []string{"e", "f", "g", "h"} { for _, child := range []string{"e", "f", "g", "h"} {
if err := tab.InsertRelation(ctx, nil, roomID, "a", child, relType); err != nil { if err := tab.InsertRelation(ctx, nil, roomID, "a", child, childType, relType); err != nil {
t.Fatal(err) t.Fatal(err)
} }
} }