From a6dc507612cc0ccf9799dd095926474a89e25685 Mon Sep 17 00:00:00 2001 From: Germain Souquet Date: Mon, 21 Feb 2022 14:37:07 +0000 Subject: [PATCH] Null-guard for undefined rootEvent when creating a thread --- src/models/room.ts | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/models/room.ts b/src/models/room.ts index 135ffb9c05c..e3cad8cb631 100644 --- a/src/models/room.ts +++ b/src/models/room.ts @@ -1398,12 +1398,17 @@ export class Room extends EventEmitter { this.emit(ThreadEvent.Update, thread); } - public createThread(rootEvent: MatrixEvent, events: MatrixEvent[] = []): Thread | undefined { - const tl = this.getTimelineForEvent(rootEvent.getId()); - const relatedEvents = tl?.getTimelineSet().getAllRelationsEventForEvent(rootEvent.getId()) ?? []; + public createThread(rootEvent: MatrixEvent | undefined, events: MatrixEvent[] = []): Thread | undefined { + if (rootEvent) { + const tl = this.getTimelineForEvent(rootEvent.getId()); + const relatedEvents = tl?.getTimelineSet().getAllRelationsEventForEvent(rootEvent.getId()); + if (relatedEvents) { + events = events.concat(relatedEvents); + } + } const thread = new Thread(rootEvent, { - initialEvents: events.concat(relatedEvents), + initialEvents: events, room: this, client: this.client, });