Skip to content

Commit 6bf8142

Browse files
author
Germain
authored
Fix initial sync fail when event fetching unsuccessful (#2150)
1 parent cf0ccaf commit 6bf8142

File tree

2 files changed

+50
-31
lines changed

2 files changed

+50
-31
lines changed

src/models/room.ts

+34-19
Original file line numberDiff line numberDiff line change
@@ -1372,13 +1372,24 @@ export class Room extends EventEmitter {
13721372
let rootEvent = this.findEventById(event.threadRootId);
13731373
// If the rootEvent does not exist in the current sync, then look for
13741374
// it over the network
1375-
const eventData = await this.client.fetchRoomEvent(this.roomId, event.threadRootId);
1376-
if (!rootEvent) {
1377-
rootEvent = new MatrixEvent(eventData);
1378-
} else {
1379-
rootEvent.setUnsigned(eventData.unsigned);
1375+
try {
1376+
let eventData;
1377+
if (event.threadRootId) {
1378+
eventData = await this.client.fetchRoomEvent(this.roomId, event.threadRootId);
1379+
}
1380+
1381+
if (!rootEvent) {
1382+
rootEvent = new MatrixEvent(eventData);
1383+
} else {
1384+
rootEvent.setUnsigned(eventData.unsigned);
1385+
}
1386+
} finally {
1387+
// The root event might be not be visible to the person requesting
1388+
// it. If it wasn't fetched successfully the thread will work
1389+
// in "limited" mode and won't benefit from all the APIs a homeserver
1390+
// can provide to enhance the thread experience
1391+
thread = this.createThread(rootEvent, events);
13801392
}
1381-
thread = this.createThread(rootEvent, events);
13821393
}
13831394

13841395
if (event.getUnsigned().transaction_id) {
@@ -1393,26 +1404,30 @@ export class Room extends EventEmitter {
13931404
this.emit(ThreadEvent.Update, thread);
13941405
}
13951406

1396-
public createThread(rootEvent: MatrixEvent, events?: MatrixEvent[]): Thread {
1407+
public createThread(rootEvent: MatrixEvent, events?: MatrixEvent[]): Thread | undefined {
13971408
const thread = new Thread(rootEvent, {
13981409
initialEvents: events,
13991410
room: this,
14001411
client: this.client,
14011412
});
1402-
this.threads.set(thread.id, thread);
1403-
this.reEmitter.reEmit(thread, [
1404-
ThreadEvent.Update,
1405-
ThreadEvent.Ready,
1406-
"Room.timeline",
1407-
"Room.timelineReset",
1408-
]);
1413+
// If we managed to create a thread and figure out its `id`
1414+
// then we can use it
1415+
if (thread.id) {
1416+
this.threads.set(thread.id, thread);
1417+
this.reEmitter.reEmit(thread, [
1418+
ThreadEvent.Update,
1419+
ThreadEvent.Ready,
1420+
"Room.timeline",
1421+
"Room.timelineReset",
1422+
]);
1423+
1424+
if (!this.lastThread || this.lastThread.rootEvent.localTimestamp < rootEvent.localTimestamp) {
1425+
this.lastThread = thread;
1426+
}
14091427

1410-
if (!this.lastThread || this.lastThread.rootEvent.localTimestamp < rootEvent.localTimestamp) {
1411-
this.lastThread = thread;
1428+
this.emit(ThreadEvent.New, thread);
1429+
return thread;
14121430
}
1413-
1414-
this.emit(ThreadEvent.New, thread);
1415-
return thread;
14161431
}
14171432

14181433
/**

src/models/thread.ts

+16-12
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,10 @@ export class Thread extends TypedEventEmitter<ThreadEvent> {
6060

6161
public initialEventsFetched = false;
6262

63+
public readonly id: string;
64+
6365
constructor(
64-
public readonly rootEvent: MatrixEvent,
66+
public readonly rootEvent: MatrixEvent | undefined,
6567
opts: IThreadOpts,
6668
) {
6769
super();
@@ -82,6 +84,15 @@ export class Thread extends TypedEventEmitter<ThreadEvent> {
8284
"Room.timelineReset",
8385
]);
8486

87+
// If we weren't able to find the root event, it's probably missing
88+
// and we define the thread ID from one of the thread relation
89+
if (!rootEvent) {
90+
this.id = opts?.initialEvents
91+
?.find(event => event.isThreadRelation)?.relationEventId;
92+
} else {
93+
this.id = rootEvent.getId();
94+
}
95+
8596
opts?.initialEvents?.forEach(event => this.addEvent(event));
8697

8798
this.room.on("Room.localEchoUpdated", this.onEcho);
@@ -177,9 +188,9 @@ export class Thread extends TypedEventEmitter<ThreadEvent> {
177188
this.emit(ThreadEvent.Update, this);
178189
}
179190

180-
private initialiseThread(rootEvent: MatrixEvent): void {
191+
private initialiseThread(rootEvent: MatrixEvent | undefined): void {
181192
const bundledRelationship = rootEvent
182-
.getServerAggregatedRelation<IThreadBundledRelationship>(RelationType.Thread);
193+
?.getServerAggregatedRelation<IThreadBundledRelationship>(RelationType.Thread);
183194

184195
if (this.hasServerSideSupport && bundledRelationship) {
185196
this.replyCount = bundledRelationship.count;
@@ -190,7 +201,7 @@ export class Thread extends TypedEventEmitter<ThreadEvent> {
190201
this.lastEvent = event;
191202
}
192203

193-
if (!bundledRelationship) {
204+
if (!bundledRelationship && rootEvent) {
194205
this.addEvent(rootEvent);
195206
}
196207
}
@@ -229,15 +240,8 @@ export class Thread extends TypedEventEmitter<ThreadEvent> {
229240
}
230241
}
231242

232-
/**
233-
* The thread ID, which is the same as the root event ID
234-
*/
235-
public get id(): string {
236-
return this.rootEvent.getId();
237-
}
238-
239243
public get roomId(): string {
240-
return this.rootEvent.getRoomId();
244+
return this.room.roomId;
241245
}
242246

243247
/**

0 commit comments

Comments
 (0)