Skip to content

Commit

Permalink
Merge pull request #424 from matrix-org/kegan/msc4115-unstable
Browse files Browse the repository at this point in the history
Check for unstable prefix in MSC4115
  • Loading branch information
kegsay authored Apr 26, 2024
2 parents abf5aef + 9620a62 commit 91d34b6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
4 changes: 4 additions & 0 deletions sync2/handler2/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,8 @@ func (h *Handler) Accumulate(ctx context.Context, userID, deviceID, roomID strin
for i := range timeline.Events {
// Delete MSC4115 field as it isn't accurate when we reuse the same event for >1 user
timeline.Events[i], _ = sjson.DeleteBytes(timeline.Events[i], "unsigned.membership")
// escape .'s in the key name
timeline.Events[i], _ = sjson.DeleteBytes(timeline.Events[i], `unsigned.io\.element\.msc4115\.membership`)
parsed := gjson.ParseBytes(timeline.Events[i])
eventID := parsed.Get("event_id").Str

Expand Down Expand Up @@ -376,6 +378,8 @@ func (h *Handler) Accumulate(ctx context.Context, userID, deviceID, roomID strin
func (h *Handler) Initialise(ctx context.Context, roomID string, state []json.RawMessage) error {
for i := range state { // Delete MSC4115 field as it isn't accurate when we reuse the same event for >1 user
state[i], _ = sjson.DeleteBytes(state[i], "unsigned.membership")
// escape .'s in the key name
state[i], _ = sjson.DeleteBytes(state[i], `unsigned.io\.element\.msc4115\.membership`)
}
res, err := h.Store.Initialise(roomID, state)
if err != nil {
Expand Down
18 changes: 14 additions & 4 deletions tests-integration/lists_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package syncv3

import (
"encoding/json"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -364,6 +365,15 @@ func TestBumpEventTypesOnStartup(t *testing.T) {
}

func TestDeleteMSC4115Field(t *testing.T) {
t.Run("stable prefix", func(t *testing.T) {
testDeleteMSC4115Field(t, "membership")
})
t.Run("unstable prefix", func(t *testing.T) {
testDeleteMSC4115Field(t, "io.element.msc4115.membership")
})
}

func testDeleteMSC4115Field(t *testing.T, fieldName string) {
rig := NewTestRig(t)
defer rig.Finish()
roomID := "!TestDeleteMSC4115Field:localhost"
Expand All @@ -390,11 +400,11 @@ func TestDeleteMSC4115Field(t *testing.T) {

// ensure live events remove the field.
liveEvent := testutils.NewMessageEvent(t, alice, "live event", testutils.WithUnsigned(map[string]interface{}{
"membership": "join",
fieldName: "join",
}))
liveEventWithoutMembership := make(json.RawMessage, len(liveEvent))
copy(liveEventWithoutMembership, liveEvent)
liveEventWithoutMembership, err := sjson.DeleteBytes(liveEventWithoutMembership, "unsigned.membership")
liveEventWithoutMembership, err := sjson.DeleteBytes(liveEventWithoutMembership, "unsigned."+strings.ReplaceAll(fieldName, ".", `\.`))
if err != nil {
t.Fatalf("failed to delete unsigned.membership field")
}
Expand All @@ -411,11 +421,11 @@ func TestDeleteMSC4115Field(t *testing.T) {
stateEvent := testutils.NewStateEvent(t, "m.room.name", "", alice, map[string]interface{}{
"name": "Room Name",
}, testutils.WithUnsigned(map[string]interface{}{
"membership": "join",
fieldName: "join",
}))
stateEventWithoutMembership := make(json.RawMessage, len(stateEvent))
copy(stateEventWithoutMembership, stateEvent)
stateEventWithoutMembership, err = sjson.DeleteBytes(stateEventWithoutMembership, "unsigned.membership")
stateEventWithoutMembership, err = sjson.DeleteBytes(stateEventWithoutMembership, "unsigned."+strings.ReplaceAll(fieldName, ".", `\.`))
if err != nil {
t.Fatalf("failed to delete unsigned.membership field")
}
Expand Down

0 comments on commit 91d34b6

Please sign in to comment.