Skip to content

Commit 87171f8

Browse files
committed
Include pending events in thread summary and count again
1 parent 1606274 commit 87171f8

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

src/models/thread.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ limitations under the License.
1616

1717
import { Optional } from "matrix-events-sdk";
1818

19-
import { MatrixClient } from "../client";
19+
import { MatrixClient, PendingEventOrdering } from "../client";
2020
import { TypedReEmitter } from "../ReEmitter";
2121
import { RelationType } from "../@types/event";
22-
import { IThreadBundledRelationship, MatrixEvent, MatrixEventEvent } from "./event";
22+
import { EventStatus, IThreadBundledRelationship, MatrixEvent, MatrixEventEvent } from "./event";
2323
import { EventTimeline } from "./event-timeline";
2424
import { EventTimelineSet, EventTimelineSetHandlerMap } from './event-timeline-set';
2525
import { NotificationCountType, Room, RoomEvent } from './room';
@@ -51,6 +51,7 @@ export type EventHandlerMap = {
5151
interface IThreadOpts {
5252
room: Room;
5353
client: MatrixClient;
54+
pendingEventOrdering?: PendingEventOrdering;
5455
}
5556

5657
export 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,18 @@ 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().filter((ev) => {
311+
const isNotSent = ev.status === EventStatus.NOT_SENT;
312+
const belongsToTheThread = this.id === ev.threadRootId;
313+
return isNotSent && belongsToTheThread;
314+
});
315+
pendingEvents.map((ev) => this.processEvent(ev));
316+
}
317+
this.lastPendingEvent = pendingEvents.length ? pendingEvents[pendingEvents.length - 1] : undefined;
318+
this.pendingReplyCount = pendingEvents.length;
319+
303320
if (Thread.hasServerSideSupport && bundledRelationship) {
304321
this.replyCount = bundledRelationship.count;
305322
this._currentUserParticipated = !!bundledRelationship.current_user_participated;
@@ -393,14 +410,14 @@ export class Thread extends ReadReceipt<EmittedEvents, EventHandlerMap> {
393410
* exclude annotations from that number
394411
*/
395412
public get length(): number {
396-
return this.replyCount;
413+
return this.replyCount + this.pendingReplyCount;
397414
}
398415

399416
/**
400417
* A getter for the last event added to the thread, if known.
401418
*/
402419
public get replyToEvent(): Optional<MatrixEvent> {
403-
return this.lastEvent ?? this.lastReply();
420+
return this.lastPendingEvent ?? this.lastEvent ?? this.lastReply();
404421
}
405422

406423
public get events(): MatrixEvent[] {

0 commit comments

Comments
 (0)