From 65c68015a20faaabd5e496ffb80d8af767f16d1d Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Wed, 20 Apr 2022 08:52:23 +0100 Subject: [PATCH 1/3] Ignore m.replace relations on state events, they're invalid --- src/models/event.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/models/event.ts b/src/models/event.ts index f28176b94f0..e65aa7b053c 100644 --- a/src/models/event.ts +++ b/src/models/event.ts @@ -1336,6 +1336,10 @@ export class MatrixEvent extends TypedEventEmitter Date: Wed, 20 Apr 2022 09:31:23 +0100 Subject: [PATCH 2/3] Add tests --- spec/unit/relations.spec.ts | 49 +++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/spec/unit/relations.spec.ts b/spec/unit/relations.spec.ts index 1b479ebbef8..7e08f7491b0 100644 --- a/spec/unit/relations.spec.ts +++ b/spec/unit/relations.spec.ts @@ -130,4 +130,53 @@ describe("Relations", function() { await relationsCreated; } }); + + it("edits shouldn't apply to state events", async () => { + const userId = "@bob:example.com"; + const room = new Room("room123", null, userId); + const relations = new Relations("m.replace", "m.room.topic", room); + + // Create an instance of a state event with rel_type m.replace + const originalTopic = new MatrixEvent({ + "sender": userId, + "type": "m.room.topic", + "event_id": "$orig", + "room_id": room.roomId, + "content": { + "topic": "orig", + }, + }); + const badlyEditedTopic = new MatrixEvent({ + "sender": userId, + "type": "m.room.topic", + "event_id": "$orig", + "room_id": room.roomId, + "content": { + "topic": "topic", + "m.new_content": { + "topic": "edit", + }, + "m.relates_to": { + "event_id": "$orig", + "rel_type": "m.replace", + }, + }, + }); + + // Add the original topic and check results + { + relations.addEvent(originalTopic); + expect(originalTopic.replacingEvent()).toBe(null); + expect(originalTopic.getContent().topic).toBe("orig"); + } + + // Add the badly edited topic and check results + { + relations.addEvent(badlyEditedTopic); + expect(originalTopic.replacingEvent()).toBe(null); + expect(originalTopic.getContent().topic).toBe("orig"); + expect(badlyEditedTopic.replacingEvent()).toBe(null); + expect(badlyEditedTopic.getContent().topic).toBe("topic"); + } + }); }); From d23ae9fc82f5dfccf166e614dac5d91b39b13fb8 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Wed, 20 Apr 2022 09:34:28 +0100 Subject: [PATCH 3/3] Fix test --- spec/unit/relations.spec.ts | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/spec/unit/relations.spec.ts b/spec/unit/relations.spec.ts index 7e08f7491b0..2472bd8f688 100644 --- a/spec/unit/relations.spec.ts +++ b/spec/unit/relations.spec.ts @@ -131,7 +131,7 @@ describe("Relations", function() { } }); - it("edits shouldn't apply to state events", async () => { + it("should ignore m.replace for state events", async () => { const userId = "@bob:example.com"; const room = new Room("room123", null, userId); const relations = new Relations("m.replace", "m.room.topic", room); @@ -145,6 +145,7 @@ describe("Relations", function() { "content": { "topic": "orig", }, + "state_key": "", }); const badlyEditedTopic = new MatrixEvent({ "sender": userId, @@ -161,22 +162,17 @@ describe("Relations", function() { "rel_type": "m.replace", }, }, + "state_key": "", }); - // Add the original topic and check results - { - relations.addEvent(originalTopic); - expect(originalTopic.replacingEvent()).toBe(null); - expect(originalTopic.getContent().topic).toBe("orig"); - } + await relations.setTargetEvent(originalTopic); + expect(originalTopic.replacingEvent()).toBe(null); + expect(originalTopic.getContent().topic).toBe("orig"); - // Add the badly edited topic and check results - { - relations.addEvent(badlyEditedTopic); - expect(originalTopic.replacingEvent()).toBe(null); - expect(originalTopic.getContent().topic).toBe("orig"); - expect(badlyEditedTopic.replacingEvent()).toBe(null); - expect(badlyEditedTopic.getContent().topic).toBe("topic"); - } + await relations.addEvent(badlyEditedTopic); + expect(originalTopic.replacingEvent()).toBe(null); + expect(originalTopic.getContent().topic).toBe("orig"); + expect(badlyEditedTopic.replacingEvent()).toBe(null); + expect(badlyEditedTopic.getContent().topic).toBe("topic"); }); });