diff --git a/setup/mscs/msc2946/msc2946.go b/setup/mscs/msc2946/msc2946.go index a2d801e4..accdbd39 100644 --- a/setup/mscs/msc2946/msc2946.go +++ b/setup/mscs/msc2946/msc2946.go @@ -333,7 +333,12 @@ func (w *walker) references(roomID string) (eventLookup, error) { } el := make(eventLookup) for _, ev := range events { - el.set(ev) + // only return events that have a `via` key as per MSC1772 + // else we'll incorrectly walk redacted events (as the link + // is in the state_key) + if gjson.GetBytes(ev.Content(), "via").Exists() { + el.set(ev) + } } return el, nil } diff --git a/setup/mscs/msc2946/msc2946_test.go b/setup/mscs/msc2946/msc2946_test.go index 017319dc..a7650f0b 100644 --- a/setup/mscs/msc2946/msc2946_test.go +++ b/setup/mscs/msc2946/msc2946_test.go @@ -254,7 +254,26 @@ func TestMSC2946(t *testing.T) { if len(res.Rooms) != len(allRooms) { t.Errorf("got %d rooms, want %d", len(res.Rooms), len(allRooms)) } + }) + t.Run("can update the graph", func(t *testing.T) { + // remove R3 from the graph + rmS1ToR3 := mustCreateEvent(t, fledglingEvent{ + RoomID: subSpaceS1, + Sender: alice, + Type: msc2946.ConstSpaceChildEventType, + StateKey: &room3, + Content: map[string]interface{}{}, // redacted + }) + nopRsAPI.events[rmS1ToR3.EventID()] = rmS1ToR3 + hooks.Run(hooks.KindNewEventPersisted, rmS1ToR3) + res := postSpaces(t, 200, "alice", rootSpace, newReq(t, map[string]interface{}{})) + if len(res.Events) != 6 { // one less since we don't return redacted events + t.Errorf("got %d events, want 6", len(res.Events)) + } + if len(res.Rooms) != (len(allRooms) - 1) { // one less due to lack of R3 + t.Errorf("got %d rooms, want %d", len(res.Rooms), len(allRooms)-1) + } }) } diff --git a/setup/mscs/msc2946/storage.go b/setup/mscs/msc2946/storage.go index eb4a5efb..5798310a 100644 --- a/setup/mscs/msc2946/storage.go +++ b/setup/mscs/msc2946/storage.go @@ -81,7 +81,7 @@ func newPostgresDatabase(dbOpts *config.DatabaseOptions) (Database, error) { if d.insertEdgeStmt, err = d.db.Prepare(` INSERT INTO msc2946_edges(room_version, source_room_id, dest_room_id, rel_type, event_json) VALUES($1, $2, $3, $4, $5) - ON CONFLICT DO NOTHING + ON CONFLICT ON CONSTRAINT msc2946_edges_uniq DO UPDATE SET event_json = $5 `); err != nil { return nil, err } @@ -121,7 +121,7 @@ func newSQLiteDatabase(dbOpts *config.DatabaseOptions) (Database, error) { if d.insertEdgeStmt, err = d.db.Prepare(` INSERT INTO msc2946_edges(room_version, source_room_id, dest_room_id, rel_type, event_json) VALUES($1, $2, $3, $4, $5) - ON CONFLICT DO NOTHING + ON CONFLICT (source_room_id, dest_room_id, rel_type) DO UPDATE SET event_json = $5 `); err != nil { return nil, err }