From 2626525c65e5e7f3cf446d7ab23c26c6c345320a Mon Sep 17 00:00:00 2001 From: Kegsay Date: Fri, 15 Jan 2021 11:44:36 +0000 Subject: [PATCH] MSC2946: Allow redactions/updates for space state events (#1712) --- setup/mscs/msc2946/msc2946.go | 7 ++++++- setup/mscs/msc2946/msc2946_test.go | 19 +++++++++++++++++++ setup/mscs/msc2946/storage.go | 4 ++-- 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/setup/mscs/msc2946/msc2946.go b/setup/mscs/msc2946/msc2946.go index a2d801e4d..accdbd395 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 017319dc5..a7650f0ba 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 eb4a5efb9..5798310a6 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 }