@@ -16,10 +16,10 @@ limitations under the License.
1616
1717import { Optional } from "matrix-events-sdk" ;
1818
19- import { MatrixClient } from "../client" ;
19+ import { MatrixClient , PendingEventOrdering } from "../client" ;
2020import { TypedReEmitter } from "../ReEmitter" ;
2121import { RelationType } from "../@types/event" ;
22- import { IThreadBundledRelationship , MatrixEvent , MatrixEventEvent } from "./event" ;
22+ import { EventStatus , IThreadBundledRelationship , MatrixEvent , MatrixEventEvent } from "./event" ;
2323import { EventTimeline } from "./event-timeline" ;
2424import { EventTimelineSet , EventTimelineSetHandlerMap } from './event-timeline-set' ;
2525import { NotificationCountType , Room , RoomEvent } from './room' ;
@@ -51,6 +51,7 @@ export type EventHandlerMap = {
5151interface IThreadOpts {
5252 room : Room ;
5353 client : MatrixClient ;
54+ pendingEventOrdering ?: PendingEventOrdering ;
5455}
5556
5657export enum FeatureSupport {
@@ -88,9 +89,12 @@ export class Thread extends ReadReceipt<EmittedEvents, EventHandlerMap> {
8889
8990 private lastEvent : MatrixEvent | undefined ;
9091 private replyCount = 0 ;
92+ private lastPendingEvent : MatrixEvent | undefined ;
93+ private pendingReplyCount = 0 ;
9194
9295 public readonly room : Room ;
9396 public readonly client : MatrixClient ;
97+ private readonly pendingEventOrdering : PendingEventOrdering ;
9498
9599 public initialEventsFetched = ! Thread . hasServerSideSupport ;
96100
@@ -109,6 +113,7 @@ export class Thread extends ReadReceipt<EmittedEvents, EventHandlerMap> {
109113
110114 this . room = opts . room ;
111115 this . client = opts . client ;
116+ this . pendingEventOrdering = opts . pendingEventOrdering ?? PendingEventOrdering . Chronological ;
112117 this . timelineSet = new EventTimelineSet ( this . room , {
113118 timelineSupport : true ,
114119 pendingEvents : true ,
@@ -300,6 +305,19 @@ export class Thread extends ReadReceipt<EmittedEvents, EventHandlerMap> {
300305 bundledRelationship = this . getRootEventBundledRelationship ( ) ;
301306 }
302307
308+ let pendingEvents : MatrixEvent [ ] ;
309+ if ( this . pendingEventOrdering === PendingEventOrdering . Detached ) {
310+ pendingEvents = this . room . getPendingEvents ( )
311+ . filter ( ev => ev . isRelation ( THREAD_RELATION_TYPE . name ) && this . id === ev . threadRootId ) ;
312+ await Promise . all ( pendingEvents . map ( ev => this . processEvent ( ev ) ) ) ;
313+ } else {
314+ pendingEvents = this . events
315+ . filter ( ev => ev . isRelation ( THREAD_RELATION_TYPE . name ) )
316+ . filter ( ev => ev . status !== EventStatus . SENT && ev . status !== EventStatus . CANCELLED ) ;
317+ }
318+ this . lastPendingEvent = pendingEvents . length ? pendingEvents [ pendingEvents . length - 1 ] : undefined ;
319+ this . pendingReplyCount = pendingEvents . length ;
320+
303321 if ( Thread . hasServerSideSupport && bundledRelationship ) {
304322 this . replyCount = bundledRelationship . count ;
305323 this . _currentUserParticipated = ! ! bundledRelationship . current_user_participated ;
@@ -393,14 +411,14 @@ export class Thread extends ReadReceipt<EmittedEvents, EventHandlerMap> {
393411 * exclude annotations from that number
394412 */
395413 public get length ( ) : number {
396- return this . replyCount ;
414+ return this . replyCount + this . pendingReplyCount ;
397415 }
398416
399417 /**
400418 * A getter for the last event added to the thread, if known.
401419 */
402420 public get replyToEvent ( ) : Optional < MatrixEvent > {
403- return this . lastEvent ?? this . lastReply ( ) ;
421+ return this . lastPendingEvent ?? this . lastEvent ?? this . lastReply ( ) ;
404422 }
405423
406424 public get events ( ) : MatrixEvent [ ] {
0 commit comments