From 2738d429beb7fb2def31236b38727fd22c785304 Mon Sep 17 00:00:00 2001 From: Till Faelligen <2353100+S7evinK@users.noreply.github.com> Date: Fri, 22 Sep 2023 17:19:52 +0200 Subject: [PATCH] Move redacts to the client API, which should satisfy clients and federation --- clientapi/routing/redaction.go | 8 ++++- syncapi/synctypes/clientevent.go | 12 -------- syncapi/synctypes/clientevent_test.go | 44 --------------------------- 3 files changed, 7 insertions(+), 57 deletions(-) diff --git a/clientapi/routing/redaction.go b/clientapi/routing/redaction.go index aa579db64..f331a73c7 100644 --- a/clientapi/routing/redaction.go +++ b/clientapi/routing/redaction.go @@ -34,7 +34,8 @@ import ( ) type redactionContent struct { - Reason string `json:"reason"` + Reason string `json:"reason"` + Redacts string `json:"redacts"` } type redactionResponse struct { @@ -151,6 +152,11 @@ func SendRedaction( Type: spec.MRoomRedaction, Redacts: eventID, } + + // Room version 11 expects the "redacts" field on the + // content field, so add it here as well + r.Redacts = eventID + err = proto.SetContent(r) if err != nil { util.GetLogger(req.Context()).WithError(err).Error("proto.SetContent failed") diff --git a/syncapi/synctypes/clientevent.go b/syncapi/synctypes/clientevent.go index d894a3906..6812f8332 100644 --- a/syncapi/synctypes/clientevent.go +++ b/syncapi/synctypes/clientevent.go @@ -141,18 +141,6 @@ func ToClientEvent(se gomatrixserverlib.PDU, format ClientEventFormat, userIDFor Redacts: se.Redacts(), } - // RoomVersion V11 moved the "redacts" field to "content", so add it there as well - // For compatibility with older clients, we keep the "redacts" top-level field. - // TODO: At which point do we remove "redacts" from the top-level? - if se.Type() == spec.MRoomRedaction && se.Version() == gomatrixserverlib.RoomVersionV11 && ce.Redacts != "" { - v11content, err := sjson.SetBytes(ce.Content, "redacts", ce.Redacts) - if err != nil { - logrus.WithError(err).Warn("failed to set content.redacts") - } else { - ce.Content = v11content - } - } - switch format { case FormatAll: ce.RoomID = se.RoomID().String() diff --git a/syncapi/synctypes/clientevent_test.go b/syncapi/synctypes/clientevent_test.go index 00364d02c..662f9ea43 100644 --- a/syncapi/synctypes/clientevent_test.go +++ b/syncapi/synctypes/clientevent_test.go @@ -154,50 +154,6 @@ func TestToClientEvent(t *testing.T) { // nolint: gocyclo } } -// Merely tests that the top-level "redacts" is copied to "content.redacts" and keeps the top-level -// one as is -func TestToClientEventRedactionV11(t *testing.T) { // nolint: gocyclo - ev, err := gomatrixserverlib.MustGetRoomVersion(gomatrixserverlib.RoomVersionV11).NewEventFromTrustedJSON([]byte(`{ - "type": "m.room.redaction", - "state_key": "", - "event_id": "$test", - "room_id": "!test:localhost", - "sender": "@test:localhost", - "content": { - "reason": "Hello World" - }, - "origin_server_ts": 123456, - "unsigned": { - "prev_content": { - "name": "Goodbye World" - } - }, - "redacts": "$abc" - }`), false) - if err != nil { - t.Fatalf("failed to create Event: %s", err) - } - - ce, err := ToClientEvent(ev, FormatAll, func(roomID spec.RoomID, senderID spec.SenderID) (*spec.UserID, error) { - return queryUserIDForSender(senderID) - }) - if err != nil { - t.Fatalf("failed to create ClientEvent: %s", err) - } - - j, err := json.Marshal(ce) - if err != nil { - t.Fatalf("failed to Marshal ClientEvent: %s", err) - } - // Marshal sorts keys in structs by the order they are defined in the struct, which is alphabetical - out := `{"content":{"reason":"Hello World","redacts":"$abc"},"event_id":"$test","origin_server_ts":123456,` + - `"room_id":"!test:localhost","sender":"@test:localhost","state_key":"","type":"m.room.redaction",` + - `"unsigned":{"prev_content":{"name":"Goodbye World"}},"redacts":"$abc"}` - if !bytes.Equal([]byte(out), j) { - t.Errorf("ClientEvent marshalled to wrong bytes: wanted \n%s, got \n%s", out, string(j)) - } -} - func TestToClientFormatSync(t *testing.T) { ev, err := gomatrixserverlib.MustGetRoomVersion(gomatrixserverlib.RoomVersionV1).NewEventFromTrustedJSON([]byte(`{ "type": "m.room.name",