Skip to content

Commit ba06e43

Browse files
t3chguyKerry
andauthored
Ignore m.replace relations on state events, they're invalid (#2306)
* Ignore m.replace relations on state events, they're invalid * Add tests * Fix test Co-authored-by: Kerry <[email protected]>
1 parent ac08e52 commit ba06e43

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

spec/unit/relations.spec.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,49 @@ describe("Relations", function() {
130130
await relationsCreated;
131131
}
132132
});
133+
134+
it("should ignore m.replace for state events", async () => {
135+
const userId = "@bob:example.com";
136+
const room = new Room("room123", null, userId);
137+
const relations = new Relations("m.replace", "m.room.topic", room);
138+
139+
// Create an instance of a state event with rel_type m.replace
140+
const originalTopic = new MatrixEvent({
141+
"sender": userId,
142+
"type": "m.room.topic",
143+
"event_id": "$orig",
144+
"room_id": room.roomId,
145+
"content": {
146+
"topic": "orig",
147+
},
148+
"state_key": "",
149+
});
150+
const badlyEditedTopic = new MatrixEvent({
151+
"sender": userId,
152+
"type": "m.room.topic",
153+
"event_id": "$orig",
154+
"room_id": room.roomId,
155+
"content": {
156+
"topic": "topic",
157+
"m.new_content": {
158+
"topic": "edit",
159+
},
160+
"m.relates_to": {
161+
"event_id": "$orig",
162+
"rel_type": "m.replace",
163+
},
164+
},
165+
"state_key": "",
166+
});
167+
168+
await relations.setTargetEvent(originalTopic);
169+
expect(originalTopic.replacingEvent()).toBe(null);
170+
expect(originalTopic.getContent().topic).toBe("orig");
171+
172+
await relations.addEvent(badlyEditedTopic);
173+
expect(originalTopic.replacingEvent()).toBe(null);
174+
expect(originalTopic.getContent().topic).toBe("orig");
175+
expect(badlyEditedTopic.replacingEvent()).toBe(null);
176+
expect(badlyEditedTopic.getContent().topic).toBe("topic");
177+
});
133178
});

src/models/event.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1336,6 +1336,10 @@ export class MatrixEvent extends TypedEventEmitter<EmittedEvents, MatrixEventHan
13361336
if (this.isRedacted() && newEvent) {
13371337
return;
13381338
}
1339+
// don't allow state events to be replaced using this mechanism as per MSC2676
1340+
if (this.isState()) {
1341+
return;
1342+
}
13391343
if (this._replacingEvent !== newEvent) {
13401344
this._replacingEvent = newEvent;
13411345
this.emit(MatrixEventEvent.Replaced, this);

0 commit comments

Comments
 (0)