@@ -1372,13 +1372,24 @@ export class Room extends EventEmitter {
1372
1372
let rootEvent = this . findEventById ( event . threadRootId ) ;
1373
1373
// If the rootEvent does not exist in the current sync, then look for
1374
1374
// 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 ) ;
1380
1392
}
1381
- thread = this . createThread ( rootEvent , events ) ;
1382
1393
}
1383
1394
1384
1395
if ( event . getUnsigned ( ) . transaction_id ) {
@@ -1393,26 +1404,30 @@ export class Room extends EventEmitter {
1393
1404
this . emit ( ThreadEvent . Update , thread ) ;
1394
1405
}
1395
1406
1396
- public createThread ( rootEvent : MatrixEvent , events ?: MatrixEvent [ ] ) : Thread {
1407
+ public createThread ( rootEvent : MatrixEvent , events ?: MatrixEvent [ ] ) : Thread | undefined {
1397
1408
const thread = new Thread ( rootEvent , {
1398
1409
initialEvents : events ,
1399
1410
room : this ,
1400
1411
client : this . client ,
1401
1412
} ) ;
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
+ }
1409
1427
1410
- if ( ! this . lastThread || this . lastThread . rootEvent . localTimestamp < rootEvent . localTimestamp ) {
1411
- this . lastThread = thread ;
1428
+ this . emit ( ThreadEvent . New , thread ) ;
1429
+ return thread ;
1412
1430
}
1413
-
1414
- this . emit ( ThreadEvent . New , thread ) ;
1415
- return thread ;
1416
1431
}
1417
1432
1418
1433
/**
0 commit comments