Skip to content

Commit ba1f6ff

Browse files
authored
Tweak thread creation & event adding to fix bugs around relations (#2369)
* Remove legacy code which caused threads to begin life with too many events * Update tests & behaviour
1 parent 3e4f02b commit ba1f6ff

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

spec/unit/room.spec.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1937,6 +1937,15 @@ describe("Room", function() {
19371937
expect(() => room.createThread(rootEvent.getId(), rootEvent, [])).not.toThrow();
19381938
});
19391939

1940+
it("creating thread from edited event should not conflate old versions of the event", () => {
1941+
const message = mkMessage();
1942+
const edit = mkEdit(message);
1943+
message.makeReplaced(edit);
1944+
1945+
const thread = room.createThread("$000", message, [], true);
1946+
expect(thread).toHaveLength(0);
1947+
});
1948+
19401949
it("Edits update the lastReply event", async () => {
19411950
room.client.supportsExperimentalThreads = () => true;
19421951

@@ -2036,17 +2045,15 @@ describe("Room", function() {
20362045
},
20372046
});
20382047

2039-
let prom = emitPromise(room, ThreadEvent.New);
2048+
const prom = emitPromise(room, ThreadEvent.New);
20402049
room.addLiveEvents([threadRoot, threadResponse1, threadResponse2, threadResponse2Reaction]);
20412050
const thread = await prom;
20422051

20432052
expect(thread).toHaveLength(2);
20442053
expect(thread.replyToEvent.getId()).toBe(threadResponse2.getId());
20452054

2046-
prom = emitPromise(thread, ThreadEvent.Update);
20472055
const threadResponse2ReactionRedaction = mkRedaction(threadResponse2Reaction);
20482056
room.addLiveEvents([threadResponse2ReactionRedaction]);
2049-
await prom;
20502057
expect(thread).toHaveLength(2);
20512058
expect(thread.replyToEvent.getId()).toBe(threadResponse2.getId());
20522059
});

src/models/room.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1659,8 +1659,10 @@ export class Room extends TypedEventEmitter<EmittedEvents, RoomEventHandlerMap>
16591659
if (rootEvent) {
16601660
const tl = this.getTimelineForEvent(rootEvent.getId());
16611661
const relatedEvents = tl?.getTimelineSet().getAllRelationsEventForEvent(rootEvent.getId());
1662-
if (relatedEvents) {
1663-
events = events.concat(relatedEvents);
1662+
if (relatedEvents?.length) {
1663+
// Include all relations of the root event, given it'll be visible in both timelines,
1664+
// except `m.replace` as that will already be applied atop the event using `MatrixEvent::makeReplaced`
1665+
events = events.concat(relatedEvents.filter(e => !e.isRelation(RelationType.Replace)));
16641666
}
16651667
}
16661668

src/models/thread.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,10 @@ export class Thread extends TypedEventEmitter<EmittedEvents, EventHandlerMap> {
221221
this._currentUserParticipated = true;
222222
}
223223

224-
// Add all annotations and replace relations to the timeline so that the relations are processed accordingly
225224
if ([RelationType.Annotation, RelationType.Replace].includes(event.getRelation()?.rel_type as RelationType)) {
226-
this.addEventToTimeline(event, toStartOfTimeline);
225+
// Apply annotations and replace relations to the relations of the timeline only
226+
this.timelineSet.setRelationsTarget(event);
227+
this.timelineSet.aggregateRelations(event);
227228
return;
228229
}
229230

0 commit comments

Comments
 (0)